namespace

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2015 License: MIT, MIT Imports: 5 Imported by: 0

Documentation

Overview

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/jbenet/go-datastore"
  nsds "github.com/jbenet/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
package main

import (
	"fmt"

	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	nsds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/namespace"
)

func main() {
	mp := ds.NewMapDatastore()
	ns := nsds.Wrap(mp, ds.NewKey("/foo/bar"))

	k := ds.NewKey("/beep")
	v := "boop"

	ns.Put(k, v)
	fmt.Printf("ns.Put %s %s\n", k, v)

	v2, _ := ns.Get(k)
	fmt.Printf("ns.Get %s -> %s\n", k, v2)

	k3 := ds.NewKey("/foo/bar/beep")
	v3, _ := mp.Get(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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrefixTransform

func PrefixTransform(prefix ds.Key) ktds.KeyTransform

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.

func Wrap

func Wrap(child ds.Datastore, prefix ds.Key) *datastore

Wrap wraps a given datastore with a key-prefix.

Types

This section is empty.

Jump to

Keyboard shortcuts

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