Update translation units for style guide.
This commit is contained in:
@@ -11,7 +11,10 @@ impeller_component("archivist") {
|
||||
public = [ "archive.h" ]
|
||||
|
||||
sources = [
|
||||
"archivable.cc",
|
||||
"archivable.h",
|
||||
"archive.cc",
|
||||
"archive.h",
|
||||
"archive_class_registration.cc",
|
||||
"archive_class_registration.h",
|
||||
"archive_database.cc",
|
||||
|
||||
11
engine/src/flutter/impeller/archivist/archivable.cc
Normal file
11
engine/src/flutter/impeller/archivist/archivable.cc
Normal file
@@ -0,0 +1,11 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "impeller/archivist/archivable.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
//
|
||||
|
||||
} // namespace impeller
|
||||
28
engine/src/flutter/impeller/archivist/archivable.h
Normal file
28
engine/src/flutter/impeller/archivist/archivable.h
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class ArchiveLocation;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// @brief Instances of `Archivable`s can be read from and written to a
|
||||
/// persistent archive.
|
||||
///
|
||||
class Archivable {
|
||||
public:
|
||||
using ArchiveName = uint64_t;
|
||||
|
||||
virtual ArchiveName GetArchiveName() const = 0;
|
||||
|
||||
virtual bool Write(ArchiveLocation& item) const = 0;
|
||||
|
||||
virtual bool Read(ArchiveLocation& item) = 0;
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
@@ -15,41 +15,43 @@
|
||||
namespace impeller {
|
||||
|
||||
Archive::Archive(const std::string& path, bool recreate)
|
||||
: _db(std::make_unique<ArchiveDatabase>(path, recreate)) {}
|
||||
: database_(std::make_unique<ArchiveDatabase>(path, recreate)) {}
|
||||
|
||||
Archive::~Archive() {
|
||||
FML_DCHECK(_transactionCount == 0) << "There must be no pending transactions";
|
||||
FML_DCHECK(transaction_count_ == 0)
|
||||
<< "There must be no pending transactions";
|
||||
}
|
||||
|
||||
bool Archive::isReady() const {
|
||||
return _db->isReady();
|
||||
bool Archive::IsReady() const {
|
||||
return database_->IsReady();
|
||||
}
|
||||
|
||||
bool Archive::archiveInstance(const ArchiveDef& definition,
|
||||
const ArchiveSerializable& archivable,
|
||||
bool Archive::ArchiveInstance(const ArchiveDef& definition,
|
||||
const Archivable& archivable,
|
||||
int64_t& lastInsertIDOut) {
|
||||
if (!isReady()) {
|
||||
if (!IsReady()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto transaction = _db->acquireTransaction(_transactionCount);
|
||||
auto transaction = database_->CreateTransaction(transaction_count_);
|
||||
|
||||
const auto* registration = _db->registrationForDefinition(definition);
|
||||
const auto* registration =
|
||||
database_->GetRegistrationForDefinition(definition);
|
||||
|
||||
if (registration == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto statement = registration->insertStatement();
|
||||
auto statement = registration->GetInsertStatement();
|
||||
|
||||
if (!statement.isReady() || !statement.reset()) {
|
||||
if (!statement.IsReady() || !statement.Reset()) {
|
||||
/*
|
||||
* Must be able to reset the statement for a new write
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
|
||||
auto itemName = archivable.archiveName();
|
||||
auto itemName = archivable.GetArchiveName();
|
||||
|
||||
/*
|
||||
* The lifecycle of the archive item is tied to this scope and there is no
|
||||
@@ -57,25 +59,25 @@ bool Archive::archiveInstance(const ArchiveDef& definition,
|
||||
* for its members to be references. It does not manage the lifetimes of
|
||||
* anything.
|
||||
*/
|
||||
ArchiveItem item(*this, statement, *registration, itemName);
|
||||
ArchiveLocation item(*this, statement, *registration, itemName);
|
||||
|
||||
/*
|
||||
* We need to bind the primary key only if the item does not provide its own
|
||||
*/
|
||||
if (!definition.autoAssignName &&
|
||||
!statement.bind(ArchiveClassRegistration::NameIndex, itemName)) {
|
||||
!statement.WriteValue(ArchiveClassRegistration::NameIndex, itemName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!archivable.serialize(item)) {
|
||||
if (!archivable.Write(item)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (statement.run() != ArchiveStatement::Result::Done) {
|
||||
if (statement.Run() != ArchiveStatement::Result::kDone) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t lastInsert = _db->lastInsertRowID();
|
||||
int64_t lastInsert = database_->GetLastInsertRowID();
|
||||
|
||||
if (!definition.autoAssignName &&
|
||||
lastInsert != static_cast<int64_t>(itemName)) {
|
||||
@@ -88,30 +90,31 @@ bool Archive::archiveInstance(const ArchiveDef& definition,
|
||||
* If any of the nested calls fail, we would have already checked for the
|
||||
* failure and returned.
|
||||
*/
|
||||
transaction.markWritesSuccessful();
|
||||
transaction.MarkWritesAsReadyForCommit();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Archive::unarchiveInstance(const ArchiveDef& definition,
|
||||
ArchiveSerializable::ArchiveName name,
|
||||
ArchiveSerializable& archivable) {
|
||||
UnarchiveStep stepper = [&archivable](ArchiveItem& item) {
|
||||
archivable.deserialize(item);
|
||||
bool Archive::UnarchiveInstance(const ArchiveDef& definition,
|
||||
Archivable::ArchiveName name,
|
||||
Archivable& archivable) {
|
||||
UnarchiveStep stepper = [&archivable](ArchiveLocation& item) {
|
||||
archivable.Read(item);
|
||||
return false /* no-more after single read */;
|
||||
};
|
||||
|
||||
return unarchiveInstances(definition, stepper, name) == 1;
|
||||
return UnarchiveInstances(definition, stepper, name) == 1;
|
||||
}
|
||||
|
||||
size_t Archive::unarchiveInstances(const ArchiveDef& definition,
|
||||
size_t Archive::UnarchiveInstances(const ArchiveDef& definition,
|
||||
Archive::UnarchiveStep stepper,
|
||||
ArchiveSerializable::ArchiveName name) {
|
||||
if (!isReady()) {
|
||||
Archivable::ArchiveName name) {
|
||||
if (!IsReady()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const auto* registration = _db->registrationForDefinition(definition);
|
||||
const auto* registration =
|
||||
database_->GetRegistrationForDefinition(definition);
|
||||
|
||||
if (registration == nullptr) {
|
||||
return 0;
|
||||
@@ -119,9 +122,9 @@ size_t Archive::unarchiveInstances(const ArchiveDef& definition,
|
||||
|
||||
const bool isQueryingSingle = name != ArchiveNameAuto;
|
||||
|
||||
auto statement = registration->queryStatement(isQueryingSingle);
|
||||
auto statement = registration->GetQueryStatement(isQueryingSingle);
|
||||
|
||||
if (!statement.isReady() || !statement.reset()) {
|
||||
if (!statement.IsReady() || !statement.Reset()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -130,13 +133,13 @@ size_t Archive::unarchiveInstances(const ArchiveDef& definition,
|
||||
* If a single statement is being queried for, bind the name as a statement
|
||||
* argument.
|
||||
*/
|
||||
if (!statement.bind(ArchiveClassRegistration::NameIndex, name)) {
|
||||
if (!statement.WriteValue(ArchiveClassRegistration::NameIndex, name)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (statement.columnCount() !=
|
||||
registration->memberCount() + 1 /* primary key */) {
|
||||
if (statement.GetColumnCount() !=
|
||||
registration->GetMemberCount() + 1 /* primary key */) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -144,17 +147,17 @@ size_t Archive::unarchiveInstances(const ArchiveDef& definition,
|
||||
* Acquire a transaction but never mark it successful since we will never
|
||||
* be committing any writes to the database during unarchiving.
|
||||
*/
|
||||
auto transaction = _db->acquireTransaction(_transactionCount);
|
||||
auto transaction = database_->CreateTransaction(transaction_count_);
|
||||
|
||||
size_t itemsRead = 0;
|
||||
|
||||
while (statement.run() == ArchiveStatement::Result::Row) {
|
||||
while (statement.Run() == ArchiveStatement::Result::kRow) {
|
||||
itemsRead++;
|
||||
|
||||
/*
|
||||
* Prepare a fresh archive item for the given statement
|
||||
*/
|
||||
ArchiveItem item(*this, statement, *registration, name);
|
||||
ArchiveLocation item(*this, statement, *registration, name);
|
||||
|
||||
if (!stepper(item)) {
|
||||
break;
|
||||
@@ -168,47 +171,45 @@ size_t Archive::unarchiveInstances(const ArchiveDef& definition,
|
||||
return itemsRead;
|
||||
}
|
||||
|
||||
ArchiveItem::ArchiveItem(Archive& context,
|
||||
ArchiveStatement& statement,
|
||||
const ArchiveClassRegistration& registration,
|
||||
ArchiveSerializable::ArchiveName name)
|
||||
: _context(context),
|
||||
_statement(statement),
|
||||
_registration(registration),
|
||||
_name(name),
|
||||
_currentClass(registration.className()) {}
|
||||
ArchiveLocation::ArchiveLocation(Archive& context,
|
||||
ArchiveStatement& statement,
|
||||
const ArchiveClassRegistration& registration,
|
||||
Archivable::ArchiveName name)
|
||||
: context_(context),
|
||||
statement_(statement),
|
||||
registration_(registration),
|
||||
name_(name),
|
||||
current_class_(registration.GetClassName()) {}
|
||||
|
||||
ArchiveSerializable::ArchiveName ArchiveItem::name() const {
|
||||
return _name;
|
||||
Archivable::ArchiveName ArchiveLocation::Name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
bool ArchiveItem::encode(ArchiveSerializable::Member member,
|
||||
const std::string& item) {
|
||||
auto found = _registration.findColumn(_currentClass, member);
|
||||
return found.second ? _statement.bind(found.first, item) : false;
|
||||
bool ArchiveLocation::Write(ArchiveDef::Member member,
|
||||
const std::string& item) {
|
||||
auto found = registration_.FindColumn(current_class_, member);
|
||||
return found.second ? statement_.WriteValue(found.first, item) : false;
|
||||
}
|
||||
|
||||
bool ArchiveItem::encodeIntegral(ArchiveSerializable::Member member,
|
||||
int64_t item) {
|
||||
auto found = _registration.findColumn(_currentClass, member);
|
||||
return found.second ? _statement.bind(found.first, item) : false;
|
||||
bool ArchiveLocation::WriteIntegral(ArchiveDef::Member member, int64_t item) {
|
||||
auto found = registration_.FindColumn(current_class_, member);
|
||||
return found.second ? statement_.WriteValue(found.first, item) : false;
|
||||
}
|
||||
|
||||
bool ArchiveItem::encode(ArchiveSerializable::Member member, double item) {
|
||||
auto found = _registration.findColumn(_currentClass, member);
|
||||
return found.second ? _statement.bind(found.first, item) : false;
|
||||
bool ArchiveLocation::Write(ArchiveDef::Member member, double item) {
|
||||
auto found = registration_.FindColumn(current_class_, member);
|
||||
return found.second ? statement_.WriteValue(found.first, item) : false;
|
||||
}
|
||||
|
||||
bool ArchiveItem::encode(ArchiveSerializable::Member member,
|
||||
const Allocation& item) {
|
||||
auto found = _registration.findColumn(_currentClass, member);
|
||||
return found.second ? _statement.bind(found.first, item) : false;
|
||||
bool ArchiveLocation::Write(ArchiveDef::Member member, const Allocation& item) {
|
||||
auto found = registration_.FindColumn(current_class_, member);
|
||||
return found.second ? statement_.WriteValue(found.first, item) : false;
|
||||
}
|
||||
|
||||
bool ArchiveItem::encode(ArchiveSerializable::Member member,
|
||||
const ArchiveDef& otherDef,
|
||||
const ArchiveSerializable& other) {
|
||||
auto found = _registration.findColumn(_currentClass, member);
|
||||
bool ArchiveLocation::Write(ArchiveDef::Member member,
|
||||
const ArchiveDef& otherDef,
|
||||
const Archivable& other) {
|
||||
auto found = registration_.FindColumn(current_class_, member);
|
||||
|
||||
if (!found.second) {
|
||||
return false;
|
||||
@@ -220,25 +221,25 @@ bool ArchiveItem::encode(ArchiveSerializable::Member member,
|
||||
* archival (via `other.archiveName()`).
|
||||
*/
|
||||
int64_t lastInsert = 0;
|
||||
if (!_context.archiveInstance(otherDef, other, lastInsert)) {
|
||||
if (!context_.ArchiveInstance(otherDef, other, lastInsert)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Bind the name of the serialiable
|
||||
*/
|
||||
if (!_statement.bind(found.first, lastInsert)) {
|
||||
if (!statement_.WriteValue(found.first, lastInsert)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::pair<bool, int64_t> ArchiveItem::encodeVectorKeys(
|
||||
std::pair<bool, int64_t> ArchiveLocation::WriteVectorKeys(
|
||||
std::vector<int64_t>&& members) {
|
||||
ArchiveVector vector(std::move(members));
|
||||
int64_t vectorID = 0;
|
||||
if (!_context.archiveInstance(ArchiveVector::ArchiveDefinition, //
|
||||
if (!context_.ArchiveInstance(ArchiveVector::ArchiveDefinition, //
|
||||
vector, //
|
||||
vectorID)) {
|
||||
return {false, 0};
|
||||
@@ -246,11 +247,11 @@ std::pair<bool, int64_t> ArchiveItem::encodeVectorKeys(
|
||||
return {true, vectorID};
|
||||
}
|
||||
|
||||
bool ArchiveItem::decodeVectorKeys(ArchiveSerializable::ArchiveName name,
|
||||
std::vector<int64_t>& members) {
|
||||
bool ArchiveLocation::ReadVectorKeys(Archivable::ArchiveName name,
|
||||
std::vector<int64_t>& members) {
|
||||
ArchiveVector vector;
|
||||
|
||||
if (!_context.unarchiveInstance(ArchiveVector::ArchiveDefinition, name,
|
||||
if (!context_.UnarchiveInstance(ArchiveVector::ArchiveDefinition, name,
|
||||
vector)) {
|
||||
return false;
|
||||
}
|
||||
@@ -262,32 +263,30 @@ bool ArchiveItem::decodeVectorKeys(ArchiveSerializable::ArchiveName name,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ArchiveItem::decode(ArchiveSerializable::Member member,
|
||||
std::string& item) {
|
||||
auto found = _registration.findColumn(_currentClass, member);
|
||||
return found.second ? _statement.column(found.first, item) : false;
|
||||
bool ArchiveLocation::Read(ArchiveDef::Member member, std::string& item) {
|
||||
auto found = registration_.FindColumn(current_class_, member);
|
||||
return found.second ? statement_.ReadValue(found.first, item) : false;
|
||||
}
|
||||
|
||||
bool ArchiveItem::decodeIntegral(ArchiveSerializable::Member member,
|
||||
int64_t& item) {
|
||||
auto found = _registration.findColumn(_currentClass, member);
|
||||
return found.second ? _statement.column(found.first, item) : false;
|
||||
bool ArchiveLocation::ReadIntegral(ArchiveDef::Member member, int64_t& item) {
|
||||
auto found = registration_.FindColumn(current_class_, member);
|
||||
return found.second ? statement_.ReadValue(found.first, item) : false;
|
||||
}
|
||||
|
||||
bool ArchiveItem::decode(ArchiveSerializable::Member member, double& item) {
|
||||
auto found = _registration.findColumn(_currentClass, member);
|
||||
return found.second ? _statement.column(found.first, item) : false;
|
||||
bool ArchiveLocation::Read(ArchiveDef::Member member, double& item) {
|
||||
auto found = registration_.FindColumn(current_class_, member);
|
||||
return found.second ? statement_.ReadValue(found.first, item) : false;
|
||||
}
|
||||
|
||||
bool ArchiveItem::decode(ArchiveSerializable::Member member, Allocation& item) {
|
||||
auto found = _registration.findColumn(_currentClass, member);
|
||||
return found.second ? _statement.column(found.first, item) : false;
|
||||
bool ArchiveLocation::Read(ArchiveDef::Member member, Allocation& item) {
|
||||
auto found = registration_.FindColumn(current_class_, member);
|
||||
return found.second ? statement_.ReadValue(found.first, item) : false;
|
||||
}
|
||||
|
||||
bool ArchiveItem::decode(ArchiveSerializable::Member member,
|
||||
const ArchiveDef& otherDef,
|
||||
ArchiveSerializable& other) {
|
||||
auto found = _registration.findColumn(_currentClass, member);
|
||||
bool ArchiveLocation::Read(ArchiveDef::Member member,
|
||||
const ArchiveDef& otherDef,
|
||||
Archivable& other) {
|
||||
auto found = registration_.FindColumn(current_class_, member);
|
||||
|
||||
/*
|
||||
* Make sure a member is present at that column
|
||||
@@ -300,14 +299,14 @@ bool ArchiveItem::decode(ArchiveSerializable::Member member,
|
||||
* Try to find the foreign key in the current items row
|
||||
*/
|
||||
int64_t foreignKey = 0;
|
||||
if (!_statement.column(found.first, foreignKey)) {
|
||||
if (!statement_.ReadValue(found.first, foreignKey)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find the other item and unarchive by this foreign key
|
||||
*/
|
||||
if (!_context.unarchiveInstance(otherDef, foreignKey, other)) {
|
||||
if (!context_.UnarchiveInstance(otherDef, foreignKey, other)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,36 +10,27 @@
|
||||
#include <vector>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/archivist/archivable.h"
|
||||
#include "impeller/base/allocation.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class ArchiveItem;
|
||||
class ArchiveLocation;
|
||||
class ArchiveClassRegistration;
|
||||
class ArchiveDatabase;
|
||||
class ArchiveStatement;
|
||||
|
||||
class ArchiveSerializable {
|
||||
public:
|
||||
struct ArchiveDef {
|
||||
using Member = uint64_t;
|
||||
using Members = std::vector<Member>;
|
||||
using ArchiveName = uint64_t;
|
||||
|
||||
virtual ArchiveName archiveName() const = 0;
|
||||
|
||||
virtual bool serialize(ArchiveItem& item) const = 0;
|
||||
|
||||
virtual bool deserialize(ArchiveItem& item) = 0;
|
||||
};
|
||||
|
||||
struct ArchiveDef {
|
||||
const ArchiveDef* superClass;
|
||||
const std::string className;
|
||||
const bool autoAssignName;
|
||||
const ArchiveSerializable::Members members;
|
||||
const Members members;
|
||||
};
|
||||
|
||||
static const ArchiveSerializable::ArchiveName ArchiveNameAuto = 0;
|
||||
static const Archivable::ArchiveName ArchiveNameAuto = 0;
|
||||
|
||||
class Archive {
|
||||
public:
|
||||
@@ -47,84 +38,79 @@ class Archive {
|
||||
|
||||
~Archive();
|
||||
|
||||
bool isReady() const;
|
||||
bool IsReady() const;
|
||||
|
||||
template <
|
||||
class T,
|
||||
class = std::enable_if<std::is_base_of<ArchiveSerializable, T>::value>>
|
||||
bool archive(const T& archivable) {
|
||||
template <class T,
|
||||
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
|
||||
bool Write(const T& archivable) {
|
||||
const ArchiveDef& def = T::ArchiveDefinition;
|
||||
int64_t unusedLast = 0;
|
||||
return archiveInstance(def, archivable, unusedLast);
|
||||
int64_t unused_last = 0;
|
||||
return ArchiveInstance(def, archivable, unused_last);
|
||||
}
|
||||
|
||||
template <
|
||||
class T,
|
||||
class = std::enable_if<std::is_base_of<ArchiveSerializable, T>::value>>
|
||||
bool unarchive(ArchiveSerializable::ArchiveName name, T& archivable) {
|
||||
template <class T,
|
||||
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
|
||||
bool Read(Archivable::ArchiveName name, T& archivable) {
|
||||
const ArchiveDef& def = T::ArchiveDefinition;
|
||||
return unarchiveInstance(def, name, archivable);
|
||||
return UnarchiveInstance(def, name, archivable);
|
||||
}
|
||||
|
||||
using UnarchiveStep = std::function<bool /*continue*/ (ArchiveItem&)>;
|
||||
using UnarchiveStep = std::function<bool /*continue*/ (ArchiveLocation&)>;
|
||||
|
||||
template <
|
||||
class T,
|
||||
class = std::enable_if<std::is_base_of<ArchiveSerializable, T>::value>>
|
||||
size_t unarchive(UnarchiveStep stepper) {
|
||||
template <class T,
|
||||
class = std::enable_if<std::is_base_of<Archivable, T>::value>>
|
||||
size_t Read(UnarchiveStep stepper) {
|
||||
const ArchiveDef& def = T::ArchiveDefinition;
|
||||
return unarchiveInstances(def, stepper, ArchiveNameAuto);
|
||||
return UnarchiveInstances(def, stepper, ArchiveNameAuto);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<ArchiveDatabase> _db;
|
||||
int64_t _transactionCount = 0;
|
||||
std::unique_ptr<ArchiveDatabase> database_;
|
||||
int64_t transaction_count_ = 0;
|
||||
|
||||
friend class ArchiveItem;
|
||||
friend class ArchiveLocation;
|
||||
|
||||
bool archiveInstance(const ArchiveDef& definition,
|
||||
const ArchiveSerializable& archivable,
|
||||
bool ArchiveInstance(const ArchiveDef& definition,
|
||||
const Archivable& archivable,
|
||||
int64_t& lastInsertID);
|
||||
bool unarchiveInstance(const ArchiveDef& definition,
|
||||
ArchiveSerializable::ArchiveName name,
|
||||
ArchiveSerializable& archivable);
|
||||
size_t unarchiveInstances(const ArchiveDef& definition,
|
||||
bool UnarchiveInstance(const ArchiveDef& definition,
|
||||
Archivable::ArchiveName name,
|
||||
Archivable& archivable);
|
||||
size_t UnarchiveInstances(const ArchiveDef& definition,
|
||||
UnarchiveStep stepper,
|
||||
ArchiveSerializable::ArchiveName optionalName);
|
||||
Archivable::ArchiveName optionalName);
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Archive);
|
||||
};
|
||||
|
||||
class ArchiveItem {
|
||||
class ArchiveLocation {
|
||||
public:
|
||||
template <class T, class = std::enable_if<std::is_integral<T>::value>>
|
||||
bool encode(ArchiveSerializable::Member member, T item) {
|
||||
return encodeIntegral(member, static_cast<int64_t>(item));
|
||||
bool Write(ArchiveDef::Member member, T item) {
|
||||
return WriteIntegral(member, static_cast<int64_t>(item));
|
||||
}
|
||||
|
||||
bool encode(ArchiveSerializable::Member member, double item);
|
||||
bool Write(ArchiveDef::Member member, double item);
|
||||
|
||||
bool encode(ArchiveSerializable::Member member, const std::string& item);
|
||||
bool Write(ArchiveDef::Member member, const std::string& item);
|
||||
|
||||
bool encode(ArchiveSerializable::Member member, const Allocation& allocation);
|
||||
bool Write(ArchiveDef::Member member, const Allocation& allocation);
|
||||
|
||||
template <
|
||||
class T,
|
||||
class = std::enable_if<std::is_base_of<ArchiveSerializable, T>::value>>
|
||||
bool encodeArchivable(ArchiveSerializable::Member member, const T& other) {
|
||||
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 encode(member, otherDef, other);
|
||||
return Write(member, otherDef, other);
|
||||
}
|
||||
|
||||
template <class T, class = std::enable_if<std::is_enum<T>::value>>
|
||||
bool encodeEnum(ArchiveSerializable::Member member, const T& item) {
|
||||
return encodeIntegral(member, static_cast<int64_t>(item));
|
||||
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<ArchiveSerializable, T>::value>>
|
||||
bool encode(ArchiveSerializable::Member member, const std::vector<T>& items) {
|
||||
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
|
||||
*/
|
||||
@@ -134,7 +120,7 @@ class ArchiveItem {
|
||||
const ArchiveDef& itemDefinition = T::ArchiveDefinition;
|
||||
for (const auto& item : items) {
|
||||
int64_t added = 0;
|
||||
bool result = _context.archiveInstance(itemDefinition, item, added);
|
||||
bool result = context_.ArchiveInstance(itemDefinition, item, added);
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
@@ -144,69 +130,66 @@ class ArchiveItem {
|
||||
/*
|
||||
* The keys are flattened into the vectors table. Write to that table
|
||||
*/
|
||||
auto vectorInsert = encodeVectorKeys(std::move(members));
|
||||
auto vectorInsert = WriteVectorKeys(std::move(members));
|
||||
|
||||
if (!vectorInsert.first) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return encodeIntegral(member, vectorInsert.second);
|
||||
return WriteIntegral(member, vectorInsert.second);
|
||||
}
|
||||
|
||||
template <class Super,
|
||||
class Current,
|
||||
class = std::enable_if<
|
||||
std::is_base_of<ArchiveSerializable, Super>::value &&
|
||||
std::is_base_of<ArchiveSerializable, Current>::value>>
|
||||
bool encodeSuper(const Current& thiz) {
|
||||
std::string oldClass = _currentClass;
|
||||
_currentClass = Super::ArchiveDefinition.className;
|
||||
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);
|
||||
_currentClass = oldClass;
|
||||
current_class_ = oldClass;
|
||||
return success;
|
||||
}
|
||||
|
||||
template <class T, class = std::enable_if<std::is_integral<T>::value>>
|
||||
bool decode(ArchiveSerializable::Member member, T& item) {
|
||||
bool Read(ArchiveDef::Member member, T& item) {
|
||||
int64_t decoded = 0;
|
||||
auto result = decodeIntegral(member, decoded);
|
||||
auto result = ReadIntegral(member, decoded);
|
||||
item = static_cast<T>(decoded);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool decode(ArchiveSerializable::Member member, double& item);
|
||||
bool Read(ArchiveDef::Member member, double& item);
|
||||
|
||||
bool decode(ArchiveSerializable::Member member, std::string& item);
|
||||
bool Read(ArchiveDef::Member member, std::string& item);
|
||||
|
||||
bool decode(ArchiveSerializable::Member member, Allocation& allocation);
|
||||
bool Read(ArchiveDef::Member member, Allocation& allocation);
|
||||
|
||||
template <
|
||||
class T,
|
||||
class = std::enable_if<std::is_base_of<ArchiveSerializable, T>::value>>
|
||||
bool decodeArchivable(ArchiveSerializable::Member member, T& other) {
|
||||
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 decodeEnum(ArchiveSerializable::Member member, T& item) {
|
||||
bool ReadEnum(ArchiveDef::Member member, T& item) {
|
||||
int64_t desugared = 0;
|
||||
if (decodeIntegral(member, desugared)) {
|
||||
if (ReadIntegral(member, desugared)) {
|
||||
item = static_cast<T>(desugared);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <
|
||||
class T,
|
||||
class = std::enable_if<std::is_base_of<ArchiveSerializable, T>::value>>
|
||||
bool decode(ArchiveSerializable::Member member, std::vector<T>& items) {
|
||||
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 (!decodeIntegral(member, vectorForeignKey)) {
|
||||
if (!ReadIntegral(member, vectorForeignKey)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -214,7 +197,7 @@ class ArchiveItem {
|
||||
* Get vector keys
|
||||
*/
|
||||
std::vector<int64_t> keys;
|
||||
if (!decodeVectorKeys(vectorForeignKey, keys)) {
|
||||
if (!ReadVectorKeys(vectorForeignKey, keys)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -222,7 +205,7 @@ class ArchiveItem {
|
||||
for (const auto& key : keys) {
|
||||
items.emplace_back();
|
||||
|
||||
if (!_context.unarchiveInstance(otherDef, key, items.back())) {
|
||||
if (!context_.UnarchiveInstance(otherDef, key, items.back())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -232,51 +215,50 @@ class ArchiveItem {
|
||||
|
||||
template <class Super,
|
||||
class Current,
|
||||
class = std::enable_if<
|
||||
std::is_base_of<ArchiveSerializable, Super>::value &&
|
||||
std::is_base_of<ArchiveSerializable, Current>::value>>
|
||||
bool decodeSuper(Current& thiz) {
|
||||
std::string oldClass = _currentClass;
|
||||
_currentClass = Super::ArchiveDefinition.className;
|
||||
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);
|
||||
_currentClass = oldClass;
|
||||
current_class_ = oldClass;
|
||||
return success;
|
||||
}
|
||||
|
||||
ArchiveSerializable::ArchiveName name() const;
|
||||
Archivable::ArchiveName Name() const;
|
||||
|
||||
private:
|
||||
Archive& _context;
|
||||
ArchiveStatement& _statement;
|
||||
const ArchiveClassRegistration& _registration;
|
||||
ArchiveSerializable::ArchiveName _name;
|
||||
std::string _currentClass;
|
||||
Archive& context_;
|
||||
ArchiveStatement& statement_;
|
||||
const ArchiveClassRegistration& registration_;
|
||||
Archivable::ArchiveName name_;
|
||||
std::string current_class_;
|
||||
|
||||
friend class Archive;
|
||||
|
||||
ArchiveItem(Archive& context,
|
||||
ArchiveStatement& statement,
|
||||
const ArchiveClassRegistration& registration,
|
||||
ArchiveSerializable::ArchiveName name);
|
||||
ArchiveLocation(Archive& context,
|
||||
ArchiveStatement& statement,
|
||||
const ArchiveClassRegistration& registration,
|
||||
Archivable::ArchiveName name);
|
||||
|
||||
bool encodeIntegral(ArchiveSerializable::Member member, int64_t item);
|
||||
bool WriteIntegral(ArchiveDef::Member member, int64_t item);
|
||||
|
||||
bool decodeIntegral(ArchiveSerializable::Member member, int64_t& item);
|
||||
bool ReadIntegral(ArchiveDef::Member member, int64_t& item);
|
||||
|
||||
std::pair<bool, int64_t> encodeVectorKeys(std::vector<int64_t>&& members);
|
||||
std::pair<bool, int64_t> WriteVectorKeys(std::vector<int64_t>&& members);
|
||||
|
||||
bool decodeVectorKeys(ArchiveSerializable::ArchiveName name,
|
||||
std::vector<int64_t>& members);
|
||||
bool ReadVectorKeys(Archivable::ArchiveName name,
|
||||
std::vector<int64_t>& members);
|
||||
|
||||
bool encode(ArchiveSerializable::Member member,
|
||||
const ArchiveDef& otherDef,
|
||||
const ArchiveSerializable& other);
|
||||
bool Write(ArchiveDef::Member member,
|
||||
const ArchiveDef& otherDef,
|
||||
const Archivable& other);
|
||||
|
||||
bool decode(ArchiveSerializable::Member member,
|
||||
const ArchiveDef& otherDef,
|
||||
ArchiveSerializable& other);
|
||||
bool Read(ArchiveDef::Member member,
|
||||
const ArchiveDef& otherDef,
|
||||
Archivable& other);
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ArchiveItem);
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ArchiveLocation);
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -17,7 +17,7 @@ static const char* const ArchiveTablePrefix = "RL_";
|
||||
|
||||
ArchiveClassRegistration::ArchiveClassRegistration(ArchiveDatabase& database,
|
||||
ArchiveDef definition)
|
||||
: _database(database), _className(definition.className), _memberCount(0) {
|
||||
: database_(database), class_name_(definition.className) {
|
||||
/*
|
||||
* Each class in the archive class hierarchy is assigned an entry in the
|
||||
* class map.
|
||||
@@ -26,36 +26,36 @@ ArchiveClassRegistration::ArchiveClassRegistration(ArchiveDatabase& database,
|
||||
size_t currentMember = 1;
|
||||
while (current != nullptr) {
|
||||
auto membersInCurrent = current->members.size();
|
||||
_memberCount += membersInCurrent;
|
||||
member_count_ += membersInCurrent;
|
||||
MemberColumnMap map;
|
||||
for (const auto& member : current->members) {
|
||||
map[member] = currentMember++;
|
||||
}
|
||||
_classMap[current->className] = map;
|
||||
class_map_[current->className] = map;
|
||||
current = current->superClass;
|
||||
}
|
||||
|
||||
_isReady = createTable(definition.autoAssignName);
|
||||
is_ready_ = CreateTable(definition.autoAssignName);
|
||||
}
|
||||
|
||||
const std::string& ArchiveClassRegistration::className() const {
|
||||
return _className;
|
||||
const std::string& ArchiveClassRegistration::GetClassName() const {
|
||||
return class_name_;
|
||||
}
|
||||
|
||||
size_t ArchiveClassRegistration::memberCount() const {
|
||||
return _memberCount;
|
||||
size_t ArchiveClassRegistration::GetMemberCount() const {
|
||||
return member_count_;
|
||||
}
|
||||
|
||||
bool ArchiveClassRegistration::isReady() const {
|
||||
return _isReady;
|
||||
bool ArchiveClassRegistration::IsReady() const {
|
||||
return is_ready_;
|
||||
}
|
||||
|
||||
ArchiveClassRegistration::ColumnResult ArchiveClassRegistration::findColumn(
|
||||
ArchiveClassRegistration::ColumnResult ArchiveClassRegistration::FindColumn(
|
||||
const std::string& className,
|
||||
ArchiveSerializable::Member member) const {
|
||||
auto found = _classMap.find(className);
|
||||
ArchiveDef::Member member) const {
|
||||
auto found = class_map_.find(className);
|
||||
|
||||
if (found == _classMap.end()) {
|
||||
if (found == class_map_.end()) {
|
||||
return {0, false};
|
||||
}
|
||||
|
||||
@@ -70,8 +70,8 @@ ArchiveClassRegistration::ColumnResult ArchiveClassRegistration::findColumn(
|
||||
return {foundColumn->second, true};
|
||||
}
|
||||
|
||||
bool ArchiveClassRegistration::createTable(bool autoIncrement) {
|
||||
if (_className.size() == 0 || _memberCount == 0) {
|
||||
bool ArchiveClassRegistration::CreateTable(bool autoIncrement) {
|
||||
if (class_name_.size() == 0 || member_count_ == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -82,14 +82,14 @@ bool ArchiveClassRegistration::createTable(bool autoIncrement) {
|
||||
* a statement and check its validity before running.
|
||||
*/
|
||||
stream << "CREATE TABLE IF NOT EXISTS " << ArchiveTablePrefix
|
||||
<< _className.c_str() << " (" << ArchivePrimaryKeyColumnName;
|
||||
<< class_name_.c_str() << " (" << ArchivePrimaryKeyColumnName;
|
||||
|
||||
if (autoIncrement) {
|
||||
stream << " INTEGER PRIMARY KEY AUTOINCREMENT, ";
|
||||
} else {
|
||||
stream << " INTEGER PRIMARY KEY, ";
|
||||
}
|
||||
for (size_t i = 0, columns = _memberCount; i < columns; i++) {
|
||||
for (size_t i = 0, columns = member_count_; i < columns; i++) {
|
||||
stream << ArchiveColumnPrefix << std::to_string(i + 1);
|
||||
if (i != columns - 1) {
|
||||
stream << ", ";
|
||||
@@ -97,29 +97,30 @@ bool ArchiveClassRegistration::createTable(bool autoIncrement) {
|
||||
}
|
||||
stream << ");";
|
||||
|
||||
auto statement = _database.acquireStatement(stream.str());
|
||||
auto statement = database_.CreateStatement(stream.str());
|
||||
|
||||
if (!statement.isReady()) {
|
||||
if (!statement.IsReady()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!statement.reset()) {
|
||||
if (!statement.Reset()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return statement.run() == ArchiveStatement::Result::Done;
|
||||
return statement.Run() == ArchiveStatement::Result::kDone;
|
||||
}
|
||||
|
||||
ArchiveStatement ArchiveClassRegistration::queryStatement(bool single) const {
|
||||
ArchiveStatement ArchiveClassRegistration::GetQueryStatement(
|
||||
bool single) const {
|
||||
std::stringstream stream;
|
||||
stream << "SELECT " << ArchivePrimaryKeyColumnName << ", ";
|
||||
for (size_t i = 0, members = _memberCount; i < members; i++) {
|
||||
for (size_t i = 0, members = member_count_; i < members; i++) {
|
||||
stream << ArchiveColumnPrefix << std::to_string(i + 1);
|
||||
if (i != members - 1) {
|
||||
stream << ",";
|
||||
}
|
||||
}
|
||||
stream << " FROM " << ArchiveTablePrefix << _className;
|
||||
stream << " FROM " << ArchiveTablePrefix << class_name_;
|
||||
|
||||
if (single) {
|
||||
stream << " WHERE " << ArchivePrimaryKeyColumnName << " = ?";
|
||||
@@ -129,22 +130,22 @@ ArchiveStatement ArchiveClassRegistration::queryStatement(bool single) const {
|
||||
|
||||
stream << ";";
|
||||
|
||||
return _database.acquireStatement(stream.str());
|
||||
return database_.CreateStatement(stream.str());
|
||||
}
|
||||
|
||||
ArchiveStatement ArchiveClassRegistration::insertStatement() const {
|
||||
ArchiveStatement ArchiveClassRegistration::GetInsertStatement() const {
|
||||
std::stringstream stream;
|
||||
stream << "INSERT OR REPLACE INTO " << ArchiveTablePrefix << _className
|
||||
stream << "INSERT OR REPLACE INTO " << ArchiveTablePrefix << class_name_
|
||||
<< " VALUES ( ?, ";
|
||||
for (size_t i = 0; i < _memberCount; i++) {
|
||||
for (size_t i = 0; i < member_count_; i++) {
|
||||
stream << "?";
|
||||
if (i != _memberCount - 1) {
|
||||
if (i != member_count_ - 1) {
|
||||
stream << ", ";
|
||||
}
|
||||
}
|
||||
stream << ");";
|
||||
|
||||
return _database.acquireStatement(stream.str());
|
||||
return database_.CreateStatement(stream.str());
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -12,36 +12,36 @@ namespace impeller {
|
||||
class ArchiveClassRegistration {
|
||||
public:
|
||||
using ColumnResult = std::pair<size_t, bool>;
|
||||
ColumnResult findColumn(const std::string& className,
|
||||
ArchiveSerializable::Member member) const;
|
||||
ColumnResult FindColumn(const std::string& className,
|
||||
ArchiveDef::Member member) const;
|
||||
|
||||
const std::string& className() const;
|
||||
const std::string& GetClassName() const;
|
||||
|
||||
size_t memberCount() const;
|
||||
size_t GetMemberCount() const;
|
||||
|
||||
bool isReady() const;
|
||||
bool IsReady() const;
|
||||
|
||||
ArchiveStatement insertStatement() const;
|
||||
ArchiveStatement GetInsertStatement() const;
|
||||
|
||||
ArchiveStatement queryStatement(bool single) const;
|
||||
ArchiveStatement GetQueryStatement(bool single) const;
|
||||
|
||||
static const size_t NameIndex = 0;
|
||||
|
||||
private:
|
||||
using MemberColumnMap = std::map<ArchiveSerializable::Member, size_t>;
|
||||
using MemberColumnMap = std::map<ArchiveDef::Member, size_t>;
|
||||
using ClassMap = std::map<std::string, MemberColumnMap>;
|
||||
|
||||
friend class ArchiveDatabase;
|
||||
|
||||
ArchiveClassRegistration(ArchiveDatabase& database, ArchiveDef definition);
|
||||
|
||||
bool createTable(bool autoIncrement);
|
||||
bool CreateTable(bool autoIncrement);
|
||||
|
||||
ArchiveDatabase& _database;
|
||||
ClassMap _classMap;
|
||||
std::string _className;
|
||||
size_t _memberCount;
|
||||
bool _isReady;
|
||||
ArchiveDatabase& database_;
|
||||
ClassMap class_map_;
|
||||
std::string class_name_;
|
||||
size_t member_count_ = 0;
|
||||
bool is_ready_ = false;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ArchiveClassRegistration);
|
||||
};
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
#define DB_HANDLE reinterpret_cast<sqlite3*>(_db)
|
||||
#define DB_HANDLE reinterpret_cast<sqlite3*>(database_)
|
||||
|
||||
ArchiveDatabase::ArchiveDatabase(const std::string& filename, bool recreate) {
|
||||
if (recreate) {
|
||||
@@ -30,45 +30,45 @@ ArchiveDatabase::ArchiveDatabase(const std::string& filename, bool recreate) {
|
||||
|
||||
sqlite3* db = nullptr;
|
||||
auto res = ::sqlite3_open(filename.c_str(), &db);
|
||||
_db = db;
|
||||
database_ = db;
|
||||
|
||||
if (res != SQLITE_OK || _db == nullptr) {
|
||||
if (res != SQLITE_OK || database_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
_beginTransaction = std::unique_ptr<ArchiveStatement>(
|
||||
new ArchiveStatement(_db, "BEGIN TRANSACTION;"));
|
||||
begin_transaction_stmt_ = std::unique_ptr<ArchiveStatement>(
|
||||
new ArchiveStatement(database_, "BEGIN TRANSACTION;"));
|
||||
|
||||
if (!_beginTransaction->isReady()) {
|
||||
if (!begin_transaction_stmt_->IsReady()) {
|
||||
return;
|
||||
}
|
||||
|
||||
_endTransaction = std::unique_ptr<ArchiveStatement>(
|
||||
new ArchiveStatement(_db, "END TRANSACTION;"));
|
||||
end_transaction_stmt_ = std::unique_ptr<ArchiveStatement>(
|
||||
new ArchiveStatement(database_, "END TRANSACTION;"));
|
||||
|
||||
if (!_endTransaction->isReady()) {
|
||||
if (!end_transaction_stmt_->IsReady()) {
|
||||
return;
|
||||
}
|
||||
|
||||
_rollbackTransaction = std::unique_ptr<ArchiveStatement>(
|
||||
new ArchiveStatement(_db, "ROLLBACK TRANSACTION;"));
|
||||
rollback_transaction_stmt_ = std::unique_ptr<ArchiveStatement>(
|
||||
new ArchiveStatement(database_, "ROLLBACK TRANSACTION;"));
|
||||
|
||||
if (!_rollbackTransaction->isReady()) {
|
||||
if (!rollback_transaction_stmt_->IsReady()) {
|
||||
return;
|
||||
}
|
||||
|
||||
_ready = true;
|
||||
ready_ = true;
|
||||
}
|
||||
|
||||
ArchiveDatabase::~ArchiveDatabase() {
|
||||
::sqlite3_close(DB_HANDLE);
|
||||
}
|
||||
|
||||
bool ArchiveDatabase::isReady() const {
|
||||
return _ready;
|
||||
bool ArchiveDatabase::IsReady() const {
|
||||
return ready_;
|
||||
}
|
||||
|
||||
int64_t ArchiveDatabase::lastInsertRowID() {
|
||||
int64_t ArchiveDatabase::GetLastInsertRowID() {
|
||||
return ::sqlite3_last_insert_rowid(DB_HANDLE);
|
||||
}
|
||||
|
||||
@@ -77,13 +77,13 @@ static inline const ArchiveClassRegistration* RegistrationIfReady(
|
||||
if (registration == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
return registration->isReady() ? registration : nullptr;
|
||||
return registration->IsReady() ? registration : nullptr;
|
||||
}
|
||||
|
||||
const ArchiveClassRegistration* ArchiveDatabase::registrationForDefinition(
|
||||
const ArchiveClassRegistration* ArchiveDatabase::GetRegistrationForDefinition(
|
||||
const ArchiveDef& definition) {
|
||||
auto found = _registrations.find(definition.className);
|
||||
if (found != _registrations.end()) {
|
||||
auto found = registrations_.find(definition.className);
|
||||
if (found != registrations_.end()) {
|
||||
/*
|
||||
* This class has already been registered.
|
||||
*/
|
||||
@@ -96,7 +96,7 @@ const ArchiveClassRegistration* ArchiveDatabase::registrationForDefinition(
|
||||
auto registration = std::unique_ptr<ArchiveClassRegistration>(
|
||||
new ArchiveClassRegistration(*this, definition));
|
||||
auto res =
|
||||
_registrations.emplace(definition.className, std::move(registration));
|
||||
registrations_.emplace(definition.className, std::move(registration));
|
||||
|
||||
/*
|
||||
* If the new class registation is ready, return it to the caller.
|
||||
@@ -105,17 +105,17 @@ const ArchiveClassRegistration* ArchiveDatabase::registrationForDefinition(
|
||||
: nullptr;
|
||||
}
|
||||
|
||||
ArchiveStatement ArchiveDatabase::acquireStatement(
|
||||
ArchiveStatement ArchiveDatabase::CreateStatement(
|
||||
const std::string& statementString) const {
|
||||
return ArchiveStatement{_db, statementString};
|
||||
return ArchiveStatement{database_, statementString};
|
||||
}
|
||||
|
||||
ArchiveTransaction ArchiveDatabase::acquireTransaction(
|
||||
ArchiveTransaction ArchiveDatabase::CreateTransaction(
|
||||
int64_t& transactionCount) {
|
||||
return ArchiveTransaction{transactionCount, //
|
||||
*_beginTransaction, //
|
||||
*_endTransaction, //
|
||||
*_rollbackTransaction};
|
||||
return ArchiveTransaction{transactionCount, //
|
||||
*begin_transaction_stmt_, //
|
||||
*end_transaction_stmt_, //
|
||||
*rollback_transaction_stmt_};
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -22,27 +22,27 @@ class ArchiveDatabase {
|
||||
|
||||
~ArchiveDatabase();
|
||||
|
||||
bool isReady() const;
|
||||
bool IsReady() const;
|
||||
|
||||
int64_t lastInsertRowID();
|
||||
int64_t GetLastInsertRowID();
|
||||
|
||||
const ArchiveClassRegistration* registrationForDefinition(
|
||||
const ArchiveClassRegistration* GetRegistrationForDefinition(
|
||||
const ArchiveDef& definition);
|
||||
|
||||
ArchiveTransaction acquireTransaction(int64_t& transactionCount);
|
||||
ArchiveTransaction CreateTransaction(int64_t& transactionCount);
|
||||
|
||||
private:
|
||||
void* _db = nullptr;
|
||||
bool _ready = false;
|
||||
void* database_ = nullptr;
|
||||
bool ready_ = false;
|
||||
std::map<std::string, std::unique_ptr<ArchiveClassRegistration>>
|
||||
_registrations;
|
||||
std::unique_ptr<ArchiveStatement> _beginTransaction;
|
||||
std::unique_ptr<ArchiveStatement> _endTransaction;
|
||||
std::unique_ptr<ArchiveStatement> _rollbackTransaction;
|
||||
registrations_;
|
||||
std::unique_ptr<ArchiveStatement> begin_transaction_stmt_;
|
||||
std::unique_ptr<ArchiveStatement> end_transaction_stmt_;
|
||||
std::unique_ptr<ArchiveStatement> rollback_transaction_stmt_;
|
||||
|
||||
friend class ArchiveClassRegistration;
|
||||
|
||||
ArchiveStatement acquireStatement(const std::string& statementString) const;
|
||||
ArchiveStatement CreateStatement(const std::string& statementString) const;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ArchiveDatabase);
|
||||
};
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
#define STATEMENT_HANDLE reinterpret_cast<::sqlite3_stmt*>(_statement)
|
||||
#define STATEMENT_HANDLE reinterpret_cast<::sqlite3_stmt*>(statement_handle_)
|
||||
|
||||
ArchiveStatement::ArchiveStatement(void* db, const std::string& statememt) {
|
||||
::sqlite3_stmt* statementHandle = nullptr;
|
||||
@@ -20,28 +20,28 @@ ArchiveStatement::ArchiveStatement(void* db, const std::string& statememt) {
|
||||
static_cast<int>(statememt.size()), //
|
||||
&statementHandle, //
|
||||
nullptr);
|
||||
_statement = statementHandle;
|
||||
_ready = res == SQLITE_OK && _statement != nullptr;
|
||||
statement_handle_ = statementHandle;
|
||||
ready_ = res == SQLITE_OK && statement_handle_ != nullptr;
|
||||
}
|
||||
|
||||
ArchiveStatement::ArchiveStatement(ArchiveStatement&& other)
|
||||
: _statement(other._statement), _ready(other._ready) {
|
||||
other._statement = nullptr;
|
||||
other._ready = false;
|
||||
: statement_handle_(other.statement_handle_), ready_(other.ready_) {
|
||||
other.statement_handle_ = nullptr;
|
||||
other.ready_ = false;
|
||||
}
|
||||
|
||||
ArchiveStatement::~ArchiveStatement() {
|
||||
if (_statement != nullptr) {
|
||||
if (statement_handle_ != nullptr) {
|
||||
auto res = ::sqlite3_finalize(STATEMENT_HANDLE);
|
||||
FML_CHECK(res == SQLITE_OK) << "Unable to finalize the archive.";
|
||||
}
|
||||
}
|
||||
|
||||
bool ArchiveStatement::isReady() const {
|
||||
return _ready;
|
||||
bool ArchiveStatement::IsReady() const {
|
||||
return ready_;
|
||||
}
|
||||
|
||||
bool ArchiveStatement::reset() {
|
||||
bool ArchiveStatement::Reset() {
|
||||
if (::sqlite3_reset(STATEMENT_HANDLE) != SQLITE_OK) {
|
||||
return false;
|
||||
}
|
||||
@@ -67,14 +67,14 @@ static constexpr int ToColumn(size_t index) {
|
||||
return static_cast<int>(index);
|
||||
}
|
||||
|
||||
size_t ArchiveStatement::columnCount() {
|
||||
size_t ArchiveStatement::GetColumnCount() {
|
||||
return ::sqlite3_column_count(STATEMENT_HANDLE);
|
||||
}
|
||||
|
||||
/*
|
||||
* Bind Variants
|
||||
*/
|
||||
bool ArchiveStatement::bind(size_t index, const std::string& item) {
|
||||
bool ArchiveStatement::WriteValue(size_t index, const std::string& item) {
|
||||
return ::sqlite3_bind_text(STATEMENT_HANDLE, //
|
||||
ToParam(index), //
|
||||
item.data(), //
|
||||
@@ -82,19 +82,19 @@ bool ArchiveStatement::bind(size_t index, const std::string& item) {
|
||||
SQLITE_TRANSIENT) == SQLITE_OK;
|
||||
}
|
||||
|
||||
bool ArchiveStatement::bindIntegral(size_t index, int64_t item) {
|
||||
bool ArchiveStatement::BindIntegral(size_t index, int64_t item) {
|
||||
return ::sqlite3_bind_int64(STATEMENT_HANDLE, //
|
||||
ToParam(index), //
|
||||
item) == SQLITE_OK;
|
||||
}
|
||||
|
||||
bool ArchiveStatement::bind(size_t index, double item) {
|
||||
bool ArchiveStatement::WriteValue(size_t index, double item) {
|
||||
return ::sqlite3_bind_double(STATEMENT_HANDLE, //
|
||||
ToParam(index), //
|
||||
item) == SQLITE_OK;
|
||||
}
|
||||
|
||||
bool ArchiveStatement::bind(size_t index, const Allocation& item) {
|
||||
bool ArchiveStatement::WriteValue(size_t index, const Allocation& item) {
|
||||
return ::sqlite3_bind_blob(STATEMENT_HANDLE, //
|
||||
ToParam(index), //
|
||||
item.GetBuffer(), //
|
||||
@@ -105,12 +105,12 @@ bool ArchiveStatement::bind(size_t index, const Allocation& item) {
|
||||
/*
|
||||
* Column Variants
|
||||
*/
|
||||
bool ArchiveStatement::columnIntegral(size_t index, int64_t& item) {
|
||||
bool ArchiveStatement::ColumnIntegral(size_t index, int64_t& item) {
|
||||
item = ::sqlite3_column_int64(STATEMENT_HANDLE, ToColumn(index));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ArchiveStatement::column(size_t index, double& item) {
|
||||
bool ArchiveStatement::ReadValue(size_t index, double& item) {
|
||||
item = ::sqlite3_column_double(STATEMENT_HANDLE, ToColumn(index));
|
||||
return true;
|
||||
}
|
||||
@@ -123,7 +123,7 @@ bool ArchiveStatement::column(size_t index, double& item) {
|
||||
* TL;DR: Access blobs then bytes.
|
||||
*/
|
||||
|
||||
bool ArchiveStatement::column(size_t index, std::string& item) {
|
||||
bool ArchiveStatement::ReadValue(size_t index, std::string& item) {
|
||||
/*
|
||||
* Get the character data
|
||||
*/
|
||||
@@ -142,7 +142,7 @@ bool ArchiveStatement::column(size_t index, std::string& item) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ArchiveStatement::column(size_t index, Allocation& item) {
|
||||
bool ArchiveStatement::ReadValue(size_t index, Allocation& item) {
|
||||
/*
|
||||
* Get a blob pointer
|
||||
*/
|
||||
@@ -165,14 +165,14 @@ bool ArchiveStatement::column(size_t index, Allocation& item) {
|
||||
return true;
|
||||
}
|
||||
|
||||
ArchiveStatement::Result ArchiveStatement::run() {
|
||||
ArchiveStatement::Result ArchiveStatement::Run() {
|
||||
switch (::sqlite3_step(STATEMENT_HANDLE)) {
|
||||
case SQLITE_DONE:
|
||||
return Result::Done;
|
||||
return Result::kDone;
|
||||
case SQLITE_ROW:
|
||||
return Result::Row;
|
||||
return Result::kRow;
|
||||
default:
|
||||
return Result::Failure;
|
||||
return Result::kFailure;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,53 +17,53 @@ class ArchiveStatement {
|
||||
|
||||
ArchiveStatement(ArchiveStatement&& message);
|
||||
|
||||
bool isReady() const;
|
||||
bool IsReady() const;
|
||||
|
||||
bool reset();
|
||||
bool Reset();
|
||||
|
||||
bool bind(size_t index, const std::string& item);
|
||||
bool WriteValue(size_t index, const std::string& item);
|
||||
|
||||
template <class T, class = std::enable_if<std::is_integral<T>::value>>
|
||||
bool bind(size_t index, T item) {
|
||||
return bindIntegral(index, static_cast<int64_t>(item));
|
||||
bool WriteValue(size_t index, T item) {
|
||||
return BindIntegral(index, static_cast<int64_t>(item));
|
||||
}
|
||||
|
||||
bool bind(size_t index, double item);
|
||||
bool WriteValue(size_t index, double item);
|
||||
|
||||
bool bind(size_t index, const Allocation& item);
|
||||
bool WriteValue(size_t index, const Allocation& item);
|
||||
|
||||
template <class T, class = std::enable_if<std::is_integral<T>::value>>
|
||||
bool column(size_t index, T& item) {
|
||||
return columnIntegral(index, item);
|
||||
bool ReadValue(size_t index, T& item) {
|
||||
return ColumnIntegral(index, item);
|
||||
}
|
||||
|
||||
bool column(size_t index, double& item);
|
||||
bool ReadValue(size_t index, double& item);
|
||||
|
||||
bool column(size_t index, std::string& item);
|
||||
bool ReadValue(size_t index, std::string& item);
|
||||
|
||||
bool column(size_t index, Allocation& item);
|
||||
bool ReadValue(size_t index, Allocation& item);
|
||||
|
||||
size_t columnCount();
|
||||
size_t GetColumnCount();
|
||||
|
||||
enum class Result {
|
||||
Done,
|
||||
Row,
|
||||
Failure,
|
||||
kDone,
|
||||
kRow,
|
||||
kFailure,
|
||||
};
|
||||
|
||||
Result run();
|
||||
[[nodiscard]] Result Run();
|
||||
|
||||
private:
|
||||
void* _statement = nullptr;
|
||||
bool _ready = false;
|
||||
void* statement_handle_ = nullptr;
|
||||
bool ready_ = false;
|
||||
|
||||
friend class ArchiveDatabase;
|
||||
|
||||
ArchiveStatement(void* db, const std::string& statememt);
|
||||
|
||||
bool bindIntegral(size_t index, int64_t item);
|
||||
bool BindIntegral(size_t index, int64_t item);
|
||||
|
||||
bool columnIntegral(size_t index, int64_t& item);
|
||||
bool ColumnIntegral(size_t index, int64_t& item);
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ArchiveStatement);
|
||||
};
|
||||
|
||||
@@ -13,40 +13,40 @@ ArchiveTransaction::ArchiveTransaction(int64_t& transactionCount,
|
||||
ArchiveStatement& beginStatement,
|
||||
ArchiveStatement& endStatement,
|
||||
ArchiveStatement& rollbackStatement)
|
||||
: _endStatement(endStatement),
|
||||
_rollbackStatement(rollbackStatement),
|
||||
_transactionCount(transactionCount) {
|
||||
if (_transactionCount == 0) {
|
||||
_cleanup = beginStatement.run() == ArchiveStatement::Result::Done;
|
||||
: end_stmt_(endStatement),
|
||||
rollback_stmt_(rollbackStatement),
|
||||
transaction_count_(transactionCount) {
|
||||
if (transaction_count_ == 0) {
|
||||
cleanup_ = beginStatement.Run() == ArchiveStatement::Result::kDone;
|
||||
}
|
||||
_transactionCount++;
|
||||
transaction_count_++;
|
||||
}
|
||||
|
||||
ArchiveTransaction::ArchiveTransaction(ArchiveTransaction&& other)
|
||||
: _endStatement(other._endStatement),
|
||||
_rollbackStatement(other._rollbackStatement),
|
||||
_transactionCount(other._transactionCount),
|
||||
_cleanup(other._cleanup),
|
||||
_successful(other._successful) {
|
||||
other._abandoned = true;
|
||||
: end_stmt_(other.end_stmt_),
|
||||
rollback_stmt_(other.rollback_stmt_),
|
||||
transaction_count_(other.transaction_count_),
|
||||
cleanup_(other.cleanup_),
|
||||
successful_(other.successful_) {
|
||||
other.abandoned_ = true;
|
||||
}
|
||||
|
||||
ArchiveTransaction::~ArchiveTransaction() {
|
||||
if (_abandoned) {
|
||||
if (abandoned_) {
|
||||
return;
|
||||
}
|
||||
|
||||
FML_CHECK(_transactionCount != 0);
|
||||
if (_transactionCount == 1 && _cleanup) {
|
||||
auto res = _successful ? _endStatement.run() : _rollbackStatement.run();
|
||||
FML_CHECK(res == ArchiveStatement::Result::Done)
|
||||
FML_CHECK(transaction_count_ != 0);
|
||||
if (transaction_count_ == 1 && cleanup_) {
|
||||
auto res = successful_ ? end_stmt_.Run() : rollback_stmt_.Run();
|
||||
FML_CHECK(res == ArchiveStatement::Result::kDone)
|
||||
<< "Must be able to commit the nested transaction";
|
||||
}
|
||||
_transactionCount--;
|
||||
transaction_count_--;
|
||||
}
|
||||
|
||||
void ArchiveTransaction::markWritesSuccessful() {
|
||||
_successful = true;
|
||||
void ArchiveTransaction::MarkWritesAsReadyForCommit() {
|
||||
successful_ = true;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -11,21 +11,30 @@ namespace impeller {
|
||||
|
||||
class ArchiveStatement;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// @brief All writes made to the archive within a transaction that is not
|
||||
/// marked as ready for commit will be rolled back with the
|
||||
/// transaction ends.
|
||||
///
|
||||
/// All transactions are obtained from the `ArchiveDatabase`.
|
||||
///
|
||||
/// @see `ArchiveDatabase`
|
||||
///
|
||||
class ArchiveTransaction {
|
||||
public:
|
||||
ArchiveTransaction(ArchiveTransaction&& transaction);
|
||||
|
||||
~ArchiveTransaction();
|
||||
|
||||
void markWritesSuccessful();
|
||||
void MarkWritesAsReadyForCommit();
|
||||
|
||||
private:
|
||||
ArchiveStatement& _endStatement;
|
||||
ArchiveStatement& _rollbackStatement;
|
||||
int64_t& _transactionCount;
|
||||
bool _cleanup = false;
|
||||
bool _successful = false;
|
||||
bool _abandoned = false;
|
||||
ArchiveStatement& end_stmt_;
|
||||
ArchiveStatement& rollback_stmt_;
|
||||
int64_t& transaction_count_;
|
||||
bool cleanup_ = false;
|
||||
bool successful_ = false;
|
||||
bool abandoned_ = false;
|
||||
|
||||
friend class ArchiveDatabase;
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
namespace impeller {
|
||||
|
||||
ArchiveVector::ArchiveVector(std::vector<int64_t>&& keys)
|
||||
: _keys(std::move(keys)) {}
|
||||
: keys_(std::move(keys)) {}
|
||||
|
||||
ArchiveVector::ArchiveVector() {}
|
||||
|
||||
@@ -20,35 +20,35 @@ const ArchiveDef ArchiveVector::ArchiveDefinition = {
|
||||
/* .members = */ {0},
|
||||
};
|
||||
|
||||
ArchiveSerializable::ArchiveName ArchiveVector::archiveName() const {
|
||||
Archivable::ArchiveName ArchiveVector::GetArchiveName() const {
|
||||
return ArchiveNameAuto;
|
||||
}
|
||||
|
||||
const std::vector<int64_t> ArchiveVector::keys() const {
|
||||
return _keys;
|
||||
return keys_;
|
||||
}
|
||||
|
||||
bool ArchiveVector::serialize(ArchiveItem& item) const {
|
||||
bool ArchiveVector::Write(ArchiveLocation& item) const {
|
||||
std::stringstream stream;
|
||||
for (size_t i = 0, count = _keys.size(); i < count; i++) {
|
||||
stream << _keys[i];
|
||||
for (size_t i = 0, count = keys_.size(); i < count; i++) {
|
||||
stream << keys_[i];
|
||||
if (i != count - 1) {
|
||||
stream << ",";
|
||||
}
|
||||
}
|
||||
return item.encode(0, stream.str());
|
||||
return item.Write(0, stream.str());
|
||||
}
|
||||
|
||||
bool ArchiveVector::deserialize(ArchiveItem& item) {
|
||||
bool ArchiveVector::Read(ArchiveLocation& item) {
|
||||
std::string flattened;
|
||||
if (!item.decode(0, flattened)) {
|
||||
if (!item.Read(0, flattened)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::stringstream stream(flattened);
|
||||
int64_t single = 0;
|
||||
while (stream >> single) {
|
||||
_keys.emplace_back(single);
|
||||
keys_.emplace_back(single);
|
||||
stream.ignore();
|
||||
}
|
||||
|
||||
|
||||
@@ -9,22 +9,22 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class ArchiveVector : public ArchiveSerializable {
|
||||
class ArchiveVector : public Archivable {
|
||||
public:
|
||||
static const ArchiveDef ArchiveDefinition;
|
||||
|
||||
ArchiveName archiveName() const override;
|
||||
ArchiveName GetArchiveName() const override;
|
||||
|
||||
const std::vector<int64_t> keys() const;
|
||||
|
||||
bool serialize(ArchiveItem& item) const override;
|
||||
bool Write(ArchiveLocation& item) const override;
|
||||
|
||||
bool deserialize(ArchiveItem& item) override;
|
||||
bool Read(ArchiveLocation& item) override;
|
||||
|
||||
private:
|
||||
std::vector<int64_t> _keys;
|
||||
std::vector<int64_t> keys_;
|
||||
|
||||
friend class ArchiveItem;
|
||||
friend class ArchiveLocation;
|
||||
|
||||
ArchiveVector();
|
||||
|
||||
|
||||
@@ -12,30 +12,33 @@
|
||||
namespace impeller {
|
||||
namespace testing {
|
||||
|
||||
static ArchiveSerializable::ArchiveName LastSample = 0;
|
||||
static Archivable::ArchiveName LastSample = 0;
|
||||
|
||||
class Sample : public ArchiveSerializable {
|
||||
class Sample : public Archivable {
|
||||
public:
|
||||
Sample(uint64_t count = 42) : _someData(count), _name(++LastSample) {}
|
||||
Sample(uint64_t count = 42) : some_data_(count) {}
|
||||
|
||||
uint64_t someData() const { return _someData; }
|
||||
uint64_t GetSomeData() const { return some_data_; }
|
||||
|
||||
ArchiveName archiveName() const override { return _name; }
|
||||
// |Archivable|
|
||||
ArchiveName GetArchiveName() const override { return name_; }
|
||||
|
||||
bool serialize(ArchiveItem& item) const override {
|
||||
return item.encode(999, _someData);
|
||||
// |Archivable|
|
||||
bool Write(ArchiveLocation& item) const override {
|
||||
return item.Write(999, some_data_);
|
||||
};
|
||||
|
||||
bool deserialize(ArchiveItem& item) override {
|
||||
_name = item.name();
|
||||
return item.decode(999, _someData);
|
||||
// |Archivable|
|
||||
bool Read(ArchiveLocation& item) override {
|
||||
name_ = item.Name();
|
||||
return item.Read(999, some_data_);
|
||||
};
|
||||
|
||||
static const ArchiveDef ArchiveDefinition;
|
||||
|
||||
private:
|
||||
uint64_t _someData;
|
||||
ArchiveName _name;
|
||||
uint64_t some_data_;
|
||||
ArchiveName name_ = ++LastSample;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Sample);
|
||||
};
|
||||
@@ -51,7 +54,7 @@ TEST(ArchiveTest, SimpleInitialization) {
|
||||
auto name = "/tmp/sample.db";
|
||||
{
|
||||
Archive archive(name, true);
|
||||
ASSERT_TRUE(archive.isReady());
|
||||
ASSERT_TRUE(archive.IsReady());
|
||||
}
|
||||
ASSERT_EQ(::remove(name), 0);
|
||||
}
|
||||
@@ -60,7 +63,7 @@ TEST(ArchiveTest, AddStorageClass) {
|
||||
auto name = "/tmp/sample2.db";
|
||||
{
|
||||
Archive archive(name, true);
|
||||
ASSERT_TRUE(archive.isReady());
|
||||
ASSERT_TRUE(archive.IsReady());
|
||||
}
|
||||
ASSERT_EQ(::remove(name), 0);
|
||||
}
|
||||
@@ -69,9 +72,9 @@ TEST(ArchiveTest, AddData) {
|
||||
auto name = "/tmp/sample3.db";
|
||||
{
|
||||
Archive archive(name, true);
|
||||
ASSERT_TRUE(archive.isReady());
|
||||
ASSERT_TRUE(archive.IsReady());
|
||||
Sample sample;
|
||||
ASSERT_TRUE(archive.archive(sample));
|
||||
ASSERT_TRUE(archive.Write(sample));
|
||||
}
|
||||
ASSERT_EQ(::remove(name), 0);
|
||||
}
|
||||
@@ -80,11 +83,11 @@ TEST(ArchiveTest, AddDataMultiple) {
|
||||
auto name = "/tmp/sample4.db";
|
||||
{
|
||||
Archive archive(name, true);
|
||||
ASSERT_TRUE(archive.isReady());
|
||||
ASSERT_TRUE(archive.IsReady());
|
||||
|
||||
for (size_t i = 0; i < 100; i++) {
|
||||
Sample sample(i + 1);
|
||||
ASSERT_TRUE(archive.archive(sample));
|
||||
ASSERT_TRUE(archive.Write(sample));
|
||||
}
|
||||
}
|
||||
ASSERT_EQ(::remove(name), 0);
|
||||
@@ -94,27 +97,24 @@ TEST(ArchiveTest, ReadData) {
|
||||
auto name = "/tmp/sample5.db";
|
||||
{
|
||||
Archive archive(name, true);
|
||||
ASSERT_TRUE(archive.isReady());
|
||||
ASSERT_TRUE(archive.IsReady());
|
||||
|
||||
size_t count = 50;
|
||||
|
||||
std::vector<ArchiveSerializable::ArchiveName> keys;
|
||||
std::vector<Archivable::ArchiveName> keys;
|
||||
std::vector<uint64_t> values;
|
||||
|
||||
keys.reserve(count);
|
||||
values.reserve(count);
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
Sample sample(i + 1);
|
||||
keys.push_back(sample.archiveName());
|
||||
values.push_back(sample.someData());
|
||||
ASSERT_TRUE(archive.archive(sample));
|
||||
keys.push_back(sample.GetArchiveName());
|
||||
values.push_back(sample.GetSomeData());
|
||||
ASSERT_TRUE(archive.Write(sample));
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
Sample sample;
|
||||
ASSERT_TRUE(archive.unarchive(keys[i], sample));
|
||||
ASSERT_EQ(values[i], sample.someData());
|
||||
ASSERT_TRUE(archive.Read(keys[i], sample));
|
||||
ASSERT_EQ(values[i], sample.GetSomeData());
|
||||
}
|
||||
}
|
||||
ASSERT_EQ(::remove(name), 0);
|
||||
@@ -127,11 +127,11 @@ TEST(ArchiveTest, ReadDataWithNames) {
|
||||
auto name = "/tmp/sample6.db";
|
||||
{
|
||||
Archive archive(name, true);
|
||||
ASSERT_TRUE(archive.isReady());
|
||||
ASSERT_TRUE(archive.IsReady());
|
||||
|
||||
size_t count = 8;
|
||||
|
||||
std::vector<ArchiveSerializable::ArchiveName> keys;
|
||||
std::vector<Archivable::ArchiveName> keys;
|
||||
std::vector<uint64_t> values;
|
||||
|
||||
keys.reserve(count);
|
||||
@@ -139,16 +139,16 @@ TEST(ArchiveTest, ReadDataWithNames) {
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
Sample sample(i + 1);
|
||||
keys.push_back(sample.archiveName());
|
||||
values.push_back(sample.someData());
|
||||
ASSERT_TRUE(archive.archive(sample));
|
||||
keys.push_back(sample.GetArchiveName());
|
||||
values.push_back(sample.GetSomeData());
|
||||
ASSERT_TRUE(archive.Write(sample));
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
Sample sample;
|
||||
ASSERT_TRUE(archive.unarchive(keys[i], sample));
|
||||
ASSERT_EQ(values[i], sample.someData());
|
||||
ASSERT_EQ(keys[i], sample.archiveName());
|
||||
ASSERT_TRUE(archive.Read(keys[i], sample));
|
||||
ASSERT_EQ(values[i], sample.GetSomeData());
|
||||
ASSERT_EQ(keys[i], sample.GetArchiveName());
|
||||
}
|
||||
}
|
||||
ASSERT_EQ(::remove(name), 0);
|
||||
|
||||
Reference in New Issue
Block a user