~/bend-docscommunity

storage.bend relies on unsafe/foreign

raw source on the hub · import bend-kit-storage@0.1.0.0/storage.bend as Storage

Content-addressed object store: Merkle trees, sync plans, and reachability GC with proved laws. Source: https://github.com/paymog/bend-kit/tree/main/storage

4 imports
import Base
import bend-kit-bytes@0.3.3.0/bytes.bend as Bytes
import bend-kit-files@0.3.1.0/files.bend as Files
import bend-kit-crypto@0.2.2.2/crypto.bend as Crypto

Types

type Tree source · line 8 · raw

@-B:Data -> Data

The content: a file, or a directory as a chain of named entries that ends in End.

type Obj source · line 14 · raw

@-D:Data -> @-B:Data -> Data

One stored object. An OItem names its child and the rest of its directory by digest.

type Store source · line 21 · raw

@-D:Data -> @-B:Data -> Data

Objects by digest. get answers the first entry whose digest eq accepts. ponytail: a list, so get and put are O(n); index by digest when stores pass a few thousand objects.

type Blob source · line 283 · raw

Data

Octets packed four to a U32, low byte first, as Bytes packs them. Words past len are 0.

type Step source · line 722 · raw

Data

Definitions

def Store.new source · line 25 · raw

@-D:Data -> @-B:Data -> Store<D, B>

def Store.put.if source · line 52 · raw

@-D:Data -> @-B:Data -> @found:Bool -> @s:Store<D, B> -> @d:D -> @o:Obj<D, B> -> Store<D, B>

def Store.size source · line 71 · raw

@-D:Data -> @-B:Data -> @s:Store<D, B> -> U32

def Store.cat source · line 79 · raw

@-D:Data -> @-B:Data -> @xs:Store<D, B> -> @ys:Store<D, B> -> Store<D, B>

xs, then ys.

def Tree.item source · line 87 · raw

@-B:Data -> @n:String -> @c:Maybe<&2, Tree<B>> -> @r:Maybe<&2, Tree<B>> -> Maybe<&2, Tree<B>>

Some only when both halves loaded.

def closure.item source · line 114 · raw

@-D:Data -> @-B:Data -> @d:D -> @o:Obj<D, B> -> @c:Maybe<&2, Store<D, B>> -> @r:Maybe<&2, Store<D, B>> -> Maybe<&2, Store<D, B>>

def live.cat source · line 176 · raw

@-D:Data -> @-B:Data -> @a:Maybe<&2, Store<D, B>> -> @b:Maybe<&2, Store<D, B>> -> Maybe<&2, Store<D, B>>

def keep.if source · line 201 · raw

@-D:Data -> @-B:Data -> @live:Bool -> @k:D -> @o:Obj<D, B> -> @r:Store<D, B> -> Store<D, B>

def admit.if source · line 227 · raw

@-D:Data -> @-B:Data -> @ok:Bool -> @s:Store<D, B> -> @keep:Store<D, B> -> Maybe<&2, Store<D, B>>

def fits source · line 261 · raw

@-B:Data -> @fuel:Nat -> @t:Tree<B> -> Bool

True when load with this fuel reaches every node of t.

def Blob.read source · line 287 · raw

@n:Nat -> @r:Pair(Array<U32>, U32) -> @+i:U32 -> @acc:List<&2, U32> -> List<&2, U32>

r is the array beside the word read at i - 1.

def Blob.of source · line 295 · raw

@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Blob

def Blob.write source · line 299 · raw

@ws:List<&2, U32> -> @a:Array<U32> -> @+i:U32 -> Array<U32>

def Blob.bytes source · line 306 · raw

@x:Blob -> 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes

def Blob.string source · line 310 · raw

@+s:String -> Blob

def limit source · line 314 · raw

Nat

Loads and collections give up past this many levels. Each directory entry is one level.

def char.ok source · line 317 · raw

@+c:U32 -> Bool

def chars.ok source · line 320 · raw

@s:String -> Bool

def name.ok source · line 328 · raw

@+n:String -> Bool

A directory entry name: one to 255 octets, not "." or "..", with no NUL, newline, or "/".

def hex.ok source · line 333 · raw

@s:String -> Bool

def digest.ok source · line 341 · raw

@+d:String -> Bool

64 lowercase hex characters.

def encode source · line 345 · raw

@o:Obj<String, Blob> -> 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes

The canonical encoding: "file <len>\n" and the octets, "end\n", or "item <child> <rest> <name>\n".

def decode.when source · line 355 · raw

@ok:Bool -> @o:Obj<String, Blob> -> Maybe<&2, Obj<String, Blob>>

def decode.item source · line 363 · raw

@+rest:String -> Maybe<&2, Obj<String, Blob>>

rest is "<child> <rest> <name>".

def decode.file source · line 371 · raw

@m:Maybe<&2, U32> -> @+digits:String -> @+at:U32 -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Maybe<&2, Obj<String, Blob>>

def decode.head source · line 380 · raw

@+head:String -> @+at:U32 -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Maybe<&2, Obj<String, Blob>>

def decode.cut source · line 389 · raw

@+at:U32 -> @r:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes) -> Maybe<&2, Obj<String, Blob>>

def decode.at source · line 393 · raw

@r:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, Maybe<&2, U32>) -> Maybe<&2, Obj<String, Blob>>

def decode source · line 401 · raw

@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Maybe<&2, Obj<String, Blob>>

The object that encode wrote, or None for any other octets.

def Run source · line 406 · raw

@-A:Type -> Type

def Run.pure source · line 409 · raw

@-A:Type -> @x:A -> Run(A)

def Run.fail source · line 412 · raw

@-A:Type -> @e:Pair(U32, String) -> Run(A)

def Run.next source · line 415 · raw

@-A:Type -> @-B:Type -> @r:Result<&1, &1, Pair(U32, String), A> -> @f:(@_:A -> Run(B)) -> Run(B)

def Run.bind source · line 422 · raw

@-A:Type -> @-B:Type -> @m:Run(A) -> @f:(@_:A -> Run(B)) -> Run(B)

def Run.lift source · line 425 · raw

@-A:Type -> @m:IO(A) -> Run(A)

def Run.check source · line 428 · raw

@ok:Bool -> @+why:String -> Run(Unit)

def Run.some source · line 435 · raw

@-A:Data -> @m:Maybe<&2, A> -> @+why:String -> Run(A)

def digest.hex source · line 442 · raw

@r:Pair(U32, Array<U32>) -> String

def digest source · line 447 · raw

@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(String)

The SHA-256 of b, as 64 lowercase hex characters.

def commit.done source · line 453 · raw

@+d:String -> @o:Obj<String, Blob> -> @xs:Store<String, Blob> -> Run(Pair(String, Store<String, Blob>))

def commit.node source · line 456 · raw

@+o:Obj<String, Blob> -> @xs:Store<String, Blob> -> Run(Pair(String, Store<String, Blob>))

def commit.item source · line 461 · raw

@+n:String -> @c:Pair(String, Store<String, Blob>) -> @r:Pair(String, Store<String, Blob>) -> Run(Pair(String, Store<String, Blob>))

def commit source · line 469 · raw

@t:Tree<Blob> -> Run(Pair(String, Store<String, Blob>))

hash and objects with H = SHA-256 of encode: the root digest and the objects, children first.

def receive.two source · line 481 · raw

@+s:Store<String, Blob> -> @+claimed:String -> @bb:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes) -> Run(Store<String, Blob>)

def receive source · line 489 · raw

@+s:Store<String, Blob> -> @+claimed:String -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(Store<String, Blob>)

Ingress: the store with the object that b encodes. Fails when b does not decode or its SHA-256 is not claimed.

def Disk.object source · line 494 · raw

@+dir:String -> @+d:String -> String

def Disk.init source · line 497 · raw

@+dir:String -> Run(Unit)

def write.close source · line 503 · raw

@r:Pair(File, Result<&1, &1, Pair(U32, String), Unit>) -> Run(Unit)

def write.file source · line 510 · raw

@+path:String -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(Unit)

def write source · line 517 · raw

@+dir:String -> @+name:String -> @+path:String -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(Unit)

Writes b to path through dir/tmp and a rename, so a reader sees all of b or nothing.

def read.chunk source · line 523 · raw

@r:Pair(File, Result<&1, &1, Pair(U32, String), 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes>) -> Run(Pair(File, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes))

def read.piece source · line 532 · raw

@f:File -> Run(Pair(File, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes))

def read.go source · line 536 · raw

@n:Nat -> @fb:Pair(File, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes) -> @acc:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes)

n more reads of up to 1 MiB each; fb holds the file and the last piece.

def read.check source · line 547 · raw

@+size:U32 -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes)

def info.size source · line 553 · raw

@i:0xc022f29d7abb4bf4d2f497d0b5159f5a/files.Info -> U32

def read.sized source · line 557 · raw

@+path:String -> @+size:U32 -> Run(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes)

def read source · line 564 · raw

@+path:String -> Run(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes)

The whole file at path.

def Disk.put.if source · line 570 · raw

@there:Bool -> @+dir:String -> @+d:String -> @+path:String -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(Unit)

def Disk.put source · line 578 · raw

@+dir:String -> @+d:String -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(Unit)

Writes the encoded object under its digest unless the file exists. Objects never change.

def Disk.save source · line 584 · raw

@+dir:String -> @xs:Store<String, Blob> -> Run(Unit)

def Disk.receive.two source · line 594 · raw

@+dir:String -> @+s:Store<String, Blob> -> @+claimed:String -> @bb:Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes) -> Run(Store<String, Blob>)

Ingress into the store at dir: receive, then write the octets under their digest.

def Disk.receive source · line 601 · raw

@+dir:String -> @+s:Store<String, Blob> -> @+claimed:String -> @b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Run(Store<String, Blob>)

def Disk.load.go source · line 604 · raw

@+dir:String -> @names:List<&2, String> -> @+s:Store<String, Blob> -> Run(Store<String, Blob>)

def Disk.load source · line 615 · raw

@+dir:String -> Run(Store<String, Blob>)

Every object at dir, each checked against its file name. A tampered or undecodable object fails the load.

def Disk.set_root source · line 620 · raw

@+dir:String -> @+name:String -> @+d:String -> Run(Unit)

def Disk.root.of source · line 625 · raw

@+name:String -> @+d:String -> Run(String)

def Disk.root source · line 630 · raw

@+dir:String -> @+name:String -> Run(String)

def Disk.roots.go source · line 635 · raw

@+dir:String -> @names:List<&2, String> -> Run(List<&2, String>)

def Disk.roots source · line 646 · raw

@+dir:String -> Run(List<&2, String>)

The digest of every named root.

def Disk.drop.one source · line 651 · raw

@live:Bool -> @+dir:String -> @+d:String -> Run(U32)

def Disk.drop source · line 660 · raw

@+dir:String -> @s:Store<String, Blob> -> @+g:Store<String, Blob> -> @+n:U32 -> Run(U32)

def Disk.collect.of source · line 669 · raw

@+dir:String -> @+retained:List<&2, String> -> @+s:Store<String, Blob> -> Run(U32)

def Disk.collect source · line 676 · raw

@+dir:String -> @+retained:List<&2, String> -> Run(U32)

Deletes every object that no retained root reaches and answers how many it deleted. Fails, and deletes nothing, when a retained root does not load. No writer may run during the collection.

def sync.send source · line 681 · raw

@+dir:String -> @ys:Store<String, Blob> -> @+dst:Store<String, Blob> -> @+n:U32 -> Run(U32)

def sync.of source · line 690 · raw

@+dst:String -> @+root:String -> @a:Store<String, Blob> -> @+b:Store<String, Blob> -> Run(U32)

def sync source · line 696 · raw

@+src:String -> @+dst:String -> @+root:String -> Run(U32)

Sends dst every object it needs to load root as src does, through ingress. Answers how many it sent.

def Disk.checkout source · line 703 · raw

@+dir:String -> @+root:String -> Run(Tree<Blob>)

The tree under root at dir. Fails when an object is missing or tampered, or the tree is too deep.

def Disk.commit.save source · line 708 · raw

@+dir:String -> @r:Pair(String, Store<String, Blob>) -> Run(String)

def Disk.commit source · line 715 · raw

@+dir:String -> @t:Tree<Blob> -> Run(String)

Commits t to the store at dir and answers its root digest.

def snap source · line 728 · raw

@fuel:Nat -> @+path:String -> @step:Step -> Run(Tree<Blob>)

The tree at path. Regular files become TFile and directories become TItem chains in name order.

def snapshot source · line 749 · raw

@+path:String -> Run(Tree<Blob>)

def restore.go source · line 753 · raw

@t:Tree<Blob> -> @+path:String -> @chain:Bool -> Run(Unit)

Writes t at path. chain is True when t is the rest of the listing of directory path.

def restore source · line 776 · raw

@t:Tree<Blob> -> @+path:String -> Run(Unit)

Writes t as a new file or directory at path. Fails if path exists.

Templates

template Store.head source · line 29 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @s:Store<D, B> -> @+d:D -> Bool

eq(d, first key), or False when s is empty.

template Store.get.go source · line 37 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @s:Store<D, B> -> @+d:D -> @hit:Bool -> Maybe<&2, Obj<D, B>>

hit is Store.head(s, d), so the walk stops at the first match.

template Store.get source · line 46 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @+s:Store<D, B> -> @+d:D -> Maybe<&2, Obj<D, B>>

template Store.has source · line 49 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @+s:Store<D, B> -> @+d:D -> Bool

template Store.put source · line 60 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @+s:Store<D, B> -> @+d:D -> @o:Obj<D, B> -> Store<D, B>

Adds o under d unless d is present. The first object under a digest stays.

template Store.apply source · line 64 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @xs:Store<D, B> -> @+s:Store<D, B> -> Store<D, B>

Puts each (digest, object) in order.

template load.go source · line 97 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @fuel:Nat -> @+s:Store<D, B> -> @m:Maybe<&2, Obj<D, B>> -> Maybe<&2, Tree<B>>

m is the object under the digest being loaded.

template load source · line 111 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @fuel:Nat -> @+s:Store<D, B> -> @+d:D -> Maybe<&2, Tree<B>>

The tree under d, or None when an object is missing or the tree is deeper than fuel.

template closure.go source · line 123 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @fuel:Nat -> @+s:Store<D, B> -> @+d:D -> @m:Maybe<&2, Obj<D, B>> -> Maybe<&2, Store<D, B>>

template closure source · line 139 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @fuel:Nat -> @+s:Store<D, B> -> @+d:D -> Maybe<&2, Store<D, B>>

Every object that load(fuel, s, d) reads, parents first, or None when load fails.

template need.head source · line 143 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @+t:Store<D, B> -> @xs:Store<D, B> -> Bool

Store.has(t, first digest of xs), or False when xs is empty.

template need.go source · line 151 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @xs:Store<D, B> -> @+t:Store<D, B> -> @found:Bool -> Store<D, B>

found is need.head(t, xs), so only the taken branch recurses.

template need source · line 162 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @+xs:Store<D, B> -> @+t:Store<D, B> -> Store<D, B>

The entries of xs whose digest t lacks, each digest once. t grows as entries are taken.

template plan.of source · line 165 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @+dst:Store<D, B> -> @m:Maybe<&2, Store<D, B>> -> Maybe<&2, Store<D, B>>

template plan source · line 173 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @fuel:Nat -> @+src:Store<D, B> -> @+dst:Store<D, B> -> @+root:D -> Maybe<&2, Store<D, B>>

The objects dst needs to load root as src does, or None when src cannot load root.

template live source · line 186 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @+fuel:Nat -> @+s:Store<D, B> -> @roots:List<&2, D> -> Maybe<&2, Store<D, B>>

The closures of every root, or None when one root does not load.

template mem source · line 194 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @ls:Store<D, B> -> @+d:D -> Bool

eq(d, k) for some entry (k, o) of ls.

template keep source · line 209 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @s:Store<D, B> -> @+ls:Store<D, B> -> Store<D, B>

The entries of s whose digest is in ls.

template gc.of source · line 216 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @s:Store<D, B> -> @m:Maybe<&2, Store<D, B>> -> Maybe<&2, Store<D, B>>

template gc source · line 224 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @fuel:Nat -> @+s:Store<D, B> -> @+roots:List<&2, D> -> Maybe<&2, Store<D, B>>

s without the objects that no retained root reaches, or None when a root does not load.

template admit source · line 235 · raw

@-D:Data -> @-B:Data -> @-eq:(@_:D -> @_:D -> Bool) -> @+s:Store<D, B> -> @+claimed:D -> @+computed:D -> @o:Obj<D, B> -> Maybe<&2, Store<D, B>>

Ingress: computed is the digest of o's encoding. None when it is not the claimed digest.

template hash source · line 239 · raw

@-D:Data -> @-B:Data -> @-H:(@_:Obj<D, B> -> D) -> @t:Tree<B> -> D

The Merkle digest of t under the object hash H.

template objects source · line 249 · raw

@-D:Data -> @-B:Data -> @-H:(@_:Obj<D, B> -> D) -> @t:Tree<B> -> Store<D, B>

The objects of t with their digests, children before parents; hash(t) is the last digest.

template valid source · line 273 · raw

@-D:Data -> @-B:Data -> @-H:(@_:Obj<D, B> -> D) -> @-eq:(@_:D -> @_:D -> Bool) -> @s:Store<D, B> -> Bool

True when every entry (k, o) has eq(k, H(o)).