puredb

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2022 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRecordNotFound = errors.New("record not found")
	ErrTableNotFound  = errors.New("table not found")
)

Functions

func Expand

func Expand(query string) []byte

func IsOK

func IsOK(query string) bool

Types

type Collection

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

Collection is an interface representing a collection of records. It is synonymous with the notion of a table in a structured database.

type CollectionInfo

type CollectionInfo interface {

	// GetRecords should return the total record count
	// along with the record id's that are currently in
	// this collection
	GetRecords() ([]int, int)

	// GetLastID should return the id of the record that
	// was last inserted
	GetLastID() int
}

CollectionInfo is an interface representing a collection and is responsible for returning information about records located within the collection.

type DB

type DB interface {

	// MakeCollection is used to instantiate a new collection.
	// It should only be used when you with to make a collection,
	// but will not be using it straight away.
	MakeCollection(collection string) error

	// DropCollection removes the named collection and all the
	// data it contains permanently.
	DropCollection(collection string) error

	// Insert inserts a single record into the named collection
	// using the record id provided. The returned record is loaded
	// into the record pointer provided.
	Insert(collection string, rec Record) (int, error)

	// InsertMany inserts multiple records and returns a set of id's
	// for the records that we just inserted.
	InsertMany(collection string, recs []Record) ([]int, error)

	// Return returns a single record using the id provided. The
	// returned record is loaded into the pointer provided.
	Return(collection string, id int, ptr Record) error

	// ReturnAll returns all the records found in the named
	// collection. The returned records are loaded into the
	// set of pointers provided. It will return the number of
	// records returned and a nil error on success.
	ReturnAll(collection string, ptrs []Record) (int, error)

	// Update updates the selected record in the named collection
	// using the id and record provided.
	Update(collection string, id int, rec Record) error

	// Delete removes a record from a collection using the
	// collection name and record id provided.
	Delete(collection string, id int) error

	// Search is a general purpose query method used to find
	// and return a set of records within a certain collection
	// based on the query criteria. All records in the collection
	// that match the provided query should be returned into the
	// set of record pointers provided. It returns the number of
	// records that matched the query and a nil error on success.
	Search(collection string, query string, ptrs []Record) (int, error)

	// GetInfo returns an instance of DBInfo which provides information
	// about the database. Any mutations made on the database invalidates
	// the last call to GetInfo. A new call to GetInfo is required to get
	// the most recent information.
	GetInfo() DBInfo

	// GetCollectionInfo returns an instance of CollectionInfo which
	// provides information for the selected collection. Any mutations
	// made to a collection invalidates the last call to GetInfo. A new
	// call to GetInfo is required to get the most recent information.
	GetCollectionInfo(collection string) CollectionInfo

	// Close synchronizes and closes the database and all collections.
	Close() error
}

DB is an interface representing an embedded database engine that is able to perform all the basic operations necessary on collections and records.

type DBInfo

type DBInfo interface {

	// GetCollections returns a map where the map key is the
	// collection name, and the map value is an instance of
	// CollectionInfo for the named collection.
	GetCollections() map[string]CollectionInfo
}

DBInfo is an interface representing a database and is responsible for returning information about the database, collections and records.

type Info

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

func (*Info) GetCollections

func (i *Info) GetCollections() map[string]CollectionInfo

type PureDB

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

PureDB is a simple embedded database engine that stores records encoded in NDJSON or JSONL format.

func Open

func Open(path string) (*PureDB, error)

Open opens a database.

func (*PureDB) Close

func (db *PureDB) Close() error

func (*PureDB) Delete

func (db *PureDB) Delete(name string, id int) error

Delete takes a table name and a record id and removes the record that has a matching id.

func (*PureDB) DropCollection

func (db *PureDB) DropCollection(name string) error

DropCollection takes a table name and completely deletes all data within the table and removes the table file itself. It returns an error if the table is not found, or if there was trouble deleting it.

func (*PureDB) GetCollectionInfo

func (db *PureDB) GetCollectionInfo(name string) CollectionInfo

func (*PureDB) GetInfo

func (db *PureDB) GetInfo() DBInfo

func (*PureDB) Insert

func (db *PureDB) Insert(name string, rec Record) (int, error)

Insert takes a table name and a struct that implements the Record interface and adds a new record to the table. It will add duplicate records. It returns the ID of the record that was just added.

func (*PureDB) InsertMany

func (db *PureDB) InsertMany(name string, recs []Record) ([]int, error)

func (*PureDB) IsOpen

func (db *PureDB) IsOpen() bool

func (*PureDB) MakeCollection

func (db *PureDB) MakeCollection(name string) error

MakeCollection takes a table name and creates and initializes a new table. It returns an error if the table is already created. CreateTable should only be used in rare cases if you wish to initialize a table, but you are not going to use it right away.

func (*PureDB) Return

func (db *PureDB) Return(name string, id int, ptr Record) error

Return takes a table name and a pointer to a struct that implements the Record interface. The found record is unmarshalled into the provided pointer. If the provided id cannot be found or does not match a record in the table an ErrRecordNotFound error is returned.

func (*PureDB) ReturnAll

func (db *PureDB) ReturnAll(name string, ptrs interface{}) (int, error)

ReturnAll returns all the records found in the named collection. The returned records are loaded into the set of pointers provided. It will return the number of records returned and a nil error on success.

func (*PureDB) Search

func (db *PureDB) Search(name string, query string, ptrs interface{}) (int, error)

Search takes a table name, a record id, and a pointer to a struct that implements the Record interface, finds the associated record from the table, and populates the struct.

func (*PureDB) Update

func (db *PureDB) Update(name string, id int, rec Record) error

Update takes a table name and a struct implementing the Record interface. It updates the record in the table that has that matches the provided Record's ID.

type Query

type Query struct {
}

type Record

type Record interface {

	// GetID should return the id of the record
	GetID() int

	// SetID should set the id of the record by
	// using the id provided
	SetID(id int)
}

Record is an interface representing a single record able to be used by a collection.

type RecordSet

type RecordSet interface {

	// Len should return the number of records
	// in the record set.
	Len() int
}

RecordSet is an interface representing a set of records able to be used by a collection.

type TableInfo

type TableInfo struct {
	LastID      int   `json:"last_id"`
	Records     []int `json:"records"`
	RecordCount int   `json:"record_count"`
}

func (*TableInfo) GetLastID

func (i *TableInfo) GetLastID() int

func (*TableInfo) GetRecords

func (i *TableInfo) GetRecords() ([]int, int)

Jump to

Keyboard shortcuts

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