store

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package store stores a text.Text collection somewhere. Typically, use the github.com/lukasschwab/tiir/pkg/tir.Interface provided by github.com/lukasschwab/tiir/pkg/config.Load, rather than constructing a store directly, to use the user-configured store.

An initialized store must be closed: call Close when you're done writing to the Interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	// Mutex for file handle operations.
	sync.Mutex
	// contains filtered or unexported fields
}

File implements Interface; see UseFile.

Loads all texts from the underlying os.File into a memory store as an intermediate form for each operation; most of the store logic is delegated to the memory implementation.

func (*File) Close

func (f *File) Close() error

Close implements Interface.

func (*File) Delete

func (f *File) Delete(id string) (*text.Text, error)

Delete implements Interface.

func (*File) List

func (f *File) List(c text.Comparator, d text.Direction) ([]*text.Text, error)

List implements Interface.

func (*File) Read

func (f *File) Read(id string) (*text.Text, error)

Read implements Interface.

func (*File) Upsert

func (f *File) Upsert(t *text.Text) (*text.Text, error)

Upsert implements Interface.

type HTTP

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

HTTP implements Interface for a remote cmd/server process. See UseHTTP.

NOTE: if we had a proto-defined service, this would probably wrap that (and the generated code would determine the inner values necessary to specify and connect). As it is, this mirrors the routes and renderers exposed by github.com/lukasschwab/tiir/cmd/server; these are hidden dependencies!

func (*HTTP) Close

func (h *HTTP) Close() error

Close implements Interface.

func (*HTTP) Delete

func (h *HTTP) Delete(id string) (*text.Text, error)

Delete implements Interface.

func (*HTTP) List

func (h *HTTP) List(c text.Comparator, d text.Direction) ([]*text.Text, error)

List implements Interface. It re-sorts the response accoding to c and d.

func (*HTTP) Read

func (h *HTTP) Read(id string) (*text.Text, error)

Read implements Interface.

func (*HTTP) Upsert

func (h *HTTP) Upsert(t *text.Text) (*text.Text, error)

Upsert implements Interface. It reads before writing to decide whether to call the server's POST route or its PATCH route, since cmd/server doesn't expose an upsert route.

type Interface

type Interface interface {
	// Close the Store, rendering it unusable for future operations.
	io.Closer
	// Read a text by ID.
	Read(id string) (*text.Text, error)
	// Delete a text by ID and return the deleted text.
	Delete(id string) (*text.Text, error)
	// Upsert a text by t.ID and return the resulting text. Assumes t.ID is set
	// and t is valid; see (*text.Text).Validate(...).
	Upsert(t *text.Text) (*text.Text, error)
	// List all texts in the store in order.
	List(c text.Comparator, d text.Direction) ([]*text.Text, error)
}

Interface for storing texts somewhere. An initialized store must be closed: call Close when you're done writing to the store.

func UseFile

func UseFile(path string) (Interface, error)

UseFile at path as a JSON store. If the file doesn't exist, it's created and initialized to an empty store.

If you don't call File.Close, the underlying os.File won't be closed.

func UseHTTP

func UseHTTP(baseURL, apiSecret string) (Interface, error)

UseHTTP requests to a remote github.com/lukasschwab/tiir/cmd/server instance (hosted at baseURL, accepting secret apiSecret) to read and write texts.

func UseMemory

func UseMemory(initialTexts ...*text.Text) Interface

UseMemory constructs a new in-memory store containing initialTexts.

Because it doesn't persist texts after the program terminates, memory stores are best suited for testing and for intermediate formats used by other Stores.

type Memory

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

Memory implements Interface in-memory. See UseMemory.

func (*Memory) Close

func (m *Memory) Close() error

Close implements Interface.

func (*Memory) Delete

func (m *Memory) Delete(id string) (*text.Text, error)

Delete implements Interface.

func (*Memory) List

func (m *Memory) List(c text.Comparator, d text.Direction) ([]*text.Text, error)

List implements Interface.

func (*Memory) Read

func (m *Memory) Read(id string) (*text.Text, error)

Read implements Interface.

func (*Memory) Upsert

func (m *Memory) Upsert(t *text.Text) (*text.Text, error)

Upsert implements Interface.

Jump to

Keyboard shortcuts

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