Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Generalizing data store interface #39

Closed
Gozala opened this issue May 26, 2020 · 2 comments
Closed

Generalizing data store interface #39

Gozala opened this issue May 26, 2020 · 2 comments
Labels
need/triage Needs initial labeling and prioritization

Comments

@Gozala
Copy link
Contributor

Gozala commented May 26, 2020

In an attempt to untangle generalized datastore interface discussion from query interface in #9, I'm forking former here.

I was trying to make an argument in favor of generalized DataStore interface that concrete data stores could be specialized implementations of. More concretely I was proposing following:

export interface Store<K, V, E> {
  has(key:K, options?:StoreOptions):Promise<boolean>
  put(key:K, value:V, options?:StoreOptions):Promise<void>
  putMany(source:Many<E>, options?:StoreOptions):AsyncIterable<E>
  get(key:K, options?:StoreOptions):Promise<V>
  getMany(source:Many<K>, options?:StoreOptions):AsyncIterable<V>
  delete(key:K, options?:StoreOptions):Promise<void>
  deleteMany(source:Many<K>, options?:StoreOptions):AsyncIterable<K>
  query(query:Query<E>, options?:StoreOptions):AsyncIterable<E>
  batch():StoreBatch<K, V>

  open():Promise<void>
  close():Promise<void>
}

type Many<T> =
  | Iterable<T>
  | AsyncIterable<T>

type StoreOptions = {
  timeout?:number
  signal?: AbortSignal
}

interface Query<E> {
  prefix?:string,
  filters?:Array<(input:E => boolean>,
  orders?:Array<(input:E[]) => E[]>,
  limit?:number,
  offset?:number,

  timeout?:number,
  signal?:AbortSignal
}

interface StoreBatch<K, V> {
  put(key:K, value:V, options?:StoreOptions):void,
  delete(key:K, options?:StoreOptions):void,
  commit():Promise<void>
}

This would allow individual stores in the system to be concrete implementations of generic interface:

import CID from "cids"
import Block from 'ipld-block'
class BlockStore implements Store <CID, Block, Block> {
   // ...
}

Note: That Block already encapsulates key and value so value and entry end up being same type.

class DataStore implements Store<Key, Buffer, [Key, Buffer]> {
  // ...
}

Note some alterations had being made to query API but they are not essential to this, just make more sense IMO.

@Gozala Gozala added the need/triage Needs initial labeling and prioritization label May 26, 2020
@Gozala
Copy link
Contributor Author

Gozala commented May 26, 2020

I should point out that this is also interesting to me given Rust IPFS effort where generalized interfaces are idiomatic.

/cc @vmx @dvc94ch

@achingbrain
Copy link
Member

Implemented in #91 - interface-store became the generic key/value store type, interface-datastore implements Store<Key, Uint8Array>, interface-blockstore implements Store<CID, Uint8Array> - so far there's been no need for the Block type & I'm hoping we can live without it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
need/triage Needs initial labeling and prioritization
Projects
None yet
Development

No branches or pull requests

2 participants