Skip to content

Commit 547fc00

Browse files
chinmaygardednfield
authored andcommitted
Clearer names for primary keys.
1 parent 89589cc commit 547fc00

8 files changed

+28
-26
lines changed

impeller/archivist/archivable.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Archivable {
3030
public:
3131
using ArchiveName = uint64_t;
3232

33-
virtual ArchiveName GetArchiveName() const = 0;
33+
virtual ArchiveName GetArchivePrimaryKey() const = 0;
3434

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

impeller/archivist/archive.cc

+11-9
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ bool Archive::ArchiveInstance(const ArchiveDef& definition,
4343
return false;
4444
}
4545

46-
auto statement = registration->GetInsertStatement();
46+
auto statement = registration->CreateInsertStatement();
4747

4848
if (!statement.IsValid() || !statement.Reset()) {
4949
/*
@@ -52,21 +52,22 @@ bool Archive::ArchiveInstance(const ArchiveDef& definition,
5252
return false;
5353
}
5454

55-
auto itemName = archivable.GetArchiveName();
55+
auto primary_key = archivable.GetArchivePrimaryKey();
5656

5757
/*
5858
* The lifecycle of the archive item is tied to this scope and there is no
5959
* way for the user to create an instance of an archive item. So its safe
6060
* for its members to be references. It does not manage the lifetimes of
6161
* anything.
6262
*/
63-
ArchiveLocation item(*this, statement, *registration, itemName);
63+
ArchiveLocation item(*this, statement, *registration, primary_key);
6464

6565
/*
6666
* We need to bind the primary key only if the item does not provide its own
6767
*/
6868
if (!definition.auto_key &&
69-
!statement.WriteValue(ArchiveClassRegistration::NameIndex, itemName)) {
69+
!statement.WriteValue(ArchiveClassRegistration::kPrimaryKeyIndex,
70+
primary_key)) {
7071
return false;
7172
}
7273

@@ -80,7 +81,7 @@ bool Archive::ArchiveInstance(const ArchiveDef& definition,
8081

8182
int64_t lastInsert = database_->GetLastInsertRowID();
8283

83-
if (!definition.auto_key && lastInsert != static_cast<int64_t>(itemName)) {
84+
if (!definition.auto_key && lastInsert != static_cast<int64_t>(primary_key)) {
8485
return false;
8586
}
8687

@@ -122,7 +123,7 @@ size_t Archive::UnarchiveInstances(const ArchiveDef& definition,
122123

123124
const bool isQueryingSingle = name != ArchiveNameAuto;
124125

125-
auto statement = registration->GetQueryStatement(isQueryingSingle);
126+
auto statement = registration->CreateQueryStatement(isQueryingSingle);
126127

127128
if (!statement.IsValid() || !statement.Reset()) {
128129
return 0;
@@ -133,7 +134,8 @@ size_t Archive::UnarchiveInstances(const ArchiveDef& definition,
133134
* If a single statement is being queried for, bind the name as a statement
134135
* argument.
135136
*/
136-
if (!statement.WriteValue(ArchiveClassRegistration::NameIndex, name)) {
137+
if (!statement.WriteValue(ArchiveClassRegistration::kPrimaryKeyIndex,
138+
name)) {
137139
return 0;
138140
}
139141
}
@@ -181,7 +183,7 @@ ArchiveLocation::ArchiveLocation(Archive& context,
181183
name_(name),
182184
current_class_(registration.GetClassName()) {}
183185

184-
Archivable::ArchiveName ArchiveLocation::Name() const {
186+
Archivable::ArchiveName ArchiveLocation::GetPrimaryKey() const {
185187
return name_;
186188
}
187189

@@ -226,7 +228,7 @@ bool ArchiveLocation::Write(ArchiveDef::Member member,
226228
}
227229

228230
/*
229-
* Bind the name of the serialiable
231+
* Bind the name of the serializable
230232
*/
231233
if (!statement_.WriteValue(found.first, lastInsert)) {
232234
return false;

impeller/archivist/archive_class_registration.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ bool ArchiveClassRegistration::CreateTable(bool autoIncrement) {
109109
return statement.Execute() == ArchiveStatement::Result::kDone;
110110
}
111111

112-
ArchiveStatement ArchiveClassRegistration::GetQueryStatement(
112+
ArchiveStatement ArchiveClassRegistration::CreateQueryStatement(
113113
bool single) const {
114114
std::stringstream stream;
115115
stream << "SELECT " << ArchivePrimaryKeyColumnName << ", ";
@@ -132,7 +132,7 @@ ArchiveStatement ArchiveClassRegistration::GetQueryStatement(
132132
return database_.CreateStatement(stream.str());
133133
}
134134

135-
ArchiveStatement ArchiveClassRegistration::GetInsertStatement() const {
135+
ArchiveStatement ArchiveClassRegistration::CreateInsertStatement() const {
136136
std::stringstream stream;
137137
stream << "INSERT OR REPLACE INTO " << class_name_ << " VALUES ( ?, ";
138138
for (size_t i = 0; i < member_count_; i++) {

impeller/archivist/archive_class_registration.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ namespace impeller {
1212

1313
class ArchiveClassRegistration {
1414
public:
15+
static constexpr size_t kPrimaryKeyIndex = 0u;
16+
17+
bool IsValid() const;
18+
1519
using ColumnResult = std::pair<size_t, bool>;
1620
ColumnResult FindColumn(const std::string& className,
1721
ArchiveDef::Member member) const;
@@ -20,13 +24,9 @@ class ArchiveClassRegistration {
2024

2125
size_t GetMemberCount() const;
2226

23-
bool IsValid() const;
24-
25-
ArchiveStatement GetInsertStatement() const;
26-
27-
ArchiveStatement GetQueryStatement(bool single) const;
27+
ArchiveStatement CreateInsertStatement() const;
2828

29-
static const size_t NameIndex = 0;
29+
ArchiveStatement CreateQueryStatement(bool single) const;
3030

3131
private:
3232
using MemberColumnMap = std::map<ArchiveDef::Member, size_t>;

impeller/archivist/archive_location.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class ArchiveLocation {
159159
return success;
160160
}
161161

162-
Archivable::ArchiveName Name() const;
162+
Archivable::ArchiveName GetPrimaryKey() const;
163163

164164
private:
165165
Archive& context_;

impeller/archivist/archive_vector.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const ArchiveDef ArchiveVector::ArchiveDefinition = {
2222
/* .members = */ {0},
2323
};
2424

25-
Archivable::ArchiveName ArchiveVector::GetArchiveName() const {
25+
Archivable::ArchiveName ArchiveVector::GetArchivePrimaryKey() const {
2626
return ArchiveNameAuto;
2727
}
2828

impeller/archivist/archive_vector.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ArchiveVector : public Archivable {
1313
public:
1414
static const ArchiveDef ArchiveDefinition;
1515

16-
ArchiveName GetArchiveName() const override;
16+
ArchiveName GetArchivePrimaryKey() const override;
1717

1818
const std::vector<int64_t> GetKeys() const;
1919

impeller/archivist/archivist_unittests.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Sample : public Archivable {
2323
uint64_t GetSomeData() const { return some_data_; }
2424

2525
// |Archivable|
26-
ArchiveName GetArchiveName() const override { return name_; }
26+
ArchiveName GetArchivePrimaryKey() const override { return name_; }
2727

2828
// |Archivable|
2929
bool Write(ArchiveLocation& item) const override {
@@ -32,7 +32,7 @@ class Sample : public Archivable {
3232

3333
// |Archivable|
3434
bool Read(ArchiveLocation& item) override {
35-
name_ = item.Name();
35+
name_ = item.GetPrimaryKey();
3636
return item.Read(999, some_data_);
3737
};
3838

@@ -92,7 +92,7 @@ TEST_F(ArchiveTest, ReadData) {
9292

9393
for (size_t i = 0; i < count; i++) {
9494
Sample sample(i + 1);
95-
keys.push_back(sample.GetArchiveName());
95+
keys.push_back(sample.GetArchivePrimaryKey());
9696
values.push_back(sample.GetSomeData());
9797
ASSERT_TRUE(archive.Write(sample));
9898
}
@@ -118,7 +118,7 @@ TEST_F(ArchiveTest, ReadDataWithNames) {
118118

119119
for (size_t i = 0; i < count; i++) {
120120
Sample sample(i + 1);
121-
keys.push_back(sample.GetArchiveName());
121+
keys.push_back(sample.GetArchivePrimaryKey());
122122
values.push_back(sample.GetSomeData());
123123
ASSERT_TRUE(archive.Write(sample));
124124
}
@@ -127,7 +127,7 @@ TEST_F(ArchiveTest, ReadDataWithNames) {
127127
Sample sample;
128128
ASSERT_TRUE(archive.Read(keys[i], sample));
129129
ASSERT_EQ(values[i], sample.GetSomeData());
130-
ASSERT_EQ(keys[i], sample.GetArchiveName());
130+
ASSERT_EQ(keys[i], sample.GetArchivePrimaryKey());
131131
}
132132
}
133133

0 commit comments

Comments
 (0)