indexed_db_cursor.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved. 2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file. 4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/browser/indexed_db/indexed_db_cursor.h" 6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 73551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/bind.h" 8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/logging.h" 9eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "content/browser/indexed_db/indexed_db_callbacks.h" 10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/browser/indexed_db/indexed_db_database_error.h" 11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/browser/indexed_db/indexed_db_tracing.h" 12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/browser/indexed_db/indexed_db_transaction.h" 13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace content { 15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)IndexedDBCursor::IndexedDBCursor( 17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) scoped_ptr<IndexedDBBackingStore::Cursor> cursor, 18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) indexed_db::CursorType cursor_type, 19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) IndexedDBDatabase::TaskType task_type, 20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) IndexedDBTransaction* transaction) 21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) : task_type_(task_type), 22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) cursor_type_(cursor_type), 23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) transaction_(transaction), 24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) cursor_(cursor.Pass()), 25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) closed_(false) { 26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) transaction_->RegisterOpenCursor(this); 27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)IndexedDBCursor::~IndexedDBCursor() { 30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) transaction_->UnregisterOpenCursor(this); 31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid IndexedDBCursor::Continue(scoped_ptr<IndexedDBKey> key, 34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) scoped_ptr<IndexedDBKey> primary_key, 357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch scoped_refptr<IndexedDBCallbacks> callbacks) { 36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch IDB_TRACE("IndexedDBCursor::Continue"); 37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) transaction_->ScheduleTask( 393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) task_type_, 403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) base::Bind(&IndexedDBCursor::CursorIterationOperation, 413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) this, 423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) base::Passed(&key), 43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) base::Passed(&primary_key), 443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) callbacks)); 45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid IndexedDBCursor::Advance(uint32 count, 48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch scoped_refptr<IndexedDBCallbacks> callbacks) { 49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch IDB_TRACE("IndexedDBCursor::Advance"); 50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) transaction_->ScheduleTask( 523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) task_type_, 533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) base::Bind( 543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) &IndexedDBCursor::CursorAdvanceOperation, this, count, callbacks)); 55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void IndexedDBCursor::CursorAdvanceOperation( 583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) uint32 count, 593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) scoped_refptr<IndexedDBCallbacks> callbacks, 60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) IndexedDBTransaction* /*transaction*/) { 613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) IDB_TRACE("IndexedDBCursor::CursorAdvanceOperation"); 623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (!cursor_ || !cursor_->Advance(count)) { 633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) cursor_.reset(); 643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) callbacks->OnSuccess(static_cast<std::string*>(NULL)); 65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return; 66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) callbacks->OnSuccess(key(), primary_key(), Value()); 69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void IndexedDBCursor::CursorIterationOperation( 723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) scoped_ptr<IndexedDBKey> key, 73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) scoped_ptr<IndexedDBKey> primary_key, 743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) scoped_refptr<IndexedDBCallbacks> callbacks, 75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) IndexedDBTransaction* /*transaction*/) { 763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) IDB_TRACE("IndexedDBCursor::CursorIterationOperation"); 773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (!cursor_ || 78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) !cursor_->Continue( 79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) key.get(), primary_key.get(), IndexedDBBackingStore::Cursor::SEEK)) { 803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) cursor_.reset(); 813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) callbacks->OnSuccess(static_cast<std::string*>(NULL)); 82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return; 83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) callbacks->OnSuccess(this->key(), this->primary_key(), Value()); 86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void IndexedDBCursor::PrefetchContinue( 89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) int number_to_fetch, 90eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch scoped_refptr<IndexedDBCallbacks> callbacks) { 91eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch IDB_TRACE("IndexedDBCursor::PrefetchContinue"); 92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) transaction_->ScheduleTask( 94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) task_type_, 953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) base::Bind(&IndexedDBCursor::CursorPrefetchIterationOperation, 963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) this, 973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) number_to_fetch, 983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) callbacks)); 99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void IndexedDBCursor::CursorPrefetchIterationOperation( 1023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) int number_to_fetch, 1033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) scoped_refptr<IndexedDBCallbacks> callbacks, 104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) IndexedDBTransaction* /*transaction*/) { 1053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) IDB_TRACE("IndexedDBCursor::CursorPrefetchIterationOperation"); 106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::vector<IndexedDBKey> found_keys; 108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::vector<IndexedDBKey> found_primary_keys; 1097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch std::vector<std::string> found_values; 110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (cursor_) 1123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) saved_cursor_.reset(cursor_->Clone()); 113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const size_t max_size_estimate = 10 * 1024 * 1024; 114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) size_t size_estimate = 0; 115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) for (int i = 0; i < number_to_fetch; ++i) { 1173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (!cursor_ || !cursor_->Continue()) { 1183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) cursor_.reset(); 119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) break; 120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) found_keys.push_back(cursor_->key()); 1233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) found_primary_keys.push_back(cursor_->primary_key()); 124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) switch (cursor_type_) { 126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) case indexed_db::CURSOR_KEY_ONLY: 1277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch found_values.push_back(std::string()); 128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) break; 129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) case indexed_db::CURSOR_KEY_AND_VALUE: { 1307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch std::string value; 131a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) value.swap(*cursor_->value()); 132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) size_estimate += value.size(); 133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) found_values.push_back(value); 134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) break; 135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) default: 137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) NOTREACHED(); 138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 1393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) size_estimate += cursor_->key().size_estimate(); 1403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) size_estimate += cursor_->primary_key().size_estimate(); 141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (size_estimate > max_size_estimate) 143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) break; 144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (!found_keys.size()) { 1473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) callbacks->OnSuccess(static_cast<std::string*>(NULL)); 148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return; 149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) callbacks->OnSuccessWithPrefetch( 152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) found_keys, found_primary_keys, found_values); 153868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void IndexedDBCursor::PrefetchReset(int used_prefetches, int) { 156eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch IDB_TRACE("IndexedDBCursor::PrefetchReset"); 157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) cursor_.swap(saved_cursor_); 158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) saved_cursor_.reset(); 159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (closed_) 161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return; 162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (cursor_) { 163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (int i = 0; i < used_prefetches; ++i) { 1647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch bool ok = cursor_->Continue(); 165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) DCHECK(ok); 166868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 167868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 168868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void IndexedDBCursor::Close() { 171eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch IDB_TRACE("IndexedDBCursor::Close"); 172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) closed_ = true; 173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) cursor_.reset(); 174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) saved_cursor_.reset(); 175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 177868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} // namespace content 178