Skip to content

Commit 5289135

Browse files
chinmaygardednfield
authored andcommitted
Make primary keys optional.
1 parent 0c595a9 commit 5289135

File tree

8 files changed

+41
-32
lines changed

8 files changed

+41
-32
lines changed

impeller/archivist/archivable.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include <cstdint>
8+
#include <optional>
89
#include <string>
910
#include <vector>
1011

@@ -22,15 +23,15 @@ struct ArchiveDef {
2223

2324
class ArchiveLocation;
2425

26+
using PrimaryKey = std::optional<int64_t>;
27+
2528
//------------------------------------------------------------------------------
2629
/// @brief Instances of `Archivable`s can be read from and written to a
2730
/// persistent archive.
2831
///
2932
class Archivable {
3033
public:
31-
using ArchiveName = uint64_t;
32-
33-
virtual ArchiveName GetArchivePrimaryKey() const = 0;
34+
virtual PrimaryKey GetPrimaryKey() const = 0;
3435

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

impeller/archivist/archive.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "impeller/archivist/archive_class_registration.h"
1111
#include "impeller/archivist/archive_database.h"
1212
#include "impeller/archivist/archive_location.h"
13+
#include "impeller/base/validation.h"
1314

1415
namespace impeller {
1516

@@ -50,7 +51,13 @@ std::optional<int64_t /* row id */> Archive::ArchiveInstance(
5051
return std::nullopt;
5152
}
5253

53-
auto primary_key = archivable.GetArchivePrimaryKey();
54+
auto primary_key = archivable.GetPrimaryKey();
55+
56+
if (!definition.auto_key && !primary_key.has_value()) {
57+
VALIDATION_LOG << "Archive definition specified that primary keys will be "
58+
"explicitly specified but none was provided when asked.";
59+
return std::nullopt;
60+
}
5461

5562
/*
5663
* The lifecycle of the archive item is tied to this scope and there is no
@@ -65,7 +72,7 @@ std::optional<int64_t /* row id */> Archive::ArchiveInstance(
6572
*/
6673
if (!definition.auto_key &&
6774
!statement.WriteValue(ArchiveClassRegistration::kPrimaryKeyIndex,
68-
primary_key)) {
75+
primary_key.value())) {
6976
return std::nullopt;
7077
}
7178

@@ -79,7 +86,8 @@ std::optional<int64_t /* row id */> Archive::ArchiveInstance(
7986

8087
int64_t lastInsert = database_->GetLastInsertRowID();
8188

82-
if (!definition.auto_key && lastInsert != static_cast<int64_t>(primary_key)) {
89+
if (!definition.auto_key &&
90+
lastInsert != static_cast<int64_t>(primary_key.value())) {
8391
return std::nullopt;
8492
}
8593

@@ -93,7 +101,7 @@ std::optional<int64_t /* row id */> Archive::ArchiveInstance(
93101
}
94102

95103
bool Archive::UnarchiveInstance(const ArchiveDef& definition,
96-
Archivable::ArchiveName name,
104+
PrimaryKey name,
97105
Archivable& archivable) {
98106
UnarchiveStep stepper = [&archivable](ArchiveLocation& item) {
99107
archivable.Read(item);
@@ -105,7 +113,7 @@ bool Archive::UnarchiveInstance(const ArchiveDef& definition,
105113

106114
size_t Archive::UnarchiveInstances(const ArchiveDef& definition,
107115
Archive::UnarchiveStep stepper,
108-
std::optional<int64_t> primary_key) {
116+
PrimaryKey primary_key) {
109117
if (!IsValid()) {
110118
return 0;
111119
}

impeller/archivist/archive.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Archive {
3535

3636
template <class T,
3737
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
38-
bool Read(Archivable::ArchiveName name, T& archivable) {
38+
bool Read(PrimaryKey name, T& archivable) {
3939
const ArchiveDef& def = T::ArchiveDefinition;
4040
return UnarchiveInstance(def, name, archivable);
4141
}
@@ -60,12 +60,12 @@ class Archive {
6060
const Archivable& archivable);
6161

6262
bool UnarchiveInstance(const ArchiveDef& definition,
63-
Archivable::ArchiveName name,
63+
PrimaryKey name,
6464
Archivable& archivable);
6565

6666
size_t UnarchiveInstances(const ArchiveDef& definition,
6767
UnarchiveStep stepper,
68-
std::optional<int64_t> primary_key = std::nullopt);
68+
PrimaryKey primary_key = std::nullopt);
6969

7070
FML_DISALLOW_COPY_AND_ASSIGN(Archive);
7171
};

impeller/archivist/archive_location.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ namespace impeller {
1212
ArchiveLocation::ArchiveLocation(Archive& context,
1313
ArchiveStatement& statement,
1414
const ArchiveClassRegistration& registration,
15-
std::optional<int64_t> name)
15+
PrimaryKey name)
1616
: context_(context),
1717
statement_(statement),
1818
registration_(registration),
1919
name_(name),
2020
current_class_(registration.GetClassName()) {}
2121

22-
Archivable::ArchiveName ArchiveLocation::GetPrimaryKey() const {
23-
return name_.value_or(0u);
22+
PrimaryKey ArchiveLocation::GetPrimaryKey() const {
23+
return name_;
2424
}
2525

2626
bool ArchiveLocation::Write(ArchiveDef::Member member,
@@ -79,7 +79,7 @@ std::optional<int64_t> ArchiveLocation::WriteVectorKeys(
7979
return context_.ArchiveInstance(ArchiveVector::ArchiveDefinition, vector);
8080
}
8181

82-
bool ArchiveLocation::ReadVectorKeys(Archivable::ArchiveName name,
82+
bool ArchiveLocation::ReadVectorKeys(PrimaryKey name,
8383
std::vector<int64_t>& members) {
8484
ArchiveVector vector;
8585
if (!context_.UnarchiveInstance(ArchiveVector::ArchiveDefinition, name,

impeller/archivist/archive_location.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ArchiveStatement;
2020

2121
class ArchiveLocation {
2222
public:
23-
Archivable::ArchiveName GetPrimaryKey() const;
23+
PrimaryKey GetPrimaryKey() const;
2424

2525
template <class T, class = std::enable_if<std::is_integral<T>::value>>
2626
bool Write(ArchiveDef::Member member, T item) {
@@ -166,24 +166,23 @@ class ArchiveLocation {
166166
Archive& context_;
167167
ArchiveStatement& statement_;
168168
const ArchiveClassRegistration& registration_;
169-
std::optional<int64_t> name_;
169+
PrimaryKey name_;
170170
std::string current_class_;
171171

172172
friend class Archive;
173173

174174
ArchiveLocation(Archive& context,
175175
ArchiveStatement& statement,
176176
const ArchiveClassRegistration& registration,
177-
std::optional<int64_t> name);
177+
PrimaryKey name);
178178

179179
bool WriteIntegral(ArchiveDef::Member member, int64_t item);
180180

181181
bool ReadIntegral(ArchiveDef::Member member, int64_t& item);
182182

183183
std::optional<int64_t> WriteVectorKeys(std::vector<int64_t>&& members);
184184

185-
bool ReadVectorKeys(Archivable::ArchiveName name,
186-
std::vector<int64_t>& members);
185+
bool ReadVectorKeys(PrimaryKey name, std::vector<int64_t>& members);
187186

188187
bool Write(ArchiveDef::Member member,
189188
const ArchiveDef& otherDef,

impeller/archivist/archive_vector.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ ArchiveVector::ArchiveVector() {}
1717

1818
const ArchiveDef ArchiveVector::ArchiveDefinition = {
1919
/* .superClass = */ nullptr,
20-
/* .className = */ "Meta_Vector",
20+
/* .className = */ "_IPLR_meta_vector_items_",
2121
/* .autoAssignName = */ true,
2222
/* .members = */ {0},
2323
};
2424

25-
Archivable::ArchiveName ArchiveVector::GetArchivePrimaryKey() const {
26-
return 0;
25+
PrimaryKey ArchiveVector::GetPrimaryKey() const {
26+
// Archive definition says the keys will be auto assigned.
27+
return std::nullopt;
2728
}
2829

2930
const std::vector<int64_t> ArchiveVector::GetKeys() const {

impeller/archivist/archive_vector.h

Lines changed: 1 addition & 1 deletion
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 GetArchivePrimaryKey() const override;
16+
PrimaryKey GetPrimaryKey() const override;
1717

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

impeller/archivist/archivist_unittests.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace impeller {
1515
namespace testing {
1616

17-
static Archivable::ArchiveName LastSample = 0;
17+
static int64_t LastSample = 0;
1818

1919
class Sample : public Archivable {
2020
public:
@@ -23,7 +23,7 @@ class Sample : public Archivable {
2323
uint64_t GetSomeData() const { return some_data_; }
2424

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

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

4141
private:
4242
uint64_t some_data_;
43-
ArchiveName name_ = ++LastSample;
43+
PrimaryKey name_ = ++LastSample;
4444

4545
FML_DISALLOW_COPY_AND_ASSIGN(Sample);
4646
};
@@ -87,12 +87,12 @@ TEST_F(ArchiveTest, ReadData) {
8787

8888
size_t count = 50;
8989

90-
std::vector<Archivable::ArchiveName> keys;
90+
std::vector<PrimaryKey::value_type> keys;
9191
std::vector<uint64_t> values;
9292

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

111111
size_t count = 8;
112112

113-
std::vector<Archivable::ArchiveName> keys;
113+
std::vector<PrimaryKey::value_type> keys;
114114
std::vector<uint64_t> values;
115115

116116
keys.reserve(count);
117117
values.reserve(count);
118118

119119
for (size_t i = 0; i < count; i++) {
120120
Sample sample(i + 1);
121-
keys.push_back(sample.GetArchivePrimaryKey());
121+
keys.push_back(sample.GetPrimaryKey().value());
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.GetArchivePrimaryKey());
130+
ASSERT_EQ(keys[i], sample.GetPrimaryKey());
131131
}
132132
}
133133

0 commit comments

Comments
 (0)