store

package
v0.0.0-...-133031b Latest Latest
Warning

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

Go to latest
Published: May 30, 2023 License: Apache-2.0 Imports: 4 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Walk

func Walk(s forest.Store, start *fields.QualifiedHash, visitor func(*fields.QualifiedHash) error) error

Walk traverses the subtree rooted at start in a breadth-first fashion invoking the visitor function on each node id in the subtree. The traversal stops either when the visitor function returns non-nil or when the entire subtree rooted at start has been visited.

If the visitor function returns an error, it will be returned wrapped and can be checked for using the errors.Is or errors.As standard library functions.

func WalkNodes

func WalkNodes(s forest.Store, start forest.Node, visitor func(forest.Node) error) (err error)

WalkNodes traverses the subtree rooted at start in a breadth-first fashion invoking the visitor function on each node in the subtree.

Types

type Archive

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

Archive is a wrapper type that extends the store.ExtendedStore interface on top of an existing forest.Store. It is safe for concurrent use.

func NewArchive

func NewArchive(store forest.Store) *Archive

NewArchive creates a thread-safe storage structure for forest nodes by wrapping an existing store implementation

func (*Archive) Add

func (m *Archive) Add(node forest.Node) (err error)

Add inserts a node into the underlying store. Importantly, this will send a notification of a new node to *all* subscribers. If the calling code is a subscriber, it will still be notified of the new node. To supress this, use AddAs() instead.

Subscribers will only be notified if the node is not already present in the archive.

func (*Archive) AddAs

func (m *Archive) AddAs(node forest.Node, addedByID Subscription) (err error)

AddAs allows adding a node to the underlying store without being notified of it as a new node. The addedByID (subscription id returned from SubscribeToNewMessages) will not be notified of the new nodes, but all other subscribers will be.

Subscribers will only be notified if the node is not already present in the archive.

func (*Archive) AncestryOf

func (a *Archive) AncestryOf(id *fields.QualifiedHash) ([]*fields.QualifiedHash, error)

AncestryOf returns the IDs of all known ancestors of the node with the given `id`. The ancestors are returned sorted by descending depth, so the root of the ancestry tree is the final node in the slice.

func (*Archive) Children

func (m *Archive) Children(id *fields.QualifiedHash) (ids []*fields.QualifiedHash, err error)

func (*Archive) CopyInto

func (m *Archive) CopyInto(s forest.Store) (err error)

func (*Archive) DescendantsOf

func (a *Archive) DescendantsOf(id *fields.QualifiedHash) ([]*fields.QualifiedHash, error)

DescendantsOf returns the IDs of all known descendants of the node with the given `id`. The order in which the descendants are returned is undefined.

func (*Archive) Destroy

func (m *Archive) Destroy()

Shut down the worker gorountine that powers this store. Subsequent calls to methods on this MessageStore have undefined behavior

func (*Archive) Get

func (m *Archive) Get(id *fields.QualifiedHash) (node forest.Node, present bool, err error)

func (*Archive) GetCommunity

func (m *Archive) GetCommunity(id *fields.QualifiedHash) (node forest.Node, present bool, err error)

func (*Archive) GetConversation

func (m *Archive) GetConversation(communityID, conversationID *fields.QualifiedHash) (node forest.Node, present bool, err error)

func (*Archive) GetIdentity

func (m *Archive) GetIdentity(id *fields.QualifiedHash) (node forest.Node, present bool, err error)

func (*Archive) GetReply

func (m *Archive) GetReply(communityID, conversationID, replyID *fields.QualifiedHash) (node forest.Node, present bool, err error)

func (*Archive) LeavesOf

func (a *Archive) LeavesOf(id *fields.QualifiedHash) ([]*fields.QualifiedHash, error)

LeavesOf returns the leaf nodes of the tree rooted at `id`. The order of the returned leaves is undefined.

func (*Archive) PresubscribeToNewMessages

func (m *Archive) PresubscribeToNewMessages(handler func(n forest.Node)) (subscriptionID Subscription)

PresubscribeToNewMessages establishes the given function as a handler to be invoked on each node added to the store. The returned subscription ID can be used to unsubscribe later, as well as to supress notifications with AddAs(). The handler function will be invoked *before* nodes are inserted into the store instead of after (like a normal Subscribe).

Handler functions are invoked synchronously on the same goroutine that invokes Add() or AddAs(), and should not block. If long-running code is needed in a handler, launch a new goroutine.

func (*Archive) Recent

func (m *Archive) Recent(nodeType fields.NodeType, quantity int) (nodes []forest.Node, err error)

Recent returns recently-created nodes of the provided NodeType. It may return both some nodes and an error

func (*Archive) RemoveSubtree

func (a *Archive) RemoveSubtree(id *fields.QualifiedHash) error

func (*Archive) SubscribeToNewMessages

func (m *Archive) SubscribeToNewMessages(handler func(n forest.Node)) (subscriptionID Subscription)

SubscribeToNewMessages establishes the given function as a handler to be invoked on each node added to the store. The returned subscription ID can be used to unsubscribe later, as well as to supress notifications with AddAs().

Handler functions are invoked synchronously on the same goroutine that invokes Add() or AddAs(), and should not block. If long-running code is needed in a handler, launch a new goroutine.

func (*Archive) UnderlyingStore

func (a *Archive) UnderlyingStore() forest.Store

UnderlyingStore returns the store that powers the archive. Use of this store is not thread-safe unless the concrete store implementation is thread-safe.

func (*Archive) UnpresubscribeToNewMessages

func (m *Archive) UnpresubscribeToNewMessages(subscriptionID Subscription)

UnpresubscribeToNewMessages removes the handler for a given subscription from the store.

func (*Archive) UnsubscribeToNewMessages

func (m *Archive) UnsubscribeToNewMessages(subscriptionID Subscription)

UnsubscribeToNewMessages removes the handler for a given subscription from the store.

type CacheStore

type CacheStore struct {
	Cache, Back forest.Store
}

CacheStore combines two other stores into one logical store. It is useful when store implementations have different performance characteristics and one is dramatically faster than the other. Once a CacheStore is created, the individual stores within it should not be directly modified.

func NewCacheStore

func NewCacheStore(cache, back forest.Store) (*CacheStore, error)

NewCacheStore creates a single logical store from the given two stores. All items from `cache` are automatically copied into `base` during the construction of the CacheStore, and from then on (assuming neither store is modified directly outside of CacheStore) all elements added are guaranteed to be added to `base`. It is recommended to use fast in-memory implementations as the `cache` layer and disk or network-based implementations as the `base` layer.

func (*CacheStore) Add

func (m *CacheStore) Add(node forest.Node) error

Add inserts the given node into both stores of the CacheStore

func (*CacheStore) Children

func (m *CacheStore) Children(id *fields.QualifiedHash) ([]*fields.QualifiedHash, error)

func (*CacheStore) CopyInto

func (m *CacheStore) CopyInto(other forest.Store) error

func (*CacheStore) Get

Get returns the requested node if it is present in either the Cache or the Back Store. If the cache is missed by the backing store is hit, the node will automatically be added to the cache.

func (*CacheStore) GetCommunity

func (m *CacheStore) GetCommunity(id *fields.QualifiedHash) (forest.Node, bool, error)

func (*CacheStore) GetConversation

func (m *CacheStore) GetConversation(communityID, conversationID *fields.QualifiedHash) (forest.Node, bool, error)

func (*CacheStore) GetIdentity

func (m *CacheStore) GetIdentity(id *fields.QualifiedHash) (forest.Node, bool, error)

func (*CacheStore) GetReply

func (m *CacheStore) GetReply(communityID, conversationID, replyID *fields.QualifiedHash) (forest.Node, bool, error)

func (*CacheStore) Recent

func (m *CacheStore) Recent(nodeType fields.NodeType, quantity int) ([]forest.Node, error)

func (*CacheStore) RemoveSubtree

func (m *CacheStore) RemoveSubtree(id *fields.QualifiedHash) error

type ExtendedStore

type ExtendedStore interface {
	forest.Store
	SubscribeToNewMessages(handler func(n forest.Node)) Subscription
	UnsubscribeToNewMessages(Subscription)
	AddAs(forest.Node, Subscription) (err error)
	AncestryOf(id *fields.QualifiedHash) ([]*fields.QualifiedHash, error)
	DescendantsOf(id *fields.QualifiedHash) ([]*fields.QualifiedHash, error)
	LeavesOf(id *fields.QualifiedHash) ([]*fields.QualifiedHash, error)
}

ExtendedStore provides a superset of the functionality of the Store interface, implementing methods for subscribing to changes and querying higher-level structural information like ancestry and descendants.

type MemoryStore

type MemoryStore struct {
	// Items is a flat map of all nodes.
	Items map[string]forest.Node
	// ChildMap describes the parent-child relationship between nodes.
	// Each list of child nodes is keyed by the parent node.
	ChildMap map[string][]string
}

MemoryStore is an in-memory node store.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore allocates a MemoryStore.

func (*MemoryStore) Add

func (m *MemoryStore) Add(node forest.Node) error

func (*MemoryStore) AddID

func (m *MemoryStore) AddID(id string, node forest.Node) error

func (*MemoryStore) Children

func (m *MemoryStore) Children(id *fields.QualifiedHash) ([]*fields.QualifiedHash, error)

func (*MemoryStore) CopyInto

func (m *MemoryStore) CopyInto(other forest.Store) error

func (*MemoryStore) Get

func (*MemoryStore) GetCommunity

func (m *MemoryStore) GetCommunity(id *fields.QualifiedHash) (forest.Node, bool, error)

func (*MemoryStore) GetConversation

func (m *MemoryStore) GetConversation(communityID, conversationID *fields.QualifiedHash) (forest.Node, bool, error)

func (*MemoryStore) GetID

func (m *MemoryStore) GetID(id string) (forest.Node, bool, error)

func (*MemoryStore) GetIdentity

func (m *MemoryStore) GetIdentity(id *fields.QualifiedHash) (forest.Node, bool, error)

func (*MemoryStore) GetReply

func (m *MemoryStore) GetReply(communityID, conversationID, replyID *fields.QualifiedHash) (forest.Node, bool, error)

func (*MemoryStore) Recent

func (m *MemoryStore) Recent(nodeType fields.NodeType, quantity int) ([]forest.Node, error)

Recent returns a slice of len `quantity` (or fewer) nodes of the given type. These nodes are the most recent (by creation time) nodes of that type known to the store.

func (*MemoryStore) RemoveSubtree

func (m *MemoryStore) RemoveSubtree(id *fields.QualifiedHash) error

type Subscription

type Subscription uint

Subscription is an identifier for a particular handler function within a SubscriberStore. It can be provided to delete a handler function or to suppress notifications to the corresponding handler.

Jump to

Keyboard shortcuts

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