package namespace
import "github.com/ipfs/go-datastore/namespace"
Package namespace introduces a namespace Datastore Shim, which basically mounts the entire child datastore under a prefix.
Use the Wrap function to wrap a datastore with any Key prefix. For example:
import ( "fmt" ds "github.com/ipfs/go-datastore" nsds "github.com/ipfs/go-datastore/namespace" ) func main() { mp := ds.NewMapDatastore() ns := nsds.Wrap(mp, ds.NewKey("/foo/bar")) // in the Namespace Datastore: ns.Put(ds.NewKey("/beep"), "boop") v2, _ := ns.Get(ds.NewKey("/beep")) // v2 == "boop" // and, in the underlying MapDatastore: v3, _ := mp.Get(ds.NewKey("/foo/bar/beep")) // v3 == "boop" }
Example¶
Code:play
package main import ( "context" "fmt" ds "github.com/ipfs/go-datastore" nsds "github.com/ipfs/go-datastore/namespace" ) func main() { ctx := context.Background() mp := ds.NewMapDatastore() ns := nsds.Wrap(mp, ds.NewKey("/foo/bar")) k := ds.NewKey("/beep") v := "boop" if err := ns.Put(ctx, k, []byte(v)); err != nil { panic(err) } fmt.Printf("ns.Put %s %s\n", k, v) v2, _ := ns.Get(ctx, k) fmt.Printf("ns.Get %s -> %s\n", k, v2) k3 := ds.NewKey("/foo/bar/beep") v3, _ := mp.Get(ctx, k3) fmt.Printf("mp.Get %s -> %s\n", k3, v3) }
Output:
ns.Put /beep boop ns.Get /beep -> boop mp.Get /foo/bar/beep -> boop
Index ¶
- func PrefixTransform(prefix ds.Key) ktds.PrefixTransform
- func Wrap(child ds.Datastore, prefix ds.Key) *ktds.Datastore
Examples ¶
Functions ¶
func PrefixTransform ¶
func PrefixTransform(prefix ds.Key) ktds.PrefixTransform
PrefixTransform constructs a KeyTransform with a pair of functions that add or remove the given prefix key.
Warning: will panic if prefix not found when it should be there. This is to avoid insidious data inconsistency errors.
DEPRECATED: Use ktds.PrefixTransform directly.
func Wrap ¶
Wrap wraps a given datastore with a key-prefix.
Source Files ¶
- Version
- v0.8.2 (latest)
- Published
- Mar 4, 2025
- Platform
- linux/amd64
- Imports
- 2 packages
- Last checked
- 5 days ago –
Tools for package owners.