Giter Site home page Giter Site logo

tchardin / amt-ipld Goto Github PK

View Code? Open in Web Editor NEW

This project forked from eifil/amt-ipld

0.0 1.0 0.0 1.08 MB

Array Mapped Trie (Persistent Vector) implementation using IPLD.

Home Page: https://npm.im/@eifil/amt-ipld

License: MIT License

TypeScript 100.00%

amt-ipld's Introduction

amt-ipld

CI codecov dependencies Status

Array Mapped Trie (Persistent Vector) implementation using IPLD.

Aims to be conformant with github.com/filecoin-project/go-amt-ipld.

Install

npm install @eifil/amt-ipld

Usage

The AMT requires a store that complies with IpldStore.

Create a new empty AMT

import { Root as AMT } from `@eifil/amt-ipld`
const amt = new AMT(store, { bitWidth: 8 })

Load an existing AMT

import { Root as AMT } from `@eifil/amt-ipld`
const amt = await AMT.load(store, rootCID, { bitWidth: 8 })

Set and get values

import { Root as AMT } from '@eifil/amt-ipld'

type Fruit = { name: string }

const fruits = new AMT<Fruit>(store, { bitWidth: 8 })

await fruits.set(0n, { name: 'apple' })
await fruits.set(1n, { name: 'orange' })
await fruits.set(3n, { name: 'pear' })

console.log(fruits.size) // 3n

const f0 = await fruits.get(0n)
const f1 = await fruits.get(1n)
const f2 = await fruits.get(2n)
const f3 = await fruits.get(3n)

console.log({ f0, f1, f2, f3 })
// {
//   f0: { name: 'apple' },
//   f1: { name: 'orange' },
//   f2: undefined,
//   f3: { name: 'pear' }
// }

for await (const [index, value] of fruits.entries()) {
  console.log({ index, value })
}
// { index: 0n, value: { name: 'apple' } }
// { index: 1n, value: { name: 'orange' } }
// { index: 3n, value: { name: 'pear' } }

// now flush unsaved data to the store and return the new root CID
const rootCID = await fruits.flush()
console.log(rootCID)
// CID(bafyreigvhzij2lv5oex4rbfo4obm63re6x4ndlzoctfmisollrzw2lhvlm)

Custom CBOR encoding/decoding

Add an encodeCBOR method to your values, and pass a decoder to the options (an object with a decodeCBOR function) to enable a custom encoding format for your values.

import { Root as AMT } from '@eifil/amt-ipld'

class Fruit {
  name: string
  constructor (name: string) {
    this.name = name
  }
  encodeCBOR () {
    return [this.name] // encode as a compact array
  }
  static decodeCBOR (obj: any) {
    return new Fruit(obj[0]) // re-hydrate a Fruit instance from array format
  }
}

const fruits = new AMT<Fruit>(store, { bitWidth: 8, decoder: Fruit })

await fruits.set(0n, new Fruit('apple'))
await fruits.flush()

const f0 = await fruits.get(0n)
console.log(f0) // Fruit { name: 'apple' }

Contribute

Feel free to dive in! Open an issue or submit PRs.

License

This project is dual-licensed under Apache 2.0 and MIT terms:

amt-ipld's People

Contributors

alanshaw avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.