dstore

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2021 License: MIT Imports: 18 Imported by: 14

Documentation

Overview

Package dstore describes a document store.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Data

func Data(b []byte) map[string]interface{}

Data as document fields.

func Empty

func Empty() map[string]interface{}

Empty document.

func From

func From(i interface{}) map[string]interface{}

From interface to map. If error, nil is returned. Uses msgpack and will fallback to json tags if msgpack tags are missing.

func Load

func Load(ctx context.Context, d Documents, path string, v interface{}) (bool, error)

Load path into value.

func Path

func Path(paths ...interface{}) string

Path returns a path string from the specified paths or path components. The components can be strings, values with a String() function.

For example,

Path("a", "b") => "/a/b"
Path("") => "/"
Path("/a/") => "/a"
Path("/a//b") => "/a/b"

func PathComponents

func PathComponents(path string) []string

PathComponents returns the components of a path.

func PathFirst

func PathFirst(path string) string

PathFirst returns first path component.

func PathFrom

func PathFrom(path string, n int) string

PathFrom skips first n components.

func PathLast

func PathLast(path string) string

PathLast returns last path component.

func Paths

func Paths(docs []*Document) []string

Paths from Document's.

func SetLogger

func SetLogger(l Logger)

SetLogger sets logger for the package.

func Spew

func Spew(iter Iterator) (string, error)

Spew writes Iterator to buffer.

func SpewOut

func SpewOut(iter Iterator, out io.Writer) error

SpewOut writes Iterator to io.Writer.

Types

type Collection

type Collection struct {
	// Path for collection.
	Path string
}

Collection is a location for Document's.

type CollectionIterator

type CollectionIterator interface {
	// Next collection, or nil.
	Next() (*Collection, error)
	// Release resources associated with the iterator.
	Release()
}

CollectionIterator is an iterator for Collection's.

func NewCollectionIterator

func NewCollectionIterator(cols []*Collection) CollectionIterator

NewCollectionIterator returns an iterator for a Collection slice.

type ContextLogger

type ContextLogger interface {
	Debugf(ctx context.Context, format string, args ...interface{})
	Infof(ctx context.Context, format string, args ...interface{})
	Warningf(ctx context.Context, format string, args ...interface{})
	Errorf(ctx context.Context, format string, args ...interface{})
}

ContextLogger interface used in this package with request context.

func NewContextLogger

func NewContextLogger(lev LogLevel) ContextLogger

NewContextLogger ...

type Document

type Document struct {
	// Path of document.
	Path string

	// CreatedAt (read only). The time at which the document was created.
	CreatedAt time.Time
	// UpdatedAt (read only). The time at which the document was last changed.
	UpdatedAt time.Time
	// contains filtered or unexported fields
}

Document at a path.

func NewDocument

func NewDocument(path string) *Document

NewDocument creates a document with data.

func (*Document) Bytes

func (d *Document) Bytes(name string) []byte

Bytes returns document data.

func (*Document) Data

func (d *Document) Data() []byte

Data returns document data.

func (*Document) Get

func (d *Document) Get(name string) (interface{}, bool)

Get value.

func (*Document) Int

func (d *Document) Int(name string) (int, bool)

Int returns document value as int.

func (*Document) Int64

func (d *Document) Int64(name string) (int64, bool)

Int64 returns document value as int64.

func (*Document) Set

func (d *Document) Set(name string, i interface{})

Set value.

func (*Document) SetAll

func (d *Document) SetAll(m map[string]interface{})

SetAll values on document. Overwrites any existing values. We do not clone the map.

func (*Document) String

func (d *Document) String(name string) (string, bool)

Int returns document data.

func (*Document) To

func (d *Document) To(i interface{}) error

To value. Uses msgpack.

func (*Document) Values added in v0.1.20

func (d *Document) Values() map[string]interface{}

Values map.

func (*Document) With

func (d *Document) With(m map[string]interface{}) *Document

With returns document with values.

func (*Document) WithData

func (d *Document) WithData(b []byte) *Document

WithData returns document with data.

type Documents

type Documents interface {
	// Create document at path.
	// ErrPathExists if path already exists.
	//
	// To marshal a value, use dstore.From(v) to convert to a map (using msgpack or json tags).
	// If merging and using dstore.From(v), fields with omitempty will no overwrite existing values.
	//
	// Paths can be nested as long as they are even length components.
	// For example,
	//
	//   collection1/key1 (OK)
	//   collection1/key1/collection2/key2 (OK)
	//   collection1 (INVALID)
	//   collection1/key1/collection2 (INVALID)
	//
	Create(ctx context.Context, path string, values map[string]interface{}) error

	// Set (or create or update) document at path.
	// This will overwrite any existing document data, unless you specify MergeAll() option.
	//
	// To marshal a value, use dstore.From(v) to convert to a map (using msgpack or json tags).
	// If merging and using dstore.From(v), fields with omitempty will no overwrite existing values.
	//
	// To update a document:
	//
	// 		update := map[string]interface{}{
	//		   "property1":        value1,
	// 		}
	// 		err := fi.Set(ctx, path, update, dstore.MergeAll())
	//
	// Paths can be nested as long as they are even length components.
	// For example,
	//
	//   collection1/key1 (OK)
	//   collection1/key1/collection2/key2 (OK)
	//   collection1 (INVALID)
	//   collection1/key1/collection2 (INVALID)
	//
	Set(ctx context.Context, path string, values map[string]interface{}, opt ...SetOption) error

	// Get path.
	// If not found, returns nil.
	Get(ctx context.Context, path string) (*Document, error)

	// Load path into value.
	// This is shorthand for Get and doc.To(&val).
	Load(ctx context.Context, path string, v interface{}) (bool, error)

	// GetAll at paths.
	// If a path is not found, it is ignored.
	GetAll(ctx context.Context, paths []string) ([]*Document, error)

	// Exists, if exists at path.
	Exists(ctx context.Context, path string) (bool, error)

	// Delete at path.
	Delete(ctx context.Context, path string) (bool, error)
	// DeleteAll paths. If a path is not found, it is ignored.
	DeleteAll(ctx context.Context, paths []string) error

	// DocumentIterator.
	DocumentIterator(ctx context.Context, parent string, opt ...Option) (Iterator, error)

	// Documents ...
	Documents(ctx context.Context, parent string, opt ...Option) ([]*Document, error)

	// Collections are parents of Documents.
	Collections(ctx context.Context, parent string) ([]*Collection, error)
}

Documents describes a Document store.

type ErrNotFound

type ErrNotFound struct {
	Path string
}

ErrNotFound if path not found.

func NewErrNotFound

func NewErrNotFound(path string) ErrNotFound

NewErrNotFound ...

func (ErrNotFound) Error

func (e ErrNotFound) Error() string

type ErrPathExists

type ErrPathExists struct {
	Path string
}

ErrPathExists is trying to set value that already exists.

func NewErrPathExists

func NewErrPathExists(path string) ErrPathExists

NewErrPathExists ...

func (ErrPathExists) Error

func (e ErrPathExists) Error() string

type Iterator

type Iterator interface {
	// Next document, or nil.
	Next() (*Document, error)
	// Release resources associated with the iterator.
	Release()
}

Iterator is an iterator for Document's.

func NewIterator

func NewIterator(docs ...*Document) Iterator

NewIterator returns an iterator for a Document slice.

type LogLevel

type LogLevel int

LogLevel ...

const (
	// DebugLevel ...
	DebugLevel LogLevel = 3
	// InfoLevel ...
	InfoLevel LogLevel = 2
	// WarnLevel ...
	WarnLevel LogLevel = 1
	// ErrLevel ...
	ErrLevel LogLevel = 0
)

func (LogLevel) String

func (l LogLevel) String() string

type Logger

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
}

Logger interface used in this package.

func NewLogger

func NewLogger(lev LogLevel) Logger

NewLogger ...

type Mem

type Mem struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Mem is an in memory Documents implementation.

func NewMem

func NewMem() *Mem

NewMem creates an in memory Documents implementation.

func (*Mem) Collections

func (m *Mem) Collections(ctx context.Context, parent string) ([]*Collection, error)

Collections ...

func (*Mem) Create

func (m *Mem) Create(ctx context.Context, path string, values map[string]interface{}) error

Create document at path. ErrPathExists if entry already exists.

func (*Mem) Delete

func (m *Mem) Delete(ctx context.Context, path string) (bool, error)

Delete document at path.

func (*Mem) DeleteAll

func (m *Mem) DeleteAll(ctx context.Context, paths []string) error

DeleteAll deletes all documents at path.

func (*Mem) DocumentIterator

func (m *Mem) DocumentIterator(ctx context.Context, parent string, opt ...Option) (Iterator, error)

DocumentIterator ...

func (*Mem) Documents

func (m *Mem) Documents(ctx context.Context, parent string, opt ...Option) ([]*Document, error)

Documents ...

func (*Mem) EventAdd added in v0.1.22

func (m *Mem) EventAdd(ctx context.Context, path string, doc events.Document) (int64, error)

func (*Mem) EventPosition added in v0.1.22

func (m *Mem) EventPosition(ctx context.Context, path string) (*events.Position, error)

func (*Mem) EventPositions

func (m *Mem) EventPositions(ctx context.Context, paths []string) (map[string]*events.Position, error)

EventPositions returns positions for event logs at the specified paths.

func (*Mem) Events

func (m *Mem) Events(ctx context.Context, path string, opt ...events.Option) (events.Iterator, error)

Events ...

func (*Mem) EventsAdd

func (m *Mem) EventsAdd(ctx context.Context, path string, docs []events.Document) (int64, error)

EventsAdd adds events to path.

func (*Mem) EventsDelete

func (m *Mem) EventsDelete(ctx context.Context, path string) (bool, error)

EventsDelete removes all events at path.

func (*Mem) Exists

func (m *Mem) Exists(ctx context.Context, path string) (bool, error)

Exists returns true if path exists.

func (*Mem) Get

func (m *Mem) Get(ctx context.Context, path string) (*Document, error)

Get document at path.

func (*Mem) GetAll

func (m *Mem) GetAll(ctx context.Context, paths []string) ([]*Document, error)

GetAll paths.

func (*Mem) Increment added in v0.1.21

func (m *Mem) Increment(ctx context.Context, path string, name string, n int64) (int64, int64, error)

func (*Mem) Load

func (m *Mem) Load(ctx context.Context, path string, v interface{}) (bool, error)

Load path into value.

func (*Mem) Now

func (m *Mem) Now() time.Time

Now returns current time.

func (*Mem) Set

func (m *Mem) Set(ctx context.Context, path string, values map[string]interface{}, opt ...SetOption) error

Set document at path.

func (*Mem) SetClock

func (m *Mem) SetClock(clock tsutil.Clock)

SetClock to use a custom Clock (for testing).

func (*Mem) SetMode added in v0.1.21

func (m *Mem) SetMode(mode Mode)

SetMode to set a mode.

func (*Mem) Update

func (m *Mem) Update(ctx context.Context, path string, values map[string]interface{}) error

Update document.

type Mode added in v0.1.21

type Mode string

Mode for any special behavior.

const (
	FirestoreCompatibilityMode Mode = "firestore"
)

Modes for compability

type Option

type Option func(*Options)

Option ...

func Index

func Index(index int) Option

Index to start at.

func Limit

func Limit(limit int) Option

Limit number of results.

func NoData

func NoData() Option

NoData don't return data.

func Prefix

func Prefix(prefix string) Option

Prefix to list.

func Where added in v0.1.20

func Where(name string, op string, value interface{}) Option

Where name op value.

type Options

type Options struct {
	// Prefix to filter on.
	Prefix string
	// Index is offset into number of documents.
	Index int
	// Limit is number of documents (max) to return.
	Limit int
	// NoData to only include only path in Document (no data).
	NoData bool
	// Where
	Where *where
}

Options ...

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions parses Options.

type SetOption

type SetOption func(*SetOptions)

SetOption ...

func MergeAll

func MergeAll() SetOption

MergeAll merges values.

type SetOptions

type SetOptions struct {
	MergeAll bool
}

SetOptions ...

func NewSetOptions

func NewSetOptions(opts ...SetOption) SetOptions

NewSetOptions parses Options.

type StringSet

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

StringSet is a set of strings.

func NewStringSet

func NewStringSet(s ...string) *StringSet

NewStringSet creates StringSet.

func NewStringSetSplit

func NewStringSetSplit(s string, delim string) *StringSet

NewStringSetSplit creates StringSet for split string.

func NewStringSetWithCapacity

func NewStringSetWithCapacity(capacity int) *StringSet

NewStringSetWithCapacity ..

func (*StringSet) Add

func (s *StringSet) Add(str string)

Add to set.

func (*StringSet) AddAll

func (s *StringSet) AddAll(strs []string)

AddAll to set.

func (*StringSet) Clear

func (s *StringSet) Clear()

Clear set.

func (*StringSet) Contains

func (s *StringSet) Contains(str string) bool

Contains returns true if set contains string.

func (*StringSet) Remove

func (s *StringSet) Remove(str string)

Remove from set.

func (*StringSet) Size

func (s *StringSet) Size() int

Size for set.

func (*StringSet) Sorted

func (s *StringSet) Sorted() []string

Sorted returns strings in set, sorted.

func (*StringSet) Strings

func (s *StringSet) Strings() []string

Strings returns strings in set.

Directories

Path Synopsis
Package events provides an event log.
Package events provides an event log.

Jump to

Keyboard shortcuts

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