1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/indexed_db/indexed_db_cursor.h"
#include <vector>
#include "base/bind.h"
#include "base/logging.h"
#include "content/browser/indexed_db/indexed_db_callbacks.h"
#include "content/browser/indexed_db/indexed_db_database_error.h"
#include "content/browser/indexed_db/indexed_db_tracing.h"
#include "content/browser/indexed_db/indexed_db_transaction.h"
#include "content/browser/indexed_db/indexed_db_value.h"
namespace content {
IndexedDBCursor::IndexedDBCursor(
scoped_ptr<IndexedDBBackingStore::Cursor> cursor,
indexed_db::CursorType cursor_type,
blink::WebIDBTaskType task_type,
IndexedDBTransaction* transaction)
: task_type_(task_type),
cursor_type_(cursor_type),
transaction_(transaction),
cursor_(cursor.Pass()),
closed_(false) {
transaction_->RegisterOpenCursor(this);
}
IndexedDBCursor::~IndexedDBCursor() {
transaction_->UnregisterOpenCursor(this);
}
void IndexedDBCursor::Continue(scoped_ptr<IndexedDBKey> key,
scoped_ptr<IndexedDBKey> primary_key,
scoped_refptr<IndexedDBCallbacks> callbacks) {
IDB_TRACE("IndexedDBCursor::Continue");
transaction_->ScheduleTask(
task_type_,
base::Bind(&IndexedDBCursor::CursorIterationOperation,
this,
base::Passed(&key),
base::Passed(&primary_key),
callbacks));
}
void IndexedDBCursor::Advance(uint32 count,
scoped_refptr<IndexedDBCallbacks> callbacks) {
IDB_TRACE("IndexedDBCursor::Advance");
transaction_->ScheduleTask(
task_type_,
base::Bind(
&IndexedDBCursor::CursorAdvanceOperation, this, count, callbacks));
}
void IndexedDBCursor::CursorAdvanceOperation(
uint32 count,
scoped_refptr<IndexedDBCallbacks> callbacks,
IndexedDBTransaction* /*transaction*/) {
IDB_TRACE("IndexedDBCursor::CursorAdvanceOperation");
leveldb::Status s;
// TODO(cmumford): Handle this error (crbug.com/363397). Although this will
// properly fail, caller will not know why, and any corruption
// will be ignored.
if (!cursor_ || !cursor_->Advance(count, &s)) {
cursor_.reset();
callbacks->OnSuccess(static_cast<IndexedDBValue*>(NULL));
return;
}
callbacks->OnSuccess(key(), primary_key(), Value());
}
void IndexedDBCursor::CursorIterationOperation(
scoped_ptr<IndexedDBKey> key,
scoped_ptr<IndexedDBKey> primary_key,
scoped_refptr<IndexedDBCallbacks> callbacks,
IndexedDBTransaction* /*transaction*/) {
IDB_TRACE("IndexedDBCursor::CursorIterationOperation");
leveldb::Status s;
// TODO(cmumford): Handle this error (crbug.com/363397). Although this will
// properly fail, caller will not know why, and any corruption
// will be ignored.
if (!cursor_ || !cursor_->Continue(key.get(),
primary_key.get(),
IndexedDBBackingStore::Cursor::SEEK,
&s) || !s.ok()) {
cursor_.reset();
callbacks->OnSuccess(static_cast<IndexedDBValue*>(NULL));
return;
}
callbacks->OnSuccess(this->key(), this->primary_key(), Value());
}
void IndexedDBCursor::PrefetchContinue(
int number_to_fetch,
scoped_refptr<IndexedDBCallbacks> callbacks) {
IDB_TRACE("IndexedDBCursor::PrefetchContinue");
transaction_->ScheduleTask(
task_type_,
base::Bind(&IndexedDBCursor::CursorPrefetchIterationOperation,
this,
number_to_fetch,
callbacks));
}
void IndexedDBCursor::CursorPrefetchIterationOperation(
int number_to_fetch,
scoped_refptr<IndexedDBCallbacks> callbacks,
IndexedDBTransaction* /*transaction*/) {
IDB_TRACE("IndexedDBCursor::CursorPrefetchIterationOperation");
std::vector<IndexedDBKey> found_keys;
std::vector<IndexedDBKey> found_primary_keys;
std::vector<IndexedDBValue> found_values;
saved_cursor_.reset();
const size_t max_size_estimate = 10 * 1024 * 1024;
size_t size_estimate = 0;
leveldb::Status s;
// TODO(cmumford): Handle this error (crbug.com/363397). Although this will
// properly fail, caller will not know why, and any corruption
// will be ignored.
for (int i = 0; i < number_to_fetch; ++i) {
if (!cursor_ || !cursor_->Continue(&s)) {
cursor_.reset();
break;
}
if (i == 0) {
// First prefetched result is always used, so that's the position
// a cursor should be reset to if the prefetch is invalidated.
saved_cursor_.reset(cursor_->Clone());
}
found_keys.push_back(cursor_->key());
found_primary_keys.push_back(cursor_->primary_key());
switch (cursor_type_) {
case indexed_db::CURSOR_KEY_ONLY:
found_values.push_back(IndexedDBValue());
break;
case indexed_db::CURSOR_KEY_AND_VALUE: {
IndexedDBValue value;
value.swap(*cursor_->value());
size_estimate += value.SizeEstimate();
found_values.push_back(value);
break;
}
default:
NOTREACHED();
}
size_estimate += cursor_->key().size_estimate();
size_estimate += cursor_->primary_key().size_estimate();
if (size_estimate > max_size_estimate)
break;
}
if (!found_keys.size()) {
callbacks->OnSuccess(static_cast<IndexedDBValue*>(NULL));
return;
}
callbacks->OnSuccessWithPrefetch(
found_keys, found_primary_keys, &found_values);
}
leveldb::Status IndexedDBCursor::PrefetchReset(int used_prefetches,
int /* unused_prefetches */) {
IDB_TRACE("IndexedDBCursor::PrefetchReset");
cursor_.swap(saved_cursor_);
saved_cursor_.reset();
leveldb::Status s;
if (closed_)
return s;
if (cursor_) {
// First prefetched result is always used.
DCHECK_GT(used_prefetches, 0);
for (int i = 0; i < used_prefetches - 1; ++i) {
bool ok = cursor_->Continue(&s);
DCHECK(ok);
}
}
return s;
}
void IndexedDBCursor::Close() {
IDB_TRACE("IndexedDBCursor::Close");
closed_ = true;
cursor_.reset();
saved_cursor_.reset();
}
} // namespace content
|