hamtcontainer

package module
v0.0.0-...-f21e4be Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2021 License: MIT Imports: 18 Imported by: 1

README

go-ipld-adl-hamt-container

go-ipld-adl-hamt-container is a wrapper around go-ipld-adl-hamt project

Creating HAMT container

package main

import (
	"fmt"

	hamtcontainer "github.com/simplecoincom/go-ipld-adl-hamt-container"
	"github.com/simplecoincom/go-ipld-adl-hamt-container/storage"
)

func main() {
	// Create the root HAMT container
	rootHAMT, err := hamtcontainer.NewHAMTBuilder(
		hamtcontainer.WithKey([]byte("root"),
	).Build()
	if err != nil {
		panic(err)
	}

	// Let's add a key
	err = rootHAMT.MustBuild(func(hamtSetter hamtcontainer.HAMTSetter) error {
		return hamtSetter.Set([]byte("foo"), "bar")
	})
	if err != nil {
		panic(err)
	}

	// Retrieve the value with key
	val, err := rootHAMT.GetAsString([]byte("foo"))
	if err != nil {
		panic(err)
	}

	fmt.Println(val) // bar
}

Linking container with Redis

import (
	"fmt"

	hamtcontainer "github.com/simplecoincom/go-ipld-adl-hamt-container"
	"github.com/simplecoincom/go-ipld-adl-hamt-container/storage"
)

func main() {
	// Linking the data into a Redis storage
	store := storage.NewRedisStorage("localhost:6379", "")

	// Create the root HAMT container
	rootHAMT, err := hamtcontainer.NewHAMTBuilder(
		hamtcontainer.WithKey([]byte("root")),
		hamtcontainer.WithStorage(store),
	).Build()
	if err != nil {
		panic(err)
	}

	// Let's add a key
	err = rootHAMT.MustBuild(func(hamtSetter hamtcontainer.HAMTSetter) error {
		return hamtSetter.Set([]byte("foo"), "bar")
	})
	if err != nil {
		panic(err)
	}

	// Retrieve the value with key
	val, err := rootHAMT.GetAsString([]byte("foo"))
	if err != nil {
		panic(err)
	}

	fmt.Println(val) // bar
}

Nested HAMT containers

import (
	"fmt"

	hamtcontainer "github.com/simplecoincom/go-ipld-adl-hamt-container"
	"github.com/simplecoincom/go-ipld-adl-hamt-container/storage"
)


func main() {
	store := storage.NewMemoryStorage()

	// Create the child HAMT container
	childHAMT, err := hamtcontainer.NewHAMTBuilder(
		hamtcontainer.WithKey([]byte("child")),
		hamtcontainer.WithStorage(store),
	).Build()
	if err != nil {
		panic(err)
	}

	// Let's add a key
	err = childHAMT.MustBuild(func(hamtSetter hamtcontainer.HAMTSetter) error {
		return hamtSetter.Set([]byte("foo"), "zar")
	})
	if err != nil {
		panic(err)
	}

	// Create the root HAMT container
	parentHAMT, err := hamtcontainer.NewHAMTBuilder(
		hamtcontainer.WithKey([]byte("parent")),
		hamtcontainer.WithStorage(store),
	).Build()
	if err != nil {
		panic(err)
	}

	// Adds the child container to root
	err = parentHAMT.MustBuild(func(hamtSetter hamtcontainer.HAMTSetter) error {
		return hamtSetter.Set([]byte("child"), childHAMT)
	})
	if err != nil {
		panic(err)
	}

	// Load child HAMT container
	newChildHAMT, err := hamtcontainer.NewHAMTBuilder(
		hamtcontainer.WithKey([]byte("child")),
		hamtcontainer.WithHAMTContainer(parentHAMT),
	).Build()
	if err != nil {
		panic(err)
	}

	// Retrieve the value with key
	val, err := newChildHAMT.GetAsString([]byte("foo"))
	if err != nil {
		panic(err)
	}

	fmt.Println(val) // zar
}

Store and Load from IPFS

import (
	"fmt"

	ipfsApi "github.com/ipfs/go-ipfs-api"
	hamtcontainer "github.com/simplecoincom/go-ipld-adl-hamt-container"
	"github.com/simplecoincom/go-ipld-adl-hamt-container/storage"
)

func main() {
	store := storage.NewIPFSStorage(ipfsApi.NewShell("http://localhost:5001"))

	// Create the first HAMT
	rootHAMT, err := hamtcontainer.NewHAMTBuilder(
		hamtcontainer.WithKey([]byte("root")),
		hamtcontainer.WithStorage(store),
	).Build()
	if err != nil {
		panic(err)
	}

	// Set some k/v
	err = rootHAMT.MustBuild(func(hamtSetter hamtcontainer.HAMTSetter) error {
		return hamtSetter.Set([]byte("foo"), []byte("bar"))
	})
	if err != nil {
		panic(err)
	}

	// Get HAMT link
	lnk, err := rootHAMT.GetLink()
	if err != nil {
		panic(err)
	}

	// Load HAMT from link
	newHC, err := hamtcontainer.NewHAMTBuilder(
		hamtcontainer.WithKey([]byte("root")),
		hamtcontainer.WithStorage(store),
		hamtcontainer.WithLink(lnk)
	).Build()
	if err != nil {
		panic(err)
	}

	// Get node value as string
	val2, err := newHC.GetAsString([]byte("foo"))
	if err != nil {
		panic(err)
	}

	fmt.Println(val2) // bar
}

Generate a .car file

import (
	"fmt"

	hamtcontainer "github.com/simplecoincom/go-ipld-adl-hamt-container"
	"github.com/simplecoincom/go-ipld-adl-hamt-container/storage"
)

func main() {
		// Linking the data into a Redis storage
	store := storage.NewRedisStorage("localhost:6379", "")

	// Create the root HAMT container
	rootHAMT, err := hamtcontainer.NewHAMTBuilder(
		hamtcontainer.WithKey([]byte("root")),
		hamtcontainer.WithStorage(store),
	).Build()
	if err != nil {
		panic(err)
	}

	// Let's add a key
	err = rootHAMT.MustBuild(func(hamtSetter hamtcontainer.HAMTSetter) error {
		return hamtSetter.Set([]byte("foo"), "bar")
	})
	if err != nil {
		panic(err)
	}

	f, err := os.Create("/tmp/files.car")

	err = rootHAMT.WriteCar(f)
	if err != nil {
		panic(err)
	}
}

Then you can run ipfs dag import /tmp/file.car to import the dag to the IPFS Node

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrHAMTNotBuild                  = errors.New("HAMT not ready, build first")
	ErrHAMTValueNotFound             = errors.New("Value not found at HAMT")
	ErrHAMTNoNestedFound             = errors.New("No nested found with the given key")
	ErrHAMTFailedToLoadNested        = errors.New("Failed to load link from nested HAMT")
	ErrHAMTUnsupportedValueType      = errors.New("Unsupported value")
	ErrHAMTUnsupportedCacheValueType = errors.New("Unsupported cache value")
	ErrHAMTFailedToGetAsLink         = errors.New("Value returned should be ipld.Link")
	ErrHAMTFailedToGetAsBytes        = errors.New("Value returned should be Bytes")
	ErrHAMTFailedToGetAsString       = errors.New("Value returned should be String")

	BitWidth   = 8
	BucketSize = 1024
)
View Source
var ErrCantUseParentAndLink = errors.New("Cannot use Parant and Link in the same build")
View Source
var ErrCantUseStorageAndNested = errors.New("Cannot use Storage and FromNested in the same build")

Functions

This section is empty.

Types

type AssemblerFunc

type AssemblerFunc func(hamtSetter HAMTSetter) error

AssemblerFunc function used to set assembler routines

type HAMTBuilder

type HAMTBuilder struct {
	// contains filtered or unexported fields
}

func NewHAMTBuilder

func NewHAMTBuilder(options ...Option) *HAMTBuilder

NewHAMTBuilder create a new HAMTBuilder helper

func (HAMTBuilder) Build

func (hb HAMTBuilder) Build() (*HAMTContainer, error)

Build creates the HAMT Container based on the params from HAMTBuilder

type HAMTContainer

type HAMTContainer struct {
	// contains filtered or unexported fields
}

func (*HAMTContainer) CID

func (hc *HAMTContainer) CID() (cid.Cid, error)

CID will return the cid.Cid for the ipld.Node Or it will return an error if the ipld.Node for the HAMT isn't built

func (*HAMTContainer) Get

func (hc *HAMTContainer) Get(key []byte) (interface{}, error)

Get will return the value by the key It will return error if the hamt not build or if the value not found

func (*HAMTContainer) GetAsBytes

func (hc *HAMTContainer) GetAsBytes(key []byte) ([]byte, error)

GetAsBytes returns a byte slice type by key The method will fail if the returned type isn't of type byte slice

func (hc *HAMTContainer) GetAsLink(key []byte) (ipld.Link, error)

GetAsLink returns a ipld.Link type by key The method will fail if the returned type isn't of type ipld.Link

func (*HAMTContainer) GetAsString

func (hc *HAMTContainer) GetAsString(key []byte) (string, error)

GetAsString returns a string type by key The method will fail if the returned type isn't of type string or failed to convert to string

func (hc *HAMTContainer) GetLink() (ipld.Link, error)

GetLink will return the ipld.Link for the ipld.Node Or it will return an error if the ipld.Node for the HAMT isn't built

func (*HAMTContainer) Key

func (hc *HAMTContainer) Key() []byte

Key returns the key that identifies the HAMT

func (hc *HAMTContainer) LoadLink(link ipld.Link) error

LoadLink will load the storage data from a new HAMTContainer Or it illl return and error if the load failed

func (*HAMTContainer) MustBuild

func (hc *HAMTContainer) MustBuild(assemblyFuncs ...AssemblerFunc) error

MustBuild is used to build the key maps It'll generate the final version of the node with the link

func (*HAMTContainer) Set

func (hc *HAMTContainer) Set(key []byte, value interface{})

Set adds k/v to the hamt but not imediately and only when build

func (*HAMTContainer) Storage

func (hc *HAMTContainer) Storage() storage.Storage

Storage returns the linking storage used by the HAMT

func (*HAMTContainer) View

func (hc *HAMTContainer) View(iterFunc func(key []byte, value interface{}) error) error

View will iterate over each item key map

func (*HAMTContainer) WriteCar

func (hc *HAMTContainer) WriteCar(writer io.Writer) error

WriteCar creates the car file

type HAMTSetter

type HAMTSetter struct {
	// contains filtered or unexported fields
}

HAMTSetter is a helper structure for set HAMT key values

func (*HAMTSetter) Set

func (hs *HAMTSetter) Set(key []byte, value interface{}) error

Set adds a new k/v content for the HAMT For string values it will add k/v pair of strings For ipld.Link values it will add string key and a link for another HAMT structure as value

type Option

type Option func(*HAMTBuilder)

func WithHAMTContainer

func WithHAMTContainer(hamtContainer *HAMTContainer) Option

WithHAMTContainer sets the parent container to load from the future HAMTContainer

func WithKey

func WithKey(key []byte) Option

WithKey sets the key for the future HAMTContainer

func WithLink(link ipld.Link) Option

WithLink sets the link for the future HAMTContainer

func WithStorage

func WithStorage(storage storage.Storage) Option

WithStorage sets the storage for the future HAMTContainer

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL