note

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package note provides types and functions for interacting with a note maps data storage system.

Package note provides types and functions for interacting with a note maps data storage system.

Index

Constants

View Source
const (
	// EmptyLoader implements the Loader interface for a note map that is
	// always empty.
	EmptyLoader emptyLoader = 0
)

Variables

View Source
var (
	InvalidID = errors.New("invalid note id")
)

Functions

func Patch

func Patch(a *TruncatedNote, ops []Operation) error

Patch applies a set of operations to a.

Types

type Database

type Database interface {
	DatabaseReader
	DatabaseWriter
	io.Closer
}

Database provides atomic isolated read and write operations over a note map.

An instance of Database should be closed when it is no longer needed.

type DatabaseReader

type DatabaseReader interface {
	// IsolatedRead invokes f with a FindLoader that will read from an
	// unchanging version of the note map.
	IsolatedRead(f func(r FindLoader) error) error
}

DatabaseReader provides isolated read operations over a note map.

type DatabaseWriter

type DatabaseWriter interface {
	// IsolatedWrite invokes f with an isolated FindLoadPatcher that can read and
	// change a note map.
	//
	// If f returns an error, none of the changes will be saved. Implementations
	// should return an error in any case when changes are not saved.
	IsolatedWrite(f func(rw FindLoadPatcher) error) error
}

DatabaseWriter provides atomic isolated write operations over a note map.

type EmptyNote

type EmptyNote ID

EmptyNote is simply an empty GraphNote with nothing more than an ID.

func (EmptyNote) GetContents

func (x EmptyNote) GetContents() ([]GraphNote, error)

func (EmptyNote) GetID

func (x EmptyNote) GetID() ID

func (EmptyNote) GetTypes

func (x EmptyNote) GetTypes() ([]GraphNote, error)

func (EmptyNote) GetValue

func (x EmptyNote) GetValue() (string, GraphNote, error)

type FindLoadPatcher

type FindLoadPatcher interface {
	Finder
	Loader
	Patcher
}

FindLoadPatcher combines the Finder, Loader, and Patcher interfaces.

type FindLoader

type FindLoader interface {
	Finder
	Loader
}

FindLoader combines the Finder and Loader interfaces.

type Finder

type Finder interface {
	Find(*Query) ([]GraphNote, error)
}

Finder can be implemented to support finding notes in a note map according to a query.

type GraphNote

type GraphNote interface {
	GetID() ID
	GetValue() (string, GraphNote, error)
	GetContents() ([]GraphNote, error)
	GetTypes() ([]GraphNote, error)
}

GraphNote is a graph-like interface to a note in a note map.

Since traversing from note to note in a note map may require fragile operations like loading query results from a storage backend, most methods can return an error instead of the requested data.

func ExpandNote

func ExpandNote(tn TruncatedNote, l Loader) GraphNote

ExpandNote uses tn and l to provide a full GraphNote implementation.

func LoadOne

func LoadOne(l Loader, id ID) (GraphNote, error)

LoadOne is a convenience function for loading just one note.

type ID

type ID string

ID is the type of values that identify note.

const EmptyID ID = ""

EmptyID is the zero or nil value for note identifiers, and never identifies a valid note.

EmptyID exists only to make code that specifies the zero value for note identifiers a bit more readable.

func RandomID

func RandomID() ID

RandomID returns a pseudo-random ID using package math/rand.

func (ID) Empty

func (id ID) Empty() bool

func (ID) String

func (x ID) String() string

type IDSlice

type IDSlice []ID

func (IDSlice) Append

func (xs IDSlice) Append(add ...ID) IDSliceDelta

func (IDSlice) Apply

func (xs IDSlice) Apply(ops []IDSliceOp) IDSlice

func (IDSlice) CanApply

func (xs IDSlice) CanApply(ops []IDSliceOp) bool

func (IDSlice) Delete

func (xs IDSlice) Delete(i, num int) IDSliceDelta

func (IDSlice) DeleteElements

func (xs IDSlice) DeleteElements(del ...ID) IDSliceDelta

func (IDSlice) Insert

func (xs IDSlice) Insert(i int, add ...ID) IDSliceDelta

func (IDSlice) PrefixMatch

func (xs IDSlice) PrefixMatch(ys []ID) int

PrefixMatch returns the number of elements at the beginning of xs that match the elements at the beginning of ys.

func (IDSlice) Retain

func (xs IDSlice) Retain(r int) IDSliceDelta

func (IDSlice) String

func (ids IDSlice) String() string

type IDSliceDelta

type IDSliceDelta []IDSliceOp

func IDSliceDiff

func IDSliceDiff(a, b []ID) IDSliceDelta

IDSliceDiff produces a set of operations that can be applied to xs to produce a slice that would match slice b.

func (IDSliceDelta) Delete

func (x IDSliceDelta) Delete(d int) IDSliceDelta

func (IDSliceDelta) Insert

func (x IDSliceDelta) Insert(add ...ID) IDSliceDelta

func (IDSliceDelta) Rebase

func (x IDSliceDelta) Rebase(base IDSliceDelta) (IDSliceDelta, error)

func (IDSliceDelta) Retain

func (x IDSliceDelta) Retain(r int) IDSliceDelta

type IDSliceOp

type IDSliceOp interface {
	// Leaves returns how many elements of a slice of length n would remain
	// to be transformed by additional ops after applying this op. Returns
	// a negative number if and only if this op cannot be coherently
	// applied to a slice of length n.
	Leaves(n int) int
	// Len returns the number of elements inserted, retained, or deleted by
	// this op.
	Len() int
	// Skip returns an equivalent op that assumes its intent is already carried
	// out for the first n elements. May panic if n > Len().
	Skip(n int) IDSliceOp
	// Rebase transforms op into a rebased op r (or nil), a subsequent op for
	// rebasing xn (or nil), and a subsequent base bn (or nil).
	Rebase(base IDSliceOp) (r IDSliceOp, xn IDSliceOp, bn IDSliceOp)
	// Compact expands this op to include o if possible, returning true if
	// successful.
	Compact(o IDSliceOp) (IDSliceOp, bool)
	Apply(IDSlice) (include IDSlice, remainder IDSlice)
	String() string
}

type IDSliceOpDelete

type IDSliceOpDelete int

func (IDSliceOpDelete) Apply

func (x IDSliceOpDelete) Apply(xs IDSlice) (IDSlice, IDSlice)

func (IDSliceOpDelete) Compact

func (x IDSliceOpDelete) Compact(op IDSliceOp) (IDSliceOp, bool)

func (IDSliceOpDelete) Leaves

func (x IDSliceOpDelete) Leaves(in int) int

func (IDSliceOpDelete) Len

func (x IDSliceOpDelete) Len() int

func (IDSliceOpDelete) Rebase

func (IDSliceOpDelete) Skip

func (x IDSliceOpDelete) Skip(n int) IDSliceOp

func (IDSliceOpDelete) String

func (x IDSliceOpDelete) String() string

type IDSliceOpInsert

type IDSliceOpInsert []ID

func (IDSliceOpInsert) Apply

func (x IDSliceOpInsert) Apply(xs IDSlice) (IDSlice, IDSlice)

func (IDSliceOpInsert) Compact

func (x IDSliceOpInsert) Compact(op IDSliceOp) (IDSliceOp, bool)

func (IDSliceOpInsert) Leaves

func (x IDSliceOpInsert) Leaves(in int) int

func (IDSliceOpInsert) Len

func (x IDSliceOpInsert) Len() int

func (IDSliceOpInsert) Rebase

func (IDSliceOpInsert) Skip

func (x IDSliceOpInsert) Skip(n int) IDSliceOp

func (IDSliceOpInsert) String

func (x IDSliceOpInsert) String() string

type IDSliceOpRetain

type IDSliceOpRetain int

func (IDSliceOpRetain) Apply

func (x IDSliceOpRetain) Apply(xs IDSlice) (IDSlice, IDSlice)

func (IDSliceOpRetain) Compact

func (x IDSliceOpRetain) Compact(op IDSliceOp) (IDSliceOp, bool)

func (IDSliceOpRetain) Leaves

func (x IDSliceOpRetain) Leaves(in int) int

func (IDSliceOpRetain) Len

func (x IDSliceOpRetain) Len() int

func (IDSliceOpRetain) Rebase

func (IDSliceOpRetain) Skip

func (x IDSliceOpRetain) Skip(n int) IDSliceOp

func (IDSliceOpRetain) String

func (x IDSliceOpRetain) String() string

type Loader

type Loader interface {
	// Load returns a slice of all found notes.
	//
	// All notes exist implicitly, even if they are empty. An error indicates
	// something actually went wrong.
	Load(ids []ID) ([]GraphNote, error)
}

Loader can be implemented to support loading notes by id.

type NoteDelta

type NoteDelta []NoteOp

func NoteDeltaFromTruncatedNote

func NoteDeltaFromTruncatedNote(n TruncatedNote) NoteDelta

func (NoteDelta) ChangeContents

func (xs NoteDelta) ChangeContents(d IDSliceDelta) NoteDelta

func (NoteDelta) ChangeTypes

func (xs NoteDelta) ChangeTypes(d IDSliceDelta) NoteDelta

func (NoteDelta) ChangeValueString

func (xs NoteDelta) ChangeValueString(d runes.StringDelta) NoteDelta

func (NoteDelta) ChangeValueType

func (xs NoteDelta) ChangeValueType(vt ID) NoteDelta

func (NoteDelta) GetContentIDs

func (xs NoteDelta) GetContentIDs() IDSlice

func (NoteDelta) GetID

func (xs NoteDelta) GetID() ID

func (NoteDelta) GetTypeIDs

func (xs NoteDelta) GetTypeIDs() IDSlice

func (NoteDelta) GetValueString

func (xs NoteDelta) GetValueString() runes.String

func (NoteDelta) GetValueTypeID

func (xs NoteDelta) GetValueTypeID() ID

func (NoteDelta) SetID

func (xs NoteDelta) SetID(id ID) NoteDelta

func (NoteDelta) Truncate

func (xs NoteDelta) Truncate() TruncatedNote

type NoteMapDelta

type NoteMapDelta []NoteMapOp

func (NoteMapDelta) ChangeNote

func (xs NoteMapDelta) ChangeNote(n NoteDelta) NoteMapDelta

type NoteMapOp

type NoteMapOp interface{}

type NoteMapOpNoteDelta

type NoteMapOpNoteDelta NoteDelta

type NoteOp

type NoteOp interface{}

type NoteOpContentsDelta

type NoteOpContentsDelta IDSliceDelta

type NoteOpID

type NoteOpID ID

type NoteOpTypesDelta

type NoteOpTypesDelta IDSliceDelta

type NoteOpValueDelta

type NoteOpValueDelta runes.StringDelta

type NoteOpValueTypeDelta

type NoteOpValueTypeDelta ID

type Op

type Op ID

Op is a minimal implementation of Operation meant to be used as a mixin for operations that affect only one note.

func (Op) AffectsID

func (x Op) AffectsID(id ID) bool

AffectsID returns true if x could change a note with ID==id.

func (Op) GetID

func (x Op) GetID() ID

type OpContentDelta

type OpContentDelta OpIDSliceDelta

func (OpContentDelta) String

func (o OpContentDelta) String() string

type OpIDSliceDelta

type OpIDSliceDelta struct {
	Op
	IDSliceOps []IDSliceOp
}

func (OpIDSliceDelta) String

func (o OpIDSliceDelta) String() string

type OpSetValue

type OpSetValue struct {
	Op
	Lexical  string
	Datatype ID
}

OpSetValue sets the value and data type of a note to Lexical and Datatype.

func (OpSetValue) String

func (os OpSetValue) String() string

type OpSetValueString

type OpSetValueString struct {
	Op
	Lexical string
}

OpSetValueString sets the value of a note to Lexical.

func (OpSetValueString) String

func (os OpSetValueString) String() string

type OpTypesDelta

type OpTypesDelta OpIDSliceDelta

func (OpTypesDelta) String

func (o OpTypesDelta) String() string

type Operation

type Operation interface {
	AffectsID(id ID) bool
}

Operation is implemented by types that can describe changes that might be made to a note map.

func Diff

func Diff(a, b TruncatedNote) []Operation

Diff produces a set of operations that if applied to a would make it match b.

Differences in ID are not considered: a and b are not required to have the same ID, and applying the operations to a will not cause it to have the same ID as b.

type OperationSlice

type OperationSlice []Operation

func (OperationSlice) InsertContent

func (os OperationSlice) InsertContent(id ID, index int, cs ...ID) OperationSlice

InsertContent returns a new OperationSlice that also inserts cs to the contents of note id at index.

func (OperationSlice) PatchContent

func (os OperationSlice) PatchContent(id ID, ops []IDSliceOp) OperationSlice

PatchContent returns a new OperationSlice that also applies ops to the contents of note id.

func (OperationSlice) PatchTypes

func (os OperationSlice) PatchTypes(id ID, ops []IDSliceOp) OperationSlice

PatchTypes returns a new OperationSlice that also applies ops to the types of note id.

func (OperationSlice) SetValue

func (os OperationSlice) SetValue(id ID, vs string, vt ID) OperationSlice

SetValue returns a new OperationSlice that also sets the value and type of note id to vs and vt.

func (OperationSlice) SetValueString

func (os OperationSlice) SetValueString(id ID, vs string) OperationSlice

SetValue returns a new OperationSlice that also sets the value of note id to vs.

type Patcher

type Patcher interface {
	Patch(ops []Operation) error
}

Patcher can be implemented to support making changes to notes in a note map by applying a set of differences to them.

type Plain

type Plain struct {
	ID          ID
	ValueString string
	ValueType   *Plain
	Contents    []*Plain
	Types       []*Plain
}

Plain is a "plain old Go object" representation of a note that uses pointers to refer directly to related notes in memory.

Most of package notes is designed for working with entire note maps which may not fit in memory. Plain is better suited for working with limited subgraphs, for example when decoding subgraphs that are to be merged into a note map.

func (*Plain) GraphNote

func (x *Plain) GraphNote() GraphNote

GraphNote returns a proxy to x that implements the GraphNote interface.

type Query

type Query struct {
}

Query limits the notes that will be found when loading information from a graph.

The default query matches all notes.

type Stage

type Stage struct {
	Ops  OperationSlice
	Base Loader
}

Stage describes a set of changes that might be made to a note map.

The default stage describes an empty set of changes to be made to an empty note map.

A default Stage{} is an empty set of changes made to an empty note map.

func (*Stage) Add

func (x *Stage) Add(o Operation) *Stage

Add simply appends o to the set of operations described by x.

func (*Stage) GetBase

func (x *Stage) GetBase() Loader

GetBase returns a non-nil Loader derived from x.Base.

func (*Stage) Note

func (x *Stage) Note(id ID) *StageNote

Note returns a note-specific StageNote focused on note with id.

type StageNote

type StageNote struct {
	Stage *Stage
	ID    ID
}

StageNote supports updating the content of a note within a batch, and also implements the GraphNote interface to read the hypothetical state of a note with the batch applied.

func MustStageNote

func MustStageNote(n *StageNote, err error) *StageNote

func (*StageNote) AddContent

func (x *StageNote) AddContent(id ID) (*StageNote, error)

AddContent expands the staged operations to add content to this note.

func (*StageNote) GetContentIDs

func (x *StageNote) GetContentIDs() ([]ID, error)

func (*StageNote) GetContents

func (x *StageNote) GetContents() ([]GraphNote, error)

func (*StageNote) GetID

func (x *StageNote) GetID() ID

func (*StageNote) GetTypeIDs

func (x *StageNote) GetTypeIDs() ([]ID, error)

func (*StageNote) GetTypes

func (x *StageNote) GetTypes() ([]GraphNote, error)

func (*StageNote) GetValue

func (x *StageNote) GetValue() (string, GraphNote, error)

func (*StageNote) InsertTypes

func (x *StageNote) InsertTypes(i int, add ...ID) error

AddContent expands the staged operations to add content to this note.

func (*StageNote) Note

func (x *StageNote) Note(id ID) *StageNote

Note returns the identified note from the underlying stage.

func (*StageNote) SetValue

func (x *StageNote) SetValue(lexical string, datatype ID) error

SetValue expands the staged operations to update the value of this note.

type TruncatedNote

type TruncatedNote struct {
	ID          ID
	ValueString string
	ValueType   ID
	Contents    []ID
	Types       []ID
}

TruncatedNote is a minimal representation of a note intended for storage integrations and for algorithms that don't need to traverse a graph of note.

func TruncateNote

func TruncateNote(n GraphNote) (TruncatedNote, error)

TruncateNote returns a TruncatedNote representation of n.

func (TruncatedNote) Equals

func (x TruncatedNote) Equals(y TruncatedNote) bool

Equals return true if and only if x is deeply equal to y.

Directories

Path Synopsis
Package textile uses Textileio ThreadsDB to implement the github.com/google/note-maps/note interfaces.
Package textile uses Textileio ThreadsDB to implement the github.com/google/note-maps/note interfaces.
Package yaml marshals notes to and from YAML format.
Package yaml marshals notes to and from YAML format.

Jump to

Keyboard shortcuts

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