hook

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2020 License: MIT Imports: 3 Imported by: 3

README

ipfs-hookds

Build Status Coverage Standard README GoDoc golang version Go Report Card

A wrapper for an IPFS datastore that adds optional before and after hooks to it's methods.

Install

go get github.com/alanshaw/ipfs-hookds

Usage

Example

Hook for after a Put:

package main

import (
	"fmt"

	"github.com/ipfs/go-datastore"
	"github.com/alanshaw/ipfs-hookds"
)

func main() {
	ds := datastore.NewMapDatastore()
	hds := hook.NewDatastore(ds, hook.WithAfterPut(func(k datastore.Key, v []byte, err error) error {
		fmt.Printf("key: %v value: %s was put to the datastore\n", k, v)
		return err
	}))
	defer hds.Close()

	key := datastore.NewKey("test")
	value := []byte("test")

	hds.Put(key, value)

	// Output:
	// key: /test value: test was put to the datastore
}

Hook into a batch Put:

package main

import (
	"fmt"

	"github.com/ipfs/go-datastore"
	"github.com/alanshaw/ipfs-hookds/batch"
	"github.com/alanshaw/ipfs-hookds"
)

func main() {
	ds := datastore.NewMapDatastore()
	hds := hook.NewDatastore(ds, hook.WithAfterBatch(func(b datastore.Batch, err error) (datastore.Batch, error) {
		return batch.NewBatch(b, batch.WithAfterPut(func(datastore.Key, []byte, error) error {
			fmt.Printf("key: %v value: %s was put to a batch\n", k, v)
			return err
		})), err
	}))
	defer hds.Close()

	key := datastore.NewKey("test")
	value := []byte("test")

	bch := hds.Batch()

	bch.Put(key, value)
	bch.Commit()

	// Output:
	// key: /test value: test was put to a batch
}

Hook into a query NextSync:

package main

import (
	"fmt"

	"github.com/ipfs/go-datastore"
	"github.com/ipfs/go-datastore/query"
	"github.com/alanshaw/ipfs-hookds/query/results"
	"github.com/alanshaw/ipfs-hookds"
)

func main() {
	ds := datastore.NewMapDatastore()
	hds := hook.NewDatastore(ds, hook.WithAfterQuery(func(q query.Query, res query.Results, err error) (query.Results, error) {
		return results.NewResults(res, results.WithAfterNextSync(func(r query.Result, ok bool) (query.Result, bool) {
			fmt.Printf("result: %v ok: %s was next\n", r, ok)
			return r, ok
		})), err
	}))
	defer hds.Close()

	key := datastore.NewKey("test")
	value := []byte("test")
	hds.Put(key, value)

	res := hds.Query(query.Query{
		Prefix: "/test"
	})

	res.NextSync()
}

API

GoDoc Reference

Contribute

Feel free to dive in! Open an issue or submit PRs.

License

MIT © Alan Shaw

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AfterBatchFunc

type AfterBatchFunc func(datastore.Batch, error) (datastore.Batch, error)

AfterBatchFunc is a handler for the after Batch hook

type AfterDeleteFunc

type AfterDeleteFunc func(datastore.Key, error) error

AfterDeleteFunc is a handler for the after Delete hook

type AfterGetFunc

type AfterGetFunc func(datastore.Key, []byte, error) ([]byte, error)

AfterGetFunc is a handler for the after Get hook

type AfterHasFunc

type AfterHasFunc func(datastore.Key, bool, error) (bool, error)

AfterHasFunc is a handler for the after Has hook

type AfterPutFunc

type AfterPutFunc func(datastore.Key, []byte, error) error

AfterPutFunc is a handler for the after Put hook

type AfterQueryFunc

type AfterQueryFunc func(query.Query, query.Results, error) (query.Results, error)

AfterQueryFunc is a handler for the after Query hook

type Batching

type Batching struct {
	// contains filtered or unexported fields
}

Batching is a datastore with hooks that also supports batching

func NewBatching

func NewBatching(ds datastore.Batching, options ...Option) *Batching

NewBatching wraps a datastore.Batching datastore and adds optional before and after hooks into it's methods

func (*Batching) Batch

func (bds *Batching) Batch() (datastore.Batch, error)

Batch creates a container for a group of updates, it calls OnBeforeBatch and OnAfterBatch hooks.

func (*Batching) Close

func (bds *Batching) Close() error

Close closes the underlying datastore

func (*Batching) Delete

func (bds *Batching) Delete(key datastore.Key) error

Delete removes the value for given `key`, it calls OnBeforeDelete and OnAfterDelete hooks.

func (*Batching) Get

func (bds *Batching) Get(key datastore.Key) ([]byte, error)

Get retrieves the object `value` named by `key`, it calls OnBeforeGet and OnAfterGet hooks.

func (*Batching) GetSize

func (bds *Batching) GetSize(key datastore.Key) (int, error)

GetSize returns the size of the `value` named by `key`.

func (*Batching) Has

func (bds *Batching) Has(key datastore.Key) (bool, error)

Has returns whether the `key` is mapped to a `value`.

func (*Batching) Put

func (bds *Batching) Put(key datastore.Key, value []byte) error

Put stores the object `value` named by `key`, it calls OnBeforePut and OnAfterPut hooks.

func (*Batching) Query

func (bds *Batching) Query(q query.Query) (query.Results, error)

Query searches the datastore and returns a query result.

func (*Batching) Sync

func (bds *Batching) Sync(prefix datastore.Key) error

Sync guarantees that any Put or Delete calls under prefix that returned before Sync(prefix) was called will be observed after Sync(prefix) returns, even if the program crashes.

type BeforeBatchFunc

type BeforeBatchFunc func()

BeforeBatchFunc is a handler for the before Batch hook

type BeforeDeleteFunc

type BeforeDeleteFunc func(datastore.Key) datastore.Key

BeforeDeleteFunc is a handler for the before Delete hook

type BeforeGetFunc

type BeforeGetFunc func(datastore.Key) datastore.Key

BeforeGetFunc is a handler for the before Get hook

type BeforeHasFunc

type BeforeHasFunc func(datastore.Key) datastore.Key

BeforeHasFunc is a handler for the before Has hook

type BeforePutFunc

type BeforePutFunc func(datastore.Key, []byte) (datastore.Key, []byte)

BeforePutFunc is a handler for the before Put hook

type BeforeQueryFunc

type BeforeQueryFunc func(query.Query) query.Query

BeforeQueryFunc is a handler for the before Query hook

type Datastore

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

Datastore is a wrapper for a datastore that adds optional before and after hooks into it's methods.

func NewDatastore

func NewDatastore(ds datastore.Datastore, options ...Option) *Datastore

NewDatastore wraps a datastore.Datastore datastore and adds optional before and after hooks into it's methods.

func (*Datastore) Close

func (hds *Datastore) Close() error

Close closes the underlying datastore

func (*Datastore) Delete

func (hds *Datastore) Delete(key datastore.Key) error

Delete removes the value for given `key`, it calls OnBeforeDelete and OnAfterDelete hooks.

func (*Datastore) Get

func (hds *Datastore) Get(key datastore.Key) ([]byte, error)

Get retrieves the object `value` named by `key`, it calls OnBeforeGet and OnAfterGet hooks.

func (*Datastore) GetSize

func (hds *Datastore) GetSize(key datastore.Key) (int, error)

GetSize returns the size of the `value` named by `key`.

func (*Datastore) Has

func (hds *Datastore) Has(key datastore.Key) (bool, error)

Has returns whether the `key` is mapped to a `value`.

func (*Datastore) Put

func (hds *Datastore) Put(key datastore.Key, value []byte) error

Put stores the object `value` named by `key`, it calls OnBeforePut and OnAfterPut hooks.

func (*Datastore) Query

func (hds *Datastore) Query(q query.Query) (query.Results, error)

Query searches the datastore and returns a query result, it calls OnBeforeQuery and OnAfterQuery hooks.

func (*Datastore) Sync

func (hds *Datastore) Sync(prefix datastore.Key) error

Sync guarantees that any Put or Delete calls under prefix that returned before Sync(prefix) was called will be observed after Sync(prefix) returns, even if the program crashes.

type Option

type Option func(*Options) error

Option is the hook datastore option type.

func WithAfterBatch

func WithAfterBatch(f AfterBatchFunc) Option

WithAfterBatch configures a hook that is called _after_ Batch. Defaults to noop.

func WithAfterDelete

func WithAfterDelete(f AfterDeleteFunc) Option

WithAfterDelete configures a hook that is called _after_ Delete. Defaults to noop.

func WithAfterGet

func WithAfterGet(f AfterGetFunc) Option

WithAfterGet configures a hook that is called _after_ Get. Defaults to noop.

func WithAfterHas

func WithAfterHas(f AfterHasFunc) Option

WithAfterHas configures a hook that is called _after_ Has. Defaults to noop.

func WithAfterPut

func WithAfterPut(f AfterPutFunc) Option

WithAfterPut configures a hook that is called _after_ Put. Defaults to noop.

func WithAfterQuery

func WithAfterQuery(f AfterQueryFunc) Option

WithAfterQuery configures a hook that is called _after_ Query. Defaults to noop.

func WithBeforeBatch

func WithBeforeBatch(f BeforeBatchFunc) Option

WithBeforeBatch configures a hook that is called _before_ Batch. Defaults to noop.

func WithBeforeDelete

func WithBeforeDelete(f BeforeDeleteFunc) Option

WithBeforeDelete configures a hook that is called _before_ Delete. Defaults to noop.

func WithBeforeGet

func WithBeforeGet(f BeforeGetFunc) Option

WithBeforeGet configures a hook that is called _before_ Get. Defaults to noop.

func WithBeforeHas

func WithBeforeHas(f BeforeHasFunc) Option

WithBeforeHas configures a hook that is called _before_ Has. Defaults to noop.

func WithBeforePut

func WithBeforePut(f BeforePutFunc) Option

WithBeforePut configures a hook that is called _before_ Put. Defaults to noop.

func WithBeforeQuery

func WithBeforeQuery(f BeforeQueryFunc) Option

WithBeforeQuery configures a hook that is called _before_ Query. Defaults to noop.

type Options

type Options struct {
	BeforeGet    BeforeGetFunc
	AfterGet     AfterGetFunc
	BeforePut    BeforePutFunc
	AfterPut     AfterPutFunc
	BeforeDelete BeforeDeleteFunc
	AfterDelete  AfterDeleteFunc
	BeforeBatch  BeforeBatchFunc
	AfterBatch   AfterBatchFunc
	BeforeHas    BeforeHasFunc
	AfterHas     AfterHasFunc
	BeforeQuery  BeforeQueryFunc
	AfterQuery   AfterQueryFunc
}

Options are hook datastore options.

func (*Options) Apply

func (o *Options) Apply(opts ...Option) error

Apply applies the given options to this Option.

Directories

Path Synopsis
query

Jump to

Keyboard shortcuts

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