Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/gdata/gdata_leveldb.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "chrome/browser/chromeos/gdata/gdata_files.h" | |
| 11 #include "leveldb/write_batch.h" | |
| 12 | |
| 13 namespace gdata { | |
| 14 namespace { | |
| 15 | |
| 16 const char kResourceIdPrefix[] = "id:"; | |
| 17 const char kPathPrefix[] = "path:"; | |
|
zel
2012/04/25 00:49:47
why do you need these prefixes at all?
achuithb
2012/04/25 00:57:53
We're storing paths and resource_ids in the same t
zel
2012/04/25 01:18:33
you should shorten them to r: and p: instead... al
achuithb
2012/04/25 01:20:26
Since the key is generated with GDataEntry::GetFil
| |
| 18 | |
| 19 // Append prefix id: to |resource_id|. | |
| 20 std::string ResourceIdToKey(const std::string& resource_id) { | |
| 21 return std::string(kResourceIdPrefix) + resource_id; | |
| 22 } | |
| 23 | |
| 24 // Append prefix path: to |path|. | |
| 25 std::string PathToKey(const FilePath& path) { | |
| 26 return std::string(kPathPrefix) + path.value(); | |
| 27 } | |
| 28 | |
| 29 GDataDB::Status GetStatus(const leveldb::Status& db_status) { | |
| 30 if (db_status.ok()) | |
| 31 return GDataDB::DB_OK; | |
| 32 | |
| 33 if (db_status.IsNotFound()) | |
| 34 return GDataDB::DB_KEY_NOT_FOUND; | |
| 35 | |
| 36 if (db_status.IsCorruption()) | |
| 37 return GDataDB::DB_CORRUPTION; | |
| 38 | |
| 39 if (db_status.IsIOError()) | |
| 40 return GDataDB::DB_IO_ERROR; | |
| 41 | |
| 42 NOTREACHED(); | |
| 43 return GDataDB::DB_INTERNAL_ERROR; | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 GDataLevelDB::GDataLevelDB() { | |
| 49 } | |
| 50 | |
| 51 GDataLevelDB::~GDataLevelDB() { | |
| 52 } | |
| 53 | |
| 54 void GDataLevelDB::Init(const FilePath& db_path) { | |
| 55 leveldb::DB* level_db = NULL; | |
| 56 leveldb::Options options; | |
| 57 options.create_if_missing = true; | |
| 58 leveldb::Status db_status = leveldb::DB::Open(options, | |
| 59 db_path.Append("level_db").value(), &level_db); | |
| 60 DCHECK(level_db); | |
| 61 // TODO(achuith): If db cannot be opened, we should try to recover it. | |
| 62 // If that fails, we should just delete it and create a new file. | |
| 63 DCHECK(db_status.ok()); | |
| 64 level_db_.reset(level_db); | |
| 65 } | |
| 66 | |
| 67 GDataDB::Status GDataLevelDB::Put(const GDataEntry& entry) { | |
| 68 // Write the serialized proto. | |
| 69 std::string serialized_proto; | |
| 70 entry.SerializeToString(&serialized_proto); | |
| 71 | |
| 72 leveldb::WriteBatch batch; | |
| 73 const std::string resource_id_key = | |
| 74 ResourceIdToKey(entry.resource_id()); | |
| 75 const std::string path_key = PathToKey(entry.GetFilePath()); | |
| 76 batch.Put(leveldb::Slice(resource_id_key), leveldb::Slice(serialized_proto)); | |
| 77 // Note we store the resource_id without prefix when it's the value. | |
| 78 batch.Put(leveldb::Slice(path_key), leveldb::Slice(entry.resource_id())); | |
| 79 leveldb::Status db_status = level_db_->Write( | |
| 80 leveldb::WriteOptions(), | |
| 81 &batch); | |
| 82 | |
| 83 DVLOG(1) << "GDataLevelDB::Put resource_id key = " << resource_id_key | |
| 84 << ", path key = " << path_key; | |
| 85 return GetStatus(db_status); | |
| 86 } | |
| 87 | |
| 88 GDataDB::Status GDataLevelDB::DeleteByResourceId( | |
| 89 const std::string& resource_id) { | |
| 90 scoped_ptr<GDataEntry> entry; | |
| 91 Status status = GetByResourceId(resource_id, &entry); | |
| 92 if (status == DB_KEY_NOT_FOUND) | |
| 93 return DB_OK; | |
| 94 else if (status != DB_OK) | |
| 95 return status; | |
| 96 | |
| 97 leveldb::WriteBatch batch; | |
| 98 const std::string resource_id_key = ResourceIdToKey(resource_id); | |
| 99 const std::string path_key = PathToKey(entry->GetFilePath()); | |
| 100 batch.Delete(leveldb::Slice(resource_id_key)); | |
| 101 batch.Delete(leveldb::Slice(path_key)); | |
| 102 | |
| 103 leveldb::Status db_status = level_db_->Write(leveldb::WriteOptions(), | |
| 104 &batch); | |
| 105 return GetStatus(db_status); | |
| 106 } | |
| 107 | |
| 108 GDataDB::Status GDataLevelDB::DeleteByPath( | |
| 109 const FilePath& path) { | |
| 110 std::string resource_id; | |
| 111 const Status status = ResourceIdForPath(path, &resource_id); | |
| 112 if (status != DB_OK) | |
| 113 return status; | |
| 114 return DeleteByResourceId(resource_id); | |
| 115 } | |
| 116 | |
| 117 GDataDB::Status GDataLevelDB::GetByResourceId(const std::string& resource_id, | |
| 118 scoped_ptr<GDataEntry>* entry) { | |
| 119 entry->reset(); | |
| 120 std::string serialized_proto; | |
| 121 const std::string resource_id_key = ResourceIdToKey(resource_id); | |
| 122 const leveldb::Status db_status = level_db_->Get(leveldb::ReadOptions(), | |
| 123 leveldb::Slice(resource_id_key), &serialized_proto); | |
| 124 | |
| 125 if (db_status.IsNotFound()) | |
| 126 return DB_KEY_NOT_FOUND; | |
| 127 | |
| 128 if (db_status.ok()) { | |
| 129 DCHECK(!serialized_proto.empty()); | |
| 130 *entry = GDataEntry::FromProtoString(serialized_proto); | |
| 131 DCHECK(entry->get()); | |
| 132 return DB_OK; | |
| 133 } | |
| 134 return GetStatus(db_status); | |
| 135 } | |
| 136 | |
| 137 GDataDB::Status GDataLevelDB::GetByPath(const FilePath& path, | |
| 138 scoped_ptr<GDataEntry>* entry) { | |
| 139 entry->reset(); | |
| 140 std::string resource_id; | |
| 141 const Status status = ResourceIdForPath(path, &resource_id); | |
| 142 if (status != DB_OK) | |
| 143 return status; | |
| 144 return GetByResourceId(resource_id, entry); | |
| 145 } | |
| 146 | |
| 147 GDataDB::Status GDataLevelDB::ResourceIdForPath(const FilePath& path, | |
| 148 std::string* resource_id) { | |
| 149 const std::string path_key = PathToKey(path); | |
| 150 const leveldb::Status db_status = level_db_->Get( | |
| 151 leveldb::ReadOptions(), path_key, resource_id); | |
| 152 | |
| 153 return GetStatus(db_status); | |
| 154 } | |
| 155 | |
| 156 scoped_ptr<GDataDBIter> GDataLevelDB::CreateIterator(const FilePath& path) { | |
| 157 return scoped_ptr<GDataDBIter>(new GDataLevelDBIter( | |
| 158 scoped_ptr<leveldb::Iterator>( | |
| 159 level_db_->NewIterator(leveldb::ReadOptions())), | |
| 160 this, | |
| 161 path)); | |
| 162 } | |
| 163 | |
| 164 GDataLevelDBIter::GDataLevelDBIter(scoped_ptr<leveldb::Iterator> level_db_iter, | |
| 165 GDataDB* db, | |
| 166 const FilePath& path) | |
| 167 : level_db_iter_(level_db_iter.Pass()), | |
| 168 db_(db), | |
| 169 path_(path) { | |
| 170 const std::string path_key = PathToKey(path); | |
| 171 level_db_iter_->Seek(leveldb::Slice(path_key)); | |
| 172 } | |
| 173 | |
| 174 GDataLevelDBIter::~GDataLevelDBIter() { | |
| 175 } | |
| 176 | |
| 177 bool GDataLevelDBIter::GetNext(std::string* path, | |
| 178 scoped_ptr<GDataEntry>* entry) { | |
| 179 DCHECK(path); | |
| 180 DCHECK(entry); | |
| 181 path->clear(); | |
| 182 entry->reset(); | |
| 183 | |
| 184 if (!level_db_iter_->Valid()) | |
| 185 return false; | |
| 186 | |
| 187 // Only consider keys under |path|. | |
| 188 const std::string path_key = PathToKey(path_); | |
| 189 leveldb::Slice key_slice(level_db_iter_->key()); | |
| 190 if (!key_slice.starts_with(path_key)) | |
| 191 return false; | |
| 192 | |
| 193 GDataDB::Status status = | |
| 194 db_->GetByResourceId(level_db_iter_->value().ToString(), entry); | |
| 195 DCHECK_EQ(GDataDB::DB_OK, status); | |
| 196 | |
| 197 key_slice.remove_prefix(sizeof(kPathPrefix) - 1); | |
| 198 path->assign(key_slice.ToString()); | |
| 199 | |
| 200 level_db_iter_->Next(); | |
| 201 return true; | |
| 202 } | |
| 203 | |
| 204 } // namespace gdata | |
| OLD | NEW |