Skip to content

Commit

Permalink
Cleanup archive structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent 147e64f commit 8d7d8e6
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 123 deletions.
6 changes: 5 additions & 1 deletion impeller/archivist/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ impeller_component("archivist") {

impeller_component("archivist_unittests") {
testonly = true
sources = [ "archivist_unittests.cc" ]
sources = [
"archivist_fixture.cc",
"archivist_fixture.h",
"archivist_unittests.cc",
]
deps = [
":archivist",
"//flutter/testing",
Expand Down
11 changes: 5 additions & 6 deletions impeller/archivist/archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

namespace impeller {

Archive::Archive(const std::string& path, bool recreate)
: database_(std::make_unique<ArchiveDatabase>(path, recreate)) {}
Archive::Archive(const std::string& path)
: database_(std::make_unique<ArchiveDatabase>(path)) {}

Archive::~Archive() {
FML_DCHECK(transaction_count_ == 0)
Expand Down Expand Up @@ -64,7 +64,7 @@ bool Archive::ArchiveInstance(const ArchiveDef& definition,
/*
* We need to bind the primary key only if the item does not provide its own
*/
if (!definition.autoAssignName &&
if (!definition.auto_key &&
!statement.WriteValue(ArchiveClassRegistration::NameIndex, itemName)) {
return false;
}
Expand All @@ -79,8 +79,7 @@ bool Archive::ArchiveInstance(const ArchiveDef& definition,

int64_t lastInsert = database_->GetLastInsertRowID();

if (!definition.autoAssignName &&
lastInsert != static_cast<int64_t>(itemName)) {
if (!definition.auto_key && lastInsert != static_cast<int64_t>(itemName)) {
return false;
}

Expand Down Expand Up @@ -256,7 +255,7 @@ bool ArchiveLocation::ReadVectorKeys(Archivable::ArchiveName name,
return false;
}

const auto& keys = vector.keys();
const auto& keys = vector.GetKeys();

std::move(keys.begin(), keys.end(), std::back_inserter(members));

Expand Down
8 changes: 4 additions & 4 deletions impeller/archivist/archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ struct ArchiveDef {
using Member = uint64_t;
using Members = std::vector<Member>;

const ArchiveDef* superClass;
const std::string className;
const bool autoAssignName;
const ArchiveDef* isa = nullptr;
const std::string table_name;
const bool auto_key = true;
const Members members;
};

static const Archivable::ArchiveName ArchiveNameAuto = 0;

class Archive {
public:
Archive(const std::string& path, bool recreate);
Archive(const std::string& path);

~Archive();

Expand Down
22 changes: 10 additions & 12 deletions impeller/archivist/archive_class_registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@

namespace impeller {

static const char* const ArchiveColumnPrefix = "item";
static const char* const ArchivePrimaryKeyColumnName = "name";
static const char* const ArchiveTablePrefix = "RL_";
static const char* const ArchiveColumnPrefix = "col_";
static const char* const ArchivePrimaryKeyColumnName = "primary_key";

ArchiveClassRegistration::ArchiveClassRegistration(ArchiveDatabase& database,
ArchiveDef definition)
: database_(database), class_name_(definition.className) {
: database_(database), class_name_(definition.table_name) {
/*
* Each class in the archive class hierarchy is assigned an entry in the
* class map.
Expand All @@ -31,11 +30,11 @@ ArchiveClassRegistration::ArchiveClassRegistration(ArchiveDatabase& database,
for (const auto& member : current->members) {
map[member] = currentMember++;
}
class_map_[current->className] = map;
current = current->superClass;
class_map_[current->table_name] = map;
current = current->isa;
}

is_ready_ = CreateTable(definition.autoAssignName);
is_ready_ = CreateTable(definition.auto_key);
}

const std::string& ArchiveClassRegistration::GetClassName() const {
Expand Down Expand Up @@ -81,8 +80,8 @@ bool ArchiveClassRegistration::CreateTable(bool autoIncrement) {
* Table names cannot participate in parameter substitution, so we prepare
* a statement and check its validity before running.
*/
stream << "CREATE TABLE IF NOT EXISTS " << ArchiveTablePrefix
<< class_name_.c_str() << " (" << ArchivePrimaryKeyColumnName;
stream << "CREATE TABLE IF NOT EXISTS " << class_name_.c_str() << " ("
<< ArchivePrimaryKeyColumnName;

if (autoIncrement) {
stream << " INTEGER PRIMARY KEY AUTOINCREMENT, ";
Expand Down Expand Up @@ -120,7 +119,7 @@ ArchiveStatement ArchiveClassRegistration::GetQueryStatement(
stream << ",";
}
}
stream << " FROM " << ArchiveTablePrefix << class_name_;
stream << " FROM " << class_name_;

if (single) {
stream << " WHERE " << ArchivePrimaryKeyColumnName << " = ?";
Expand All @@ -135,8 +134,7 @@ ArchiveStatement ArchiveClassRegistration::GetQueryStatement(

ArchiveStatement ArchiveClassRegistration::GetInsertStatement() const {
std::stringstream stream;
stream << "INSERT OR REPLACE INTO " << ArchiveTablePrefix << class_name_
<< " VALUES ( ?, ";
stream << "INSERT OR REPLACE INTO " << class_name_ << " VALUES ( ?, ";
for (size_t i = 0; i < member_count_; i++) {
stream << "?";
if (i != member_count_ - 1) {
Expand Down
10 changes: 3 additions & 7 deletions impeller/archivist/archive_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ namespace impeller {

#define DB_HANDLE reinterpret_cast<sqlite3*>(database_)

ArchiveDatabase::ArchiveDatabase(const std::string& filename, bool recreate) {
if (recreate) {
::remove(filename.c_str());
}

ArchiveDatabase::ArchiveDatabase(const std::string& filename) {
if (::sqlite3_initialize() != SQLITE_OK) {
VALIDATION_LOG << "Could not initialize sqlite.";
return;
Expand Down Expand Up @@ -82,7 +78,7 @@ static inline const ArchiveClassRegistration* RegistrationIfReady(

const ArchiveClassRegistration* ArchiveDatabase::GetRegistrationForDefinition(
const ArchiveDef& definition) {
auto found = registrations_.find(definition.className);
auto found = registrations_.find(definition.table_name);
if (found != registrations_.end()) {
/*
* This class has already been registered.
Expand All @@ -96,7 +92,7 @@ const ArchiveClassRegistration* ArchiveDatabase::GetRegistrationForDefinition(
auto registration = std::unique_ptr<ArchiveClassRegistration>(
new ArchiveClassRegistration(*this, definition));
auto res =
registrations_.emplace(definition.className, std::move(registration));
registrations_.emplace(definition.table_name, std::move(registration));

/*
* If the new class registation is ready, return it to the caller.
Expand Down
2 changes: 1 addition & 1 deletion impeller/archivist/archive_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct ArchiveDef;

class ArchiveDatabase {
public:
ArchiveDatabase(const std::string& filename, bool recreate);
ArchiveDatabase(const std::string& filename);

~ArchiveDatabase();

Expand Down
2 changes: 1 addition & 1 deletion impeller/archivist/archive_vector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Archivable::ArchiveName ArchiveVector::GetArchiveName() const {
return ArchiveNameAuto;
}

const std::vector<int64_t> ArchiveVector::keys() const {
const std::vector<int64_t> ArchiveVector::GetKeys() const {
return keys_;
}

Expand Down
2 changes: 1 addition & 1 deletion impeller/archivist/archive_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ArchiveVector : public Archivable {

ArchiveName GetArchiveName() const override;

const std::vector<int64_t> keys() const;
const std::vector<int64_t> GetKeys() const;

bool Write(ArchiveLocation& item) const override;

Expand Down
38 changes: 38 additions & 0 deletions impeller/archivist/archivist_fixture.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2013 The Flutter 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 "impeller/archivist/archivist_fixture.h"

#include "flutter/fml/paths.h"

namespace impeller {
namespace testing {

ArchivistFixture::ArchivistFixture() {
std::stringstream stream;
stream << flutter::testing::GetCurrentTestName() << ".db";
archive_file_name_ = fml::paths::JoinPaths(
{flutter::testing::GetFixturesPath(), stream.str()});
}

ArchivistFixture::~ArchivistFixture() = default;

const std::string ArchivistFixture::GetArchiveFileName() const {
return archive_file_name_;
}

void ArchivistFixture::SetUp() {
DeleteArchiveFile();
}

void ArchivistFixture::TearDown() {
// DeleteArchiveFile();
}

void ArchivistFixture::DeleteArchiveFile() const {
fml::UnlinkFile(archive_file_name_.c_str());
}

} // namespace testing
} // namespace impeller
36 changes: 36 additions & 0 deletions impeller/archivist/archivist_fixture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#pragma once

#include "flutter/fml/macros.h"
#include "flutter/testing/testing.h"

namespace impeller {
namespace testing {

class ArchivistFixture : public ::testing::Test {
public:
ArchivistFixture();

~ArchivistFixture();

// |::testing::Test|
void SetUp() override;

// |::testing::Test|
void TearDown() override;

const std::string GetArchiveFileName() const;

private:
std::string archive_file_name_;

void DeleteArchiveFile() const;

FML_DISALLOW_COPY_AND_ASSIGN(ArchivistFixture);
};

} // namespace testing
} // namespace impeller
Loading

0 comments on commit 8d7d8e6

Please sign in to comment.