Pass IPFS_PATH to repo.

This commit is contained in:
David Craven 2019-02-19 13:06:21 +01:00
parent 7a71a70b89
commit c54e107e3e
No known key found for this signature in database
GPG Key ID: DF438712EA50DBB1
5 changed files with 69 additions and 17 deletions

View File

@ -96,9 +96,9 @@ pub struct Ipfs<Types: IpfsTypes> {
impl<Types: IpfsTypes> Ipfs<Types> {
/// Creates a new ipfs node.
pub fn new(options: IpfsOptions) -> Self {
let repo_options = RepoOptions::<Types>::from(&options.config);
let repo_options = RepoOptions::<Types>::from(&options);
let repo = create_repo(repo_options);
let swarm_options = SwarmOptions::<Types>::from(&options.config);
let swarm_options = SwarmOptions::<Types>::from(&options);
let swarm = create_swarm(swarm_options, repo.clone());
Ipfs {

View File

@ -1,6 +1,6 @@
//! P2P handling for IPFS nodes.
use crate::bitswap::Strategy;
use crate::config::ConfigFile;
use crate::IpfsOptions;
use crate::repo::RepoTypes;
use libp2p::{Multiaddr, PeerId};
use libp2p::core::Swarm;
@ -23,11 +23,11 @@ pub struct SwarmOptions<TSwarmTypes: SwarmTypes> {
pub bootstrap: Vec<(Multiaddr, PeerId)>,
}
impl<TSwarmTypes: SwarmTypes> From<&ConfigFile> for SwarmOptions<TSwarmTypes> {
fn from(config: &ConfigFile) -> Self {
let key_pair = config.secio_key_pair();
impl<TSwarmTypes: SwarmTypes> From<&IpfsOptions> for SwarmOptions<TSwarmTypes> {
fn from(options: &IpfsOptions) -> Self {
let key_pair = options.config.secio_key_pair();
let peer_id = key_pair.to_peer_id();
let bootstrap = config.bootstrap();
let bootstrap = options.config.bootstrap();
SwarmOptions {
_marker: PhantomData,
key_pair,

View File

@ -0,0 +1,45 @@
//! Persistent fs backed repo
use crate::block::{Cid, Block};
use crate::repo::{BlockStore, DataStore};
use std::path::PathBuf;
#[derive(Clone, Debug)]
pub struct FsBlockStore {
path: PathBuf,
}
impl BlockStore for FsBlockStore {
fn new(path: PathBuf) -> Self {
FsBlockStore {
path,
}
}
fn contains(&self, _cid: &Cid) -> bool {
false
}
fn get(&self, _cid: &Cid) -> Option<Block> {
None
}
fn put(&self, block: Block) -> Cid {
block.cid()
}
fn remove(&self, _cid: &Cid) {
}
}
#[derive(Clone, Debug)]
pub struct RocksDataStore {
path: PathBuf,
}
impl DataStore for RocksDataStore {
fn new(path: PathBuf) -> Self {
RocksDataStore {
path
}
}
}

View File

@ -2,6 +2,7 @@
use crate::block::{Cid, Block};
use crate::repo::{BlockStore, DataStore};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
#[derive(Clone, Debug)]
@ -10,7 +11,7 @@ pub struct MemBlockStore {
}
impl BlockStore for MemBlockStore {
fn new() -> Self {
fn new(_path: PathBuf) -> Self {
MemBlockStore {
blocks: Arc::new(Mutex::new(HashMap::new()))
}
@ -44,7 +45,7 @@ pub struct MemDataStore {
}
impl DataStore for MemDataStore {
fn new() -> Self {
fn new(_path: PathBuf) -> Self {
MemDataStore {}
}
}

View File

@ -1,7 +1,8 @@
//! IPFS repo
use crate::block::{Cid, Block};
use crate::config::ConfigFile;
use crate::IpfsOptions;
use std::marker::PhantomData;
use std::path::PathBuf;
pub mod mem;
pub mod fs;
@ -12,26 +13,31 @@ pub trait RepoTypes {
type TRepo: Repo<Self::TBlockStore, Self::TDataStore>;
}
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug)]
pub struct RepoOptions<TRepoTypes: RepoTypes> {
_marker: PhantomData<TRepoTypes>,
path: PathBuf,
}
impl<TRepoTypes: RepoTypes> From<&ConfigFile> for RepoOptions<TRepoTypes> {
fn from(_config: &ConfigFile) -> Self {
impl<TRepoTypes: RepoTypes> From<&IpfsOptions> for RepoOptions<TRepoTypes> {
fn from(options: &IpfsOptions) -> Self {
RepoOptions {
_marker: PhantomData,
path: options.ipfs_path.clone(),
}
}
}
pub fn create_repo<TRepoTypes: RepoTypes>(_options: RepoOptions<TRepoTypes>) -> TRepoTypes::TRepo {
TRepoTypes::TRepo::new(TRepoTypes::TBlockStore::new(), TRepoTypes::TDataStore::new())
pub fn create_repo<TRepoTypes: RepoTypes>(options: RepoOptions<TRepoTypes>) -> TRepoTypes::TRepo {
TRepoTypes::TRepo::new(
TRepoTypes::TBlockStore::new(options.path.clone()),
TRepoTypes::TDataStore::new(options.path),
)
}
pub trait BlockStore: Clone + Send {
fn new() -> Self;
fn new(path: PathBuf) -> Self;
fn contains(&self, cid: &Cid) -> bool;
fn get(&self, cid: &Cid) -> Option<Block>;
fn put(&self, block: Block) -> Cid;
@ -39,7 +45,7 @@ pub trait BlockStore: Clone + Send {
}
pub trait DataStore: Clone + Send {
fn new() -> Self;
fn new(path: PathBuf) -> Self;
}
pub trait Repo<BS: BlockStore, DS: DataStore>: Clone + Send {