keytransform

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2019 License: MIT Imports: 2 Imported by: 29

Documentation

Overview

Package keytransform introduces a Datastore Shim that transforms keys before passing them to its child. It can be used to manipulate what keys look like to the user, for example namespacing keys, reversing them, etc.

Use the Wrap function to wrap a datastore with any KeyTransform. A KeyTransform is simply an interface with two functions, a conversion and its inverse. For example:

import (
  ktds "github.com/ipfs/go-datastore/keytransform"
  ds "github.com/ipfs/go-datastore"
)

func reverseKey(k ds.Key) ds.Key {
  return k.Reverse()
}

func invertKeys(d ds.Datastore) {
  return ktds.Wrap(d, &ktds.Pair{
    Convert: reverseKey,
    Invert: reverseKey,  // reverse is its own inverse.
  })
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Datastore

type Datastore struct {
	KeyTransform
	// contains filtered or unexported fields
}

Datastore keeps a KeyTransform function

func Wrap

func Wrap(child ds.Datastore, t KeyTransform) *Datastore

Wrap wraps a given datastore with a KeyTransform function. The resulting wrapped datastore will use the transform on all Datastore operations.

func (*Datastore) Batch added in v0.0.5

func (d *Datastore) Batch() (ds.Batch, error)

func (*Datastore) Check added in v0.0.5

func (d *Datastore) Check() error

func (*Datastore) Children added in v0.0.5

func (d *Datastore) Children() []ds.Datastore

Children implements ds.Shim

func (*Datastore) Close added in v0.0.5

func (d *Datastore) Close() error

func (*Datastore) CollectGarbage added in v0.0.5

func (d *Datastore) CollectGarbage() error

func (*Datastore) Delete added in v0.0.5

func (d *Datastore) Delete(key ds.Key) (err error)

Delete removes the value for given key

func (*Datastore) DiskUsage added in v0.0.5

func (d *Datastore) DiskUsage() (uint64, error)

DiskUsage implements the PersistentDatastore interface.

func (*Datastore) Get added in v0.0.5

func (d *Datastore) Get(key ds.Key) (value []byte, err error)

Get returns the value for given key, transforming the key first.

func (*Datastore) GetSize added in v0.0.5

func (d *Datastore) GetSize(key ds.Key) (size int, err error)

GetSize returns the size of the value named by the given key, transforming the key first.

func (*Datastore) Has added in v0.0.5

func (d *Datastore) Has(key ds.Key) (exists bool, err error)

Has returns whether the datastore has a value for a given key, transforming the key first.

func (*Datastore) Put added in v0.0.5

func (d *Datastore) Put(key ds.Key, value []byte) (err error)

Put stores the given value, transforming the key first.

func (*Datastore) Query added in v0.0.5

func (d *Datastore) Query(q dsq.Query) (dsq.Results, error)

Query implements Query, inverting keys on the way back out.

func (*Datastore) Scrub added in v0.0.5

func (d *Datastore) Scrub() error

type KeyMapping

type KeyMapping func(ds.Key) ds.Key

KeyMapping is a function that maps one key to annother

type KeyTransform

type KeyTransform interface {
	ConvertKey(ds.Key) ds.Key
	InvertKey(ds.Key) ds.Key
}

KeyTransform is an object with a pair of functions for (invertibly) transforming keys

type Pair

type Pair struct {
	Convert KeyMapping
	Invert  KeyMapping
}

Pair is a convince struct for constructing a key transform.

func (*Pair) ConvertKey

func (t *Pair) ConvertKey(k ds.Key) ds.Key

func (*Pair) InvertKey

func (t *Pair) InvertKey(k ds.Key) ds.Key

type PrefixTransform added in v0.0.5

type PrefixTransform struct {
	Prefix ds.Key
}

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 (PrefixTransform) ConvertKey added in v0.0.5

func (p PrefixTransform) ConvertKey(k ds.Key) ds.Key

ConvertKey adds the prefix.

func (PrefixTransform) InvertKey added in v0.0.5

func (p PrefixTransform) InvertKey(k ds.Key) ds.Key

InvertKey removes the prefix. panics if prefix not found.

Jump to

Keyboard shortcuts

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