Skip to content

Commit 8d7d8e6

Browse files
chinmaygardednfield
authored andcommitted
Cleanup archive structure.
1 parent 147e64f commit 8d7d8e6

11 files changed

+170
-123
lines changed

impeller/archivist/BUILD.gn

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ impeller_component("archivist") {
3434

3535
impeller_component("archivist_unittests") {
3636
testonly = true
37-
sources = [ "archivist_unittests.cc" ]
37+
sources = [
38+
"archivist_fixture.cc",
39+
"archivist_fixture.h",
40+
"archivist_unittests.cc",
41+
]
3842
deps = [
3943
":archivist",
4044
"//flutter/testing",

impeller/archivist/archive.cc

+5-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
namespace impeller {
1616

17-
Archive::Archive(const std::string& path, bool recreate)
18-
: database_(std::make_unique<ArchiveDatabase>(path, recreate)) {}
17+
Archive::Archive(const std::string& path)
18+
: database_(std::make_unique<ArchiveDatabase>(path)) {}
1919

2020
Archive::~Archive() {
2121
FML_DCHECK(transaction_count_ == 0)
@@ -64,7 +64,7 @@ bool Archive::ArchiveInstance(const ArchiveDef& definition,
6464
/*
6565
* We need to bind the primary key only if the item does not provide its own
6666
*/
67-
if (!definition.autoAssignName &&
67+
if (!definition.auto_key &&
6868
!statement.WriteValue(ArchiveClassRegistration::NameIndex, itemName)) {
6969
return false;
7070
}
@@ -79,8 +79,7 @@ bool Archive::ArchiveInstance(const ArchiveDef& definition,
7979

8080
int64_t lastInsert = database_->GetLastInsertRowID();
8181

82-
if (!definition.autoAssignName &&
83-
lastInsert != static_cast<int64_t>(itemName)) {
82+
if (!definition.auto_key && lastInsert != static_cast<int64_t>(itemName)) {
8483
return false;
8584
}
8685

@@ -256,7 +255,7 @@ bool ArchiveLocation::ReadVectorKeys(Archivable::ArchiveName name,
256255
return false;
257256
}
258257

259-
const auto& keys = vector.keys();
258+
const auto& keys = vector.GetKeys();
260259

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

impeller/archivist/archive.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ struct ArchiveDef {
2424
using Member = uint64_t;
2525
using Members = std::vector<Member>;
2626

27-
const ArchiveDef* superClass;
28-
const std::string className;
29-
const bool autoAssignName;
27+
const ArchiveDef* isa = nullptr;
28+
const std::string table_name;
29+
const bool auto_key = true;
3030
const Members members;
3131
};
3232

3333
static const Archivable::ArchiveName ArchiveNameAuto = 0;
3434

3535
class Archive {
3636
public:
37-
Archive(const std::string& path, bool recreate);
37+
Archive(const std::string& path);
3838

3939
~Archive();
4040

impeller/archivist/archive_class_registration.cc

+10-12
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111

1212
namespace impeller {
1313

14-
static const char* const ArchiveColumnPrefix = "item";
15-
static const char* const ArchivePrimaryKeyColumnName = "name";
16-
static const char* const ArchiveTablePrefix = "RL_";
14+
static const char* const ArchiveColumnPrefix = "col_";
15+
static const char* const ArchivePrimaryKeyColumnName = "primary_key";
1716

1817
ArchiveClassRegistration::ArchiveClassRegistration(ArchiveDatabase& database,
1918
ArchiveDef definition)
20-
: database_(database), class_name_(definition.className) {
19+
: database_(database), class_name_(definition.table_name) {
2120
/*
2221
* Each class in the archive class hierarchy is assigned an entry in the
2322
* class map.
@@ -31,11 +30,11 @@ ArchiveClassRegistration::ArchiveClassRegistration(ArchiveDatabase& database,
3130
for (const auto& member : current->members) {
3231
map[member] = currentMember++;
3332
}
34-
class_map_[current->className] = map;
35-
current = current->superClass;
33+
class_map_[current->table_name] = map;
34+
current = current->isa;
3635
}
3736

38-
is_ready_ = CreateTable(definition.autoAssignName);
37+
is_ready_ = CreateTable(definition.auto_key);
3938
}
4039

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

8786
if (autoIncrement) {
8887
stream << " INTEGER PRIMARY KEY AUTOINCREMENT, ";
@@ -120,7 +119,7 @@ ArchiveStatement ArchiveClassRegistration::GetQueryStatement(
120119
stream << ",";
121120
}
122121
}
123-
stream << " FROM " << ArchiveTablePrefix << class_name_;
122+
stream << " FROM " << class_name_;
124123

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

136135
ArchiveStatement ArchiveClassRegistration::GetInsertStatement() const {
137136
std::stringstream stream;
138-
stream << "INSERT OR REPLACE INTO " << ArchiveTablePrefix << class_name_
139-
<< " VALUES ( ?, ";
137+
stream << "INSERT OR REPLACE INTO " << class_name_ << " VALUES ( ?, ";
140138
for (size_t i = 0; i < member_count_; i++) {
141139
stream << "?";
142140
if (i != member_count_ - 1) {

impeller/archivist/archive_database.cc

+3-7
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ namespace impeller {
1818

1919
#define DB_HANDLE reinterpret_cast<sqlite3*>(database_)
2020

21-
ArchiveDatabase::ArchiveDatabase(const std::string& filename, bool recreate) {
22-
if (recreate) {
23-
::remove(filename.c_str());
24-
}
25-
21+
ArchiveDatabase::ArchiveDatabase(const std::string& filename) {
2622
if (::sqlite3_initialize() != SQLITE_OK) {
2723
VALIDATION_LOG << "Could not initialize sqlite.";
2824
return;
@@ -82,7 +78,7 @@ static inline const ArchiveClassRegistration* RegistrationIfReady(
8278

8379
const ArchiveClassRegistration* ArchiveDatabase::GetRegistrationForDefinition(
8480
const ArchiveDef& definition) {
85-
auto found = registrations_.find(definition.className);
81+
auto found = registrations_.find(definition.table_name);
8682
if (found != registrations_.end()) {
8783
/*
8884
* This class has already been registered.
@@ -96,7 +92,7 @@ const ArchiveClassRegistration* ArchiveDatabase::GetRegistrationForDefinition(
9692
auto registration = std::unique_ptr<ArchiveClassRegistration>(
9793
new ArchiveClassRegistration(*this, definition));
9894
auto res =
99-
registrations_.emplace(definition.className, std::move(registration));
95+
registrations_.emplace(definition.table_name, std::move(registration));
10096

10197
/*
10298
* If the new class registation is ready, return it to the caller.

impeller/archivist/archive_database.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct ArchiveDef;
1818

1919
class ArchiveDatabase {
2020
public:
21-
ArchiveDatabase(const std::string& filename, bool recreate);
21+
ArchiveDatabase(const std::string& filename);
2222

2323
~ArchiveDatabase();
2424

impeller/archivist/archive_vector.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Archivable::ArchiveName ArchiveVector::GetArchiveName() const {
2424
return ArchiveNameAuto;
2525
}
2626

27-
const std::vector<int64_t> ArchiveVector::keys() const {
27+
const std::vector<int64_t> ArchiveVector::GetKeys() const {
2828
return keys_;
2929
}
3030

impeller/archivist/archive_vector.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ArchiveVector : public Archivable {
1515

1616
ArchiveName GetArchiveName() const override;
1717

18-
const std::vector<int64_t> keys() const;
18+
const std::vector<int64_t> GetKeys() const;
1919

2020
bool Write(ArchiveLocation& item) const override;
2121

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "impeller/archivist/archivist_fixture.h"
6+
7+
#include "flutter/fml/paths.h"
8+
9+
namespace impeller {
10+
namespace testing {
11+
12+
ArchivistFixture::ArchivistFixture() {
13+
std::stringstream stream;
14+
stream << flutter::testing::GetCurrentTestName() << ".db";
15+
archive_file_name_ = fml::paths::JoinPaths(
16+
{flutter::testing::GetFixturesPath(), stream.str()});
17+
}
18+
19+
ArchivistFixture::~ArchivistFixture() = default;
20+
21+
const std::string ArchivistFixture::GetArchiveFileName() const {
22+
return archive_file_name_;
23+
}
24+
25+
void ArchivistFixture::SetUp() {
26+
DeleteArchiveFile();
27+
}
28+
29+
void ArchivistFixture::TearDown() {
30+
// DeleteArchiveFile();
31+
}
32+
33+
void ArchivistFixture::DeleteArchiveFile() const {
34+
fml::UnlinkFile(archive_file_name_.c_str());
35+
}
36+
37+
} // namespace testing
38+
} // namespace impeller
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include "flutter/fml/macros.h"
8+
#include "flutter/testing/testing.h"
9+
10+
namespace impeller {
11+
namespace testing {
12+
13+
class ArchivistFixture : public ::testing::Test {
14+
public:
15+
ArchivistFixture();
16+
17+
~ArchivistFixture();
18+
19+
// |::testing::Test|
20+
void SetUp() override;
21+
22+
// |::testing::Test|
23+
void TearDown() override;
24+
25+
const std::string GetArchiveFileName() const;
26+
27+
private:
28+
std::string archive_file_name_;
29+
30+
void DeleteArchiveFile() const;
31+
32+
FML_DISALLOW_COPY_AND_ASSIGN(ArchivistFixture);
33+
};
34+
35+
} // namespace testing
36+
} // namespace impeller

0 commit comments

Comments
 (0)