Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_leveldb.cc

Issue 10168025: GDataDB support with leveldb. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: minor Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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.h>
8
9 #include <string>
10
11 #include "base/logging.h"
12 #include "chrome/browser/chromeos/gdata/gdata_files.h"
13 #include "leveldb/write_batch.h"
14
15 namespace gdata {
16 namespace {
17
18 const char kResourceIdPrefix[] = "id:";
19 const char kPathPrefix[] = "path:";
20
21 // Append prefix id: to |resource_id|.
22 std::string ResourceIdToKey(const std::string& resource_id) {
23 return std::string(kResourceIdPrefix) + resource_id;
24 }
25
26 // Append prefix path: to |path|.
27 std::string PathToKey(const FilePath& path) {
28 return std::string(kPathPrefix) + path.value();
29 }
30
31 GDataDB::Status GetStatus(const leveldb::Status& db_status) {
32 if (db_status.ok())
33 return GDataDB::DB_OK;
34
35 if (db_status.IsNotFound())
36 return GDataDB::DB_NOT_FOUND;
37
38 if (db_status.IsCorruption())
39 return GDataDB::DB_CORRUPTION;
zel 2012/04/25 00:49:47 what do we plan to do on corruption detection? can
achuithb 2012/04/25 00:57:53 I have a TODO below; need to open a bug. I think
40
41 if (db_status.IsIOError())
42 return GDataDB::DB_IO_ERROR;
43
44 NOTREACHED();
45 return GDataDB::DB_INTERNAL_ERROR;
46 }
47
48 } // namespace
49
50 GDataLevelDB::GDataLevelDB() {
51 }
52
53 GDataLevelDB::~GDataLevelDB() {
54 }
55
56 void GDataLevelDB::Init(const FilePath& db_path) {
57 leveldb::DB* level_db = NULL;
58 leveldb::Options options;
59 options.create_if_missing = true;
60 leveldb::Status db_status = leveldb::DB::Open(options,
61 db_path.Append("level_db").value(), &level_db);
62 DCHECK(level_db);
63 // TODO(achuith): If db cannot be opened, we should try to recover it.
64 // If that fails, we should just delete it and create a new file.
65 DCHECK(db_status.ok());
66 level_db_.reset(level_db);
67 }
68
69 GDataDB::Status GDataLevelDB::Put(const GDataEntry& entry) {
70 // Write the serialized proto.
71 std::string serialized_proto;
72 entry.SerializeToString(&serialized_proto);
73
74 leveldb::WriteBatch batch;
75 const std::string resource_id_key =
76 ResourceIdToKey(entry.resource_id());
77 const std::string path_key = PathToKey(entry.GetFilePath());
78 batch.Put(leveldb::Slice(resource_id_key), leveldb::Slice(serialized_proto));
79 // Note we store the resource_id without prefix when it's the value.
80 batch.Put(leveldb::Slice(path_key), leveldb::Slice(entry.resource_id()));
81 leveldb::Status db_status = level_db_->Write(
82 leveldb::WriteOptions(),
83 &batch);
84
85 DVLOG(1) << "GDataLevelDB::Put resource_id key = " << resource_id_key
86 << ", path key = " << path_key;
87 return GetStatus(db_status);
88 }
89
90 GDataDB::Status GDataLevelDB::DeleteByResourceId(
91 const std::string& resource_id) {
92 scoped_ptr<GDataEntry> entry;
93 Status status = GetByResourceId(resource_id, &entry);
94 if (status == DB_NOT_FOUND)
95 return DB_OK;
96 else if (status != DB_OK)
97 return status;
98
99 leveldb::WriteBatch batch;
100 const std::string resource_id_key = ResourceIdToKey(resource_id);
101 const std::string path_key = PathToKey(entry->GetFilePath());
102 batch.Delete(leveldb::Slice(resource_id_key));
103 batch.Delete(leveldb::Slice(path_key));
104
105 leveldb::Status db_status = level_db_->Write(leveldb::WriteOptions(),
106 &batch);
107 return GetStatus(db_status);
108 }
109
110 GDataDB::Status GDataLevelDB::DeleteByPath(
111 const FilePath& path) {
112 std::string resource_id;
113 const Status status = ResourceIdForPath(path, &resource_id);
114 if (status != DB_OK)
115 return status;
116 return DeleteByResourceId(resource_id);
117 }
118
119 GDataDB::Status GDataLevelDB::GetByResourceId(const std::string& resource_id,
120 scoped_ptr<GDataEntry>* entry) {
121 entry->reset();
122 std::string serialized_proto;
123 const std::string resource_id_key = ResourceIdToKey(resource_id);
124 const leveldb::Status db_status = level_db_->Get(leveldb::ReadOptions(),
125 leveldb::Slice(resource_id_key), &serialized_proto);
126
127 if (db_status.IsNotFound())
128 return DB_NOT_FOUND;
129
130 if (db_status.ok()) {
131 DCHECK(!serialized_proto.empty());
132 *entry = GDataEntry::FromProtoString(serialized_proto);
133 DCHECK(entry->get());
134 return DB_OK;
135 }
136 return GetStatus(db_status);
137 }
138
139 GDataDB::Status GDataLevelDB::GetByPath(const FilePath& path,
140 scoped_ptr<GDataEntry>* entry) {
141 entry->reset();
142 std::string resource_id;
143 const Status status = ResourceIdForPath(path, &resource_id);
144 if (status != DB_OK)
145 return status;
146 return GetByResourceId(resource_id, entry);
147 }
148
149 GDataDB::Status GDataLevelDB::ResourceIdForPath(const FilePath& path,
150 std::string* resource_id) {
151 const std::string path_key = PathToKey(path);
152 const leveldb::Status db_status = level_db_->Get(
153 leveldb::ReadOptions(), path_key, resource_id);
154
155 return GetStatus(db_status);
156 }
157
158 scoped_ptr<GDataDBIter> GDataLevelDB::NewIterator(const FilePath& path) {
159 return scoped_ptr<GDataDBIter>(new GDataLevelDBIter(
160 scoped_ptr<leveldb::Iterator>(
161 level_db_->NewIterator(leveldb::ReadOptions())),
162 this,
163 path));
164 }
165
166 GDataLevelDBIter::GDataLevelDBIter(scoped_ptr<leveldb::Iterator> level_db_iter,
167 GDataDB* db,
168 const FilePath& path)
169 : level_db_iter_(level_db_iter.Pass()),
170 db_(db),
171 path_(path) {
172 const std::string path_key = PathToKey(path);
173 level_db_iter_->Seek(leveldb::Slice(path_key));
174 }
175
176 GDataLevelDBIter::~GDataLevelDBIter() {
177 }
178
179 scoped_ptr<GDataEntry> GDataLevelDBIter::GetNext() {
180 if (!level_db_iter_->Valid())
181 return scoped_ptr<GDataEntry>(NULL);
182
183 // Only consider keys under |path|.
184 const std::string path_key = PathToKey(path_);
185 if (!level_db_iter_->key().starts_with(path_key))
186 return scoped_ptr<GDataEntry>(NULL);
187
188 scoped_ptr<GDataEntry> entry;
189 GDataDB::Status status =
190 db_->GetByResourceId(level_db_iter_->value().ToString(), &entry);
191 DCHECK_EQ(GDataDB::DB_OK, status);
192 level_db_iter_->Next();
193 return entry.Pass();
194 }
195
196 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698