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

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 (d *Datastore) Batch(ctx context.Context) (ds.Batch, error)

func (*Datastore) Check

func (d *Datastore) Check(ctx context.Context) error

func (*Datastore) Children

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

Children implements ds.Shim

func (*Datastore) Close

func (d *Datastore) Close() error

func (*Datastore) CollectGarbage

func (d *Datastore) CollectGarbage(ctx context.Context) error

func (*Datastore) Delete

func (d *Datastore) Delete(ctx context.Context, key ds.Key) (err error)

Delete removes the value for given key

func (*Datastore) DiskUsage

func (d *Datastore) DiskUsage(ctx context.Context) (uint64, error)

DiskUsage implements the PersistentDatastore interface.

func (*Datastore) Get

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

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

func (*Datastore) GetSize

func (d *Datastore) GetSize(ctx context.Context, 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

func (d *Datastore) Has(ctx context.Context, 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

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

Put stores the given value, transforming the key first.

func (*Datastore) Query

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

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

func (*Datastore) Scrub

func (d *Datastore) Scrub(ctx context.Context) error

func (*Datastore) Sync

func (d *Datastore) Sync(ctx context.Context, prefix ds.Key) error

Sync implements Datastore.Sync

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

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

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.