ds

package
v0.0.48-fix Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2020 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package ds provides a Document store.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DocumentPaths

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

DocumentPaths from Document's.

func FirstPathComponent

func FirstPathComponent(path string) string

FirstPathComponent returns first path component.

func LastPathComponent

func LastPathComponent(path string) string

LastPathComponent returns last path component.

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 SetLogger

func SetLogger(l Logger)

SetLogger sets logger for the package.

func Spew

func Spew(iter DocumentIterator, opts *SpewOpts) (*bytes.Buffer, error)

Spew writes DocumentIterator to buffer.

func SpewOut

func SpewOut(iter DocumentIterator, opts *SpewOpts, out io.Writer) error

SpewOut writes DocumentIterator to io.Writer. You need to specify a path or prefix, since listing root is not supported.

Types

type Change

type Change struct {
	Path      string    `json:"path" firestore:"path"`
	Timestamp time.Time `json:"ts" firestore:"ts"`
}

Change is used to track changes at a path. If this format changes, you should also change in firestore and other backends that don't directly use this struct on set.

type Changes

type Changes interface {
	ChangeAdd(ctx context.Context, name string, id string, ref string) error
	Changes(ctx context.Context, name string, from time.Time, limit int, direction Direction) ([]*Change, time.Time, error)
}

Changes describes changes to a path.

type Collection

type Collection struct {
	// Path to Document's.
	Path string
}

Collection is a location for Document's.

func CollectionsFromIterator

func CollectionsFromIterator(iter CollectionIterator) ([]*Collection, error)

CollectionsFromIterator returns Collection's from CollectionIterator.

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 Direction

type Direction string

Direction is ascending or descending.

const (
	// Ascending direction.
	Ascending Direction = "asc"
	// Descending direction.
	Descending Direction = "desc"
)

type Document

type Document struct {
	// Path of document.
	Path string
	// Data ...
	Data []byte

	// 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
}

Document is a data at a path with metadata.

func DocumentsFromIterator

func DocumentsFromIterator(iter DocumentIterator) ([]*Document, error)

DocumentsFromIterator returns Document's from DocumentIterator.

func NewDocument

func NewDocument(path string, data []byte) *Document

NewDocument creates a datastore document.

func (*Document) Contains

func (d *Document) Contains(contains string) bool

Contains returns true if path or value contains the string.

func (*Document) Pretty

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

Pretty returns "prettified" output, if data is a format that supports it.

func (Document) String

func (d Document) String() string

type DocumentIterator

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

DocumentIterator is an iterator for Document's.

func NewDocumentIterator

func NewDocumentIterator(docs []*Document) DocumentIterator

NewDocumentIterator returns an iterator for a Document slice.

type DocumentStore

type DocumentStore interface {
	// Create data at path.
	// ErrPathExists if path already exists.
	Create(ctx context.Context, path string, b []byte) error

	// Create or set data at path.
	Set(ctx context.Context, path string, b []byte) error

	// Get path.
	// If not found, returns nil.
	Get(ctx context.Context, path string) (*Document, 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)
	// If a path is not found, it is ignored.
	DeleteAll(ctx context.Context, paths []string) error

	// Documents for Document's.
	Documents(ctx context.Context, parent string, opts *DocumentsOpts) (DocumentIterator, error)

	// Collections are parents of Document's.
	Collections(ctx context.Context, parent string) (CollectionIterator, error)
}

DocumentStore is a place for Document's.

type DocumentsOpts

type DocumentsOpts 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
	// PathOnly to only include only path in Document (no data).
	PathOnly bool
}

DocumentsOpts are options for iterating documents.

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 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 DocumentStore implementation.

func NewMem

func NewMem() *Mem

NewMem creates an in memory DocumentStore implementation.

func (*Mem) ChangeAdd

func (m *Mem) ChangeAdd(ctx context.Context, name string, id string, ref string) error

ChangeAdd ...

func (*Mem) Changes

func (m *Mem) Changes(ctx context.Context, name string, ts time.Time, limit int, direction Direction) ([]*Change, time.Time, error)

Changes ...

func (*Mem) Collections

func (m *Mem) Collections(ctx context.Context, parent string) (CollectionIterator, error)

Collections ...

func (*Mem) Create

func (m *Mem) Create(ctx context.Context, path string, b []byte) error

Create at path. ErrPathExists if entry already exists.

func (*Mem) Delete

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

Delete ...

func (*Mem) DeleteAll

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

DeleteAll ...

func (*Mem) Documents

func (m *Mem) Documents(ctx context.Context, parent string, opts *DocumentsOpts) (DocumentIterator, error)

Documents ...

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 data at path.

func (*Mem) GetAll

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

GetAll paths

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, b []byte) error

Set data at path.

func (*Mem) SetTimeNow

func (m *Mem) SetTimeNow(nowFn func() time.Time)

SetTimeNow to use a custom time.Now.

func (*Mem) StopWatching

func (m *Mem) StopWatching(path string)

StopWatching ...

func (*Mem) StopWatchingAll

func (m *Mem) StopWatchingAll()

StopWatchingAll ...

func (*Mem) URI

func (m *Mem) URI() string

URI ...

func (*Mem) Watch

func (m *Mem) Watch(path string, ln WatchLn) error

Watch ...

type PathType

type PathType string

PathType denotes the type of path.

const KeyPathType PathType = "key"

KeyPathType is a path with 2 components, meant for a syncable key/value store, like Firebase or leveldb.

const URLPathType PathType = "url"

URLPathType is a path with more than 2 components for web APIs.

type SpewFormat

type SpewFormat string

SpewFormat is format for Spew.

const (
	// SpewFormatDefault ...
	SpewFormatDefault SpewFormat = ""
	// SpewFormatTable is in a grid, each entry separated by newlines.
	SpewFormatTable SpewFormat = "table"
	// SpewFormatFlat are fields separated by newlines and entries separated by empty lines.
	SpewFormatFlat SpewFormat = "flat"
)

type SpewOpts

type SpewOpts struct {
	Format SpewFormat
}

SpewOpts are options for Spew.

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.

type Watch

type Watch interface {
	Watch(path string, ln WatchLn) error
	StopWatching(path string)
	StopWatchingAll()
}

Watch for changes at path.

type WatchEvent

type WatchEvent struct {
	Status WatchStatus
	Path   string
}

WatchEvent gives updates to watch status and version.

type WatchLn

type WatchLn func(*WatchEvent)

WatchLn is a listener that receives WatchEvent.

type WatchStatus

type WatchStatus string

WatchStatus is status for watch.

const (
	// WatchStatusNone is an known status
	WatchStatusNone WatchStatus = ""
	// WatchStatusStarting is a status for when watch is starting
	WatchStatusStarting WatchStatus = "starting"
	// WatchStatusStopping is a status for when watch is stopping
	WatchStatusStopping WatchStatus = "stopping"
	// WatchStatusData is a status for when data has changed
	WatchStatusData WatchStatus = "data"
)

Jump to

Keyboard shortcuts

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