nim-codex/dagger/stores/memorystore.nim
Dmitriy Ryajov 2fb39ca4a3
Rename bitswap (#25)
* use PeerInfo in event handlers

* use CidV1 and raw multicodec as default

* add block stream abstraction

* raises defect

* adding dataset abstraction

* move blockstream into own dir

* reorg files and fix tests

* rename dataset to blockset

* wip

* wip

* adding basic test for treehash algo

* run blockset tests along with with the rest

* remove obsolete contents

* fix chunker tests

* rename bitswap and move to stores

* rename bitwsap to blockexc and move to stores

* moare project structure reorg
2021-08-30 13:25:20 -06:00

61 lines
1.4 KiB
Nim

## Nim-Dagger
## Copyright (c) 2021 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
import std/sequtils
import pkg/chronos
import pkg/libp2p
import ../stores/blockstore
import ../blocktype
export blockstore
type
MemoryStore* = ref object of BlockStore
blocks: seq[Block] # TODO: Should be an LRU cache
method getBlocks*(
s: MemoryStore,
cids: seq[Cid]): Future[seq[Block]] {.async.} =
## Get a block from the stores
##
var res: seq[Block]
for c in cids:
res.add(s.blocks.filterIt( it.cid == c ))
return res
method hasBlock*(s: MemoryStore, cid: Cid): bool =
## check if the block exists
##
s.blocks.filterIt( it.cid == cid ).len > 0
method putBlocks*(s: MemoryStore, blocks: seq[Block]) =
## Put a block to the blockstore
##
s.blocks.add(blocks)
procCall BlockStore(s).putBlocks(blocks)
method delBlocks*(s: MemoryStore, cids: seq[Cid]) =
## delete a block/s from the block store
##
for c in cids:
s.blocks.keepItIf( it.cid != c )
procCall BlockStore(s).delBlocks(cids)
func new*(T: type MemoryStore, blocks: openArray[Block] = []): MemoryStore =
MemoryStore(
blocks: @blocks
)