nosql

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2022 License: Apache-2.0 Imports: 10 Imported by: 16

Documentation

Index

Constants

View Source
const (
	Equal = FilterOp(iota + 1)
	NotEqual
	GT
	GTE
	LT
	LTE
	Regexp
)
View Source
const (
	IndexAny    = IndexType(iota)
	StringExact // exact match for string values (usually a hash index)

)

Variables

View Source
var (
	ErrNotFound = errors.New("not found")
)

Functions

func CompareValues

func CompareValues(v1, v2 Value) int

CompareValues return 0 if values are equal, positive value if first value sorts after second, and negative otherwise.

func Register

func Register(reg Registration)

Register globally registers a database driver.

func ValuesEqual

func ValuesEqual(v1, v2 Value) bool

ValuesEqual returns true if values are strictly equal.

Types

type BatchInserter

type BatchInserter interface {
	BatchInsert(col string) DocWriter
}

BatchInserter is an optional interface for databases that can insert documents in batches.

type Bool

type Bool bool

Bool is a boolean value.

type Bytes

type Bytes []byte

Bytes is a raw binary data.

Some databases has no type to represent binary data. In this case base64 representation can be used and package will take care of converting it.

type Database

type Database interface {
	// Insert creates a document with a given key in a given collection.
	// Key can be nil meaning that implementation should generate a unique key for the item.
	// It returns the key that was generated, or the same key that was passed to it.
	Insert(ctx context.Context, col string, key Key, d Document) (Key, error)
	// FindByKey finds a document by it's Key. It returns ErrNotFound if document not exists.
	FindByKey(ctx context.Context, col string, key Key) (Document, error)
	// Query starts construction of a new query for a specified collection.
	Query(col string) Query
	// Update starts construction of document update request for a specified document and collection.
	Update(col string, key Key) Update
	// Delete starts construction of document delete request.
	Delete(col string) Delete
	// EnsureIndex creates or updates indexes on the collection to match it's arguments.
	// It should create collection if it not exists. Primary index is guaranteed to be of StringExact type.
	EnsureIndex(ctx context.Context, col string, primary Index, secondary []Index) error
	// Close closes the database connection.
	Close() error
}

Database is a minimal interface for NoSQL database implementations.

type Delete

type Delete interface {
	// WithFields adds specified filters to select document for deletion.
	WithFields(filters ...FieldFilter) Delete
	// Keys limits a set of documents to delete to ones with keys specified.
	// Delete still uses provided filters, thus it will not delete objects with these keys if they do not pass filters.
	Keys(keys ...Key) Delete
	// Do executes batch delete.
	Do(ctx context.Context) error
}

Update is a batch delete request builder.

type DocIterator

type DocIterator interface {
	// Next advances an iterator to the next document.
	Next(ctx context.Context) bool
	// Err returns a last encountered error.
	Err() error
	// Close frees all resources associated with iterator.
	Close() error
	// Key returns a key of current document.
	Key() Key
	// Doc returns current document.
	Doc() Document
}

DocIterator is an iterator over a list of documents.

type DocWriter

type DocWriter interface {
	// WriteDoc prepares document to be written. Write becomes valid only after Flush.
	WriteDoc(ctx context.Context, key Key, d Document) error
	// Flush waits for all writes to complete.
	Flush(ctx context.Context) error
	// Keys returns a list of already inserted documents.
	// Might be less then a number of written documents until Flush is called.
	Keys() []Key
	// Close closes writer and discards any unflushed documents.
	Close() error
}

DocWriter is an interface for writing documents in streaming manner.

func BatchInsert

func BatchInsert(db Database, col string) DocWriter

BatchInsert returns a streaming writer for database or emulates it if database has no support for batch inserts.

type Document

type Document map[string]Value

Document is a type of item stored in nosql database.

type FieldFilter

type FieldFilter struct {
	Path   []string // path is a path to specific field in the document
	Filter FilterOp // comparison operation
	Value  Value    // value that will be compared with field of the document
}

FieldFilter represents a single field comparison operation.

func (FieldFilter) Matches

func (f FieldFilter) Matches(d Document) bool

type FilterOp

type FilterOp int

FilterOp is a comparison operation type used for value filters.

func (FilterOp) GoString

func (op FilterOp) GoString() string

func (FilterOp) String

func (op FilterOp) String() string

type Float

type Float float64

Float is an floating point value.

Some databases might not distinguish Int value from Float. In this case the package will take care of converting it to a correct type.

type Index

type Index struct {
	Fields []string // an ordered set of fields used in index
	Type   IndexType
}

Index is an index for a collection of documents.

type IndexType

type IndexType int

IndexType is a type of index for collection.

type Int

type Int int64

Int is an int value.

Some databases might not distinguish Int value from Float. In this case implementation will take care of converting it to a correct type.

type Key

type Key []string

Key is a set of values that describe primary key of document.

func GenKey

func GenKey() Key

GenKey generates a unique key (with one field).

func KeyFrom

func KeyFrom(fields []string, doc Document) Key

KeyFrom extracts a set of fields as a Key from Document.

func (Key) Value

func (k Key) Value() Value

Value converts a Key to a value that can be stored in the database.

type OpenFunc

type OpenFunc func(addr string, ns string, opt Options) (Database, error)

OpenFunc is a function for opening a database given a address and the database name.

type Options

type Options map[string]interface{}

func (Options) GetString

func (o Options) GetString(key string, def string) string

type Query

type Query interface {
	// WithFields adds specified filters to the query.
	WithFields(filters ...FieldFilter) Query
	// Limit limits a maximal number of results returned.
	Limit(n int) Query

	// Count executes query and returns a number of items that matches it.
	Count(ctx context.Context) (int64, error)
	// One executes query and returns first document from it.
	One(ctx context.Context) (Document, error)
	// Iterate starts an iteration over query results.
	Iterate() DocIterator
}

Query is a query builder object.

type Registration

type Registration struct {
	base.Registration
	New, Open OpenFunc
	Traits    Traits
}

Registration is an information about the database driver.

func ByName

func ByName(name string) *Registration

ByName returns a registered database driver by it's name.

func List

func List() []Registration

List enumerates all globally registered database drivers.

type String

type String string

String is an UTF8 string value.

type Strings

type Strings []string

Strings is an array of strings. Used mostly to store Keys.

type Time

type Time time.Time

Time is a timestamp value.

Some databases has no type to represent time values. In this case string/json representation can be used and package will take care of converting it.

type Traits

type Traits struct {
	Number32   bool // store is limited to 32 bit precision
	FloatToInt bool // database silently converts all float values to ints (if possible)
	IntToFloat bool // database always converts all int values to floats
	TimeInMs   bool
	PageSize   int // result page size for pagination
}

type Update

type Update interface {
	// Inc increments document field with a given amount. Will also increment upserted document.
	Inc(field string, dn int) Update
	// Upsert sets a document that will be inserted in case original object does not exists already.
	// It should omit fields used by Inc - they will be added automatically.
	Upsert(d Document) Update
	// Do executes update request.
	Do(ctx context.Context) error
}

Update is an update request builder.

type Value

type Value interface {
	// contains filtered or unexported methods
}

Value is a interface that limits a set of types that nosql database can handle.

Directories

Path Synopsis
all

Jump to

Keyboard shortcuts

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