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" |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 const std::string resource_id_key = ResourceIdToKey(resource_id); | 132 const std::string resource_id_key = ResourceIdToKey(resource_id); |
133 const leveldb::Status db_status = level_db_->Get(leveldb::ReadOptions(), | 133 const leveldb::Status db_status = level_db_->Get(leveldb::ReadOptions(), |
134 leveldb::Slice(resource_id_key), &serialized_proto); | 134 leveldb::Slice(resource_id_key), &serialized_proto); |
135 | 135 |
136 if (db_status.IsNotFound()) | 136 if (db_status.IsNotFound()) |
137 return DB_KEY_NOT_FOUND; | 137 return DB_KEY_NOT_FOUND; |
138 | 138 |
139 if (db_status.ok()) { | 139 if (db_status.ok()) { |
140 DCHECK(!serialized_proto.empty()); | 140 DCHECK(!serialized_proto.empty()); |
141 *entry = GDataEntry::FromProtoString(serialized_proto); | 141 *entry = GDataEntry::FromProtoString(serialized_proto); |
142 DCHECK(entry->get()); | 142 return entry->get() ? DB_OK : DB_CORRUPTION; |
143 return DB_OK; | |
144 } | 143 } |
145 return GetStatus(db_status); | 144 return GetStatus(db_status); |
146 } | 145 } |
147 | 146 |
148 GDataDB::Status GDataLevelDB::GetByPath(const FilePath& path, | 147 GDataDB::Status GDataLevelDB::GetByPath(const FilePath& path, |
149 scoped_ptr<GDataEntry>* entry) { | 148 scoped_ptr<GDataEntry>* entry) { |
150 base::ThreadRestrictions::AssertIOAllowed(); | 149 base::ThreadRestrictions::AssertIOAllowed(); |
151 | 150 |
152 entry->reset(); | 151 entry->reset(); |
153 std::string resource_id; | 152 std::string resource_id; |
(...skipping 15 matching lines...) Expand all Loading... |
169 } | 168 } |
170 | 169 |
171 scoped_ptr<GDataDBIter> GDataLevelDB::CreateIterator(const FilePath& path) { | 170 scoped_ptr<GDataDBIter> GDataLevelDB::CreateIterator(const FilePath& path) { |
172 return scoped_ptr<GDataDBIter>(new GDataLevelDBIter( | 171 return scoped_ptr<GDataDBIter>(new GDataLevelDBIter( |
173 scoped_ptr<leveldb::Iterator>( | 172 scoped_ptr<leveldb::Iterator>( |
174 level_db_->NewIterator(leveldb::ReadOptions())), | 173 level_db_->NewIterator(leveldb::ReadOptions())), |
175 this, | 174 this, |
176 path)); | 175 path)); |
177 } | 176 } |
178 | 177 |
| 178 GDataDB::Status GDataLevelDB::PutRawForTesting( |
| 179 const std::string& resource_id, |
| 180 const std::string& raw_value) { |
| 181 const std::string resource_id_key = ResourceIdToKey(resource_id); |
| 182 leveldb::Status db_status = level_db_->Put( |
| 183 leveldb::WriteOptions(), |
| 184 leveldb::Slice(resource_id_key), |
| 185 leveldb::Slice(raw_value)); |
| 186 |
| 187 return GetStatus(db_status); |
| 188 } |
| 189 |
179 GDataLevelDBIter::GDataLevelDBIter(scoped_ptr<leveldb::Iterator> level_db_iter, | 190 GDataLevelDBIter::GDataLevelDBIter(scoped_ptr<leveldb::Iterator> level_db_iter, |
180 GDataDB* db, | 191 GDataDB* db, |
181 const FilePath& path) | 192 const FilePath& path) |
182 : level_db_iter_(level_db_iter.Pass()), | 193 : level_db_iter_(level_db_iter.Pass()), |
183 db_(db), | 194 db_(db), |
184 path_(path) { | 195 path_(path) { |
185 base::ThreadRestrictions::AssertIOAllowed(); | 196 base::ThreadRestrictions::AssertIOAllowed(); |
186 | 197 |
187 const std::string path_key = PathToKey(path); | 198 const std::string path_key = PathToKey(path); |
188 level_db_iter_->Seek(leveldb::Slice(path_key)); | 199 level_db_iter_->Seek(leveldb::Slice(path_key)); |
(...skipping 15 matching lines...) Expand all Loading... |
204 return false; | 215 return false; |
205 | 216 |
206 // Only consider keys under |path|. | 217 // Only consider keys under |path|. |
207 const std::string path_key = PathToKey(path_); | 218 const std::string path_key = PathToKey(path_); |
208 leveldb::Slice key_slice(level_db_iter_->key()); | 219 leveldb::Slice key_slice(level_db_iter_->key()); |
209 if (!key_slice.starts_with(path_key)) | 220 if (!key_slice.starts_with(path_key)) |
210 return false; | 221 return false; |
211 | 222 |
212 GDataDB::Status status = | 223 GDataDB::Status status = |
213 db_->GetByResourceId(level_db_iter_->value().ToString(), entry); | 224 db_->GetByResourceId(level_db_iter_->value().ToString(), entry); |
214 DCHECK_EQ(GDataDB::DB_OK, status); | 225 if (status != GDataDB::DB_OK) |
| 226 return false; |
215 | 227 |
216 key_slice.remove_prefix(sizeof(kPathPrefix) - 1); | 228 key_slice.remove_prefix(sizeof(kPathPrefix) - 1); |
217 path->assign(key_slice.ToString()); | 229 path->assign(key_slice.ToString()); |
218 | 230 |
219 level_db_iter_->Next(); | 231 level_db_iter_->Next(); |
220 return true; | 232 return true; |
221 } | 233 } |
222 | 234 |
223 } // namespace gdata | 235 } // namespace gdata |
OLD | NEW |