2021-12-05 23:40:51 +01:00
|
|
|
#ifndef LIBPKG_DATA_STORAGE_PRIVATE_H
|
|
|
|
#define LIBPKG_DATA_STORAGE_PRIVATE_H
|
|
|
|
|
|
|
|
#include "./package.h"
|
|
|
|
|
2022-01-18 22:25:28 +01:00
|
|
|
#include "../lmdb-safe/lmdb-reflective.hh"
|
2021-12-05 23:40:51 +01:00
|
|
|
#include "../lmdb-safe/lmdb-safe.hh"
|
|
|
|
#include "../lmdb-safe/lmdb-typed.hh"
|
|
|
|
|
|
|
|
#include <boost/multi_index/hashed_index.hpp>
|
|
|
|
#include <boost/multi_index/member.hpp>
|
|
|
|
#include <boost/multi_index/sequenced_index.hpp>
|
|
|
|
#include <boost/multi_index_container.hpp>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
namespace LibPkg {
|
|
|
|
|
|
|
|
using StorageID = std::uint32_t;
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
template <typename StorageType, typename EntryType> struct StorageCacheRef {
|
|
|
|
using Storage = StorageType;
|
|
|
|
explicit StorageCacheRef(const StorageType &relatedStorage, const std::shared_ptr<EntryType> &entry);
|
|
|
|
explicit StorageCacheRef(const StorageType &relatedStorage, const std::string &entryName);
|
|
|
|
bool operator==(const StorageCacheRef &other) const;
|
|
|
|
const StorageType *relatedStorage = nullptr;
|
|
|
|
const std::string *entryName;
|
2021-12-05 23:40:51 +01:00
|
|
|
};
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
template <typename StorageType, typename EntryType>
|
|
|
|
inline StorageCacheRef<StorageType, EntryType>::StorageCacheRef(const StorageType &relatedStorage, const std::shared_ptr<EntryType> &entry)
|
|
|
|
: relatedStorage(&relatedStorage)
|
|
|
|
, entryName(&entry->name)
|
2021-12-05 23:40:51 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
template <typename StorageType, typename EntryType>
|
|
|
|
inline StorageCacheRef<StorageType, EntryType>::StorageCacheRef(const StorageType &relatedStorage, const std::string &entryName)
|
|
|
|
: relatedStorage(&relatedStorage)
|
|
|
|
, entryName(&entryName)
|
2021-12-05 23:40:51 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
template <typename StorageType, typename EntryType>
|
|
|
|
inline bool StorageCacheRef<StorageType, EntryType>::operator==(const StorageCacheRef<StorageType, EntryType> &other) const
|
2021-12-05 23:40:51 +01:00
|
|
|
{
|
2022-01-19 23:27:14 +01:00
|
|
|
return relatedStorage == other.relatedStorage && *entryName == *other.entryName;
|
2021-12-05 23:40:51 +01:00
|
|
|
}
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
template <typename StorageRefType, typename EntryType> struct StorageCacheEntry {
|
|
|
|
using Ref = StorageRefType;
|
|
|
|
using Entry = EntryType;
|
|
|
|
using Storage = typename Ref::Storage;
|
|
|
|
explicit StorageCacheEntry(const StorageRefType &ref);
|
|
|
|
StorageRefType ref;
|
2021-12-05 23:40:51 +01:00
|
|
|
StorageID id;
|
2022-01-19 23:27:14 +01:00
|
|
|
std::shared_ptr<EntryType> entry;
|
2021-12-05 23:40:51 +01:00
|
|
|
};
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
template <typename StorageRefType, typename EntryType>
|
|
|
|
inline StorageCacheEntry<StorageRefType, EntryType>::StorageCacheEntry(const StorageRefType &ref)
|
2021-12-05 23:40:51 +01:00
|
|
|
: ref(ref)
|
|
|
|
, id(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
template <typename StorageEntryType> class StorageCacheEntries {
|
|
|
|
public:
|
|
|
|
using Ref = typename StorageEntryType::Ref;
|
|
|
|
using Entry = typename StorageEntryType::Entry;
|
|
|
|
using Storage = typename StorageEntryType::Storage;
|
|
|
|
using StorageEntry = StorageEntryType;
|
|
|
|
using EntryList = boost::multi_index::multi_index_container<StorageEntry,
|
2021-12-05 23:40:51 +01:00
|
|
|
boost::multi_index::indexed_by<boost::multi_index::sequenced<>,
|
2022-01-19 23:27:14 +01:00
|
|
|
boost::multi_index::hashed_unique<boost::multi_index::tag<Ref>, BOOST_MULTI_INDEX_MEMBER(StorageEntryType, Ref, ref)>>>;
|
|
|
|
using iterator = typename EntryList::iterator;
|
2021-12-05 23:40:51 +01:00
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
explicit StorageCacheEntries(std::size_t limit = 1000);
|
2021-12-05 23:40:51 +01:00
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
StorageEntry &findOrCreate(const Ref &ref);
|
2021-12-05 23:40:51 +01:00
|
|
|
void undo();
|
2022-01-19 23:27:14 +01:00
|
|
|
std::size_t erase(const Ref &ref);
|
|
|
|
std::size_t clear(const Storage &storage);
|
2021-12-05 23:40:51 +01:00
|
|
|
iterator begin();
|
|
|
|
iterator end();
|
|
|
|
|
|
|
|
private:
|
2022-01-19 23:27:14 +01:00
|
|
|
EntryList m_entries;
|
2021-12-05 23:40:51 +01:00
|
|
|
std::size_t m_limit;
|
|
|
|
};
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
template <typename StorageEntryType>
|
|
|
|
inline StorageCacheEntries<StorageEntryType>::StorageCacheEntries(std::size_t limit)
|
2021-12-05 23:40:51 +01:00
|
|
|
: m_limit(limit)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
template <typename StorageEntryType> inline void StorageCacheEntries<StorageEntryType>::undo()
|
2021-12-05 23:40:51 +01:00
|
|
|
{
|
2022-01-19 23:27:14 +01:00
|
|
|
m_entries.pop_front();
|
2021-12-05 23:40:51 +01:00
|
|
|
}
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
template <typename StorageEntryType> inline std::size_t StorageCacheEntries<StorageEntryType>::erase(const Ref &ref)
|
2021-12-05 23:40:51 +01:00
|
|
|
{
|
2022-01-19 23:27:14 +01:00
|
|
|
return m_entries.template get<typename StorageEntryType::Ref>().erase(ref);
|
2021-12-05 23:40:51 +01:00
|
|
|
}
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
template <typename StorageEntryType> inline auto StorageCacheEntries<StorageEntryType>::begin() -> iterator
|
2021-12-05 23:40:51 +01:00
|
|
|
{
|
2022-01-19 23:27:14 +01:00
|
|
|
return m_entries.begin();
|
2021-12-05 23:40:51 +01:00
|
|
|
}
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
template <typename StorageEntryType> inline auto StorageCacheEntries<StorageEntryType>::end() -> iterator
|
2021-12-05 23:40:51 +01:00
|
|
|
{
|
2022-01-19 23:27:14 +01:00
|
|
|
return m_entries.end();
|
2021-12-05 23:40:51 +01:00
|
|
|
}
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
template <typename StorageEntriesType, typename TransactionType, typename SpecType> struct StorageCache {
|
|
|
|
using Entries = StorageEntriesType;
|
|
|
|
using Entry = typename Entries::Entry;
|
|
|
|
using Txn = TransactionType;
|
|
|
|
using Storage = typename Entries::Storage;
|
2021-12-05 23:40:51 +01:00
|
|
|
struct StoreResult {
|
|
|
|
StorageID id = 0;
|
|
|
|
bool updated = false;
|
2022-01-19 23:27:14 +01:00
|
|
|
std::shared_ptr<typename Entries::Entry> oldEntry;
|
2021-12-05 23:40:51 +01:00
|
|
|
};
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
SpecType retrieve(Storage &storage, const std::string &entryName);
|
|
|
|
StoreResult store(Storage &storage, const std::shared_ptr<Entry> &entry, bool force);
|
|
|
|
StoreResult store(Storage &storage, Txn &txn, const std::shared_ptr<Entry> &entry);
|
|
|
|
bool invalidate(Storage &storage, const std::string &entryName);
|
|
|
|
void clear(Storage &storage);
|
|
|
|
void clearCacheOnly(Storage &storage);
|
2021-12-05 23:40:51 +01:00
|
|
|
|
|
|
|
private:
|
2022-01-19 23:27:14 +01:00
|
|
|
Entries m_entries;
|
2021-12-05 23:40:51 +01:00
|
|
|
std::mutex m_mutex;
|
|
|
|
};
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
using PackageStorage = LMDBSafe::TypedDBI<Package, LMDBSafe::index_on<Package, std::string, &Package::name>>;
|
|
|
|
using DependencyStorage = LMDBSafe::TypedDBI<DatabaseDependency, LMDBSafe::index_on<Dependency, std::string, &DatabaseDependency::name>>;
|
|
|
|
using LibraryDependencyStorage
|
|
|
|
= LMDBSafe::TypedDBI<DatabaseLibraryDependency, LMDBSafe::index_on<DatabaseLibraryDependency, std::string, &DatabaseLibraryDependency::name>>;
|
|
|
|
using PackageCacheRef = StorageCacheRef<DatabaseStorage, Package>;
|
|
|
|
using PackageCacheEntry = StorageCacheEntry<PackageCacheRef, Package>;
|
|
|
|
using PackageCacheEntries = StorageCacheEntries<PackageCacheEntry>;
|
|
|
|
using PackageCache = StorageCache<PackageCacheEntries, PackageStorage::RWTransaction, PackageSpec>;
|
|
|
|
|
|
|
|
extern template struct StorageCacheRef<DatabaseStorage, Package>;
|
|
|
|
extern template struct StorageCacheEntry<PackageCacheRef, Package>;
|
|
|
|
extern template class StorageCacheEntries<PackageCacheEntry>;
|
|
|
|
extern template struct StorageCache<PackageCacheEntries, PackageStorage::RWTransaction, PackageSpec>;
|
|
|
|
|
2021-12-05 23:40:51 +01:00
|
|
|
struct StorageDistribution {
|
|
|
|
explicit StorageDistribution(const char *path, std::uint32_t maxDbs);
|
|
|
|
|
|
|
|
std::unique_ptr<DatabaseStorage> forDatabase(std::string_view uniqueDatabaseName);
|
|
|
|
|
|
|
|
private:
|
2022-01-18 22:25:28 +01:00
|
|
|
std::shared_ptr<LMDBSafe::MDBEnv> m_env;
|
2021-12-05 23:40:51 +01:00
|
|
|
PackageCache m_packageCache;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline std::unique_ptr<DatabaseStorage> StorageDistribution::forDatabase(std::string_view uniqueDatabaseName)
|
|
|
|
{
|
|
|
|
return std::make_unique<DatabaseStorage>(m_env, m_packageCache, uniqueDatabaseName);
|
|
|
|
}
|
|
|
|
|
2022-01-19 23:27:14 +01:00
|
|
|
struct DatabaseStorage {
|
|
|
|
explicit DatabaseStorage(const std::shared_ptr<LMDBSafe::MDBEnv> &env, PackageCache &packageCache, std::string_view uniqueDatabaseName);
|
|
|
|
PackageCache &packageCache;
|
|
|
|
PackageStorage packages;
|
|
|
|
DependencyStorage providedDeps;
|
|
|
|
DependencyStorage requiredDeps;
|
|
|
|
LibraryDependencyStorage providedLibs;
|
|
|
|
LibraryDependencyStorage requiredLibs;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<LMDBSafe::MDBEnv> m_env;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::size_t hash_value(const PackageCacheRef &ref);
|
|
|
|
|
2021-12-05 23:40:51 +01:00
|
|
|
} // namespace LibPkg
|
|
|
|
|
|
|
|
#endif // LIBPKG_DATA_STORAGE_PRIVATE_H
|