adt

package
v0.0.0-...-d492368 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2023 License: Apache-2.0, MIT Imports: 15 Imported by: 0

Documentation

Overview

Package adt provides amt array and map for contract to store something big, avoid to load big data most code move from https://github.com/filecoin-project/specs-actors/tree/master/actors/util/adt

Index

Constants

View Source
const BalanceTableBitwidth = 6

BalanceTableBitwidth bitwidth of balance table HAMTs, determined empirically from mutation patterns and projections of mainnet data

Variables

View Source
var DefaultAmtOptions = []amt.Option{}

DefaultAmtOptions default amt option

View Source
var DefaultHamtOptions = []hamt.Option{
	hamt.UseHashFunction(func(input []byte) []byte {
		res := sha256.Sum256(input)
		return res[:]
	}),
}

DefaultHamtOptions specifies default options used to construct Filecoin HAMTs. Specific HAMT instances may specify additional options, especially the bitwidth.

Functions

func StoreEmptyArray

func StoreEmptyArray(s Store, bitwidth int) (cid.Cid, error)

StoreEmptyArray writes a new empty array to the store, returning its CID.

func StoreEmptyMap

func StoreEmptyMap(s Store, bitwidth int) (cid.Cid, error)

StoreEmptyMap creates and stores a new empty map, returning its CID.

func StoreEmptyMultimap

func StoreEmptyMultimap(store Store, outerBitwidth, innerBitwidth int) (cid.Cid, error)

StoreEmptyMultimap creates and stores a new empty multimap, returning its CID.

Types

type Array

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

Array stores a sparse sequence of values in an AMT.

func AsArray

func AsArray(s Store, r cid.Cid, bitwidth int) (*Array, error)

AsArray interprets a store as an AMT-based array with root `r`.

func MakeEmptyArray

func MakeEmptyArray(s Store, bitwidth int) (*Array, error)

MakeEmptyArray creates a new array backed by an empty AMT.

func (*Array) AppendContinuous

func (a *Array) AppendContinuous(value cbor.Marshaler) error

AppendContinuous appends a value to the end of the array. Assumes continuous array. If the array isn't continuous use Set and a separate counter

func (*Array) BatchDelete

func (a *Array) BatchDelete(ix []uint64, strict bool) error

BatchDelete batch delete

func (*Array) Delete

func (a *Array) Delete(i uint64) error

Delete removes the value at index `i` from the AMT, expecting it to exist.

func (*Array) ForEach

func (a *Array) ForEach(out cbor.Unmarshaler, fn func(i int64) error) error

ForEach iterates all entries in the array, deserializing each value in turn into `out` and then calling a function. Iteration halts if the function returns an error. If the output parameter is nil, deserialization is skipped.

func (*Array) Get

func (a *Array) Get(k uint64, out cbor.Unmarshaler) (bool, error)

Get retrieves array element into the 'out' unmarshaler, returning a boolean

indicating whether the element was found in the array

func (*Array) Length

func (a *Array) Length() uint64

Length get element number in array

func (*Array) Pop

func (a *Array) Pop(k uint64, out cbor.Unmarshaler) (bool, error)

Pop retrieves an array value into the 'out' unmarshaler (if non-nil), and removes the entry. Returns a boolean indicating whether the element was previously in the array.

func (*Array) Root

func (a *Array) Root() (cid.Cid, error)

Root returns the root CID of the underlying AMT.

func (*Array) Set

func (a *Array) Set(i uint64, value cbor.Marshaler) error

Set set marshaler value to array

func (*Array) TryDelete

func (a *Array) TryDelete(i uint64) (bool, error)

TryDelete removes the value at index `i` from the AMT, if it exists. returns whether the index was previously present.

type BalanceTable

type BalanceTable Map

BalanceTable a specialization of a map of addresses to (positive) token amounts. Absent keys implicitly have a balance of zero.

func AsBalanceTable

func AsBalanceTable(s Store, r cid.Cid) (*BalanceTable, error)

AsBalanceTable interprets a store as balance table with root `r`.

func (*BalanceTable) Add

func (t *BalanceTable) Add(key addr.Address, value abi.TokenAmount) error

Add adds an amount to a balance, requiring the resulting balance to be non-negative.

func (*BalanceTable) Get

func (t *BalanceTable) Get(key addr.Address) (abi.TokenAmount, error)

Get gets the balance for a key, which is zero if they key has never been added to.

func (*BalanceTable) MustSubtract

func (t *BalanceTable) MustSubtract(key addr.Address, req abi.TokenAmount) error

MustSubtract subtracts the given amount from the account's balance. Returns an error if the account has insufficient balance

func (*BalanceTable) Root

func (t *BalanceTable) Root() (cid.Cid, error)

Root returns the root cid of underlying HAMT.

func (*BalanceTable) SubtractWithMinimum

func (t *BalanceTable) SubtractWithMinimum(key addr.Address, req abi.TokenAmount, floor abi.TokenAmount) (abi.TokenAmount, error)

SubtractWithMinimum subtracts up to the specified amount from a balance, without reducing the balance below some minimum. Returns the amount subtracted.

func (*BalanceTable) Total

func (t *BalanceTable) Total() (abi.TokenAmount, error)

Total returns the total balance held by this BalanceTable

type IpldStore

type IpldStore interface {
	Get(ctx context.Context, c cid.Cid, out interface{}) error
	Put(ctx context.Context, v interface{}) (cid.Cid, error)
}

IpldStore wraps a Blockstore and provides an interface for storing and retrieving CBOR encoded data. type definition from github.com/ipfs/go-ipld-cbor link https://github.com/ipfs/go-ipld-cbor/blob/0aba2b8dbb9aa1f9c335b50ea1c1ff1646239e7b/store.go#L17 go-ipld-cbor/github.com/polydawn/refmt library use reflect heavily, and tinygo unable to build it. so redefine the same interface here

type Map

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

Map stores key-value pairs in a HAMT.

func AsMap

func AsMap(s Store, root cid.Cid, bitwidth int) (*Map, error)

AsMap interprets a store as a HAMT-based map with root `r`. The HAMT is interpreted with branching factor 2^bitwidth. We could drop this parameter if https://github.com/filecoin-project/go-hamt-ipld/issues/79 is implemented.

func MakeEmptyMap

func MakeEmptyMap(s Store, bitwidth int) (*Map, error)

MakeEmptyMap creates a new map backed by an empty HAMT.

func (*Map) CollectKeys

func (m *Map) CollectKeys() (out []string, err error)

CollectKeys collects all the keys from the map into a slice of strings.

func (*Map) Delete

func (m *Map) Delete(k abi.Keyer) error

Delete removes the value at `k` from the hamt store, expecting it to exist.

func (*Map) ForEach

func (m *Map) ForEach(out cbor.Unmarshaler, fn func(key string) error) error

ForEach iterates all entries in the map, deserializing each value in turn into `out` and then calling a function with the corresponding key. Iteration halts if the function returns an error. If the output parameter is nil, deserialization is skipped.

func (*Map) Get

func (m *Map) Get(k abi.Keyer, out cbor.Unmarshaler) (bool, error)

Get retrieves the value at `k` into `out`, if the `k` is present and `out` is non-nil. Returns whether the key was found.

func (*Map) Has

func (m *Map) Has(k abi.Keyer) (bool, error)

Has checks for the existence of a key without deserializing its value.

func (*Map) IsEmpty

func (m *Map) IsEmpty() bool

IsEmpty check whether this map is empty

func (*Map) Pop

func (m *Map) Pop(k abi.Keyer, out cbor.Unmarshaler) (bool, error)

Pop retrieves the value for `k` into the 'out' unmarshaler (if non-nil), and removes the entry. Returns a boolean indicating whether the element was previously in the map.

func (*Map) Put

func (m *Map) Put(k abi.Keyer, v cbor.Marshaler) error

Put adds value `v` with key `k` to the hamt store.

func (*Map) PutIfAbsent

func (m *Map) PutIfAbsent(k abi.Keyer, v cbor.Marshaler) (bool, error)

PutIfAbsent sets key key `k` to value `v` iff the key is not already present.

func (*Map) Root

func (m *Map) Root() (cid.Cid, error)

Root returns the root cid of underlying HAMT.

func (*Map) TryDelete

func (m *Map) TryDelete(k abi.Keyer) (bool, error)

TryDelete removes the value at `k` from the hamt store, if it exists. Returns whether the key was previously present.

type Multimap

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

Multimap stores multiple values per key in a HAMT of AMTs. The order of insertion of values for each key is retained.

func AsMultimap

func AsMultimap(s Store, r cid.Cid, outerBitwidth, innerBitwidth int) (*Multimap, error)

AsMultimap interprets a store as a HAMT-based map of AMTs with root `r`. The outer map is interpreted with a branching factor of 2^bitwidth.

func MakeEmptyMultimap

func MakeEmptyMultimap(s Store, outerBitwidth, innerBitwidth int) (*Multimap, error)

MakeEmptyMultimap creates a new map backed by an empty HAMT and flushes it to the store. The outer map has a branching factor of 2^bitwidth.

func (*Multimap) Add

func (mm *Multimap) Add(key abi.Keyer, value cbor.Marshaler) error

Add adds a value for a key.

func (*Multimap) ForAll

func (mm *Multimap) ForAll(fn func(k string, arr *Array) error) error

ForAll iterate all entries in map

func (*Multimap) ForEach

func (mm *Multimap) ForEach(key abi.Keyer, out cbor.Unmarshaler, fn func(i int64) error) error

ForEach iterates all entries for a key in the order they were inserted, deserializing each value in turn into `out` and then calling a function. Iteration halts if the function returns an error. If the output parameter is nil, deserialization is skipped.

func (*Multimap) Get

func (mm *Multimap) Get(key abi.Keyer) (*Array, bool, error)

Get return entry for specify key

func (*Multimap) RemoveAll

func (mm *Multimap) RemoveAll(key abi.Keyer) error

RemoveAll removes all values for a key.

func (*Multimap) Root

func (mm *Multimap) Root() (cid.Cid, error)

Root returns the root cid of the underlying HAMT.

type Set

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

Set interprets a Map as a set, storing keys (with empty values) in a HAMT.

func AsSet

func AsSet(s Store, r cid.Cid, bitwidth int) (*Set, error)

AsSet interprets a store as a HAMT-based set with root `r`. The HAMT is interpreted with branching factor 2^bitwidth.

func MakeEmptySet

func MakeEmptySet(s Store, bitwidth int) (*Set, error)

MakeEmptySet creates a new HAMT with root `r` and store `s`. The HAMT has branching factor 2^bitwidth.

func (*Set) CollectKeys

func (h *Set) CollectKeys() (out []string, err error)

CollectKeys collects all the keys from the set into a slice of strings.

func (*Set) Delete

func (h *Set) Delete(k abi.Keyer) error

Delete removes `k` from the set, expecting it to be present.

func (*Set) ForEach

func (h *Set) ForEach(cb func(k string) error) error

ForEach iterates over all values in the set, calling the callback for each value. Returning error from the callback stops the iteration.

func (*Set) Has

func (h *Set) Has(k abi.Keyer) (bool, error)

Has returns true iff `k` is in the set.

func (*Set) Put

func (h *Set) Put(k abi.Keyer) error

Put adds `k` to the set.

func (*Set) Root

func (h *Set) Root() (cid.Cid, error)

Root return the root cid of HAMT.

func (*Set) TryDelete

func (h *Set) TryDelete(k abi.Keyer) (bool, error)

TryDelete removes `k` from the set, if present. Returns whether the key was previously present.

type Store

type Store interface {
	Context() context.Context
	IpldStore
}

Store define store with context

func AdtStore

func AdtStore(ctx context.Context) Store

AdtStore Adapts a vanilla IPLD store as an ADT store.

Jump to

Keyboard shortcuts

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