Skip to content

Commit

Permalink
Filesystem: Add IPFS implementation
Browse files Browse the repository at this point in the history
This adds an abstraction for a data store. LMDB is included as an
implementation.

A block store is built on the data store abstraction. Blocks are chunks
data addressed by a CID (content identifier).
  • Loading branch information
garbear committed Sep 8, 2023
1 parent 5959926 commit 8fb5abc
Show file tree
Hide file tree
Showing 28 changed files with 1,667 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmake/treedata/common/subdirs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ xbmc/crypto/did crypto/did
xbmc/crypto/ed25519 crypto/ed25519
xbmc/crypto/multiformats crypto/multiformats
xbmc/crypto/random crypto/random
xbmc/datastore datastore
xbmc/dbwrappers dbwrappers
xbmc/dialogs dialogs
xbmc/favourites favourites
Expand Down
30 changes: 30 additions & 0 deletions xbmc/datastore/Block.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2018-2020 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#include "Block.h"

using namespace KODI;
using namespace DATASTORE;

CBlock::CBlock(CCID cid, std::vector<uint8_t> data) : m_cid(std::move(cid)), m_data(std::move(data))
{
}

std::vector<uint8_t> CBlock::Serialize() const
{
std::vector<uint8_t> data;

//! @todo

return data;
}

void CBlock::Deserialize(const std::vector<uint8_t>& data)
{
//! @todo
}
81 changes: 81 additions & 0 deletions xbmc/datastore/Block.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2018-2020 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#pragma once

#include "CID.h"

#include <stdint.h>
#include <utility>
#include <vector>

namespace KODI
{
namespace DATASTORE
{
/*!
* \brief Container of raw data with a CID identifier
*/
class CBlock
{
public:
/*!
* \brief Construct an empty block
*/
CBlock() = default;

/*!
* \brief Construct a block from parameters
*
* \param cid The block CID
* \param data The block data
*/
CBlock(CCID cid, std::vector<uint8_t> data);

/*!
* \brief Get the CID
*/
const CCID& CID() const { return m_cid; }

/*!
* \brief Set the CID
*/
void SetCID(CCID cid) { m_cid = std::move(cid); }

/*!
* \brief Get the data (const)
*/
const std::vector<uint8_t>& Data() const { return m_data; }

/*!
* \brief Get the data (mutable)
*/
std::vector<uint8_t>& Data() { return m_data; }

/*!
* \brief Set the data
*/
void SetData(std::vector<uint8_t> data) { m_data = std::move(data); }

/*!
* \brief Serialize as an array of bytes
*/
std::vector<uint8_t> Serialize() const;

/*!
* \brief Deserialize an array of bytes
*/
void Deserialize(const std::vector<uint8_t>& data);

private:
// Block parameters
CCID m_cid;
std::vector<uint8_t> m_data;
};
} // namespace DATASTORE
} // namespace KODI
63 changes: 63 additions & 0 deletions xbmc/datastore/BlockStore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2018-2020 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#include "BlockStore.h"

#include "Block.h"
#include "CID.h"
#include "DataStore.h"

using namespace KODI;
using namespace DATASTORE;

CBlockStore::CBlockStore(CDataStore& dataStore) : m_dataStore(dataStore)
{
}

bool CBlockStore::Has(const CCID& cid)
{
std::vector<uint8_t> key = cid.Serialize();

if (m_dataStore.Has(key.data(), key.size()))
return true;

return false;
}

bool CBlockStore::Get(const CCID& cid, CBlock& block)
{
std::vector<uint8_t> key = cid.Serialize();

if (m_dataStore.Get(key.data(), key.size(), block.Data()))
{
block.SetCID(cid);
return true;
}

return false;
}

bool CBlockStore::Put(const CBlock& block)
{
std::vector<uint8_t> key = block.CID().Serialize();

if (m_dataStore.Put(key.data(), key.size(), block.Data().data(), block.Data().size()))
return true;

return false;
}

bool CBlockStore::Delete(const CCID& cid)
{
std::vector<uint8_t> key = cid.Serialize();

if (m_dataStore.Delete(key.data(), key.size()))
return true;

return false;
}
76 changes: 76 additions & 0 deletions xbmc/datastore/BlockStore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (C) 2018-2020 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#pragma once

namespace KODI
{
namespace DATASTORE
{
class CBlock;
class CCID;
class CDataStore;

/*!
* \brief Thin wrapper over a data store for getting and putting block
* objects
*/
class CBlockStore
{
public:
/*!
* \brief Construct a block store
*
* \param dataStore The underlying data store
*/
CBlockStore(CDataStore& dataStore);
~CBlockStore() = default;

/*!
* \brief Determine if the CID can be found
*
* \param CID The CID to look for
*
* \return True if a block with the given CID was found, false otherwise
*/
bool Has(const CCID& cid);

/*!
* \brief Find a block based on its CID
*
* \param cid The CID to look for
* \param block The block, or unmodified if this returns false
*
* \return True if the block was retrieved, false for missing CID or error
*/
bool Get(const CCID& cid, CBlock& block);

/*!
* \brief Add a block to the block store, replacing any existing block
*
* \param block The block to store
*
* \return True if the block was added, false on error
*/
bool Put(const CBlock& block);

/*!
* \brief Delete a block based on its CID
*
* \param CID The CID to look for
*
* \return True on success, false on error
*/
bool Delete(const CCID& cid);

private:
// Construction parameters
CDataStore& m_dataStore;
};
} // namespace DATASTORE
} // namespace KODI
31 changes: 31 additions & 0 deletions xbmc/datastore/CID.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2018-2020 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#include "CID.h"

using namespace KODI;
using namespace DATASTORE;

CCID::CCID(CIDCodec codec, std::vector<uint8_t> multihash)
: m_codec(codec), m_multihash(std::move(multihash))
{
}

std::vector<uint8_t> CCID::Serialize() const
{
std::vector<uint8_t> data;

//! @todo

return data;
}

void CCID::Deserialize(const std::vector<uint8_t>& data)
{
//! @todo
}
Loading

0 comments on commit 8fb5abc

Please sign in to comment.