Documentation
¶
Index ¶
- Constants
- Variables
- func ToLower(terms interface{}) interface{}
- func WhiteSpaceTokenizer(text string) []string
- type Collection
- type Document
- type DocumentWalker
- type Entry
- type FieldIndexer
- type Index
- type IndexOption
- type Key
- type Query
- type QueryPart
- type Reference
- type ReferenceFunc
- type ReferenceScanFn
- type Store
- type Tokenizer
- type Transform
Constants ¶
const KeyDelimiter = 0x10
Variables ¶
var ErrInvalidJSON = errors.New("invalid json")
ErrInvalidJSON is returned when invalid JSON is parsed
var ErrNoIndex = errors.New("no index found")
ErrNoIndex is returned when no index is found to query against
var ErrNoQuery = errors.New("no query given")
ErrNoQuery is returned when an empty query is given
Functions ¶
func ToLower ¶ added in v0.5.0
func ToLower(terms interface{}) interface{}
ToLower transforms all Unicode letters mapped to their lower case. It only transforms objects that conform to the Stringer interface.
func WhiteSpaceTokenizer ¶ added in v0.5.0
WhiteSpaceTokenizer tokenizes the string based on the /\S/g regex
Types ¶
type Collection ¶
type Collection interface { // AddIndex to this collection. It doesn't matter if the index already exists. // If you want to override an index (by name) drop it first. AddIndex(index ...Index) error // DropIndex by name DropIndex(name string) error // Add a set of documents to this collection Add(jsonSet []Document) error // Get returns the data for the given key or nil if not found Get(ref Reference) (*Document, error) // Delete a document Delete(doc Document) error // Find queries the collection for documents // returns ErrNoIndex when no suitable index can be found Find(query Query) ([]Document, error) // Reference uses the configured reference function to generate a reference of the function Reference(doc Document) Reference // Iterate over documents that match the given query Iterate(query Query, walker DocumentWalker) error // IndexIterate is used for iterating over indexed values. The query keys must match exactly with all the FieldIndexer.Name() of an index // returns ErrNoIndex when no suitable index can be found IndexIterate(query Query, fn ReferenceScanFn) error }
Collection defines a logical collection of documents and indices within a store.
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document represents a JSON document in []byte format
func DocumentFromBytes ¶ added in v1.0.0
DocumentFromBytes creates a Document from a JSON string
func DocumentFromString ¶ added in v1.0.0
DocumentFromString creates a Document from a JSON string
func (Document) KeysAtPath ¶ added in v1.0.0
KeysAtPath returns the values found at the JSON path query as Key
func (Document) ValuesAtPath ¶ added in v1.0.0
ValuesAtPath returns a slice with the values found at the given JSON path query
type DocumentWalker ¶ added in v1.0.0
DocumentWalker defines a function that is used as a callback for matching documents. The key will be the document Reference (hash) and the value will be the raw document bytes
type Entry ¶
type Entry struct { // RefSize defines the number of bytes per reference RefSize int // contains filtered or unexported fields }
Entry holds references to documents from specific values that can be used in a search. It requires references to be of equal size
func EntryFrom ¶
EntryFrom creates an Entry from a reference. The RefSize is based on the number of bytes given
type FieldIndexer ¶ added in v1.0.0
type FieldIndexer interface { // Name is used for matching against a Query Name() string // Keys returns the keys that matched this document. Multiple keys are combined by the index Keys(document Document) ([]Key, error) // Tokenize may split up Keys and search terms. For example split a sentence into words. Tokenize(value interface{}) []interface{} // Transform is a function that alters the value to be indexed as well as any search criteria. // For example LowerCase is a Transform function that transforms the value to lower case. Transform(value interface{}) interface{} }
FieldIndexer is the public interface that defines functions for a field index instruction. A FieldIndexer is used when a document is indexed.
func NewFieldIndexer ¶ added in v1.0.0
func NewFieldIndexer(jsonPath string, options ...IndexOption) FieldIndexer
NewFieldIndexer creates a new fieldIndexer leave the name empty to use the json path as name. the name is to be used as query key when searching
type Index ¶
type Index interface { // Name returns the name of this index Name() string // Add indexes the document. It uses a sub-bucket of the given bucket. // It will only be indexed if the complete index matches. Add(bucket *bbolt.Bucket, ref Reference, doc Document) error // Delete document from the index Delete(bucket *bbolt.Bucket, ref Reference, doc Document) error // IsMatch determines if this index can be used for the given query. The higher the return value, the more likely it is useful. // return values lie between 0.0 and 1.0, where 1.0 is the most useful. IsMatch(query Query) float64 // Iterate over the key/value pairs given a query. Entries that match the query are passed to the iteratorFn. Iterate(bucket *bbolt.Bucket, query Query, fn iteratorFn) error // BucketName returns the bucket name for this index BucketName() []byte // Sort the query so its parts align with the index parts. // includeMissing, if true, the sort will append queryParts not matched by an index at the end. Sort(query Query, includeMissing bool) ([]QueryPart, error) // QueryPartsOutsideIndex selects the queryParts that are not convered by the index. QueryPartsOutsideIndex(query Query) ([]QueryPart, error) // Depth returns the number of indexed fields Depth() int }
Index describes an index. An index is based on a json path and has a name. The name is used for storage but also as identifier in search options.
func NewIndex ¶
func NewIndex(name string, parts ...FieldIndexer) Index
NewIndex creates a new blank index. If multiple parts are given, a compound index is created.
type IndexOption ¶ added in v1.0.0
type IndexOption func(fieldIndexer *fieldIndexer)
IndexOption is the option function for adding options to a FieldIndexer
func AliasOption ¶ added in v1.0.0
func AliasOption(alias string) IndexOption
AliasOption is the option for a FieldIndexer to add a custom JSON path that will also resolve to the same Index part
func TokenizerOption ¶ added in v1.0.0
func TokenizerOption(tokenizer Tokenizer) IndexOption
TokenizerOption is the option for a FieldIndexer to split a value to be indexed into multiple parts. Each part is then indexed separately.
func TransformerOption ¶ added in v1.0.0
func TransformerOption(transformer Transform) IndexOption
TransformerOption is the option for a FieldIndexer to apply transformation before indexing the value. The transformation is also applied to a query value that matches the indexed field.
type Key ¶
type Key []byte
Key is used as DB key type
func ComposeKey ¶
ComposeKey creates a new key from two keys
type Query ¶
type QueryPart ¶
type QueryPart interface { // Name returns the name that matches fieldIndexer.Name() so actually the alias or JSON path Name() string // Seek returns the key for cursor.Seek Seek() (Key, error) // Condition returns true if given key falls within this condition. // The optional transform fn is applied to this query part before evaluation is done. Condition(key Key, transform Transform) (bool, error) }
type Reference ¶
type Reference []byte
Reference equals a document hash. In an index, the values are references to docs.
func (Reference) EncodeToString ¶
EncodeToString encodes the reference as hex encoded string
type ReferenceFunc ¶
ReferenceFunc is the func type used for creating references. references are the key under which a document is stored. a ReferenceFunc could be the sha256 func or something that stores document in chronological order. The first would be best for random access, the latter for chronological access
type ReferenceScanFn ¶ added in v1.0.0
ReferenceScanFn is a function type which is called with an index key and a document Reference as value
type Store ¶
type Store interface { // Collection creates or returns a collection. // On the db level it's a bucket for the documents and 1 bucket per index. Collection(name string) Collection // Close closes the bbolt DB Close() error }
Store is the main interface for storing/finding documents