|
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 } | |
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()), | |
zel
2012/04/23 18:44:52
file_name() is not unique, you should use GetFileP
achuithb
2012/04/24 08:09:36
Done.
| |
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())); | |
zel
2012/04/23 18:44:52
file_name() is not the path, you should use path h
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 scoped_ptr<GDataDBIter> GDataLevelDB::NewIterator(const std::string& path) { | |
135 // TODO(achuith): Implement this. | |
136 NOTREACHED(); | |
137 return scoped_ptr<GDataDBIter>(NULL); | |
138 } | |
139 | |
140 GDataDB::Status GDataLevelDB::ResourceIdForPath(const std::string& path, | |
141 std::string* resource_id) { | |
142 const leveldb::Status db_status = path_db_->Get( | |
143 leveldb::ReadOptions(), leveldb::Slice(path), resource_id); | |
144 | |
145 if (db_status.ok()) | |
146 return DB_OK; | |
147 else if (db_status.IsNotFound()) | |
148 return DB_NOT_FOUND; | |
149 else | |
150 return DB_ERR; | |
151 } | |
152 | |
153 } // namespace gdata | |
OLD | NEW |