Skip to content

Commit 0c595a9

Browse files
chinmaygardednfield
authored andcommitted
More use of optionals in archivist.
1 parent 4a6e2d0 commit 0c595a9

7 files changed

+71
-82
lines changed

impeller/archivist/archive.cc

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ bool Archive::IsValid() const {
2525
return database_->IsValid();
2626
}
2727

28-
bool Archive::ArchiveInstance(const ArchiveDef& definition,
29-
const Archivable& archivable,
30-
int64_t& lastInsertIDOut) {
28+
std::optional<int64_t /* row id */> Archive::ArchiveInstance(
29+
const ArchiveDef& definition,
30+
const Archivable& archivable) {
3131
if (!IsValid()) {
32-
return false;
32+
return std::nullopt;
3333
}
3434

3535
auto transaction = database_->CreateTransaction(transaction_count_);
@@ -38,7 +38,7 @@ bool Archive::ArchiveInstance(const ArchiveDef& definition,
3838
database_->GetRegistrationForDefinition(definition);
3939

4040
if (registration == nullptr) {
41-
return false;
41+
return std::nullopt;
4242
}
4343

4444
auto statement = registration->CreateInsertStatement();
@@ -47,7 +47,7 @@ bool Archive::ArchiveInstance(const ArchiveDef& definition,
4747
/*
4848
* Must be able to reset the statement for a new write
4949
*/
50-
return false;
50+
return std::nullopt;
5151
}
5252

5353
auto primary_key = archivable.GetArchivePrimaryKey();
@@ -66,32 +66,30 @@ bool Archive::ArchiveInstance(const ArchiveDef& definition,
6666
if (!definition.auto_key &&
6767
!statement.WriteValue(ArchiveClassRegistration::kPrimaryKeyIndex,
6868
primary_key)) {
69-
return false;
69+
return std::nullopt;
7070
}
7171

7272
if (!archivable.Write(item)) {
73-
return false;
73+
return std::nullopt;
7474
}
7575

7676
if (statement.Execute() != ArchiveStatement::Result::kDone) {
77-
return false;
77+
return std::nullopt;
7878
}
7979

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

8282
if (!definition.auto_key && lastInsert != static_cast<int64_t>(primary_key)) {
83-
return false;
83+
return std::nullopt;
8484
}
8585

86-
lastInsertIDOut = lastInsert;
87-
8886
/*
8987
* If any of the nested calls fail, we would have already checked for the
9088
* failure and returned.
9189
*/
9290
transaction.MarkWritesAsReadyForCommit();
9391

94-
return true;
92+
return lastInsert;
9593
}
9694

9795
bool Archive::UnarchiveInstance(const ArchiveDef& definition,
@@ -107,7 +105,7 @@ bool Archive::UnarchiveInstance(const ArchiveDef& definition,
107105

108106
size_t Archive::UnarchiveInstances(const ArchiveDef& definition,
109107
Archive::UnarchiveStep stepper,
110-
Archivable::ArchiveName name) {
108+
std::optional<int64_t> primary_key) {
111109
if (!IsValid()) {
112110
return 0;
113111
}
@@ -119,7 +117,7 @@ size_t Archive::UnarchiveInstances(const ArchiveDef& definition,
119117
return 0;
120118
}
121119

122-
const bool isQueryingSingle = name != ArchiveNameAuto;
120+
const bool isQueryingSingle = primary_key.has_value();
123121

124122
auto statement = registration->CreateQueryStatement(isQueryingSingle);
125123

@@ -129,11 +127,11 @@ size_t Archive::UnarchiveInstances(const ArchiveDef& definition,
129127

130128
if (isQueryingSingle) {
131129
/*
132-
* If a single statement is being queried for, bind the name as a statement
133-
* argument.
130+
* If a single statement is being queried for, bind the primary key as a
131+
* statement argument.
134132
*/
135133
if (!statement.WriteValue(ArchiveClassRegistration::kPrimaryKeyIndex,
136-
name)) {
134+
primary_key.value())) {
137135
return 0;
138136
}
139137
}
@@ -157,7 +155,7 @@ size_t Archive::UnarchiveInstances(const ArchiveDef& definition,
157155
/*
158156
* Prepare a fresh archive item for the given statement
159157
*/
160-
ArchiveLocation item(*this, statement, *registration, name);
158+
ArchiveLocation item(*this, statement, *registration, primary_key);
161159

162160
if (!stepper(item)) {
163161
break;

impeller/archivist/archive.h

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

77
#include <memory>
8+
#include <optional>
89
#include <string>
910
#include <type_traits>
1011
#include <vector>
@@ -17,8 +18,6 @@ namespace impeller {
1718
class ArchiveLocation;
1819
class ArchiveDatabase;
1920

20-
static const Archivable::ArchiveName ArchiveNameAuto = 0;
21-
2221
class Archive {
2322
public:
2423
Archive(const std::string& path);
@@ -31,8 +30,7 @@ class Archive {
3130
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
3231
bool Write(const T& archivable) {
3332
const ArchiveDef& def = T::ArchiveDefinition;
34-
int64_t unused_last = 0;
35-
return ArchiveInstance(def, archivable, unused_last);
33+
return ArchiveInstance(def, archivable).has_value();
3634
}
3735

3836
template <class T,
@@ -42,13 +40,13 @@ class Archive {
4240
return UnarchiveInstance(def, name, archivable);
4341
}
4442

45-
using UnarchiveStep = std::function<bool /*continue*/ (ArchiveLocation&)>;
43+
using UnarchiveStep = std::function<bool(ArchiveLocation&)>;
4644

4745
template <class T,
4846
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
4947
size_t Read(UnarchiveStep stepper) {
5048
const ArchiveDef& def = T::ArchiveDefinition;
51-
return UnarchiveInstances(def, stepper, ArchiveNameAuto);
49+
return UnarchiveInstances(def, stepper);
5250
}
5351

5452
private:
@@ -57,15 +55,17 @@ class Archive {
5755

5856
friend class ArchiveLocation;
5957

60-
bool ArchiveInstance(const ArchiveDef& definition,
61-
const Archivable& archivable,
62-
int64_t& lastInsertID);
58+
std::optional<int64_t /* row id */> ArchiveInstance(
59+
const ArchiveDef& definition,
60+
const Archivable& archivable);
61+
6362
bool UnarchiveInstance(const ArchiveDef& definition,
6463
Archivable::ArchiveName name,
6564
Archivable& archivable);
65+
6666
size_t UnarchiveInstances(const ArchiveDef& definition,
6767
UnarchiveStep stepper,
68-
Archivable::ArchiveName optionalName);
68+
std::optional<int64_t> primary_key = std::nullopt);
6969

7070
FML_DISALLOW_COPY_AND_ASSIGN(Archive);
7171
};

impeller/archivist/archive_class_registration.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,24 @@ bool ArchiveClassRegistration::IsValid() const {
4949
return is_ready_;
5050
}
5151

52-
ArchiveClassRegistration::ColumnResult ArchiveClassRegistration::FindColumn(
52+
std::optional<size_t> ArchiveClassRegistration::FindColumnIndex(
5353
const std::string& className,
5454
ArchiveDef::Member member) const {
5555
auto found = class_map_.find(className);
5656

5757
if (found == class_map_.end()) {
58-
return {0, false};
58+
return std::nullopt;
5959
}
6060

6161
const auto& memberToColumns = found->second;
6262

6363
auto foundColumn = memberToColumns.find(member);
6464

6565
if (foundColumn == memberToColumns.end()) {
66-
return {0, false};
66+
return std::nullopt;
6767
}
6868

69-
return {foundColumn->second, true};
69+
return foundColumn->second;
7070
}
7171

7272
bool ArchiveClassRegistration::CreateTable(bool autoIncrement) {

impeller/archivist/archive_class_registration.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
#include <map>
6+
#include <optional>
67

78
#include "flutter/fml/macros.h"
89
#include "impeller/archivist/archive.h"
@@ -16,9 +17,8 @@ class ArchiveClassRegistration {
1617

1718
bool IsValid() const;
1819

19-
using ColumnResult = std::pair<size_t, bool>;
20-
ColumnResult FindColumn(const std::string& className,
21-
ArchiveDef::Member member) const;
20+
std::optional<size_t> FindColumnIndex(const std::string& className,
21+
ArchiveDef::Member member) const;
2222

2323
const std::string& GetClassName() const;
2424

impeller/archivist/archive_location.cc

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

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

2626
bool ArchiveLocation::Write(ArchiveDef::Member member,
2727
const std::string& item) {
28-
auto found = registration_.FindColumn(current_class_, member);
29-
return found.second ? statement_.WriteValue(found.first, item) : false;
28+
auto index = registration_.FindColumnIndex(current_class_, member);
29+
return index.has_value() ? statement_.WriteValue(index.value(), item) : false;
3030
}
3131

3232
bool ArchiveLocation::WriteIntegral(ArchiveDef::Member member, int64_t item) {
33-
auto found = registration_.FindColumn(current_class_, member);
34-
return found.second ? statement_.WriteValue(found.first, item) : false;
33+
auto index = registration_.FindColumnIndex(current_class_, member);
34+
return index.has_value() ? statement_.WriteValue(index.value(), item) : false;
3535
}
3636

3737
bool ArchiveLocation::Write(ArchiveDef::Member member, double item) {
38-
auto found = registration_.FindColumn(current_class_, member);
39-
return found.second ? statement_.WriteValue(found.first, item) : false;
38+
auto index = registration_.FindColumnIndex(current_class_, member);
39+
return index.has_value() ? statement_.WriteValue(index.value(), item) : false;
4040
}
4141

4242
bool ArchiveLocation::Write(ArchiveDef::Member member, const Allocation& item) {
43-
auto found = registration_.FindColumn(current_class_, member);
44-
return found.second ? statement_.WriteValue(found.first, item) : false;
43+
auto index = registration_.FindColumnIndex(current_class_, member);
44+
return index.has_value() ? statement_.WriteValue(index.value(), item) : false;
4545
}
4646

4747
bool ArchiveLocation::Write(ArchiveDef::Member member,
4848
const ArchiveDef& otherDef,
4949
const Archivable& other) {
50-
auto found = registration_.FindColumn(current_class_, member);
50+
auto index = registration_.FindColumnIndex(current_class_, member);
5151

52-
if (!found.second) {
52+
if (!index.has_value()) {
5353
return false;
5454
}
5555

@@ -58,86 +58,76 @@ bool ArchiveLocation::Write(ArchiveDef::Member member,
5858
* have a name that is auto assigned. In that case, we cannot ask it before
5959
* archival (via `other.archiveName()`).
6060
*/
61-
int64_t lastInsert = 0;
62-
if (!context_.ArchiveInstance(otherDef, other, lastInsert)) {
61+
auto row_id = context_.ArchiveInstance(otherDef, other);
62+
if (!row_id.has_value()) {
6363
return false;
6464
}
6565

6666
/*
6767
* Bind the name of the serializable
6868
*/
69-
if (!statement_.WriteValue(found.first, lastInsert)) {
69+
if (!statement_.WriteValue(index.value(), row_id.value())) {
7070
return false;
7171
}
7272

7373
return true;
7474
}
7575

76-
std::pair<bool, int64_t> ArchiveLocation::WriteVectorKeys(
76+
std::optional<int64_t> ArchiveLocation::WriteVectorKeys(
7777
std::vector<int64_t>&& members) {
7878
ArchiveVector vector(std::move(members));
79-
int64_t vectorID = 0;
80-
if (!context_.ArchiveInstance(ArchiveVector::ArchiveDefinition, //
81-
vector, //
82-
vectorID)) {
83-
return {false, 0};
84-
}
85-
return {true, vectorID};
79+
return context_.ArchiveInstance(ArchiveVector::ArchiveDefinition, vector);
8680
}
8781

8882
bool ArchiveLocation::ReadVectorKeys(Archivable::ArchiveName name,
8983
std::vector<int64_t>& members) {
9084
ArchiveVector vector;
91-
9285
if (!context_.UnarchiveInstance(ArchiveVector::ArchiveDefinition, name,
9386
vector)) {
9487
return false;
9588
}
96-
9789
const auto& keys = vector.GetKeys();
98-
9990
std::move(keys.begin(), keys.end(), std::back_inserter(members));
100-
10191
return true;
10292
}
10393

10494
bool ArchiveLocation::Read(ArchiveDef::Member member, std::string& item) {
105-
auto found = registration_.FindColumn(current_class_, member);
106-
return found.second ? statement_.ReadValue(found.first, item) : false;
95+
auto index = registration_.FindColumnIndex(current_class_, member);
96+
return index.has_value() ? statement_.ReadValue(index.value(), item) : false;
10797
}
10898

10999
bool ArchiveLocation::ReadIntegral(ArchiveDef::Member member, int64_t& item) {
110-
auto found = registration_.FindColumn(current_class_, member);
111-
return found.second ? statement_.ReadValue(found.first, item) : false;
100+
auto index = registration_.FindColumnIndex(current_class_, member);
101+
return index.has_value() ? statement_.ReadValue(index.value(), item) : false;
112102
}
113103

114104
bool ArchiveLocation::Read(ArchiveDef::Member member, double& item) {
115-
auto found = registration_.FindColumn(current_class_, member);
116-
return found.second ? statement_.ReadValue(found.first, item) : false;
105+
auto index = registration_.FindColumnIndex(current_class_, member);
106+
return index.has_value() ? statement_.ReadValue(index.value(), item) : false;
117107
}
118108

119109
bool ArchiveLocation::Read(ArchiveDef::Member member, Allocation& item) {
120-
auto found = registration_.FindColumn(current_class_, member);
121-
return found.second ? statement_.ReadValue(found.first, item) : false;
110+
auto index = registration_.FindColumnIndex(current_class_, member);
111+
return index.has_value() ? statement_.ReadValue(index.value(), item) : false;
122112
}
123113

124114
bool ArchiveLocation::Read(ArchiveDef::Member member,
125115
const ArchiveDef& otherDef,
126116
Archivable& other) {
127-
auto found = registration_.FindColumn(current_class_, member);
117+
auto index = registration_.FindColumnIndex(current_class_, member);
128118

129119
/*
130120
* Make sure a member is present at that column
131121
*/
132-
if (!found.second) {
122+
if (!index.has_value()) {
133123
return false;
134124
}
135125

136126
/*
137127
* Try to find the foreign key in the current items row
138128
*/
139129
int64_t foreignKey = 0;
140-
if (!statement_.ReadValue(found.first, foreignKey)) {
130+
if (!statement_.ReadValue(index.value(), foreignKey)) {
141131
return false;
142132
}
143133

0 commit comments

Comments
 (0)