keytransform

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2021 License: MIT, MIT Imports: 2 Imported by: 0

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/fs-repo-migrations/ipfs-10-to-11/_vendor/github.com/ipfs/go-datastore/keytransform"
  ds "github.com/ipfs/fs-repo-migrations/ipfs-10-to-11/_vendor/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

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

func (*Datastore) Check

func (d *Datastore) Check() 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() error

func (*Datastore) Delete

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

Delete removes the value for given key

func (*Datastore) DiskUsage

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

DiskUsage implements the PersistentDatastore interface.

func (*Datastore) Get

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

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

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

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

Put stores the given value, transforming the key first.

func (*Datastore) Query

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

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

func (*Datastore) Scrub

func (d *Datastore) Scrub() error

func (*Datastore) Sync

func (d *Datastore) Sync(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.

Jump to

Keyboard shortcuts

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