Skip to content

Instantly share code, notes, and snippets.

@ned14
Created August 30, 2015 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ned14/163ac57c937bda61e5c9 to your computer and use it in GitHub Desktop.
Save ned14/163ac57c937bda61e5c9 to your computer and use it in GitHub Desktop.
#include <vector>
#include <future>
#include <filesystem>
using std::future;
namespace filesystem = std::experimental::filesystem;
class data_store
{
public:
//! Disposition flags
static constexpr size_t writeable = (1 << 0);
static constexpr size_t immediately_durable_index = (1 << 1);
static constexpr size_t immediately_durable_blob = (1 << 2);
//! Open a data store at path with disposition flags
data_store(size_t flags = 0, filesystem::path path = "store");
//! The kind of 128-bit hash used
enum class hash_t : unsigned char
{
unknown = 0,
spooky, //!< We are using SpookyHash (0.3 cycles/byte)
blake2b //!< We are using Blake2b (3 cycles/bytes)
};
//! Reference to a blob
class blob_reference
{
union
{
__m128i _hash_sse;
unsigned long long int _hash_uint64[2];
};
hash_t _hash_type;
constexpr blob_reference() noexcept : _hash_uint64{ 0, 0 }, _hash_type(hash_t::unknown) { }
};
//! Scatter-gather buffers type
using buffers_type = std::vector<std::pair<const char *, size_t>>;
//! Store a blob
future<blob_reference> store_blob(buffers_type buffers) noexcept;
//! Load a blob
future<void> load_blob(buffers_type buffers, blob_reference blobref) noexcept;
//! Map load a blob
future<std::shared_ptr<buffers_type>> load_blob(blob_reference blobref) noexcept;
//! Refresh the index, returning the time count of the new index
future<unsigned> refresh_index() noexcept;
//! Look up a key (T will be std::string or const char *)
template<class T> future<blob_reference> lookup(T &&key, bool refresh_index = false) noexcept;
//! Potentional transaction commit outcomes
enum class transaction_status
{
success=0, //!< The transaction was successfully committed
conflict, //!< This transaction conflicted with another transaction (index is stale)
stale //!< One or more blob references could not be found (perhaps blob is too old)
};
//! A RAII update transaction object
class transaction
{
public:
transaction(transaction &&) noexcept;
transaction &operator=(transaction &&) noexcept;
//! Disposes of the transaction if commit was not called
~transaction();
//! Commits the transaction, returning outcome
future<transaction_status> commit() noexcept;
//! Adds an update to be part of the transaction
template<class T> void add(T &&key, blob_reference value);
};
//! Begin an update transaction
transaction begin_transaction(bool reload_index = false);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment