Chromium Code Reviews| 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,153 @@ |
| +// 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); |
| +} |
| + |
| +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()), |
|
zel
2012/04/23 18:44:52
file_name() is not unique, you should use GetFileP
achuithb
2012/04/24 08:09:36
Done.
|
| + 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())); |
|
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.
|
| + |
| + 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); |
| +} |
| + |
| +scoped_ptr<GDataDBIter> GDataLevelDB::NewIterator(const std::string& path) { |
| + // TODO(achuith): Implement this. |
| + NOTREACHED(); |
| + return scoped_ptr<GDataDBIter>(NULL); |
| +} |
| + |
| +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; |
| +} |
| + |
| +} // namespace gdata |
| Property changes on: chrome/browser/chromeos/gdata/gdata_leveldb.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |