Index: chrome/browser/chromeos/gdata/gdata_leveldb.cc |
=================================================================== |
--- chrome/browser/chromeos/gdata/gdata_leveldb.cc (revision 0) |
+++ chrome/browser/chromeos/gdata/gdata_leveldb.cc (revision 0) |
@@ -0,0 +1,198 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/chromeos/gdata/gdata_leveldb.h" |
+ |
+#include <string.h> |
+ |
+#include <string> |
+ |
+#include "base/logging.h" |
+#include "chrome/browser/chromeos/gdata/gdata_files.h" |
+#include "leveldb/write_batch.h" |
+ |
+namespace gdata { |
+namespace { |
+ |
+// Helper class to convert |resource_id| and |path| to leveldb::Slice. |
+// We append prefixes id: and path: respectively to the keys. |
+class GDataSlice { |
+ public: |
+ // Methods to convert |resource_id| and |path| to leveldb::Slice, by |
+ // appending id: and path: prefix. These slices point into slice_temp_. |
+ leveldb::Slice ResourceIdToSlice(const std::string& resource_id); |
+ leveldb::Slice PathToSlice(const FilePath& path); |
+ |
+ private: |
+ static const char kResourceIdPrefix[]; |
+ static const char kPathPrefix[]; |
+ |
+ // Backing storage for leveldb::Slice. The lifetime of GDataSlice must |
+ // exceed that of the slice otherwise the slice will point to garbage. |
+ std::string slice_temp_; |
+}; |
+ |
+const char GDataSlice::kResourceIdPrefix[] = "id:"; |
+const char GDataSlice::kPathPrefix[] = "path:"; |
+ |
+leveldb::Slice GDataSlice::ResourceIdToSlice(const std::string& resource_id) { |
+ slice_temp_ = kResourceIdPrefix + resource_id; |
+ return leveldb::Slice(slice_temp_); |
+} |
+ |
+leveldb::Slice GDataSlice::PathToSlice(const FilePath& path) { |
+ slice_temp_ = kPathPrefix + path.value(); |
+ return leveldb::Slice(slice_temp_); |
+} |
+ |
+} // namespace |
+ |
+GDataLevelDB::GDataLevelDB() { |
+} |
+ |
+GDataLevelDB::~GDataLevelDB() { |
+} |
+ |
+void GDataLevelDB::Init(const FilePath& db_path) { |
+ leveldb::DB* level_db = NULL; |
+ leveldb::Options options; |
+ options.create_if_missing = true; |
+ leveldb::Status status = leveldb::DB::Open(options, |
+ db_path.Append("level_db").value(), &level_db); |
+ DCHECK(level_db); |
+ DCHECK(status.ok()); |
satorux1
2012/04/24 18:26:44
instead of DCHECK, shouldn't we return false?
achuithb
2012/04/24 19:43:58
I think we should handle the failure, ie, try to
|
+ level_db_.reset(level_db); |
+} |
+ |
+GDataDB::Status GDataLevelDB::Put(const GDataEntry& file) { |
satorux1
2012/04/24 18:26:44
file -> entry
achuithb
2012/04/24 19:43:58
Done.
|
+ // Write the serialized proto. |
+ std::string serialized_proto; |
+ file.SerializeToString(&serialized_proto); |
+ |
+ leveldb::WriteBatch batch; |
+ GDataSlice gdata_slice; |
+ batch.Put(gdata_slice.ResourceIdToSlice(file.resource_id()), |
satorux1
2012/04/24 18:26:44
I found GDataSlice class to be rather complex. I g
achuithb
2012/04/24 19:43:58
Done.
|
+ leveldb::Slice(serialized_proto)); |
+ // Note we store the resource_id without prefix when it's the value. |
+ batch.Put(gdata_slice.PathToSlice(file.GetFilePath()), |
+ leveldb::Slice(file.resource_id())); |
+ leveldb::Status status = level_db_->Write( |
+ leveldb::WriteOptions(), |
+ &batch); |
+ |
+ DVLOG(1) << "GDataLevelDB::Put " |
+ << gdata_slice.ResourceIdToSlice(file.resource_id()).ToString() |
+ << ", " << gdata_slice.PathToSlice(file.GetFilePath()).ToString(); |
+ DCHECK(status.ok()); |
satorux1
2012/04/24 18:26:44
remove DCHECK? I guess it can fail if the disk is
achuithb
2012/04/24 19:43:58
Done.
|
+ return status.ok() ? DB_OK : DB_ERR; |
+} |
+ |
+GDataDB::Status GDataLevelDB::DeleteByResourceId( |
+ const std::string& resource_id) { |
+ scoped_ptr<GDataEntry> file; |
+ Status status = GetByResourceId(resource_id, &file); |
+ if (status == DB_ERR) |
+ return DB_ERR; |
+ else if (status == DB_NOT_FOUND) |
+ return DB_OK; |
satorux1
2012/04/24 18:26:44
is it OK? shouldn't we propagate that the resource
achuithb
2012/04/24 19:43:58
I believe not-found is handled as ok by the underl
|
+ |
+ leveldb::WriteBatch batch; |
+ GDataSlice gdata_slice; |
+ batch.Delete(gdata_slice.ResourceIdToSlice(resource_id)); |
+ batch.Delete(gdata_slice.PathToSlice(file->GetFilePath())); |
+ |
+ leveldb::Status db_status = level_db_->Write( |
+ leveldb::WriteOptions(), &batch); |
+ |
+ return db_status.ok() ? DB_OK : DB_ERR; |
+} |
+ |
+GDataDB::Status GDataLevelDB::DeleteByPath( |
+ const FilePath& path) { |
+ std::string resource_id; |
+ const Status status = ResourceIdForPath(path, &resource_id); |
+ if (status != DB_OK) |
+ return status; |
+ return DeleteByResourceId(resource_id); |
+} |
+ |
+GDataDB::Status GDataLevelDB::GetByResourceId(const std::string& resource_id, |
+ scoped_ptr<GDataEntry>* file) { |
+ file->reset(); |
+ std::string serialized_proto; |
+ const leveldb::Status status = level_db_->Get(leveldb::ReadOptions(), |
+ GDataSlice().ResourceIdToSlice(resource_id), &serialized_proto); |
+ |
+ if (status.IsNotFound()) |
+ return DB_NOT_FOUND; |
+ |
+ if (status.ok()) { |
+ DCHECK(!serialized_proto.empty()); |
+ *file = GDataEntry::FromProtoString(serialized_proto); |
+ DCHECK(file->get()); |
+ return DB_OK; |
+ } |
+ return DB_ERR; |
+} |
+ |
+GDataDB::Status GDataLevelDB::GetByPath(const FilePath& path, |
+ scoped_ptr<GDataEntry>* file) { |
+ file->reset(); |
+ std::string resource_id; |
+ const Status status = ResourceIdForPath(path, &resource_id); |
+ if (status != DB_OK) |
+ return status; |
+ return GetByResourceId(resource_id, file); |
+} |
+ |
+GDataDB::Status GDataLevelDB::ResourceIdForPath(const FilePath& path, |
+ std::string* resource_id) { |
+ const leveldb::Status db_status = level_db_->Get( |
+ leveldb::ReadOptions(), GDataSlice().PathToSlice(path), resource_id); |
+ |
+ if (db_status.ok()) |
+ return DB_OK; |
+ else if (db_status.IsNotFound()) |
+ return DB_NOT_FOUND; |
+ else |
+ return DB_ERR; |
satorux1
2012/04/24 18:26:44
we might want to have a function that converts lev
achuithb
2012/04/24 19:43:58
Done.
|
+} |
+ |
+scoped_ptr<GDataDBIter> GDataLevelDB::NewIterator(const FilePath& path) { |
+ return scoped_ptr<GDataDBIter>(new GDataLevelDBIter( |
+ scoped_ptr<leveldb::Iterator>( |
+ level_db_->NewIterator(leveldb::ReadOptions())), |
+ this, |
+ path)); |
+} |
+ |
+GDataLevelDBIter::GDataLevelDBIter(scoped_ptr<leveldb::Iterator> level_db_iter, |
+ GDataDB* db, |
+ const FilePath& path) |
+ : level_db_iter_(level_db_iter.Pass()), |
+ db_(db), |
+ path_(path) { |
+ level_db_iter_->Seek(GDataSlice().PathToSlice(path)); |
+} |
+ |
+GDataLevelDBIter::~GDataLevelDBIter() { |
+} |
+ |
+scoped_ptr<GDataEntry> GDataLevelDBIter::GetNext() { |
+ if (!level_db_iter_->Valid()) |
+ return scoped_ptr<GDataEntry>(NULL); |
+ |
+ // Only consider keys under |path|. |
+ if (!level_db_iter_->key().starts_with(GDataSlice().PathToSlice(path_))) |
+ return scoped_ptr<GDataEntry>(NULL); |
+ |
+ scoped_ptr<GDataEntry> entry; |
+ GDataDB::Status status = |
+ db_->GetByResourceId(level_db_iter_->value().ToString(), &entry); |
+ DCHECK_EQ(GDataDB::DB_OK, status); |
+ level_db_iter_->Next(); |
+ return entry.Pass(); |
+} |
+ |
+} // namespace gdata |
Property changes on: chrome/browser/chromeos/gdata/gdata_leveldb.cc |
___________________________________________________________________ |
Added: svn:eol-style |
+ LF |