Documentation
¶
Overview ¶
Package gkvlite provides a simple, ordered, ACID, key-value persistence library. It provides persistent, immutable data structure abstrations.
Index ¶
- Constants
- Variables
- type AllocStats
- type Collection
- func (t *Collection) AllocStats() (res AllocStats)
- func (t *Collection) Delete(key []byte) (wasDeleted bool, err error)
- func (t *Collection) EvictSomeItems() (numEvicted uint64)
- func (t *Collection) Get(key []byte) (val []byte, err error)
- func (t *Collection) GetItem(key []byte, withValue bool) (i *Item, err error)
- func (t *Collection) GetTotals() (numItems uint64, numBytes uint64, err error)
- func (t *Collection) MarshalJSON() ([]byte, error)
- func (t *Collection) MaxItem(withValue bool) (*Item, error)
- func (t *Collection) MinItem(withValue bool) (*Item, error)
- func (t *Collection) Name() string
- func (t *Collection) Set(key []byte, val []byte) error
- func (t *Collection) SetItem(item *Item) (err error)
- func (t *Collection) UnmarshalJSON(d []byte) error
- func (t *Collection) VisitItemsAscend(target []byte, withValue bool, v ItemVisitor) error
- func (t *Collection) VisitItemsAscendEx(target []byte, withValue bool, visitor ItemVisitorEx) error
- func (t *Collection) VisitItemsDescend(target []byte, withValue bool, v ItemVisitor) error
- func (t *Collection) VisitItemsDescendEx(target []byte, withValue bool, visitor ItemVisitorEx) error
- func (t *Collection) Write() error
- type Item
- type ItemCallback
- type ItemVisitor
- type ItemVisitorEx
- type KeyCompare
- type Store
- func (s *Store) Close()
- func (s *Store) CopyTo(dstFile StoreFile, flushEvery int) (res *Store, err error)
- func (s *Store) Flush() error
- func (s *Store) FlushRevert() error
- func (s *Store) GetCollection(name string) *Collection
- func (s *Store) GetCollectionNames() []string
- func (o *Store) ItemAddRef(c *Collection, i *Item)
- func (o *Store) ItemAlloc(c *Collection, keyLength uint16) *Item
- func (o *Store) ItemDecRef(c *Collection, i *Item)
- func (o *Store) ItemValRead(c *Collection, i *Item, r io.ReaderAt, offset int64, valLength uint32) error
- func (o *Store) ItemValWrite(c *Collection, i *Item, w io.WriterAt, offset int64) error
- func (s *Store) MakePrivateCollection(compare KeyCompare) *Collection
- func (s *Store) RemoveCollection(name string)
- func (s *Store) SetCollection(name string, compare KeyCompare) *Collection
- func (s *Store) Snapshot() (snapshot *Store)
- func (s *Store) Stats(out map[string]uint64)
- type StoreCallbacks
- type StoreFile
Constants ¶
const VERSION = uint32(4)
Variables ¶
var MAGIC_BEG []byte = []byte("0g1t2r")
var MAGIC_END []byte = []byte("3e4a5p")
Functions ¶
This section is empty.
Types ¶
type AllocStats ¶
type AllocStats struct { MkNodes int64 FreeNodes int64 // Number of invocations of the freeNode() API. AllocNodes int64 CurFreeNodes int64 // Current length of freeNodes list. MkNodeLocs int64 FreeNodeLocs int64 // Number of invocations of the freeNodeLoc() API. AllocNodeLocs int64 CurFreeNodeLocs int64 // Current length of freeNodeLocs list. MkRootNodeLocs int64 FreeRootNodeLocs int64 // Number of invocations of the freeRootNodeLoc() API. AllocRootNodeLocs int64 CurFreeRootNodeLocs int64 // Current length of freeRootNodeLocs list. }
type Collection ¶
type Collection struct { AppData unsafe.Pointer // For app-specific data; atomic CAS recommended. // contains filtered or unexported fields }
A persistable collection of ordered key-values (Item's).
func (*Collection) AllocStats ¶
func (t *Collection) AllocStats() (res AllocStats)
func (*Collection) Delete ¶
func (t *Collection) Delete(key []byte) (wasDeleted bool, err error)
Deletes an item of a given key.
func (*Collection) EvictSomeItems ¶
func (t *Collection) EvictSomeItems() (numEvicted uint64)
Evict some clean items found by randomly walking a tree branch. For concurrent users, only the single mutator thread should call EvictSomeItems(), making it serialized with mutations.
func (*Collection) Get ¶
func (t *Collection) Get(key []byte) (val []byte, err error)
Retrieve a value by its key. Returns nil if the item is not in the collection. The returned value should be treated as immutable.
func (*Collection) GetItem ¶
func (t *Collection) GetItem(key []byte, withValue bool) (i *Item, err error)
Retrieve an item by its key. Use withValue of false if you don't need the item's value (Item.Val may be nil), which might be able to save on I/O and memory resources, especially for large values. The returned Item should be treated as immutable.
func (*Collection) GetTotals ¶
func (t *Collection) GetTotals() (numItems uint64, numBytes uint64, err error)
Returns total number of items and total key bytes plus value bytes.
func (*Collection) MarshalJSON ¶
func (t *Collection) MarshalJSON() ([]byte, error)
Returns JSON representation of root node file location.
func (*Collection) MaxItem ¶
func (t *Collection) MaxItem(withValue bool) (*Item, error)
Retrieves the item with the "largest" key. The returned item should be treated as immutable.
func (*Collection) MinItem ¶
func (t *Collection) MinItem(withValue bool) (*Item, error)
Retrieves the item with the "smallest" key. The returned item should be treated as immutable.
func (*Collection) Name ¶
func (t *Collection) Name() string
func (*Collection) Set ¶
func (t *Collection) Set(key []byte, val []byte) error
Replace or insert an item of a given key.
func (*Collection) SetItem ¶
func (t *Collection) SetItem(item *Item) (err error)
Replace or insert an item of a given key. A random item Priority (e.g., rand.Int31()) will usually work well, but advanced users may consider using non-random item priorities at the risk of unbalancing the lookup tree. The input Item instance should be considered immutable and owned by the Collection.
func (*Collection) UnmarshalJSON ¶
func (t *Collection) UnmarshalJSON(d []byte) error
Unmarshals JSON representation of root node file location.
func (*Collection) VisitItemsAscend ¶
func (t *Collection) VisitItemsAscend(target []byte, withValue bool, v ItemVisitor) error
Visit items greater-than-or-equal to the target key in ascending order.
func (*Collection) VisitItemsAscendEx ¶
func (t *Collection) VisitItemsAscendEx(target []byte, withValue bool, visitor ItemVisitorEx) error
Visit items greater-than-or-equal to the target key in ascending order; with depth info.
func (*Collection) VisitItemsDescend ¶
func (t *Collection) VisitItemsDescend(target []byte, withValue bool, v ItemVisitor) error
Visit items less-than the target key in descending order.
func (*Collection) VisitItemsDescendEx ¶
func (t *Collection) VisitItemsDescendEx(target []byte, withValue bool, visitor ItemVisitorEx) error
Visit items less-than the target key in descending order; with depth info.
func (*Collection) Write ¶
func (t *Collection) Write() error
Writes dirty items of a collection BUT (WARNING) does NOT write new root records. Use Store.Flush() to write root records, which would make these writes visible to the next file re-opening/re-loading.
type Item ¶
type Item struct { Transient unsafe.Pointer // For any ephemeral data; atomic CAS recommended. Key, Val []byte // Val may be nil if not fetched into memory yet. Priority int32 // Use rand.Int31() for probabilistic balancing. }
A persistable item.
func (*Item) Copy ¶
The returned Item will not have been allocated through the optional StoreCallbacks.ItemAlloc() callback.
func (*Item) NumBytes ¶
func (i *Item) NumBytes(c *Collection) int
Number of Key bytes plus number of Val bytes.
func (*Item) NumValBytes ¶
func (i *Item) NumValBytes(c *Collection) int
type ItemCallback ¶
type ItemCallback func(*Collection, *Item) (*Item, error)
type ItemVisitor ¶
type ItemVisitorEx ¶
type KeyCompare ¶
User-supplied key comparison func should return 0 if a == b, -1 if a < b, and +1 if a > b. For example: bytes.Compare()
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
A persistable store holding collections of ordered keys & values.
func NewStoreEx ¶
func NewStoreEx(file StoreFile, callbacks StoreCallbacks) (*Store, error)
func (*Store) CopyTo ¶
Copy all active collections and their items to a different file. If flushEvery > 0, then during the item copying, Flush() will be invoked at every flushEvery'th item and at the end of the item copying. The copy will not include any old items or nodes so the copy should be more compact if flushEvery is relatively large.
func (*Store) Flush ¶
Writes (appends) any dirty, unpersisted data to file. As a greater-window-of-data-loss versus higher-performance tradeoff, consider having many mutations (Set()'s & Delete()'s) and then have a less occasional Flush() instead of Flush()'ing after every mutation. Users may also wish to file.Sync() after a Flush() for extra data-loss protection.
func (*Store) FlushRevert ¶
Reverts the last Flush(), bringing the Store back to its state at its next-to-last Flush() or to an empty Store (with no Collections) if there were no next-to-last Flush(). This call will truncate the Store file.
func (*Store) GetCollection ¶
func (s *Store) GetCollection(name string) *Collection
Retrieves a named Collection.
func (*Store) GetCollectionNames ¶
func (*Store) ItemAddRef ¶
func (o *Store) ItemAddRef(c *Collection, i *Item)
func (*Store) ItemDecRef ¶
func (o *Store) ItemDecRef(c *Collection, i *Item)
func (*Store) ItemValRead ¶
func (*Store) ItemValWrite ¶
func (*Store) MakePrivateCollection ¶
func (s *Store) MakePrivateCollection(compare KeyCompare) *Collection
Returns a new, unregistered (non-named) collection. This allows advanced users to manage collections of private collections.
func (*Store) RemoveCollection ¶
The Collection removal won't be reflected into persistence until you do a Flush(). Invoking RemoveCollection(x) and then SetCollection(x) is a fast way to empty a Collection.
func (*Store) SetCollection ¶
func (s *Store) SetCollection(name string, compare KeyCompare) *Collection
SetCollection() is used to create a named Collection, or to modify the KeyCompare function on an existing Collection. In either case, a new Collection to use is returned. A newly created Collection and any mutations on it won't be persisted until you do a Flush().
type StoreCallbacks ¶
type StoreCallbacks struct {
BeforeItemWrite, AfterItemRead ItemCallback
// Optional callback to allocate an Item with an Item.Key. If
// your app uses ref-counting, the returned Item should have
// logical ref-count of 1.
ItemAlloc func(c *Collection, keyLength uint16) *Item
// Optional callback to allow you to track gkvlite's ref-counts on
// an Item. Apps might use this for buffer management and putting
// Item's on a free-list.
ItemAddRef func(c *Collection, i *Item)
// Optional callback to allow you to track gkvlite's ref-counts on
// an Item. Apps might use this for buffer management and putting
// Item's on a free-list.
ItemDecRef func(c *Collection, i *Item)
// Optional callback to control on-disk size, in bytes, of an item's value.
ItemValLength func(c *Collection, i *Item) int
// Optional callback to allow you to write item bytes differently.
ItemValWrite func(c *Collection, i *Item,
w io.WriterAt, offset int64) error
// Optional callback to read item bytes differently. For example,
// the app might have an optimization to just remember the reader
// & file offsets in the item.Transient field for lazy reading.
ItemValRead func(c *Collection, i *Item,
r io.ReaderAt, offset int64, valLength uint32) error
// Invoked when a Store is reloaded (during NewStoreEx()) from
// disk, this callback allows the user to optionally supply a key
// comparison func for each collection. Otherwise, the default is
// the bytes.Compare func.
KeyCompareForCollection func(collName string) KeyCompare
}
Allows applications to override or interpose before/after events.