nim-codex/ipfs/repo.nim

28 lines
512 B
Nim
Raw Normal View History

import std/options
import std/tables
import std/hashes
2021-01-14 12:41:54 +00:00
import pkg/libp2p
import ./ipfsobject
export options
export ipfsobject
type
Repo* = ref object
storage: Table[Cid, IpfsObject]
2021-01-14 12:41:54 +00:00
proc hash(id: Cid): Hash =
hash($id)
proc store*(repo: Repo, obj: IpfsObject) =
repo.storage[obj.cid] = obj
2021-01-14 12:41:54 +00:00
proc contains*(repo: Repo, id: Cid): bool =
repo.storage.hasKey(id)
proc retrieve*(repo: Repo, id: Cid): Option[IpfsObject] =
if repo.contains(id):
repo.storage[id].some
else:
IpfsObject.none