Skip to content

Commit

Permalink
Separate archive_location into its own TU.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent 8d7d8e6 commit 89589cc
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 232 deletions.
2 changes: 2 additions & 0 deletions impeller/archivist/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ impeller_component("archivist") {
"archive_class_registration.h",
"archive_database.cc",
"archive_database.h",
"archive_location.cc",
"archive_location.h",
"archive_statement.cc",
"archive_statement.h",
"archive_transaction.cc",
Expand Down
12 changes: 12 additions & 0 deletions impeller/archivist/archivable.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@
#pragma once

#include <cstdint>
#include <string>
#include <vector>

namespace impeller {

struct ArchiveDef {
using Member = uint64_t;
using Members = std::vector<Member>;

const ArchiveDef* isa = nullptr;
const std::string table_name;
const bool auto_key = true;
const Members members;
};

class ArchiveLocation;

//------------------------------------------------------------------------------
Expand Down
17 changes: 9 additions & 8 deletions impeller/archivist/archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "flutter/fml/logging.h"
#include "impeller/archivist/archive_class_registration.h"
#include "impeller/archivist/archive_database.h"
#include "impeller/archivist/archive_location.h"
#include "impeller/archivist/archive_statement.h"
#include "impeller/archivist/archive_vector.h"

Expand All @@ -22,14 +23,14 @@ Archive::~Archive() {
<< "There must be no pending transactions";
}

bool Archive::IsReady() const {
return database_->IsReady();
bool Archive::IsValid() const {
return database_->IsValid();
}

bool Archive::ArchiveInstance(const ArchiveDef& definition,
const Archivable& archivable,
int64_t& lastInsertIDOut) {
if (!IsReady()) {
if (!IsValid()) {
return false;
}

Expand All @@ -44,7 +45,7 @@ bool Archive::ArchiveInstance(const ArchiveDef& definition,

auto statement = registration->GetInsertStatement();

if (!statement.IsReady() || !statement.Reset()) {
if (!statement.IsValid() || !statement.Reset()) {
/*
* Must be able to reset the statement for a new write
*/
Expand Down Expand Up @@ -73,7 +74,7 @@ bool Archive::ArchiveInstance(const ArchiveDef& definition,
return false;
}

if (statement.Run() != ArchiveStatement::Result::kDone) {
if (statement.Execute() != ArchiveStatement::Result::kDone) {
return false;
}

Expand Down Expand Up @@ -108,7 +109,7 @@ bool Archive::UnarchiveInstance(const ArchiveDef& definition,
size_t Archive::UnarchiveInstances(const ArchiveDef& definition,
Archive::UnarchiveStep stepper,
Archivable::ArchiveName name) {
if (!IsReady()) {
if (!IsValid()) {
return 0;
}

Expand All @@ -123,7 +124,7 @@ size_t Archive::UnarchiveInstances(const ArchiveDef& definition,

auto statement = registration->GetQueryStatement(isQueryingSingle);

if (!statement.IsReady() || !statement.Reset()) {
if (!statement.IsValid() || !statement.Reset()) {
return 0;
}

Expand All @@ -150,7 +151,7 @@ size_t Archive::UnarchiveInstances(const ArchiveDef& definition,

size_t itemsRead = 0;

while (statement.Run() == ArchiveStatement::Result::kRow) {
while (statement.Execute() == ArchiveStatement::Result::kRow) {
itemsRead++;

/*
Expand Down
193 changes: 1 addition & 192 deletions impeller/archivist/archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,11 @@

#include "flutter/fml/macros.h"
#include "impeller/archivist/archivable.h"
#include "impeller/base/allocation.h"

namespace impeller {

class ArchiveLocation;
class ArchiveClassRegistration;
class ArchiveDatabase;
class ArchiveStatement;

struct ArchiveDef {
using Member = uint64_t;
using Members = std::vector<Member>;

const ArchiveDef* isa = nullptr;
const std::string table_name;
const bool auto_key = true;
const Members members;
};

static const Archivable::ArchiveName ArchiveNameAuto = 0;

Expand All @@ -38,7 +25,7 @@ class Archive {

~Archive();

bool IsReady() const;
bool IsValid() const;

template <class T,
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
Expand Down Expand Up @@ -83,182 +70,4 @@ class Archive {
FML_DISALLOW_COPY_AND_ASSIGN(Archive);
};

class ArchiveLocation {
public:
template <class T, class = std::enable_if<std::is_integral<T>::value>>
bool Write(ArchiveDef::Member member, T item) {
return WriteIntegral(member, static_cast<int64_t>(item));
}

bool Write(ArchiveDef::Member member, double item);

bool Write(ArchiveDef::Member member, const std::string& item);

bool Write(ArchiveDef::Member member, const Allocation& allocation);

template <class T,
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
bool WriteArchivable(ArchiveDef::Member member, const T& other) {
const ArchiveDef& otherDef = T::ArchiveDefinition;
return Write(member, otherDef, other);
}

template <class T, class = std::enable_if<std::is_enum<T>::value>>
bool WriteEnum(ArchiveDef::Member member, const T& item) {
return WriteIntegral(member, static_cast<int64_t>(item));
}

template <class T,
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
bool Write(ArchiveDef::Member member, const std::vector<T>& items) {
/*
* All items in the vector are individually encoded and their keys noted
*/
std::vector<int64_t> members;
members.reserve(items.size());

const ArchiveDef& itemDefinition = T::ArchiveDefinition;
for (const auto& item : items) {
int64_t added = 0;
bool result = context_.ArchiveInstance(itemDefinition, item, added);
if (!result) {
return false;
}
members.emplace_back(added);
}

/*
* The keys are flattened into the vectors table. Write to that table
*/
auto vectorInsert = WriteVectorKeys(std::move(members));

if (!vectorInsert.first) {
return false;
}

return WriteIntegral(member, vectorInsert.second);
}

template <class Super,
class Current,
class = std::enable_if<std::is_base_of<Archivable, Super>::value &&
std::is_base_of<Archivable, Current>::value>>
bool WriteSuper(const Current& thiz) {
std::string oldClass = current_class_;
current_class_ = Super::ArchiveDefinition.className;
auto success = thiz.Super::serialize(*this);
current_class_ = oldClass;
return success;
}

template <class T, class = std::enable_if<std::is_integral<T>::value>>
bool Read(ArchiveDef::Member member, T& item) {
int64_t decoded = 0;
auto result = ReadIntegral(member, decoded);
item = static_cast<T>(decoded);
return result;
}

bool Read(ArchiveDef::Member member, double& item);

bool Read(ArchiveDef::Member member, std::string& item);

bool Read(ArchiveDef::Member member, Allocation& allocation);

template <class T,
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
bool ReadArchivable(ArchiveDef::Member member, T& other) {
const ArchiveDef& otherDef = T::ArchiveDefinition;
return decode(member, otherDef, other);
}

template <class T, class = std::enable_if<std::is_enum<T>::value>>
bool ReadEnum(ArchiveDef::Member member, T& item) {
int64_t desugared = 0;
if (ReadIntegral(member, desugared)) {
item = static_cast<T>(desugared);
return true;
}
return false;
}

template <class T,
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
bool Read(ArchiveDef::Member member, std::vector<T>& items) {
/*
* From the member, find the foreign key of the vector
*/
int64_t vectorForeignKey = 0;
if (!ReadIntegral(member, vectorForeignKey)) {
return false;
}

/*
* Get vector keys
*/
std::vector<int64_t> keys;
if (!ReadVectorKeys(vectorForeignKey, keys)) {
return false;
}

const ArchiveDef& otherDef = T::ArchiveDefinition;
for (const auto& key : keys) {
items.emplace_back();

if (!context_.UnarchiveInstance(otherDef, key, items.back())) {
return false;
}
}

return true;
}

template <class Super,
class Current,
class = std::enable_if<std::is_base_of<Archivable, Super>::value &&
std::is_base_of<Archivable, Current>::value>>
bool ReadSuper(Current& thiz) {
std::string oldClass = current_class_;
current_class_ = Super::ArchiveDefinition.className;
auto success = thiz.Super::deserialize(*this);
current_class_ = oldClass;
return success;
}

Archivable::ArchiveName Name() const;

private:
Archive& context_;
ArchiveStatement& statement_;
const ArchiveClassRegistration& registration_;
Archivable::ArchiveName name_;
std::string current_class_;

friend class Archive;

ArchiveLocation(Archive& context,
ArchiveStatement& statement,
const ArchiveClassRegistration& registration,
Archivable::ArchiveName name);

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

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

std::pair<bool, int64_t> WriteVectorKeys(std::vector<int64_t>&& members);

bool ReadVectorKeys(Archivable::ArchiveName name,
std::vector<int64_t>& members);

bool Write(ArchiveDef::Member member,
const ArchiveDef& otherDef,
const Archivable& other);

bool Read(ArchiveDef::Member member,
const ArchiveDef& otherDef,
Archivable& other);

FML_DISALLOW_COPY_AND_ASSIGN(ArchiveLocation);
};

} // namespace impeller
6 changes: 3 additions & 3 deletions impeller/archivist/archive_class_registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ size_t ArchiveClassRegistration::GetMemberCount() const {
return member_count_;
}

bool ArchiveClassRegistration::IsReady() const {
bool ArchiveClassRegistration::IsValid() const {
return is_ready_;
}

Expand Down Expand Up @@ -98,15 +98,15 @@ bool ArchiveClassRegistration::CreateTable(bool autoIncrement) {

auto statement = database_.CreateStatement(stream.str());

if (!statement.IsReady()) {
if (!statement.IsValid()) {
return false;
}

if (!statement.Reset()) {
return false;
}

return statement.Run() == ArchiveStatement::Result::kDone;
return statement.Execute() == ArchiveStatement::Result::kDone;
}

ArchiveStatement ArchiveClassRegistration::GetQueryStatement(
Expand Down
3 changes: 2 additions & 1 deletion impeller/archivist/archive_class_registration.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "flutter/fml/macros.h"
#include "impeller/archivist/archive.h"
#include "impeller/archivist/archive_statement.h"

namespace impeller {

Expand All @@ -19,7 +20,7 @@ class ArchiveClassRegistration {

size_t GetMemberCount() const;

bool IsReady() const;
bool IsValid() const;

ArchiveStatement GetInsertStatement() const;

Expand Down
Loading

0 comments on commit 89589cc

Please sign in to comment.