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 | |
| 12 namespace gdata { | |
| 13 | |
| 14 // static | |
| 15 scoped_ptr<GDataDB> GDataDB::Create(const FilePath& db_path) { | |
| 16 DVLOG(1) << "GDataDB::Create " << db_path.value(); | |
| 17 GDataLevelDB* level_db = new GDataLevelDB(); | |
| 18 level_db->Init(db_path); | |
| 19 return scoped_ptr<GDataDB>(level_db); | |
| 20 } | |
|
satorux1
2012/04/23 17:40:41
having this here looks weird. You might want to in
achuithb
2012/04/24 08:09:36
Done.
| |
| 21 | |
| 22 GDataLevelDB::GDataLevelDB() { | |
| 23 } | |
| 24 | |
| 25 GDataLevelDB::~GDataLevelDB() { | |
| 26 } | |
| 27 | |
| 28 void GDataLevelDB::Init(const FilePath& db_path) { | |
| 29 // Open the primary database with file_path key. | |
| 30 leveldb::DB* level_db = NULL; | |
| 31 leveldb::Options options; | |
| 32 options.create_if_missing = true; | |
| 33 leveldb::Status status = leveldb::DB::Open(options, | |
| 34 db_path.Append("main").value(), &level_db); | |
| 35 DCHECK(level_db); | |
| 36 DCHECK(status.ok()); | |
| 37 main_db_.reset(level_db); | |
| 38 level_db = NULL; | |
| 39 | |
| 40 // Secondary database keyed on path. | |
| 41 status = leveldb::DB::Open(options, | |
| 42 db_path.Append("path").value(), &level_db); | |
| 43 DCHECK(level_db); | |
| 44 DCHECK(status.ok()); | |
| 45 path_db_.reset(level_db); | |
| 46 } | |
| 47 | |
| 48 GDataDB::Status GDataLevelDB::Put(const GDataEntry& file) { | |
| 49 // TODO(achuith): Batch write operation | |
| 50 // Write the serialized proto. | |
| 51 std::string serialized_proto; | |
| 52 file.SerializeToString(&serialized_proto); | |
| 53 leveldb::Status status = main_db_->Put( | |
| 54 leveldb::WriteOptions(), | |
| 55 leveldb::Slice(file.resource_id()), | |
| 56 leveldb::Slice(serialized_proto)); | |
| 57 if (!status.ok()) { | |
| 58 NOTREACHED(); | |
| 59 DeleteByResourceId(file.resource_id()); | |
| 60 return DB_ERR; | |
| 61 } | |
| 62 | |
| 63 // Write the path. | |
| 64 status = path_db_->Put( | |
| 65 leveldb::WriteOptions(), | |
| 66 leveldb::Slice(file.file_name()), | |
| 67 leveldb::Slice(file.resource_id())); | |
| 68 if (!status.ok()) { | |
| 69 NOTREACHED(); | |
| 70 DeleteByResourceId(file.resource_id()); | |
| 71 return DB_ERR; | |
| 72 } | |
| 73 return DB_OK; | |
| 74 } | |
| 75 | |
| 76 GDataDB::Status GDataLevelDB::DeleteByResourceId( | |
| 77 const std::string& resource_id) { | |
| 78 scoped_ptr<GDataEntry> file; | |
| 79 Status status = GetByResourceId(resource_id, &file); | |
| 80 if (status == DB_ERR) | |
| 81 return DB_ERR; | |
| 82 else if (status == DB_NOT_FOUND) | |
| 83 return DB_OK; | |
| 84 | |
| 85 leveldb::Status db_status1 = main_db_->Delete( | |
| 86 leveldb::WriteOptions(), | |
| 87 leveldb::Slice(resource_id)); | |
| 88 | |
| 89 leveldb::Status db_status2 = path_db_->Delete( | |
| 90 leveldb::WriteOptions(), | |
| 91 leveldb::Slice(file->file_name())); | |
|
satorux1
2012/04/23 17:40:41
what if status1 succeeds but status2 fails? I thou
achuithb
2012/04/24 08:09:36
Done.
| |
| 92 | |
| 93 return db_status1.ok() && db_status2.ok() ? DB_OK : DB_ERR; | |
| 94 } | |
| 95 | |
| 96 GDataDB::Status GDataLevelDB::DeleteByPath( | |
| 97 const std::string& path) { | |
| 98 std::string resource_id; | |
| 99 const Status status = ResourceIdForPath(path, &resource_id); | |
| 100 if (status != DB_OK) | |
| 101 return status; | |
| 102 return DeleteByResourceId(resource_id); | |
| 103 } | |
| 104 | |
| 105 GDataDB::Status GDataLevelDB::GetByResourceId(const std::string& resource_id, | |
| 106 scoped_ptr<GDataEntry>* file) { | |
| 107 file->reset(); | |
| 108 std::string serialized_proto; | |
| 109 const leveldb::Status status = main_db_->Get(leveldb::ReadOptions(), | |
| 110 leveldb::Slice(resource_id), &serialized_proto); | |
| 111 | |
| 112 if (status.IsNotFound()) | |
| 113 return DB_NOT_FOUND; | |
| 114 | |
| 115 if (status.ok()) { | |
| 116 DCHECK(!serialized_proto.empty()); | |
| 117 *file = GDataEntry::FromProtoString(serialized_proto); | |
| 118 DCHECK(file->get()); | |
| 119 return DB_OK; | |
| 120 } | |
| 121 return DB_ERR; | |
| 122 } | |
| 123 | |
| 124 GDataDB::Status GDataLevelDB::GetByPath(const std::string& path, | |
| 125 scoped_ptr<GDataEntry>* file) { | |
| 126 file->reset(); | |
| 127 std::string resource_id; | |
| 128 const Status status = ResourceIdForPath(path, &resource_id); | |
| 129 if (status != DB_OK) | |
| 130 return status; | |
| 131 return GetByResourceId(resource_id, file); | |
| 132 } | |
| 133 | |
| 134 GDataDB::Status GDataLevelDB::ResourceIdForPath(const std::string& path, | |
| 135 std::string* resource_id) { | |
| 136 const leveldb::Status db_status = path_db_->Get( | |
| 137 leveldb::ReadOptions(), leveldb::Slice(path), resource_id); | |
| 138 | |
| 139 if (db_status.ok()) | |
| 140 return DB_OK; | |
| 141 else if (db_status.IsNotFound()) | |
| 142 return DB_NOT_FOUND; | |
| 143 else | |
| 144 return DB_ERR; | |
| 145 } | |
| 146 | |
| 147 scoped_ptr<GDataDBIter> GDataLevelDB::NewIterator(const std::string& path) { | |
| 148 return scoped_ptr<GDataDBIter>(new GDataLevelDBIter( | |
| 149 scoped_ptr<leveldb::Iterator>( | |
| 150 path_db_->NewIterator(leveldb::ReadOptions())), | |
| 151 this, | |
| 152 path)); | |
| 153 } | |
| 154 | |
| 155 GDataLevelDBIter::GDataLevelDBIter(scoped_ptr<leveldb::Iterator> level_db_iter, | |
| 156 GDataDB* db, | |
| 157 const std::string& path) | |
| 158 : level_db_iter_(level_db_iter.Pass()), | |
| 159 db_(db), | |
| 160 path_(path) { | |
| 161 level_db_iter_->Seek(path); | |
| 162 } | |
| 163 | |
| 164 GDataLevelDBIter::~GDataLevelDBIter() { | |
| 165 } | |
| 166 | |
| 167 scoped_ptr<GDataEntry> GDataLevelDBIter::GetNext() { | |
| 168 if (!level_db_iter_->Valid()) | |
| 169 return scoped_ptr<GDataEntry>(NULL); | |
| 170 | |
| 171 // Only consider keys under |path|. | |
| 172 if (level_db_iter_->key().ToString().find(path_) != 0) | |
|
satorux1
2012/04/23 17:40:41
shouldn't this be a prefix match? Use StartsWith()
| |
| 173 return scoped_ptr<GDataEntry>(NULL); | |
| 174 | |
| 175 scoped_ptr<GDataEntry> entry; | |
| 176 GDataDB::Status status = | |
| 177 db_->GetByResourceId(level_db_iter_->value().ToString(), &entry); | |
| 178 DCHECK_EQ(GDataDB::DB_OK, status); | |
| 179 level_db_iter_->Next(); | |
| 180 return entry.Pass(); | |
| 181 } | |
| 182 | |
| 183 } // namespace gdata | |
| OLD | NEW |