Skip to content

Commit 188890c

Browse files
chinmaygardednfield
authored andcommitted
Support user readable table names.
1 parent 7b3ab41 commit 188890c

9 files changed

+186
-175
lines changed

impeller/archivist/archivable.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@
1212
namespace impeller {
1313

1414
struct ArchiveDef {
15-
using Member = uint64_t;
16-
using Members = std::vector<Member>;
17-
18-
const ArchiveDef* isa = nullptr;
1915
const std::string table_name;
2016
const bool auto_key = true;
21-
const Members members;
17+
const std::vector<std::string> members;
2218
};
2319

2420
class ArchiveLocation;
@@ -31,6 +27,8 @@ using PrimaryKey = std::optional<int64_t>;
3127
///
3228
class Archivable {
3329
public:
30+
virtual ~Archivable() = default;
31+
3432
virtual PrimaryKey GetPrimaryKey() const = 0;
3533

3634
virtual bool Write(ArchiveLocation& item) const = 0;

impeller/archivist/archive.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,25 @@ class Archive {
2727
bool IsValid() const;
2828

2929
template <class T,
30-
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
31-
bool Write(const T& archivable) {
32-
const ArchiveDef& def = T::ArchiveDefinition;
30+
class = std::enable_if_t<std::is_base_of<Archivable, T>::value>>
31+
[[nodiscard]] bool Write(const T& archivable) {
32+
const ArchiveDef& def = T::kArchiveDefinition;
3333
return ArchiveInstance(def, archivable).has_value();
3434
}
3535

3636
template <class T,
37-
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
38-
bool Read(PrimaryKey name, T& archivable) {
39-
const ArchiveDef& def = T::ArchiveDefinition;
37+
class = std::enable_if_t<std::is_base_of<Archivable, T>::value>>
38+
[[nodiscard]] bool Read(PrimaryKey name, T& archivable) {
39+
const ArchiveDef& def = T::kArchiveDefinition;
4040
return UnarchiveInstance(def, name, archivable);
4141
}
4242

4343
using UnarchiveStep = std::function<bool(ArchiveLocation&)>;
4444

4545
template <class T,
46-
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
47-
size_t Read(UnarchiveStep stepper) {
48-
const ArchiveDef& def = T::ArchiveDefinition;
46+
class = std::enable_if_t<std::is_base_of<Archivable, T>::value>>
47+
[[nodiscard]] size_t Read(UnarchiveStep stepper) {
48+
const ArchiveDef& def = T::kArchiveDefinition;
4949
return UnarchiveInstances(def, stepper);
5050
}
5151

impeller/archivist/archive_class_registration.cc

+32-55
Original file line numberDiff line numberDiff line change
@@ -11,66 +11,41 @@
1111

1212
namespace impeller {
1313

14-
static const char* const ArchiveColumnPrefix = "col_";
15-
static const char* const ArchivePrimaryKeyColumnName = "primary_key";
14+
static constexpr const char* kArchivePrimaryKeyColumnName = "primary_key";
1615

1716
ArchiveClassRegistration::ArchiveClassRegistration(ArchiveDatabase& database,
1817
ArchiveDef definition)
19-
: database_(database), class_name_(definition.table_name) {
20-
/*
21-
* Each class in the archive class hierarchy is assigned an entry in the
22-
* class map.
23-
*/
24-
const ArchiveDef* current = &definition;
25-
size_t currentMember = 1;
26-
while (current != nullptr) {
27-
auto membersInCurrent = current->members.size();
28-
member_count_ += membersInCurrent;
29-
MemberColumnMap map;
30-
for (const auto& member : current->members) {
31-
map[member] = currentMember++;
32-
}
33-
class_map_[current->table_name] = map;
34-
current = current->isa;
18+
: database_(database), definition_(std::move(definition)) {
19+
for (size_t i = 0; i < definition.members.size(); i++) {
20+
// The first index entry is the primary key. So add one to the index.
21+
column_map_[definition.members[i]] = i + 1;
3522
}
36-
37-
is_ready_ = CreateTable(definition.auto_key);
23+
is_valid_ = CreateTable();
3824
}
3925

4026
const std::string& ArchiveClassRegistration::GetClassName() const {
41-
return class_name_;
27+
return definition_.table_name;
4228
}
4329

4430
size_t ArchiveClassRegistration::GetMemberCount() const {
45-
return member_count_;
31+
return column_map_.size();
4632
}
4733

4834
bool ArchiveClassRegistration::IsValid() const {
49-
return is_ready_;
35+
return is_valid_;
5036
}
5137

5238
std::optional<size_t> ArchiveClassRegistration::FindColumnIndex(
53-
const std::string& className,
54-
ArchiveDef::Member member) const {
55-
auto found = class_map_.find(className);
56-
57-
if (found == class_map_.end()) {
58-
return std::nullopt;
59-
}
60-
61-
const auto& memberToColumns = found->second;
62-
63-
auto foundColumn = memberToColumns.find(member);
64-
65-
if (foundColumn == memberToColumns.end()) {
39+
const std::string& member) const {
40+
auto found = column_map_.find(member);
41+
if (found == column_map_.end()) {
6642
return std::nullopt;
6743
}
68-
69-
return foundColumn->second;
44+
return found->second;
7045
}
7146

72-
bool ArchiveClassRegistration::CreateTable(bool autoIncrement) {
73-
if (class_name_.size() == 0 || member_count_ == 0) {
47+
bool ArchiveClassRegistration::CreateTable() {
48+
if (definition_.table_name.empty() || definition_.members.empty()) {
7449
return false;
7550
}
7651

@@ -80,16 +55,17 @@ bool ArchiveClassRegistration::CreateTable(bool autoIncrement) {
8055
* Table names cannot participate in parameter substitution, so we prepare
8156
* a statement and check its validity before running.
8257
*/
83-
stream << "CREATE TABLE IF NOT EXISTS " << class_name_.c_str() << " ("
84-
<< ArchivePrimaryKeyColumnName;
58+
stream << "CREATE TABLE IF NOT EXISTS " << definition_.table_name << " ("
59+
<< kArchivePrimaryKeyColumnName;
8560

86-
if (autoIncrement) {
61+
if (definition_.auto_key) {
8762
stream << " INTEGER PRIMARY KEY AUTOINCREMENT, ";
8863
} else {
8964
stream << " INTEGER PRIMARY KEY, ";
9065
}
91-
for (size_t i = 0, columns = member_count_; i < columns; i++) {
92-
stream << ArchiveColumnPrefix << std::to_string(i + 1);
66+
67+
for (size_t i = 0, columns = definition_.members.size(); i < columns; i++) {
68+
stream << definition_.members[i];
9369
if (i != columns - 1) {
9470
stream << ", ";
9571
}
@@ -112,19 +88,19 @@ bool ArchiveClassRegistration::CreateTable(bool autoIncrement) {
11288
ArchiveStatement ArchiveClassRegistration::CreateQueryStatement(
11389
bool single) const {
11490
std::stringstream stream;
115-
stream << "SELECT " << ArchivePrimaryKeyColumnName << ", ";
116-
for (size_t i = 0, members = member_count_; i < members; i++) {
117-
stream << ArchiveColumnPrefix << std::to_string(i + 1);
118-
if (i != members - 1) {
91+
stream << "SELECT " << kArchivePrimaryKeyColumnName << ", ";
92+
for (size_t i = 0, columns = definition_.members.size(); i < columns; i++) {
93+
stream << definition_.members[i];
94+
if (i != columns - 1) {
11995
stream << ",";
12096
}
12197
}
122-
stream << " FROM " << class_name_;
98+
stream << " FROM " << definition_.table_name;
12399

124100
if (single) {
125-
stream << " WHERE " << ArchivePrimaryKeyColumnName << " = ?";
101+
stream << " WHERE " << kArchivePrimaryKeyColumnName << " = ?";
126102
} else {
127-
stream << " ORDER BY " << ArchivePrimaryKeyColumnName << " ASC";
103+
stream << " ORDER BY " << kArchivePrimaryKeyColumnName << " ASC";
128104
}
129105

130106
stream << ";";
@@ -134,10 +110,11 @@ ArchiveStatement ArchiveClassRegistration::CreateQueryStatement(
134110

135111
ArchiveStatement ArchiveClassRegistration::CreateInsertStatement() const {
136112
std::stringstream stream;
137-
stream << "INSERT OR REPLACE INTO " << class_name_ << " VALUES ( ?, ";
138-
for (size_t i = 0; i < member_count_; i++) {
113+
stream << "INSERT OR REPLACE INTO " << definition_.table_name
114+
<< " VALUES ( ?, ";
115+
for (size_t i = 0, columns = definition_.members.size(); i < columns; i++) {
139116
stream << "?";
140-
if (i != member_count_ - 1) {
117+
if (i != columns - 1) {
141118
stream << ", ";
142119
}
143120
}

impeller/archivist/archive_class_registration.h

+6-9
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ class ArchiveClassRegistration {
1717

1818
bool IsValid() const;
1919

20-
std::optional<size_t> FindColumnIndex(const std::string& className,
21-
ArchiveDef::Member member) const;
20+
std::optional<size_t> FindColumnIndex(const std::string& member) const;
2221

2322
const std::string& GetClassName() const;
2423

@@ -29,20 +28,18 @@ class ArchiveClassRegistration {
2928
ArchiveStatement CreateQueryStatement(bool single) const;
3029

3130
private:
32-
using MemberColumnMap = std::map<ArchiveDef::Member, size_t>;
33-
using ClassMap = std::map<std::string, MemberColumnMap>;
31+
using MemberColumnMap = std::map<std::string, size_t>;
3432

3533
friend class ArchiveDatabase;
3634

3735
ArchiveClassRegistration(ArchiveDatabase& database, ArchiveDef definition);
3836

39-
bool CreateTable(bool autoIncrement);
37+
bool CreateTable();
4038

4139
ArchiveDatabase& database_;
42-
ClassMap class_map_;
43-
std::string class_name_;
44-
size_t member_count_ = 0;
45-
bool is_ready_ = false;
40+
const ArchiveDef definition_;
41+
MemberColumnMap column_map_;
42+
bool is_valid_ = false;
4643

4744
FML_DISALLOW_COPY_AND_ASSIGN(ArchiveClassRegistration);
4845
};

impeller/archivist/archive_location.cc

+23-24
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,37 @@ ArchiveLocation::ArchiveLocation(Archive& context,
1616
: context_(context),
1717
statement_(statement),
1818
registration_(registration),
19-
primary_key_(name),
20-
current_class_(registration.GetClassName()) {}
19+
primary_key_(name) {}
2120

2221
PrimaryKey ArchiveLocation::GetPrimaryKey() const {
2322
return primary_key_;
2423
}
2524

26-
bool ArchiveLocation::Write(ArchiveDef::Member member,
25+
bool ArchiveLocation::Write(const std::string& member,
2726
const std::string& item) {
28-
auto index = registration_.FindColumnIndex(current_class_, member);
27+
auto index = registration_.FindColumnIndex(member);
2928
return index.has_value() ? statement_.WriteValue(index.value(), item) : false;
3029
}
3130

32-
bool ArchiveLocation::WriteIntegral(ArchiveDef::Member member, int64_t item) {
33-
auto index = registration_.FindColumnIndex(current_class_, member);
31+
bool ArchiveLocation::WriteIntegral(const std::string& member, int64_t item) {
32+
auto index = registration_.FindColumnIndex(member);
3433
return index.has_value() ? statement_.WriteValue(index.value(), item) : false;
3534
}
3635

37-
bool ArchiveLocation::Write(ArchiveDef::Member member, double item) {
38-
auto index = registration_.FindColumnIndex(current_class_, member);
36+
bool ArchiveLocation::Write(const std::string& member, double item) {
37+
auto index = registration_.FindColumnIndex(member);
3938
return index.has_value() ? statement_.WriteValue(index.value(), item) : false;
4039
}
4140

42-
bool ArchiveLocation::Write(ArchiveDef::Member member, const Allocation& item) {
43-
auto index = registration_.FindColumnIndex(current_class_, member);
41+
bool ArchiveLocation::Write(const std::string& member, const Allocation& item) {
42+
auto index = registration_.FindColumnIndex(member);
4443
return index.has_value() ? statement_.WriteValue(index.value(), item) : false;
4544
}
4645

47-
bool ArchiveLocation::Write(ArchiveDef::Member member,
46+
bool ArchiveLocation::Write(const std::string& member,
4847
const ArchiveDef& otherDef,
4948
const Archivable& other) {
50-
auto index = registration_.FindColumnIndex(current_class_, member);
49+
auto index = registration_.FindColumnIndex(member);
5150

5251
if (!index.has_value()) {
5352
return false;
@@ -76,13 +75,13 @@ bool ArchiveLocation::Write(ArchiveDef::Member member,
7675
std::optional<int64_t> ArchiveLocation::WriteVectorKeys(
7776
std::vector<int64_t>&& members) {
7877
ArchiveVector vector(std::move(members));
79-
return context_.ArchiveInstance(ArchiveVector::ArchiveDefinition, vector);
78+
return context_.ArchiveInstance(ArchiveVector::kArchiveDefinition, vector);
8079
}
8180

8281
bool ArchiveLocation::ReadVectorKeys(PrimaryKey name,
8382
std::vector<int64_t>& members) {
8483
ArchiveVector vector;
85-
if (!context_.UnarchiveInstance(ArchiveVector::ArchiveDefinition, name,
84+
if (!context_.UnarchiveInstance(ArchiveVector::kArchiveDefinition, name,
8685
vector)) {
8786
return false;
8887
}
@@ -91,30 +90,30 @@ bool ArchiveLocation::ReadVectorKeys(PrimaryKey name,
9190
return true;
9291
}
9392

94-
bool ArchiveLocation::Read(ArchiveDef::Member member, std::string& item) {
95-
auto index = registration_.FindColumnIndex(current_class_, member);
93+
bool ArchiveLocation::Read(const std::string& member, std::string& item) {
94+
auto index = registration_.FindColumnIndex(member);
9695
return index.has_value() ? statement_.ReadValue(index.value(), item) : false;
9796
}
9897

99-
bool ArchiveLocation::ReadIntegral(ArchiveDef::Member member, int64_t& item) {
100-
auto index = registration_.FindColumnIndex(current_class_, member);
98+
bool ArchiveLocation::ReadIntegral(const std::string& member, int64_t& item) {
99+
auto index = registration_.FindColumnIndex(member);
101100
return index.has_value() ? statement_.ReadValue(index.value(), item) : false;
102101
}
103102

104-
bool ArchiveLocation::Read(ArchiveDef::Member member, double& item) {
105-
auto index = registration_.FindColumnIndex(current_class_, member);
103+
bool ArchiveLocation::Read(const std::string& member, double& item) {
104+
auto index = registration_.FindColumnIndex(member);
106105
return index.has_value() ? statement_.ReadValue(index.value(), item) : false;
107106
}
108107

109-
bool ArchiveLocation::Read(ArchiveDef::Member member, Allocation& item) {
110-
auto index = registration_.FindColumnIndex(current_class_, member);
108+
bool ArchiveLocation::Read(const std::string& member, Allocation& item) {
109+
auto index = registration_.FindColumnIndex(member);
111110
return index.has_value() ? statement_.ReadValue(index.value(), item) : false;
112111
}
113112

114-
bool ArchiveLocation::Read(ArchiveDef::Member member,
113+
bool ArchiveLocation::Read(const std::string& member,
115114
const ArchiveDef& otherDef,
116115
Archivable& other) {
117-
auto index = registration_.FindColumnIndex(current_class_, member);
116+
auto index = registration_.FindColumnIndex(member);
118117

119118
/*
120119
* Make sure a member is present at that column

0 commit comments

Comments
 (0)