| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h" | 5 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 if (!db_iterator_->Valid()) | 86 if (!db_iterator_->Valid()) |
| 87 return map_iterator_->second.second; | 87 return map_iterator_->second.second; |
| 88 if (map_iterator_ == db_->pending_.end()) | 88 if (map_iterator_ == db_->pending_.end()) |
| 89 return db_iterator_->value(); | 89 return db_iterator_->value(); |
| 90 | 90 |
| 91 const leveldb::Slice db_key = db_iterator_->key(); | 91 const leveldb::Slice db_key = db_iterator_->key(); |
| 92 const leveldb::Slice map_key = map_iterator_->first; | 92 const leveldb::Slice map_key = map_iterator_->first; |
| 93 if (db_key.compare(map_key) < 0) | 93 if (db_key.compare(map_key) < 0) |
| 94 return db_iterator_->value(); | 94 return db_iterator_->value(); |
| 95 | 95 |
| 96 DCHECK(map_iterator_->second.first == DB_PUT); | 96 DCHECK(map_iterator_->second.first == PUT_OPERATION); |
| 97 return map_iterator_->second.second; | 97 return map_iterator_->second.second; |
| 98 } | 98 } |
| 99 | 99 |
| 100 void LevelDBWrapper::Iterator::AdvanceIterators() { | 100 void LevelDBWrapper::Iterator::AdvanceIterators() { |
| 101 // Iterator is valid iff any of below holds: | 101 // Iterator is valid iff any of below holds: |
| 102 // - |db_itr.key| < |map_itr.key| OR |map_itr| == end() | 102 // - |db_itr.key| < |map_itr.key| OR |map_itr| == end() |
| 103 // - (|db_itr.key| >= |map_itr.key| OR !|db_itr|->IsValid()) | 103 // - (|db_itr.key| >= |map_itr.key| OR !|db_itr|->IsValid()) |
| 104 // AND |map_itr.operation| == DB_PUT | 104 // AND |map_itr.operation| == PUT_OPERATION |
| 105 | 105 |
| 106 if (map_iterator_ == db_->pending_.end()) | 106 if (map_iterator_ == db_->pending_.end()) |
| 107 return; | 107 return; |
| 108 | 108 |
| 109 while (map_iterator_ != db_->pending_.end() && db_iterator_->Valid()) { | 109 while (map_iterator_ != db_->pending_.end() && db_iterator_->Valid()) { |
| 110 int cmp_key = db_iterator_->key().compare(map_iterator_->first); | 110 int cmp_key = db_iterator_->key().compare(map_iterator_->first); |
| 111 if (cmp_key < 0 || map_iterator_->second.first == DB_PUT) | 111 if (cmp_key < 0 || map_iterator_->second.first == PUT_OPERATION) |
| 112 return; | 112 return; |
| 113 // |db_itr.key| >= |map_itr.key| && |map_itr.operation| != DB_PUT | 113 // |db_itr.key| >= |map_itr.key| && |map_itr.operation| != PUT_OPERATION |
| 114 if (cmp_key == 0) | 114 if (cmp_key == 0) |
| 115 db_iterator_->Next(); | 115 db_iterator_->Next(); |
| 116 ++map_iterator_; | 116 ++map_iterator_; |
| 117 } | 117 } |
| 118 | 118 |
| 119 if (db_iterator_->Valid()) | 119 if (db_iterator_->Valid()) |
| 120 return; | 120 return; |
| 121 | 121 |
| 122 while (map_iterator_ != db_->pending_.end() && | 122 while (map_iterator_ != db_->pending_.end() && |
| 123 map_iterator_->second.first == DB_DELETE) | 123 map_iterator_->second.first == DELETE_OPERATION) |
| 124 ++map_iterator_; | 124 ++map_iterator_; |
| 125 } | 125 } |
| 126 | 126 |
| 127 // --------------------------------------------------------------------------- | 127 // --------------------------------------------------------------------------- |
| 128 // LevelDBWrapper class | 128 // LevelDBWrapper class |
| 129 // --------------------------------------------------------------------------- | 129 // --------------------------------------------------------------------------- |
| 130 LevelDBWrapper::LevelDBWrapper(scoped_ptr<leveldb::DB> db) | 130 LevelDBWrapper::LevelDBWrapper(scoped_ptr<leveldb::DB> db) |
| 131 : db_(db.Pass()) { | 131 : db_(db.Pass()) { |
| 132 DCHECK(db_); | 132 DCHECK(db_); |
| 133 } | 133 } |
| 134 | 134 |
| 135 LevelDBWrapper::~LevelDBWrapper() {} | 135 LevelDBWrapper::~LevelDBWrapper() {} |
| 136 | 136 |
| 137 void LevelDBWrapper::Put(const std::string& key, | 137 void LevelDBWrapper::Put(const std::string& key, |
| 138 const std::string& value) { | 138 const std::string& value) { |
| 139 pending_[key] = Transaction(DB_PUT, value); | 139 pending_[key] = Transaction(PUT_OPERATION, value); |
| 140 } | 140 } |
| 141 | 141 |
| 142 void LevelDBWrapper::Delete(const std::string& key) { | 142 void LevelDBWrapper::Delete(const std::string& key) { |
| 143 pending_[key] = Transaction(DB_DELETE, std::string()); | 143 pending_[key] = Transaction(DELETE_OPERATION, std::string()); |
| 144 } | 144 } |
| 145 | 145 |
| 146 leveldb::Status LevelDBWrapper::Get(const std::string& key, | 146 leveldb::Status LevelDBWrapper::Get(const std::string& key, |
| 147 std::string* value) { | 147 std::string* value) { |
| 148 PendingOperationMap::iterator itr = pending_.find(key); | 148 PendingOperationMap::iterator itr = pending_.find(key); |
| 149 if (itr == pending_.end()) | 149 if (itr == pending_.end()) |
| 150 return db_->Get(leveldb::ReadOptions(), key, value); | 150 return db_->Get(leveldb::ReadOptions(), key, value); |
| 151 | 151 |
| 152 const Transaction& transaction = itr->second; | 152 const Transaction& transaction = itr->second; |
| 153 switch (transaction.first) { | 153 switch (transaction.first) { |
| 154 case DB_PUT: | 154 case PUT_OPERATION: |
| 155 *value = transaction.second; | 155 *value = transaction.second; |
| 156 return leveldb::Status(); | 156 return leveldb::Status(); |
| 157 case DB_DELETE: | 157 case DELETE_OPERATION: |
| 158 return leveldb::Status::NotFound(leveldb::Slice()); | 158 return leveldb::Status::NotFound(leveldb::Slice()); |
| 159 } | 159 } |
| 160 NOTREACHED(); | 160 NOTREACHED(); |
| 161 return leveldb::Status::NotSupported("Not supported operation."); | 161 return leveldb::Status::NotSupported("Not supported operation."); |
| 162 } | 162 } |
| 163 | 163 |
| 164 scoped_ptr<LevelDBWrapper::Iterator> LevelDBWrapper::NewIterator() { | 164 scoped_ptr<LevelDBWrapper::Iterator> LevelDBWrapper::NewIterator() { |
| 165 return make_scoped_ptr(new Iterator(this)); | 165 return make_scoped_ptr(new Iterator(this)); |
| 166 } | 166 } |
| 167 | 167 |
| 168 leveldb::Status LevelDBWrapper::Commit() { | 168 leveldb::Status LevelDBWrapper::Commit() { |
| 169 leveldb::WriteBatch batch; | 169 leveldb::WriteBatch batch; |
| 170 for (PendingOperationMap::iterator itr = pending_.begin(); | 170 for (PendingOperationMap::iterator itr = pending_.begin(); |
| 171 itr != pending_.end(); ++itr) { | 171 itr != pending_.end(); ++itr) { |
| 172 const leveldb::Slice key(itr->first); | 172 const leveldb::Slice key(itr->first); |
| 173 const Transaction& transaction = itr->second; | 173 const Transaction& transaction = itr->second; |
| 174 switch (transaction.first) { | 174 switch (transaction.first) { |
| 175 case DB_PUT: | 175 case PUT_OPERATION: |
| 176 batch.Put(key, transaction.second); | 176 batch.Put(key, transaction.second); |
| 177 break; | 177 break; |
| 178 case DB_DELETE: | 178 case DELETE_OPERATION: |
| 179 batch.Delete(key); | 179 batch.Delete(key); |
| 180 break; | 180 break; |
| 181 } | 181 } |
| 182 } | 182 } |
| 183 | 183 |
| 184 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); | 184 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); |
| 185 // TODO(peria): Decide what to do depending on |status|. | 185 // TODO(peria): Decide what to do depending on |status|. |
| 186 if (status.ok()) | 186 if (status.ok()) |
| 187 Clear(); | 187 Clear(); |
| 188 | 188 |
| 189 return status; | 189 return status; |
| 190 } | 190 } |
| 191 | 191 |
| 192 void LevelDBWrapper::Clear() { | 192 void LevelDBWrapper::Clear() { |
| 193 pending_.clear(); | 193 pending_.clear(); |
| 194 } | 194 } |
| 195 | 195 |
| 196 leveldb::DB* LevelDBWrapper::GetLevelDBForTesting() { | 196 leveldb::DB* LevelDBWrapper::GetLevelDBForTesting() { |
| 197 return db_.get(); | 197 return db_.get(); |
| 198 } | 198 } |
| 199 | 199 |
| 200 } // namespace drive_backend | 200 } // namespace drive_backend |
| 201 } // namespace sync_file_system | 201 } // namespace sync_file_system |
| OLD | NEW |