package keytransform
import "github.com/ipfs/go-datastore/keytransform"
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 ¶
- type Datastore
- func Wrap(child ds.Datastore, t KeyTransform) *Datastore
- func (d *Datastore) Batch(ctx context.Context) (ds.Batch, error)
- func (d *Datastore) Check(ctx context.Context) error
- func (d *Datastore) Children() []ds.Datastore
- func (d *Datastore) Close() error
- func (d *Datastore) CollectGarbage(ctx context.Context) error
- func (d *Datastore) Delete(ctx context.Context, key ds.Key) (err error)
- func (d *Datastore) DiskUsage(ctx context.Context) (uint64, error)
- func (d *Datastore) Get(ctx context.Context, key ds.Key) (value []byte, err error)
- func (d *Datastore) GetSize(ctx context.Context, key ds.Key) (size int, err error)
- func (d *Datastore) Has(ctx context.Context, key ds.Key) (exists bool, err error)
- func (d *Datastore) Put(ctx context.Context, key ds.Key, value []byte) (err error)
- func (d *Datastore) Query(ctx context.Context, q dsq.Query) (dsq.Results, error)
- func (d *Datastore) Scrub(ctx context.Context) error
- func (d *Datastore) Sync(ctx context.Context, prefix ds.Key) error
- type KeyMapping
- type KeyTransform
- type Pair
- type PrefixTransform
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 ¶
func (*Datastore) Check ¶
func (*Datastore) Children ¶
Children implements ds.Shim
func (*Datastore) Close ¶
func (*Datastore) CollectGarbage ¶
func (*Datastore) Delete ¶
Delete removes the value for given key
func (*Datastore) DiskUsage ¶
DiskUsage implements the PersistentDatastore interface.
func (*Datastore) Get ¶
Get returns the value for given key, transforming the key first.
func (*Datastore) GetSize ¶
GetSize returns the size of the value named by the given key, transforming the key first.
func (*Datastore) Has ¶
Has returns whether the datastore has a value for a given key, transforming the key first.
func (*Datastore) Put ¶
Put stores the given value, transforming the key first.
func (*Datastore) Query ¶
Query implements Query, inverting keys on the way back out.
func (*Datastore) Scrub ¶
func (*Datastore) Sync ¶
Sync implements Datastore.Sync
type KeyMapping ¶
KeyMapping is a function that maps one key to annother
type KeyTransform ¶
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 (*Pair) InvertKey ¶
type 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.
func (PrefixTransform) ConvertKey ¶
func (p PrefixTransform) ConvertKey(k ds.Key) ds.Key
ConvertKey adds the prefix.
func (PrefixTransform) InvertKey ¶
func (p PrefixTransform) InvertKey(k ds.Key) ds.Key
InvertKey removes the prefix. panics if prefix not found.
Source Files ¶
doc.go interface.go keytransform.go transforms.go
- Version
- v0.8.2 (latest)
- Published
- Mar 4, 2025
- Platform
- linux/amd64
- Imports
- 3 packages
- Last checked
- 1 month ago –
Tools for package owners.