Commit e6281bba authored by satorux@chromium.org's avatar satorux@chromium.org

gdata: Remove obsolete GDataDB related code

Turned out the original design was not good, and the code won't be used.

BUG=140314
TEST=compile
TBR=ben@chromium.org

Review URL: https://chromiumcodereview.appspot.com/10828163

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@149955 0039d316-1c4b-4281-b951-d872f2087c98
parent 4967673d
// 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.
#ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_DB_H_
#define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_DB_H_
#include <string>
#include "base/memory/scoped_ptr.h"
class FilePath;
namespace gdata {
class GDataEntry;
class GDataDBIter;
// GData Database interface class.
class GDataDB {
public:
enum Status {
DB_OK = 0,
DB_KEY_NOT_FOUND, // Key not found.
DB_CORRUPTION, // Database file corrupt.
DB_IO_ERROR, // File I/O error.
DB_INTERNAL_ERROR,
};
virtual ~GDataDB() {}
// Puts |entry| to the database.
virtual Status Put(const GDataEntry& entry) = 0;
// Deletes a database entry with key |resource_id| or |path| respectively.
virtual Status DeleteByResourceId(const std::string& resource_id) = 0;
virtual Status DeleteByPath(const FilePath& path) = 0;
// Fetches a GDataEntry* by key |resource_id| or |path| respectively.
virtual Status GetByResourceId(const std::string& resource_id,
scoped_ptr<GDataEntry>* entry) = 0;
virtual Status GetByPath(const FilePath& path,
scoped_ptr<GDataEntry>* entry) = 0;
// Creates an iterator to fetch all GDataEntry's under |path|.
// Will not return NULL.
virtual scoped_ptr<GDataDBIter> CreateIterator(const FilePath& path) = 0;
// Puts |raw_value| keyed with |resource_id| to the database.
// Used for testing (ex. injecting incompatible proto).
virtual Status PutRawForTesting(const std::string& resource_id,
const std::string& raw_value) = 0;
protected:
GDataDB() {}
};
// GData Database Iterator interface class.
class GDataDBIter {
public:
virtual ~GDataDBIter() {}
// Fetches the next |entry| in the iteration sequence. Returns false when
// there are no more entries.
virtual bool GetNext(std::string* path, scoped_ptr<GDataEntry>* entry) = 0;
protected:
GDataDBIter() {}
};
} // namespace gdata
#endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_DB_H_
// 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_db_factory.h"
#include "base/file_path.h"
#include "base/logging.h"
#include "chrome/browser/chromeos/gdata/gdata_leveldb.h"
namespace gdata {
namespace db_factory {
scoped_ptr<GDataDB> CreateGDataDB(const FilePath& db_path) {
DVLOG(1) << "CreateGDataDB " << db_path.value();
GDataLevelDB* level_db = new GDataLevelDB();
level_db->Init(db_path);
return scoped_ptr<GDataDB>(level_db);
}
} // namespace db_factory
} // namespace gdata
// 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.
#ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_DB_FACTORY_H_
#define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_DB_FACTORY_H_
#include "base/memory/scoped_ptr.h"
class FilePath;
namespace gdata {
class GDataDB;
namespace db_factory {
// Factory method to create an instance of GDataDB.
scoped_ptr<GDataDB> CreateGDataDB(const FilePath& db_path);
} // namespace db_factory
} // namespace gdata
#endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_DB_FACTORY_H_
// 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_db.h"
#include "base/message_loop.h"
#include "base/string_number_conversions.h"
#include "chrome/browser/chromeos/gdata/gdata.pb.h"
#include "chrome/browser/chromeos/gdata/gdata_db_factory.h"
#include "chrome/browser/chromeos/gdata/gdata_files.h"
#include "chrome/browser/chromeos/gdata/gdata_test_util.h"
#include "chrome/test/base/testing_profile.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gdata {
namespace {
class GDataDBTest : public testing::Test {
public:
GDataDBTest() {
}
virtual ~GDataDBTest() {
}
protected:
// testing::Test implementation.
virtual void SetUp() OVERRIDE;
// Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry
// matching |source| does not exist.
void TestGetNotFound(const GDataEntry& source);
// Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry
// matching |source| exists.
void TestGetFound(const GDataEntry& source);
// Tests GDataDB::GetPath and GDataDB::ResourceId, ensuring that an entry
// matching |source| is corrupt.
void TestGetCorrupt(const GDataEntry& source);
// Initialize the database with the following entries:
// drive/dir1
// drive/dir2
// drive/dir1/dir3
// drive/dir1/file4
// drive/dir1/file5
// drive/dir2/file6
// drive/dir2/file7
// drive/dir2/file8
// drive/dir1/dir3/file9
// drive/dir1/dir3/file10
void InitDB();
// Helper functions to add a directory/file, incrementing index.
GDataDirectory* AddDirectory(GDataDirectory* parent,
GDataDirectoryService* root, int sequence_id);
GDataFile* AddFile(GDataDirectory* parent,
GDataDirectoryService* directory_service, int sequence_id);
// Tests GDataDB::NewIterator and GDataDBIter::GetNext.
// Creates an iterator with start at |parent|, and iterates comparing with
// expected |filenames|.
void TestIter(const std::string& parent,
const char* file_paths[],
size_t file_paths_size);
scoped_ptr<TestingProfile> profile_;
scoped_ptr<GDataDB> gdata_db_;
MessageLoopForUI message_loop;
};
void GDataDBTest::SetUp() {
profile_.reset(new TestingProfile());
gdata_db_ = db_factory::CreateGDataDB(
profile_->GetPath().Append("testdb"));
}
void GDataDBTest::TestGetNotFound(const GDataEntry& source) {
scoped_ptr<GDataEntry> entry;
GDataDB::Status status = gdata_db_->GetByPath(source.GetFilePath(), &entry);
EXPECT_EQ(GDataDB::DB_KEY_NOT_FOUND, status);
EXPECT_FALSE(entry.get());
status = gdata_db_->GetByResourceId(source.resource_id(), &entry);
EXPECT_EQ(GDataDB::DB_KEY_NOT_FOUND, status);
EXPECT_FALSE(entry.get());
}
void GDataDBTest::TestGetFound(const GDataEntry& source) {
scoped_ptr<GDataEntry> entry;
GDataDB::Status status = gdata_db_->GetByPath(source.GetFilePath(), &entry);
EXPECT_EQ(GDataDB::DB_OK, status);
ASSERT_TRUE(entry.get());
EXPECT_EQ(source.title(), entry->title());
EXPECT_EQ(source.resource_id(), entry->resource_id());
EXPECT_EQ(source.content_url(), entry->content_url());
entry.reset();
status = gdata_db_->GetByResourceId(source.resource_id(), &entry);
EXPECT_EQ(GDataDB::DB_OK, status);
ASSERT_TRUE(entry.get());
EXPECT_EQ(source.title(), entry->title());
EXPECT_EQ(source.resource_id(), entry->resource_id());
EXPECT_EQ(source.content_url(), entry->content_url());
}
void GDataDBTest::TestGetCorrupt(const GDataEntry& source) {
scoped_ptr<GDataEntry> entry;
GDataDB::Status status = gdata_db_->GetByPath(source.GetFilePath(), &entry);
EXPECT_EQ(GDataDB::DB_CORRUPTION, status);
EXPECT_FALSE(entry.get());
status = gdata_db_->GetByResourceId(source.resource_id(), &entry);
EXPECT_EQ(GDataDB::DB_CORRUPTION, status);
EXPECT_FALSE(entry.get());
}
void GDataDBTest::InitDB() {
int sequence_id = 1;
GDataDirectoryService directory_service;
GDataDirectory* dir1 = AddDirectory(directory_service.root(),
&directory_service, sequence_id++);
GDataDirectory* dir2 = AddDirectory(directory_service.root(),
&directory_service, sequence_id++);
GDataDirectory* dir3 = AddDirectory(dir1, &directory_service, sequence_id++);
AddFile(dir1, &directory_service, sequence_id++);
AddFile(dir1, &directory_service, sequence_id++);
AddFile(dir2, &directory_service, sequence_id++);
AddFile(dir2, &directory_service, sequence_id++);
AddFile(dir2, &directory_service, sequence_id++);
AddFile(dir3, &directory_service, sequence_id++);
AddFile(dir3, &directory_service, sequence_id++);
}
GDataDirectory* GDataDBTest::AddDirectory(
GDataDirectory* parent,
GDataDirectoryService* directory_service,
int sequence_id) {
GDataDirectory* dir = new GDataDirectory(parent, directory_service);
const std::string dir_name = "dir" + base::IntToString(sequence_id);
const std::string resource_id = std::string("dir_resource_id:") +
dir_name;
dir->set_title(dir_name);
dir->SetBaseNameFromTitle();
dir->set_resource_id(resource_id);
GDataFileError error = GDATA_FILE_ERROR_FAILED;
directory_service->AddEntryToDirectory(
parent->GetFilePath(),
dir,
base::Bind(&test_util::CopyErrorCodeFromFileOperationCallback, &error));
test_util::RunBlockingPoolTask();
EXPECT_EQ(GDATA_FILE_OK, error);
GDataDB::Status status = gdata_db_->Put(*dir);
EXPECT_EQ(GDataDB::DB_OK, status);
DVLOG(1) << "AddDirectory " << dir->GetFilePath().value()
<< ", " << resource_id;
return dir;
}
GDataFile* GDataDBTest::AddFile(GDataDirectory* parent,
GDataDirectoryService* directory_service,
int sequence_id) {
GDataFile* file = new GDataFile(parent, directory_service);
const std::string title = "file" + base::IntToString(sequence_id);
const std::string resource_id = std::string("file_resource_id:") +
title;
file->set_title(title);
file->SetBaseNameFromTitle();
file->set_resource_id(resource_id);
GDataFileError error = GDATA_FILE_ERROR_FAILED;
directory_service->AddEntryToDirectory(
parent->GetFilePath(),
file,
base::Bind(&test_util::CopyErrorCodeFromFileOperationCallback, &error));
test_util::RunBlockingPoolTask();
EXPECT_EQ(GDATA_FILE_OK, error);
GDataDB::Status status = gdata_db_->Put(*file);
EXPECT_EQ(GDataDB::DB_OK, status);
DVLOG(1) << "AddFile " << file->GetFilePath().value()
<< ", " << resource_id;
return file;
}
void GDataDBTest::TestIter(const std::string& parent,
const char* file_paths[],
size_t file_paths_size) {
scoped_ptr<GDataDBIter> iter = gdata_db_->CreateIterator(
FilePath::FromUTF8Unsafe(parent));
for (size_t i = 0; ; ++i) {
scoped_ptr<GDataEntry> entry;
std::string path;
if (!iter->GetNext(&path, &entry)) {
EXPECT_EQ(i, file_paths_size);
break;
}
ASSERT_LT(i, file_paths_size);
// TODO(achuith): Also test entry->GetFilePath().
EXPECT_EQ(FilePath(file_paths[i]).BaseName().value(), entry->base_name());
EXPECT_EQ(file_paths[i], path);
DVLOG(1) << "Iter " << path;
}
}
} // namespace
TEST_F(GDataDBTest, PutTest) {
GDataDirectoryService directory_service;
GDataDirectory dir(directory_service.root(), &directory_service);
dir.set_title("dir");
dir.set_base_name("dir");
dir.set_resource_id("dir_resource_id");
dir.set_content_url(GURL("http://content/dir"));
dir.set_upload_url(GURL("http://upload/dir"));
TestGetNotFound(dir);
GDataDB::Status status = gdata_db_->Put(dir);
ASSERT_EQ(GDataDB::DB_OK, status);
TestGetFound(dir);
scoped_ptr<GDataEntry> entry;
status = gdata_db_->GetByPath(dir.GetFilePath(), &entry);
ASSERT_EQ(GDataDB::DB_OK, status);
EXPECT_EQ(dir.upload_url(), entry->AsGDataDirectory()->upload_url());
EXPECT_TRUE(entry->AsGDataDirectory()->file_info().is_directory);
status = gdata_db_->DeleteByPath(dir.GetFilePath());
ASSERT_EQ(GDataDB::DB_OK, status);
TestGetNotFound(dir);
GDataFile file(&dir, &directory_service);
file.set_title("file");
file.set_base_name("file");
file.set_resource_id("file_resource_id");
file.set_content_url(GURL("http://content/dir/file"));
file.set_file_md5("file_md5");
TestGetNotFound(file);
status = gdata_db_->Put(file);
ASSERT_EQ(GDataDB::DB_OK, status);
TestGetFound(file);
status = gdata_db_->GetByPath(file.GetFilePath(), &entry);
ASSERT_EQ(GDataDB::DB_OK, status);
EXPECT_EQ(file.file_md5(), entry->AsGDataFile()->file_md5());
EXPECT_FALSE(entry->AsGDataFile()->file_info().is_directory);
status = gdata_db_->DeleteByPath(file.GetFilePath());
ASSERT_EQ(GDataDB::DB_OK, status);
TestGetNotFound(file);
}
TEST_F(GDataDBTest, IterTest) {
InitDB();
const char* dir1_children[] = {
"drive/dir1",
"drive/dir1/dir3",
"drive/dir1/dir3/file10",
"drive/dir1/dir3/file9",
"drive/dir1/file4",
"drive/dir1/file5",
};
TestIter("drive/dir1", dir1_children, arraysize(dir1_children));
const char* dir2_children[] = {
"drive/dir2",
"drive/dir2/file6",
"drive/dir2/file7",
"drive/dir2/file8",
};
TestIter("drive/dir2", dir2_children, arraysize(dir2_children));
const char* dir3_children[] = {
"drive/dir1/dir3",
"drive/dir1/dir3/file10",
"drive/dir1/dir3/file9",
};
TestIter("drive/dir1/dir3", dir3_children, arraysize(dir3_children));
const char* file10[] = {
"drive/dir1/dir3/file10",
};
TestIter(file10[0], file10, arraysize(file10));
const char* all_entries[] = {
"drive/dir1",
"drive/dir1/dir3",
"drive/dir1/dir3/file10",
"drive/dir1/dir3/file9",
"drive/dir1/file4",
"drive/dir1/file5",
"drive/dir2",
"drive/dir2/file6",
"drive/dir2/file7",
"drive/dir2/file8",
};
TestIter("", all_entries, arraysize(all_entries));
TestIter("dir4", NULL, 0);
}
TEST_F(GDataDBTest, IncompatibleProtoTest) {
GDataDirectoryService directory_service;
GDataFile file(directory_service.root(), &directory_service);
file.set_title("file");
file.set_base_name("file");
file.set_resource_id("file_resource_id");
file.set_content_url(GURL("http://content/dir/file"));
file.set_file_md5("file_md5");
// Add a file and check if it's found.
GDataDB::Status status = gdata_db_->Put(file);
ASSERT_EQ(GDataDB::DB_OK, status);
TestGetFound(file);
// Check if the iterator works too.
const char* all_entries[] = {
"drive/file",
};
TestIter("", all_entries, arraysize(all_entries));
// Tweak the file proto to simulate an incompatible proto in the DB.
GDataEntryProto entry_proto;
file.ToProto(&entry_proto);
// This will make FromProto() fail.
entry_proto.clear_upload_url();
std::string serialized_proto;
entry_proto.SerializeToString(&serialized_proto);
gdata_db_->PutRawForTesting("file_resource_id", serialized_proto);
// Check if the corruption is detected.
TestGetCorrupt(file);
// We should no longer be able to find the file by iteration.
TestIter("", NULL, 0);
}
} // namespace gdata
......@@ -733,33 +733,6 @@ void GDataEntry::SerializeToString(std::string* serialized_proto) const {
}
}
// static
scoped_ptr<GDataEntry> GDataEntry::FromProtoString(
const std::string& serialized_proto) {
// First try to parse as GDataDirectoryProto. Note that this can succeed for
// a serialized_proto that's really a GDataEntryProto - we have to check
// is_directory to be sure.
GDataDirectoryProto dir_proto;
bool ok = dir_proto.ParseFromString(serialized_proto);
if (ok && dir_proto.gdata_entry().file_info().is_directory()) {
scoped_ptr<GDataDirectory> dir(new GDataDirectory(NULL, NULL));
if (!dir->FromProto(dir_proto))
return scoped_ptr<GDataEntry>(NULL);
return scoped_ptr<GDataEntry>(dir.release());
}
GDataEntryProto entry_proto;
ok = entry_proto.ParseFromString(serialized_proto);
if (ok) {
DCHECK(!entry_proto.file_info().is_directory());
scoped_ptr<GDataFile> file(new GDataFile(NULL, NULL));
if (!file->FromProto(entry_proto))
return scoped_ptr<GDataEntry>(NULL);
return scoped_ptr<GDataEntry>(file.release());
}
return scoped_ptr<GDataEntry>(NULL);
}
void GDataDirectoryService::SerializeToString(
std::string* serialized_proto) const {
GDataRootDirectoryProto proto;
......
......@@ -89,11 +89,7 @@ class GDataEntry {
GDataDirectoryService* directory_service);
// Serialize/Parse to/from string via proto classes.
// TODO(achuith): Correctly set up parent_ and root_ links in
// FromProtoString.
void SerializeToString(std::string* serialized_proto) const;
static scoped_ptr<GDataEntry> FromProtoString(
const std::string& serialized_proto);
// Converts the proto representation to the platform file.
static void ConvertProtoToPlatformFileInfo(
......
// 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 "base/threading/thread_restrictions.h"
#include "chrome/browser/chromeos/gdata/gdata_files.h"
#include "leveldb/write_batch.h"
namespace gdata {
namespace {
const char kResourceIdPrefix[] = "r:";
const char kPathPrefix[] = "p:";
// Append prefix id: to |resource_id|.
std::string ResourceIdToKey(const std::string& resource_id) {
return std::string(kResourceIdPrefix) + resource_id;
}
// Append prefix path: to |path|.
std::string PathToKey(const FilePath& path) {
return std::string(kPathPrefix) + path.value();
}
GDataDB::Status GetStatus(const leveldb::Status& db_status) {
if (db_status.ok())
return GDataDB::DB_OK;
if (db_status.IsNotFound())
return GDataDB::DB_KEY_NOT_FOUND;
if (db_status.IsCorruption())
return GDataDB::DB_CORRUPTION;
if (db_status.IsIOError())
return GDataDB::DB_IO_ERROR;
NOTREACHED();
return GDataDB::DB_INTERNAL_ERROR;
}
} // namespace
GDataLevelDB::GDataLevelDB() {
}
GDataLevelDB::~GDataLevelDB() {
}
void GDataLevelDB::Init(const FilePath& db_path) {
base::ThreadRestrictions::AssertIOAllowed();
leveldb::DB* level_db = NULL;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status db_status = leveldb::DB::Open(options,
db_path.Append("level_db").value(), &level_db);
DCHECK(level_db);
// TODO(achuith): If db cannot be opened, we should try to recover it.
// If that fails, we should just delete it and create a new file.
DCHECK(db_status.ok());
level_db_.reset(level_db);
}
GDataDB::Status GDataLevelDB::Put(const GDataEntry& entry) {
base::ThreadRestrictions::AssertIOAllowed();
// Write the serialized proto.
std::string serialized_proto;
entry.SerializeToString(&serialized_proto);
leveldb::WriteBatch batch;
const std::string resource_id_key =
ResourceIdToKey(entry.resource_id());
const std::string path_key = PathToKey(entry.GetFilePath());
batch.Put(leveldb::Slice(resource_id_key), leveldb::Slice(serialized_proto));
// Note we store the resource_id without prefix when it's the value.
batch.Put(leveldb::Slice(path_key), leveldb::Slice(entry.resource_id()));
leveldb::Status db_status = level_db_->Write(
leveldb::WriteOptions(),
&batch);
DVLOG(1) << "GDataLevelDB::Put resource_id key = " << resource_id_key
<< ", path key = " << path_key;
return GetStatus(db_status);
}
GDataDB::Status GDataLevelDB::DeleteByResourceId(
const std::string& resource_id) {
base::ThreadRestrictions::AssertIOAllowed();
scoped_ptr<GDataEntry> entry;
Status status = GetByResourceId(resource_id, &entry);
if (status == DB_KEY_NOT_FOUND)
return DB_OK;
else if (status != DB_OK)
return status;
leveldb::WriteBatch batch;
const std::string resource_id_key = ResourceIdToKey(resource_id);
const std::string path_key = PathToKey(entry->GetFilePath());
batch.Delete(leveldb::Slice(resource_id_key));
batch.Delete(leveldb::Slice(path_key));
leveldb::Status db_status = level_db_->Write(leveldb::WriteOptions(),
&batch);
return GetStatus(db_status);
}
GDataDB::Status GDataLevelDB::DeleteByPath(
const FilePath& path) {
base::ThreadRestrictions::AssertIOAllowed();
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>* entry) {
base::ThreadRestrictions::AssertIOAllowed();
entry->reset();
std::string serialized_proto;
const std::string resource_id_key = ResourceIdToKey(resource_id);
const leveldb::Status db_status = level_db_->Get(leveldb::ReadOptions(),
leveldb::Slice(resource_id_key), &serialized_proto);
if (db_status.IsNotFound())
return DB_KEY_NOT_FOUND;
if (db_status.ok()) {
DCHECK(!serialized_proto.empty());
*entry = GDataEntry::FromProtoString(serialized_proto);
return entry->get() ? DB_OK : DB_CORRUPTION;
}
return GetStatus(db_status);
}
GDataDB::Status GDataLevelDB::GetByPath(const FilePath& path,
scoped_ptr<GDataEntry>* entry) {
base::ThreadRestrictions::AssertIOAllowed();
entry->reset();
std::string resource_id;
const Status status = ResourceIdForPath(path, &resource_id);
if (status != DB_OK)
return status;
return GetByResourceId(resource_id, entry);
}
GDataDB::Status GDataLevelDB::ResourceIdForPath(const FilePath& path,
std::string* resource_id) {
base::ThreadRestrictions::AssertIOAllowed();
const std::string path_key = PathToKey(path);
const leveldb::Status db_status = level_db_->Get(
leveldb::ReadOptions(), path_key, resource_id);
return GetStatus(db_status);
}
scoped_ptr<GDataDBIter> GDataLevelDB::CreateIterator(const FilePath& path) {
return scoped_ptr<GDataDBIter>(new GDataLevelDBIter(
scoped_ptr<leveldb::Iterator>(
level_db_->NewIterator(leveldb::ReadOptions())),
this,
path));
}
GDataDB::Status GDataLevelDB::PutRawForTesting(
const std::string& resource_id,
const std::string& raw_value) {
const std::string resource_id_key = ResourceIdToKey(resource_id);
leveldb::Status db_status = level_db_->Put(
leveldb::WriteOptions(),
leveldb::Slice(resource_id_key),
leveldb::Slice(raw_value));
return GetStatus(db_status);
}
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) {
base::ThreadRestrictions::AssertIOAllowed();
const std::string path_key = PathToKey(path);
level_db_iter_->Seek(leveldb::Slice(path_key));
}
GDataLevelDBIter::~GDataLevelDBIter() {
}
bool GDataLevelDBIter::GetNext(std::string* path,
scoped_ptr<GDataEntry>* entry) {
base::ThreadRestrictions::AssertIOAllowed();
DCHECK(path);
DCHECK(entry);
path->clear();
entry->reset();
if (!level_db_iter_->Valid())
return false;
// Only consider keys under |path|.
const std::string path_key = PathToKey(path_);
leveldb::Slice key_slice(level_db_iter_->key());
if (!key_slice.starts_with(path_key))
return false;
GDataDB::Status status =
db_->GetByResourceId(level_db_iter_->value().ToString(), entry);
if (status != GDataDB::DB_OK)
return false;
key_slice.remove_prefix(sizeof(kPathPrefix) - 1);
path->assign(key_slice.ToString());
level_db_iter_->Next();
return true;
}
} // namespace gdata
// 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.
#ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_LEVELDB_H_
#define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_LEVELDB_H_
#include <leveldb/db.h>
#include <string>
#include "base/file_path.h"
#include "chrome/browser/chromeos/gdata/gdata_db.h"
namespace gdata {
class GDataLevelDB : public GDataDB {
public:
GDataLevelDB();
void Init(const FilePath& db_path);
private:
virtual ~GDataLevelDB();
// GDataDB implementation.
virtual Status Put(const GDataEntry& file) OVERRIDE;
virtual Status DeleteByResourceId(const std::string& resource_id) OVERRIDE;
virtual Status DeleteByPath(const FilePath& path) OVERRIDE;
virtual Status GetByResourceId(const std::string& resource_id,
scoped_ptr<GDataEntry>* file) OVERRIDE;
virtual Status GetByPath(const FilePath& path,
scoped_ptr<GDataEntry>* file) OVERRIDE;
virtual scoped_ptr<GDataDBIter> CreateIterator(const FilePath& path) OVERRIDE;
virtual Status PutRawForTesting(const std::string& resource_id,
const std::string& raw_value) OVERRIDE;
// Returns |resource_id| for |path| by looking up path_db_.
Status ResourceIdForPath(const FilePath& path, std::string* resource_id);
scoped_ptr<leveldb::DB> level_db_;
};
class GDataLevelDBIter : public GDataDBIter {
public:
GDataLevelDBIter(scoped_ptr<leveldb::Iterator> level_db_iter,
GDataDB* db, const FilePath& path);
private:
virtual ~GDataLevelDBIter();
// GDataDBIter implementation.
virtual bool GetNext(std::string* path,
scoped_ptr<GDataEntry>* entry) OVERRIDE;
scoped_ptr<leveldb::Iterator> level_db_iter_;
GDataDB* db_;
const FilePath path_;
};
} // namespace gdata
#endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_LEVELDB_H_
......@@ -558,9 +558,6 @@
'browser/chromeos/gdata/gdata_cache_metadata.h',
'browser/chromeos/gdata/gdata_contacts_service.cc',
'browser/chromeos/gdata/gdata_contacts_service.h',
'browser/chromeos/gdata/gdata_db.h',
'browser/chromeos/gdata/gdata_db_factory.cc',
'browser/chromeos/gdata/gdata_db_factory.h',
'browser/chromeos/gdata/gdata_documents_service.cc',
'browser/chromeos/gdata/gdata_documents_service.h',
'browser/chromeos/gdata/gdata_download_observer.cc',
......@@ -573,8 +570,6 @@
'browser/chromeos/gdata/gdata_file_system_proxy.h',
'browser/chromeos/gdata/gdata_files.cc',
'browser/chromeos/gdata/gdata_files.h',
'browser/chromeos/gdata/gdata_leveldb.cc',
'browser/chromeos/gdata/gdata_leveldb.h',
'browser/chromeos/gdata/gdata_operation_registry.cc',
'browser/chromeos/gdata/gdata_operation_registry.h',
'browser/chromeos/gdata/gdata_operation_runner.cc',
......
......@@ -1110,7 +1110,6 @@
'browser/chromeos/gdata/gdata_cache_unittest.cc',
'browser/chromeos/gdata/gdata_contacts_service_stub.cc',
'browser/chromeos/gdata/gdata_contacts_service_stub.h',
'browser/chromeos/gdata/gdata_db_unittest.cc',
'browser/chromeos/gdata/gdata_file_system_unittest.cc',
'browser/chromeos/gdata/gdata_files_unittest.cc',
'browser/chromeos/gdata/gdata_operation_registry_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment