OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/chromeos/gdata/gdata_leveldb.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_leveldb.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
11 #include "chrome/browser/chromeos/gdata/gdata_files.h" | 11 #include "chrome/browser/chromeos/gdata/gdata_files.h" |
12 #include "leveldb/write_batch.h" | 12 #include "leveldb/write_batch.h" |
13 #include "third_party/protobuf/src/google/protobuf/message_lite.h" | |
13 | 14 |
14 namespace gdata { | 15 namespace gdata { |
15 namespace { | 16 namespace { |
16 | 17 |
17 const char kResourceIdPrefix[] = "r:"; | 18 const char kResourceIdPrefix[] = "r:"; |
18 const char kPathPrefix[] = "p:"; | 19 const char kPathPrefix[] = "p:"; |
19 | 20 |
20 // Append prefix id: to |resource_id|. | 21 // Append prefix id: to |resource_id|. |
21 std::string ResourceIdToKey(const std::string& resource_id) { | 22 std::string ResourceIdToKey(const std::string& resource_id) { |
22 return std::string(kResourceIdPrefix) + resource_id; | 23 return std::string(kResourceIdPrefix) + resource_id; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 const std::string resource_id_key = ResourceIdToKey(resource_id); | 133 const std::string resource_id_key = ResourceIdToKey(resource_id); |
133 const leveldb::Status db_status = level_db_->Get(leveldb::ReadOptions(), | 134 const leveldb::Status db_status = level_db_->Get(leveldb::ReadOptions(), |
134 leveldb::Slice(resource_id_key), &serialized_proto); | 135 leveldb::Slice(resource_id_key), &serialized_proto); |
135 | 136 |
136 if (db_status.IsNotFound()) | 137 if (db_status.IsNotFound()) |
137 return DB_KEY_NOT_FOUND; | 138 return DB_KEY_NOT_FOUND; |
138 | 139 |
139 if (db_status.ok()) { | 140 if (db_status.ok()) { |
140 DCHECK(!serialized_proto.empty()); | 141 DCHECK(!serialized_proto.empty()); |
141 *entry = GDataEntry::FromProtoString(serialized_proto); | 142 *entry = GDataEntry::FromProtoString(serialized_proto); |
142 DCHECK(entry->get()); | 143 if (!entry->get()) |
achuithb
2012/07/10 17:21:19
nit: can we make this a ternary return? It saves 2
satorux1
2012/07/10 17:28:35
Done.
| |
144 return DB_CORRUPTION; | |
143 return DB_OK; | 145 return DB_OK; |
144 } | 146 } |
145 return GetStatus(db_status); | 147 return GetStatus(db_status); |
146 } | 148 } |
147 | 149 |
148 GDataDB::Status GDataLevelDB::GetByPath(const FilePath& path, | 150 GDataDB::Status GDataLevelDB::GetByPath(const FilePath& path, |
149 scoped_ptr<GDataEntry>* entry) { | 151 scoped_ptr<GDataEntry>* entry) { |
150 base::ThreadRestrictions::AssertIOAllowed(); | 152 base::ThreadRestrictions::AssertIOAllowed(); |
151 | 153 |
152 entry->reset(); | 154 entry->reset(); |
(...skipping 16 matching lines...) Expand all Loading... | |
169 } | 171 } |
170 | 172 |
171 scoped_ptr<GDataDBIter> GDataLevelDB::CreateIterator(const FilePath& path) { | 173 scoped_ptr<GDataDBIter> GDataLevelDB::CreateIterator(const FilePath& path) { |
172 return scoped_ptr<GDataDBIter>(new GDataLevelDBIter( | 174 return scoped_ptr<GDataDBIter>(new GDataLevelDBIter( |
173 scoped_ptr<leveldb::Iterator>( | 175 scoped_ptr<leveldb::Iterator>( |
174 level_db_->NewIterator(leveldb::ReadOptions())), | 176 level_db_->NewIterator(leveldb::ReadOptions())), |
175 this, | 177 this, |
176 path)); | 178 path)); |
177 } | 179 } |
178 | 180 |
181 GDataDB::Status GDataLevelDB::PutRawForTesting( | |
182 const std::string& resource_id, | |
183 const google::protobuf::MessageLite& protobuf) { | |
184 std::string serialized_proto; | |
185 protobuf.SerializeToString(&serialized_proto); | |
186 | |
187 const std::string resource_id_key = | |
188 ResourceIdToKey(resource_id); | |
189 leveldb::Status db_status = level_db_->Put( | |
190 leveldb::WriteOptions(), | |
191 leveldb::Slice(resource_id_key), | |
192 leveldb::Slice(serialized_proto)); | |
193 | |
194 return GetStatus(db_status); | |
195 } | |
196 | |
179 GDataLevelDBIter::GDataLevelDBIter(scoped_ptr<leveldb::Iterator> level_db_iter, | 197 GDataLevelDBIter::GDataLevelDBIter(scoped_ptr<leveldb::Iterator> level_db_iter, |
180 GDataDB* db, | 198 GDataDB* db, |
181 const FilePath& path) | 199 const FilePath& path) |
182 : level_db_iter_(level_db_iter.Pass()), | 200 : level_db_iter_(level_db_iter.Pass()), |
183 db_(db), | 201 db_(db), |
184 path_(path) { | 202 path_(path) { |
185 base::ThreadRestrictions::AssertIOAllowed(); | 203 base::ThreadRestrictions::AssertIOAllowed(); |
186 | 204 |
187 const std::string path_key = PathToKey(path); | 205 const std::string path_key = PathToKey(path); |
188 level_db_iter_->Seek(leveldb::Slice(path_key)); | 206 level_db_iter_->Seek(leveldb::Slice(path_key)); |
(...skipping 15 matching lines...) Expand all Loading... | |
204 return false; | 222 return false; |
205 | 223 |
206 // Only consider keys under |path|. | 224 // Only consider keys under |path|. |
207 const std::string path_key = PathToKey(path_); | 225 const std::string path_key = PathToKey(path_); |
208 leveldb::Slice key_slice(level_db_iter_->key()); | 226 leveldb::Slice key_slice(level_db_iter_->key()); |
209 if (!key_slice.starts_with(path_key)) | 227 if (!key_slice.starts_with(path_key)) |
210 return false; | 228 return false; |
211 | 229 |
212 GDataDB::Status status = | 230 GDataDB::Status status = |
213 db_->GetByResourceId(level_db_iter_->value().ToString(), entry); | 231 db_->GetByResourceId(level_db_iter_->value().ToString(), entry); |
214 DCHECK_EQ(GDataDB::DB_OK, status); | 232 if (status != GDataDB::DB_OK) |
233 return false; | |
215 | 234 |
216 key_slice.remove_prefix(sizeof(kPathPrefix) - 1); | 235 key_slice.remove_prefix(sizeof(kPathPrefix) - 1); |
217 path->assign(key_slice.ToString()); | 236 path->assign(key_slice.ToString()); |
218 | 237 |
219 level_db_iter_->Next(); | 238 level_db_iter_->Next(); |
220 return true; | 239 return true; |
221 } | 240 } |
222 | 241 |
223 } // namespace gdata | 242 } // namespace gdata |
OLD | NEW |