Documentation
¶
Index ¶
- Variables
- type Config
- type DB
- func (db *DB[T]) Close() error
- func (db *DB[T]) CreateIndex(name, pattern string, comparator ...func(a, b T) bool) error
- func (db *DB[T]) Del(key string) (prev *T, err error)
- func (db *DB[T]) DropIndex(name string) error
- func (db *DB[T]) Get(key string) (*T, error)
- func (db *DB[T]) Indexes() ([]string, error)
- func (db *DB[T]) Len() (int, error)
- func (db *DB[T]) Load(rd io.Reader) error
- func (db *DB[T]) ReadConfig(config *Config[T]) error
- func (db *DB[T]) ReplaceIndex(name, pattern string, comparator ...func(a, b T) bool) error
- func (db *DB[T]) Set(key string, val T, options ...*SetOptions) (prev *T, err error)
- func (db *DB[T]) SetConfig(config Config[T]) error
- func (db *DB[T]) Shrink() error
- func (db *DB[T]) Snapshot(wr io.Writer) error
- func (db *DB[T]) Update(fn func(tx *Tx[T]) error) error
- func (db *DB[T]) View(fn func(tx *Tx[T]) error) error
- type IndexOptions
- type PivotKV
- type SetOptions
- type SyncPolicy
- type Tx
- func (tx *Tx[T]) Ascend(index string, iterator func(key string, value T) bool) error
- func (tx *Tx[T]) AscendEqual(index string, pivot PivotKV[T], iterator func(key string, value T) bool) error
- func (tx *Tx[T]) AscendGreaterOrEqual(index string, pivot PivotKV[T], iterator func(key string, value T) bool) error
- func (tx *Tx[T]) AscendKeys(pattern string, iterator func(key string, value T) bool) error
- func (tx *Tx[T]) AscendLessThan(index string, lessThan PivotKV[T], iterator func(key string, value T) bool) error
- func (tx *Tx[T]) AscendRange(index string, greaterOrEqual, lessThan PivotKV[T], ...) error
- func (tx *Tx[T]) Commit() error
- func (tx *Tx[T]) CreateIndex(name, pattern string, less ...func(a, b T) bool) error
- func (tx *Tx[T]) CreateIndexOptions(name, pattern string, opts *IndexOptions, less ...func(a, b T) bool) error
- func (tx *Tx[T]) Delete(key string) (val *T, err error)
- func (tx *Tx[T]) DeleteAll() error
- func (tx *Tx[T]) Descend(index string, iterator func(key string, value T) bool) error
- func (tx *Tx[T]) DescendEqual(index string, pivot PivotKV[T], iterator func(key string, value T) bool) error
- func (tx *Tx[T]) DescendGreaterThan(index string, pivot PivotKV[T], iterator func(key string, value T) bool) error
- func (tx *Tx[T]) DescendKeys(pattern string, iterator func(key string, value T) bool) error
- func (tx *Tx[T]) DescendLessOrEqual(index string, pivot PivotKV[T], iterator func(key string, value T) bool) error
- func (tx *Tx[T]) DescendRange(index string, lessOrEqual, greaterThan PivotKV[T], ...) error
- func (tx *Tx[T]) DropIndex(name string) error
- func (tx *Tx[T]) Get(key string, ignoreExpired ...bool) (val *T, err error)
- func (tx *Tx[T]) GetLess(index string) (func(a, b T) bool, error)
- func (tx *Tx[T]) IndexLen(index string) (int, error)
- func (tx *Tx[T]) Indexes() ([]string, error)
- func (tx *Tx[T]) Len() (int, error)
- func (tx *Tx[T]) Rollback() error
- func (tx *Tx[T]) Set(key string, value T, opts *SetOptions) (previous *T, replaced bool, err error)
- func (tx *Tx[_]) TTL(key string) (time.Duration, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTxNotWritable is returned when performing a write operation on a // read-only transaction. ErrTxNotWritable = errors.New("tx not writable") // ErrTxClosed is returned when committing or rolling back a transaction // that has already been committed or rolled back. ErrTxClosed = errors.New("tx closed") // ErrNotFound is returned when an item or index is not in the database. ErrNotFound = errors.New("not found") // ErrIndexNotFound is returned when an index is not in the database. ErrIndexNotFound = errors.New("index not found") // ErrInvalid is returned when the database file is an invalid format. ErrInvalid = errors.New("invalid database") // ErrDatabaseClosed is returned when the database is closed. ErrDatabaseClosed = errors.New("database closed") // ErrIndexExists is returned when an index already exists in the database. ErrIndexExists = errors.New("index exists") // ErrInvalidOperation is returned when an operation cannot be completed. ErrInvalidOperation = errors.New("invalid operation") // ErrInvalidSyncPolicy is returned for an invalid SyncPolicy value. ErrInvalidSyncPolicy = errors.New("invalid sync policy") // ErrShrinkInProcess is returned when a shrink operation is in-process. ErrShrinkInProcess = errors.New("shrink is in-process") // ErrPersistenceActive is returned when post-loading data from an only on-memory database ErrPersistenceActive = errors.New("persistence active") // ErrTxIterating is returned when Set or Delete are called while iterating. ErrTxIterating = errors.New("tx is iterating") // ErrEmptyKey is returned when an empty key is provided. ErrEmptyKey = errors.New("empty key") )
var ( // ErrSyncFile is returned when the file cannot be synced. ErrSyncFile = errors.New("file cannot be synced") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config[T any] struct { // SyncPolicy adjusts how often the data is synced to disk. // This value can be Never, EverySecond, or Always. // The default is EverySecond. SyncPolicy SyncPolicy // AutoShrinkPercentage is used by the background process to trigger // a shrink of the aof file when the size of the file is larger than the // percentage of the result of the previous shrunk file. // For example, if this value is 100, and the last shrink process // resulted in a 100mb file, then the new aof file must be 200mb before // a shrink is triggered. AutoShrinkPercentage int // AutoShrinkMinSize defines the minimum size of the aof file before // an automatic shrink can occur. AutoShrinkMinSize int // AutoShrinkDisabled turns off automatic background shrinking AutoShrinkDisabled bool // OnExpired is used to custom handle the deletion option when a Key // has been expired. OnExpired func(keys []string) // OnExpiredSync will be called inside the same transaction that is // performing the deletion of expired items. If OnExpired is present then // this callback will not be called. If this callback is present, then the // deletion of the timed-out item is the explicit responsibility of this // callback. // WARNING: This callback is called multiple times on each expired key if not deleted inside function within // the same tick of the background process. OnExpiredSync func(key string, value T, tx *Tx[T]) error }
Config represents database configuration options. These options are used to change various behaviors of the database.
type DB ¶
type DB[T any] struct { // contains filtered or unexported fields }
DB represents a collection of Key-value pairs that persist on disk. Transactions are used for all forms of data access to the DB.
func Open ¶
Open opens a database at the provided path and optionally an object whose struct should be used for the values. If the file does not exist then it will be created automatically. If the file exists then the database will be loaded from the file (do not open a database on the same file twice). the returned DB object can be used for future operations
func (*DB[T]) Close ¶
Close releases all database resources. All transactions must be closed before closing the database.
func (*DB[T]) CreateIndex ¶
CreateIndex builds a new index and populates it with items. The items are ordered in a b-tree and can be retrieved using the Ascend* and Descend* methods. An error will occur if an index with the same name already exists.
When a pattern is provided, the index will be populated with keys that match the specified pattern. This is a very simple pattern match where '*' matches on any number characters and '?' matches on any one character. The comparator function compares if object 'a' is less than object 'b'. It allows for indexes to create custom ordering
func (*DB[T]) Del ¶
Del removes the value for a given key. It's a wrapper around Update with a single Delete Write-transaction call. Return previous value if any.
func (*DB[T]) Get ¶
Get returns the value for a given key. If the key does not exist then nil, It's a wrapper around View with a single Get Read-transaction call. ErrNotFound is returned if the key does not exist.
func (*DB[T]) Load ¶
Load loads commands from reader. This operation blocks all reads and writes. Note that this can only work for fully in-memory databases
func (*DB[T]) ReadConfig ¶
ReadConfig returns the database configuration.
func (*DB[T]) ReplaceIndex ¶
ReplaceIndex builds a new index and populates it with items. The items are ordered in a b-tree and can be retrieved using the Ascend* and Descend* methods. If a previous index with the same name exists, that index will be deleted.
func (*DB[T]) Set ¶
func (db *DB[T]) Set(key string, val T, options ...*SetOptions) (prev *T, err error)
Set sets the value for a given key. It's a wrapper around Update with a single Set Write-transaction call. Return previous value if any.
func (*DB[T]) Shrink ¶
Shrink will make the database file smaller by removing redundant log entries. This operation does not block the database.
func (*DB[T]) Snapshot ¶
Snapshot writes a snapshot of the database to a writer. This operation blocks all writes, but not reads. This can be used for snapshots and backups for pure in-memory databases. For persistent databases, the database file can be copied
func (*DB[T]) Update ¶
Update executes a function within a managed read/write transaction. The transaction has been committed when no error is returned. In the event that an error is returned, the transaction will be rolled back. When a non-nil error is returned from the function, the transaction will be rolled back and the that error will be return to the caller of Update().
Executing a manual commit or rollback from inside the function will result in a panic.
type IndexOptions ¶
type IndexOptions struct {
// CaseInsensitiveKeyMatching allow for case-insensitive
// matching on keys when setting Key/values.
CaseInsensitiveKeyMatching bool
}
IndexOptions provides an index with additional features or alternate functionality.
type PivotKV ¶
PivotKV is a Key/value pair that is used to pivot a range of items. The Key is used to find the item in the database by key and as fallback if no value is given The Value is used to find the item in the database by value if an index is present
type SetOptions ¶
type SetOptions struct {
// Expires indicates that the Set() Key-value will expire
Expires bool
// TTL is how much time the Key-value will exist in the database
// before being evicted. The Expires field must also be set to true.
// TTL stands for Time-To-Live.
TTL time.Duration
}
SetOptions represents options that may be included with the Set() command.
type SyncPolicy ¶
type SyncPolicy int
SyncPolicy represents how often data is synced to disk.
const ( // Never is used to disable syncing data to disk. // The faster and less safe method. Never SyncPolicy = 0 // EverySecond is used to sync data to disk every second. // It's pretty fast, and you can lose 1 second of data if there // is a disaster. // This is the recommended setting. EverySecond SyncPolicy = 1 // Always is used to sync data after every write to disk. // Slow. Very safe. Always SyncPolicy = 2 )
type Tx ¶
type Tx[T any] struct { // contains filtered or unexported fields }
Tx represents a transaction on the database. This transaction can either be read-only or read/write. Read-only transactions can be used for retrieving values for keys and iterating through keys and values. Read/write transactions can set and delete keys.
All transactions must be committed or rolled-back when done.
func (*Tx[T]) Ascend ¶
Ascend calls the iterator for every item in the database within the range until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the comparator function of the defined index. When an index is not provided, the results will be ordered by the item Key. An invalid index will return an error.
func (*Tx[T]) AscendEqual ¶
func (tx *Tx[T]) AscendEqual(index string, pivot PivotKV[T], iterator func(key string, value T) bool) error
AscendEqual calls the iterator for every item in the database that equals pivot, until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the comparator function of the defined index. When an index is not provided, the results will be ordered by the item Key. An invalid index will return an error.
func (*Tx[T]) AscendGreaterOrEqual ¶
func (tx *Tx[T]) AscendGreaterOrEqual(index string, pivot PivotKV[T], iterator func(key string, value T) bool) error
AscendGreaterOrEqual calls the iterator for every item in the database within the range [pivot, last], until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the comparator function of the defined index. When an index is not provided, the results will be ordered by the item Key. An invalid index will return an error.
func (*Tx[T]) AscendKeys ¶
AscendKeys allows for iterating through keys based on the specified pattern.
func (*Tx[T]) AscendLessThan ¶
func (tx *Tx[T]) AscendLessThan(index string, lessThan PivotKV[T], iterator func(key string, value T) bool) error
AscendLessThan calls the iterator for every item in the database within the range [first, pivot), until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the comparator function of the defined index. When an index is not provided, the results will be ordered by the item Key. An invalid index will return an error. excluding the pivot
func (*Tx[T]) AscendRange ¶
func (tx *Tx[T]) AscendRange(index string, greaterOrEqual, lessThan PivotKV[T], iterator func(key string, value T) bool) error
AscendRange calls the iterator for every item in the database within the range [greaterOrEqual, lessThan), until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the comparator function of the defined index. When an index is not provided, the results will be ordered by the item Key. An invalid index will return an error. including greaterOrEqual, excluding lessThan
func (*Tx[T]) Commit ¶
Commit writes all changes to disk. An error is returned when a write error occurs, or when a Commit() is called from a read-only transaction.
func (*Tx[T]) CreateIndex ¶
CreateIndex builds a new index and populates it with items. The items are ordered in a b-tree and can be retrieved using the Ascend* and Descend* methods. An error will occur if an index with the same name already exists.
When a pattern is provided, the index will be populated with keys that match the specified pattern. This is a very simple pattern match where '*' matches on any number characters and '?' matches on any one character. The less function compares if string 'a' is less than string 'b'. It allows for indexes to create custom ordering. It's possible that the strings may be textual or binary. It's up to the provided less function to handle the content format and comparison. There are some default less function that can be used such as IndexString, IndexBinary, etc.
func (*Tx[T]) CreateIndexOptions ¶
func (tx *Tx[T]) CreateIndexOptions(name, pattern string, opts *IndexOptions, less ...func(a, b T) bool) error
CreateIndexOptions is the same as CreateIndex except that it allows for additional options.
func (*Tx[T]) Delete ¶
Delete removes an item from the database based on the item's Key. If the item does not exist or if the item has expired then ErrNotFound is returned.
Only a writable transaction can be used for this operation. This operation is not allowed during iterations such as Ascend* & Descend*.
func (*Tx[T]) Descend ¶
Descend calls the iterator for every item in the database within the range [last, first], until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the comparator function of the defined index. When an index is not provided, the results will be ordered by the item Key. An invalid index will return an error.
func (*Tx[T]) DescendEqual ¶
func (tx *Tx[T]) DescendEqual(index string, pivot PivotKV[T], iterator func(key string, value T) bool) error
DescendEqual calls the iterator for every item in the database that equals pivot, until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the comparator function of the defined index. When an index is not provided, the results will be ordered by the item Key. An invalid index will return an error.
func (*Tx[T]) DescendGreaterThan ¶
func (tx *Tx[T]) DescendGreaterThan(index string, pivot PivotKV[T], iterator func(key string, value T) bool) error
DescendGreaterThan calls the iterator for every item in the database within the range [last, pivot), until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the comparator function of the defined index. When an index is not provided, the results will be ordered by the item Key. An invalid index will return an error.
func (*Tx[T]) DescendKeys ¶
DescendKeys allows for iterating through keys based on the specified pattern.
func (*Tx[T]) DescendLessOrEqual ¶
func (tx *Tx[T]) DescendLessOrEqual(index string, pivot PivotKV[T], iterator func(key string, value T) bool) error
DescendLessOrEqual calls the iterator for every item in the database within the range [pivot, first], until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the comparator function of the defined index. When an index is not provided, the results will be ordered by the item Key. An invalid index will return an error.
func (*Tx[T]) DescendRange ¶
func (tx *Tx[T]) DescendRange(index string, lessOrEqual, greaterThan PivotKV[T], iterator func(key string, value T) bool) error
DescendRange calls the iterator for every item in the database within the range [lessOrEqual, greaterThan), until iterator returns false. When an index is provided, the results will be ordered by the item values as specified by the comparator function of the defined index. When an index is not provided, the results will be ordered by the item Key. An invalid index will return an error.
func (*Tx[T]) Get ¶
Get returns a value for a Key. If the item does not exist or if the item has expired then ErrNotFound is returned. If ignoreExpired is true, then the found value will be returned even if it is expired.
func (*Tx[T]) GetLess ¶
GetLess returns the comparator function for an index. This is handy for doing ad-hoc compares inside a transaction. Returns ErrNotFound if the index is not found or there is no comparator function bound to the index
func (*Tx[T]) IndexLen ¶ added in v0.2.0
IndexLen returns the number of items in the index if index identifier is empty the total Len of the DB will be returned
func (*Tx[T]) Rollback ¶
Rollback closes the transaction and reverts all mutable operations that were performed on the transaction such as Set() and Delete().
Read-only transactions can only be rolled back, not committed.
func (*Tx[T]) Set ¶
func (tx *Tx[T]) Set(key string, value T, opts *SetOptions) (previous *T, replaced bool, err error)
Set inserts or replaces an item in the database based on the Key. The opts param may be used for additional functionality such as forcing the item to be evicted at a specified time. When the return value for err is nil the operation succeeded. When the return value of replaced is true, then the operation replaced an existing item whose value will be returned through the previousValue variable. The results of this operation will not be available to other transactions until the current transaction has successfully committed.
Only a writable transaction can be used with this operation. This operation is not allowed during iterations such as Ascend* & Descend*.