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

Unified 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: fixes 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 side-by-side diff with in-line comments
Download patch
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,183 @@
+// 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>
+
+#include "base/logging.h"
+#include "chrome/browser/chromeos/gdata/gdata_files.h"
+
+namespace gdata {
+
+// static
+scoped_ptr<GDataDB> GDataDB::Create(const FilePath& db_path) {
+ DVLOG(1) << "GDataDB::Create " << db_path.value();
+ GDataLevelDB* level_db = new GDataLevelDB();
+ level_db->Init(db_path);
+ return scoped_ptr<GDataDB>(level_db);
+}
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.
+
+GDataLevelDB::GDataLevelDB() {
+}
+
+GDataLevelDB::~GDataLevelDB() {
+}
+
+void GDataLevelDB::Init(const FilePath& db_path) {
+ // Open the primary database with file_path key.
+ leveldb::DB* level_db = NULL;
+ leveldb::Options options;
+ options.create_if_missing = true;
+ leveldb::Status status = leveldb::DB::Open(options,
+ db_path.Append("main").value(), &level_db);
+ DCHECK(level_db);
+ DCHECK(status.ok());
+ main_db_.reset(level_db);
+ level_db = NULL;
+
+ // Secondary database keyed on path.
+ status = leveldb::DB::Open(options,
+ db_path.Append("path").value(), &level_db);
+ DCHECK(level_db);
+ DCHECK(status.ok());
+ path_db_.reset(level_db);
+}
+
+GDataDB::Status GDataLevelDB::Put(const GDataEntry& file) {
+ // TODO(achuith): Batch write operation
+ // Write the serialized proto.
+ std::string serialized_proto;
+ file.SerializeToString(&serialized_proto);
+ leveldb::Status status = main_db_->Put(
+ leveldb::WriteOptions(),
+ leveldb::Slice(file.resource_id()),
+ leveldb::Slice(serialized_proto));
+ if (!status.ok()) {
+ NOTREACHED();
+ DeleteByResourceId(file.resource_id());
+ return DB_ERR;
+ }
+
+ // Write the path.
+ status = path_db_->Put(
+ leveldb::WriteOptions(),
+ leveldb::Slice(file.file_name()),
+ leveldb::Slice(file.resource_id()));
+ if (!status.ok()) {
+ NOTREACHED();
+ DeleteByResourceId(file.resource_id());
+ return DB_ERR;
+ }
+ return DB_OK;
+}
+
+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;
+
+ leveldb::Status db_status1 = main_db_->Delete(
+ leveldb::WriteOptions(),
+ leveldb::Slice(resource_id));
+
+ leveldb::Status db_status2 = path_db_->Delete(
+ leveldb::WriteOptions(),
+ 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.
+
+ return db_status1.ok() && db_status2.ok() ? DB_OK : DB_ERR;
+}
+
+GDataDB::Status GDataLevelDB::DeleteByPath(
+ const std::string& 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 = main_db_->Get(leveldb::ReadOptions(),
+ leveldb::Slice(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 std::string& 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 std::string& path,
+ std::string* resource_id) {
+ const leveldb::Status db_status = path_db_->Get(
+ leveldb::ReadOptions(), leveldb::Slice(path), resource_id);
+
+ if (db_status.ok())
+ return DB_OK;
+ else if (db_status.IsNotFound())
+ return DB_NOT_FOUND;
+ else
+ return DB_ERR;
+}
+
+scoped_ptr<GDataDBIter> GDataLevelDB::NewIterator(const std::string& path) {
+ return scoped_ptr<GDataDBIter>(new GDataLevelDBIter(
+ scoped_ptr<leveldb::Iterator>(
+ path_db_->NewIterator(leveldb::ReadOptions())),
+ this,
+ path));
+}
+
+GDataLevelDBIter::GDataLevelDBIter(scoped_ptr<leveldb::Iterator> level_db_iter,
+ GDataDB* db,
+ const std::string& path)
+ : level_db_iter_(level_db_iter.Pass()),
+ db_(db),
+ path_(path) {
+ level_db_iter_->Seek(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().ToString().find(path_) != 0)
satorux1 2012/04/23 17:40:41 shouldn't this be a prefix match? Use StartsWith()
+ 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

Powered by Google App Engine
This is Rietveld 408576698