memstore

package
v0.7.7 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2019 License: Apache-2.0 Imports: 10 Imported by: 106

Documentation

Overview

Package b implements a B+tree.

Changelog

2014-06-26: Lower GC presure by recycling things.

2014-04-18: Added new method Put.

Generic types

Keys and their associated values are interface{} typed, similar to all of the containers in the standard library.

Semiautomatic production of a type specific variant of this package is supported via

$ make generic

This command will write to stdout a version of the btree.go file where every key type occurrence is replaced by the word 'key' (written in all CAPS) and every value type occurrence is replaced by the word 'value' (written in all CAPS). Then you have to replace these tokens with your desired type(s), using any technique you're comfortable with.

This is how, for example, 'example/int.go' was created:

$ mkdir example
$
$ # Note: the command bellow must be actually written using the words
$ # 'key' and 'value' in all CAPS. The proper form is avoided in this
$ # documentation to not confuse any text replacement mechanism.
$
$ make generic | sed -e 's/key/int/g' -e 's/value/int/g' > example/int.go

No other changes to int.go are necessary, it compiles just fine.

Running the benchmarks for 1000 keys on a machine with Intel i5-4670 CPU @ 3.4GHz, Go release 1.3.

$ go test -bench 1e3 example/all_test.go example/int.go
PASS
BenchmarkSetSeq1e3	   10000	    146740 ns/op
BenchmarkGetSeq1e3	   10000	    108261 ns/op
BenchmarkSetRnd1e3	   10000	    254359 ns/op
BenchmarkGetRnd1e3	   10000	    134621 ns/op
BenchmarkDelRnd1e3	   10000	    211864 ns/op
BenchmarkSeekSeq1e3	   10000	    148628 ns/op
BenchmarkSeekRnd1e3	   10000	    215166 ns/op
BenchmarkNext1e3	  200000	      9211 ns/op
BenchmarkPrev1e3	  200000	      8843 ns/op
ok  	command-line-arguments	25.071s
$

Index

Constants

View Source
const QuadStoreType = "memstore"

Variables

This section is empty.

Functions

This section is empty.

Types

type AllIterator added in v0.3.1

type AllIterator struct {
	graph.Iterator
	// contains filtered or unexported fields
}

func (*AllIterator) AsShape added in v0.7.6

func (it *AllIterator) AsShape() graph.IteratorShape

type Cmp added in v0.7.0

type Cmp func(a, b int64) int

Cmp compares a and b. Return value is:

< 0 if a <  b
  0 if a == b
> 0 if a >  b

type Enumerator added in v0.7.0

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

Enumerator captures the state of enumerating a tree. It is returned from the Seek* methods. The enumerator is aware of any mutations made to the tree in the process of enumerating it and automatically resumes the enumeration at the proper key, if possible.

However, once an Enumerator returns io.EOF to signal "no more items", it does no more attempt to "resync" on tree mutation(s). In other words, io.EOF from an Enumaretor is "sticky" (idempotent).

func (*Enumerator) Close added in v0.7.0

func (e *Enumerator) Close()

Close recycles e to a pool for possible later reuse. No references to e should exist or such references must not be used afterwards.

func (*Enumerator) Next added in v0.7.0

func (e *Enumerator) Next() (k int64, v *primitive, err error)

Next returns the currently enumerated item, if it exists and moves to the next item in the key collation order. If there is no item to return, err == io.EOF is returned.

func (*Enumerator) Prev added in v0.7.0

func (e *Enumerator) Prev() (k int64, v *primitive, err error)

Prev returns the currently enumerated item, if it exists and moves to the previous item in the key collation order. If there is no item to return, err == io.EOF is returned.

type Iterator added in v0.3.1

type Iterator struct {
	graph.Iterator
	// contains filtered or unexported fields
}

func NewIterator added in v0.4.0

func NewIterator(tree *Tree, qs *QuadStore, d quad.Direction, value int64) *Iterator

func (*Iterator) AsShape added in v0.7.6

func (it *Iterator) AsShape() graph.IteratorShape

func (*Iterator) Sorted added in v0.3.1

func (it *Iterator) Sorted() bool

type QuadDirectionIndex added in v0.4.0

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

func NewQuadDirectionIndex added in v0.4.0

func NewQuadDirectionIndex() QuadDirectionIndex

func (QuadDirectionIndex) Get added in v0.4.0

func (qdi QuadDirectionIndex) Get(d quad.Direction, id int64) (*Tree, bool)

func (QuadDirectionIndex) Tree added in v0.4.0

func (qdi QuadDirectionIndex) Tree(d quad.Direction, id int64) *Tree

type QuadStore added in v0.4.1

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

func New added in v0.6.1

func New(quads ...quad.Quad) *QuadStore

New creates a new in-memory quad store and loads provided quads.

func (*QuadStore) AddBNode added in v0.7.0

func (qs *QuadStore) AddBNode() int64

AddNode adds a blank node (with no value) to quad store. It returns an id of the node.

func (*QuadStore) AddQuad added in v0.7.0

func (qs *QuadStore) AddQuad(q quad.Quad) (int64, bool)

AddQuad adds a quad to quad store. It returns an id of the quad. False is returned as a second parameter if quad exists already.

func (*QuadStore) AddValue added in v0.7.0

func (qs *QuadStore) AddValue(v quad.Value) (int64, bool)

AddNode adds a value to quad store. It returns an id of the value. False is returned as a second parameter if value exists already.

func (*QuadStore) ApplyDeltas added in v0.4.1

func (qs *QuadStore) ApplyDeltas(deltas []graph.Delta, ignoreOpts graph.IgnoreOpts) error

func (*QuadStore) Close added in v0.4.1

func (qs *QuadStore) Close() error

func (*QuadStore) Delete added in v0.7.0

func (qs *QuadStore) Delete(id int64) bool

func (*QuadStore) NameOf added in v0.4.1

func (qs *QuadStore) NameOf(v graph.Ref) quad.Value

func (*QuadStore) NewQuadWriter added in v0.7.6

func (qs *QuadStore) NewQuadWriter() (quad.WriteCloser, error)

func (*QuadStore) NodesAllIterator added in v0.4.1

func (qs *QuadStore) NodesAllIterator() graph.Iterator

func (*QuadStore) Quad added in v0.4.1

func (qs *QuadStore) Quad(index graph.Ref) quad.Quad

func (*QuadStore) QuadDirection added in v0.4.1

func (qs *QuadStore) QuadDirection(val graph.Ref, d quad.Direction) graph.Ref

func (*QuadStore) QuadIterator added in v0.4.1

func (qs *QuadStore) QuadIterator(d quad.Direction, value graph.Ref) graph.Iterator

func (*QuadStore) QuadIteratorSize added in v0.7.6

func (qs *QuadStore) QuadIteratorSize(ctx context.Context, d quad.Direction, v graph.Ref) (graph.Size, error)

func (*QuadStore) QuadsAllIterator added in v0.4.1

func (qs *QuadStore) QuadsAllIterator() graph.Iterator

func (*QuadStore) Stats added in v0.7.6

func (qs *QuadStore) Stats(ctx context.Context, exact bool) (graph.Stats, error)

func (*QuadStore) ValueOf added in v0.4.1

func (qs *QuadStore) ValueOf(name quad.Value) graph.Ref

func (*QuadStore) WriteQuad deprecated added in v0.6.1

func (qs *QuadStore) WriteQuad(q quad.Quad) error

WriteQuad adds a quad to quad store.

Deprecated: use AddQuad instead.

func (*QuadStore) WriteQuads added in v0.7.6

func (qs *QuadStore) WriteQuads(buf []quad.Quad) (int, error)

WriteQuads implements quad.Writer.

type Tree added in v0.7.0

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

Tree is a B+tree.

func TreeNew added in v0.7.0

func TreeNew(cmp Cmp) *Tree

TreeNew returns a newly created, empty Tree. The compare function is used for key collation.

func (*Tree) Clear added in v0.7.0

func (t *Tree) Clear()

Clear removes all K/V pairs from the tree.

func (*Tree) Close added in v0.7.0

func (t *Tree) Close()

Close performs Clear and recycles t to a pool for possible later reuse. No references to t should exist or such references must not be used afterwards.

func (*Tree) Delete added in v0.7.0

func (t *Tree) Delete(k int64) (ok bool)

Delete removes the k's KV pair, if it exists, in which case Delete returns true.

func (*Tree) First added in v0.7.0

func (t *Tree) First() (k int64, v *primitive)

First returns the first item of the tree in the key collating order, or (zero-value, zero-value) if the tree is empty.

func (*Tree) Get added in v0.7.0

func (t *Tree) Get(k int64) (v *primitive, ok bool)

Get returns the value associated with k and true if it exists. Otherwise Get returns (zero-value, false).

func (*Tree) Last added in v0.7.0

func (t *Tree) Last() (k int64, v *primitive)

Last returns the last item of the tree in the key collating order, or (zero-value, zero-value) if the tree is empty.

func (*Tree) Len added in v0.7.0

func (t *Tree) Len() int

Len returns the number of items in the tree.

func (*Tree) Put added in v0.7.0

func (t *Tree) Put(k int64, upd func(oldV *primitive, exists bool) (newV *primitive, write bool)) (oldV *primitive, written bool)

Put combines Get and Set in a more efficient way where the tree is walked only once. The upd(ater) receives (old-value, true) if a KV pair for k exists or (zero-value, false) otherwise. It can then return a (new-value, true) to create or overwrite the existing value in the KV pair, or (whatever, false) if it decides not to create or not to update the value of the KV pair.

tree.Set(k, v) call conceptually equals calling

tree.Put(k, func(int64, bool){ return v, true })

modulo the differing return values.

func (*Tree) Seek added in v0.7.0

func (t *Tree) Seek(k int64) (e *Enumerator, ok bool)

Seek returns an Enumerator positioned on a an item such that k >= item's key. ok reports if k == item.key The Enumerator's position is possibly after the last item in the tree.

func (*Tree) SeekFirst added in v0.7.0

func (t *Tree) SeekFirst() (e *Enumerator, err error)

SeekFirst returns an enumerator positioned on the first KV pair in the tree, if any. For an empty tree, err == io.EOF is returned and e will be nil.

func (*Tree) SeekLast added in v0.7.0

func (t *Tree) SeekLast() (e *Enumerator, err error)

SeekLast returns an enumerator positioned on the last KV pair in the tree, if any. For an empty tree, err == io.EOF is returned and e will be nil.

func (*Tree) Set added in v0.7.0

func (t *Tree) Set(k int64, v *primitive)

Set sets the value associated with k.

Jump to

Keyboard shortcuts

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