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 "base/threading/thread_restrictions.h" | |
11 #include "chrome/browser/chromeos/gdata/gdata_files.h" | |
12 #include "leveldb/write_batch.h" | |
13 | |
14 namespace gdata { | |
15 namespace { | |
16 | |
17 const char kResourceIdPrefix[] = "r:"; | |
18 const char kPathPrefix[] = "p:"; | |
19 | |
20 // Append prefix id: to |resource_id|. | |
21 std::string ResourceIdToKey(const std::string& resource_id) { | |
22 return std::string(kResourceIdPrefix) + resource_id; | |
23 } | |
24 | |
25 // Append prefix path: to |path|. | |
26 std::string PathToKey(const FilePath& path) { | |
27 return std::string(kPathPrefix) + path.value(); | |
28 } | |
29 | |
30 GDataDB::Status GetStatus(const leveldb::Status& db_status) { | |
31 if (db_status.ok()) | |
32 return GDataDB::DB_OK; | |
33 | |
34 if (db_status.IsNotFound()) | |
35 return GDataDB::DB_KEY_NOT_FOUND; | |
36 | |
37 if (db_status.IsCorruption()) | |
38 return GDataDB::DB_CORRUPTION; | |
39 | |
40 if (db_status.IsIOError()) | |
41 return GDataDB::DB_IO_ERROR; | |
42 | |
43 NOTREACHED(); | |
44 return GDataDB::DB_INTERNAL_ERROR; | |
45 } | |
46 | |
47 } // namespace | |
48 | |
49 GDataLevelDB::GDataLevelDB() { | |
50 } | |
51 | |
52 GDataLevelDB::~GDataLevelDB() { | |
53 } | |
54 | |
55 void GDataLevelDB::Init(const FilePath& db_path) { | |
56 base::ThreadRestrictions::AssertIOAllowed(); | |
57 | |
58 leveldb::DB* level_db = NULL; | |
59 leveldb::Options options; | |
60 options.create_if_missing = true; | |
61 leveldb::Status db_status = leveldb::DB::Open(options, | |
62 db_path.Append("level_db").value(), &level_db); | |
63 DCHECK(level_db); | |
64 // TODO(achuith): If db cannot be opened, we should try to recover it. | |
65 // If that fails, we should just delete it and create a new file. | |
66 DCHECK(db_status.ok()); | |
67 level_db_.reset(level_db); | |
68 } | |
69 | |
70 GDataDB::Status GDataLevelDB::Put(const GDataEntry& entry) { | |
71 base::ThreadRestrictions::AssertIOAllowed(); | |
72 | |
73 // Write the serialized proto. | |
74 std::string serialized_proto; | |
75 entry.SerializeToString(&serialized_proto); | |
76 | |
77 leveldb::WriteBatch batch; | |
78 const std::string resource_id_key = | |
79 ResourceIdToKey(entry.resource_id()); | |
80 const std::string path_key = PathToKey(entry.GetFilePath()); | |
81 batch.Put(leveldb::Slice(resource_id_key), leveldb::Slice(serialized_proto)); | |
82 // Note we store the resource_id without prefix when it's the value. | |
83 batch.Put(leveldb::Slice(path_key), leveldb::Slice(entry.resource_id())); | |
84 leveldb::Status db_status = level_db_->Write( | |
85 leveldb::WriteOptions(), | |
86 &batch); | |
87 | |
88 DVLOG(1) << "GDataLevelDB::Put resource_id key = " << resource_id_key | |
89 << ", path key = " << path_key; | |
90 return GetStatus(db_status); | |
91 } | |
92 | |
93 GDataDB::Status GDataLevelDB::DeleteByResourceId( | |
94 const std::string& resource_id) { | |
95 base::ThreadRestrictions::AssertIOAllowed(); | |
96 | |
97 scoped_ptr<GDataEntry> entry; | |
98 Status status = GetByResourceId(resource_id, &entry); | |
99 if (status == DB_KEY_NOT_FOUND) | |
100 return DB_OK; | |
101 else if (status != DB_OK) | |
102 return status; | |
103 | |
104 leveldb::WriteBatch batch; | |
105 const std::string resource_id_key = ResourceIdToKey(resource_id); | |
106 const std::string path_key = PathToKey(entry->GetFilePath()); | |
107 batch.Delete(leveldb::Slice(resource_id_key)); | |
108 batch.Delete(leveldb::Slice(path_key)); | |
109 | |
110 leveldb::Status db_status = level_db_->Write(leveldb::WriteOptions(), | |
111 &batch); | |
112 return GetStatus(db_status); | |
113 } | |
114 | |
115 GDataDB::Status GDataLevelDB::DeleteByPath( | |
116 const FilePath& path) { | |
117 base::ThreadRestrictions::AssertIOAllowed(); | |
118 | |
119 std::string resource_id; | |
120 const Status status = ResourceIdForPath(path, &resource_id); | |
121 if (status != DB_OK) | |
122 return status; | |
123 return DeleteByResourceId(resource_id); | |
124 } | |
125 | |
126 GDataDB::Status GDataLevelDB::GetByResourceId(const std::string& resource_id, | |
127 scoped_ptr<GDataEntry>* entry) { | |
128 base::ThreadRestrictions::AssertIOAllowed(); | |
129 | |
130 entry->reset(); | |
131 std::string serialized_proto; | |
132 const std::string resource_id_key = ResourceIdToKey(resource_id); | |
133 const leveldb::Status db_status = level_db_->Get(leveldb::ReadOptions(), | |
134 leveldb::Slice(resource_id_key), &serialized_proto); | |
135 | |
136 if (db_status.IsNotFound()) | |
137 return DB_KEY_NOT_FOUND; | |
138 | |
139 if (db_status.ok()) { | |
140 DCHECK(!serialized_proto.empty()); | |
141 *entry = GDataEntry::FromProtoString(serialized_proto); | |
142 return entry->get() ? DB_OK : DB_CORRUPTION; | |
143 } | |
144 return GetStatus(db_status); | |
145 } | |
146 | |
147 GDataDB::Status GDataLevelDB::GetByPath(const FilePath& path, | |
148 scoped_ptr<GDataEntry>* entry) { | |
149 base::ThreadRestrictions::AssertIOAllowed(); | |
150 | |
151 entry->reset(); | |
152 std::string resource_id; | |
153 const Status status = ResourceIdForPath(path, &resource_id); | |
154 if (status != DB_OK) | |
155 return status; | |
156 return GetByResourceId(resource_id, entry); | |
157 } | |
158 | |
159 GDataDB::Status GDataLevelDB::ResourceIdForPath(const FilePath& path, | |
160 std::string* resource_id) { | |
161 base::ThreadRestrictions::AssertIOAllowed(); | |
162 | |
163 const std::string path_key = PathToKey(path); | |
164 const leveldb::Status db_status = level_db_->Get( | |
165 leveldb::ReadOptions(), path_key, resource_id); | |
166 | |
167 return GetStatus(db_status); | |
168 } | |
169 | |
170 scoped_ptr<GDataDBIter> GDataLevelDB::CreateIterator(const FilePath& path) { | |
171 return scoped_ptr<GDataDBIter>(new GDataLevelDBIter( | |
172 scoped_ptr<leveldb::Iterator>( | |
173 level_db_->NewIterator(leveldb::ReadOptions())), | |
174 this, | |
175 path)); | |
176 } | |
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 | |
190 GDataLevelDBIter::GDataLevelDBIter(scoped_ptr<leveldb::Iterator> level_db_iter, | |
191 GDataDB* db, | |
192 const FilePath& path) | |
193 : level_db_iter_(level_db_iter.Pass()), | |
194 db_(db), | |
195 path_(path) { | |
196 base::ThreadRestrictions::AssertIOAllowed(); | |
197 | |
198 const std::string path_key = PathToKey(path); | |
199 level_db_iter_->Seek(leveldb::Slice(path_key)); | |
200 } | |
201 | |
202 GDataLevelDBIter::~GDataLevelDBIter() { | |
203 } | |
204 | |
205 bool GDataLevelDBIter::GetNext(std::string* path, | |
206 scoped_ptr<GDataEntry>* entry) { | |
207 base::ThreadRestrictions::AssertIOAllowed(); | |
208 | |
209 DCHECK(path); | |
210 DCHECK(entry); | |
211 path->clear(); | |
212 entry->reset(); | |
213 | |
214 if (!level_db_iter_->Valid()) | |
215 return false; | |
216 | |
217 // Only consider keys under |path|. | |
218 const std::string path_key = PathToKey(path_); | |
219 leveldb::Slice key_slice(level_db_iter_->key()); | |
220 if (!key_slice.starts_with(path_key)) | |
221 return false; | |
222 | |
223 GDataDB::Status status = | |
224 db_->GetByResourceId(level_db_iter_->value().ToString(), entry); | |
225 if (status != GDataDB::DB_OK) | |
226 return false; | |
227 | |
228 key_slice.remove_prefix(sizeof(kPathPrefix) - 1); | |
229 path->assign(key_slice.ToString()); | |
230 | |
231 level_db_iter_->Next(); | |
232 return true; | |
233 } | |
234 | |
235 } // namespace gdata | |
OLD | NEW |