reldb

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AutoKeyBuffer = 100

	// PreAutoKavlb prefix for available key.
	PreAutoKavlb = "tank%avlb_"
	// PreAutoKused prefix for used key.
	PreAutoKused = "tank%used_"
	// PrefixLink prefix for a link declaration.
	PrefixLink = "link%"
	// PrefixTable prefix for a table entry.
	PrefixTable = "tbl%"

	// Delimiter between the tableName and the key
	Delimiter     = "_"
	LinkDelimiter = "@"

	InvalidId = errors.New("InvalidId")
)

Functions

func Count

func Count[T IObject](db IRelationalDB) int

Count return the count of object in the table based on the tableName induced by the T parameter.

func DeepDelete

func DeepDelete[T IObject](db IRelationalDB, id string) error

DeepDelete the object determine with the id and the tableName induced by the T parameter and all object directly connected.

func Delete

func Delete[T IObject](db IRelationalDB, id string) error

Delete the object determine with the id and the tableName induced by the T parameter. id is released and related link are deleted.

func Encode

func Encode(obj *IObject) []byte

func Exist

func Exist[T IObject](db IRelationalDB, id string) bool

Exist return true if the object determine with the id and the tableName induced by the T parameter exist in db.

func Foreach

func Foreach[T IObject](db IRelationalDB, do func(id string, value *T))

Foreach iterate on the table based on the tableName induced by the T parameter and execute the do function on each value.

func Link[S IObject, T IObject](s *ObjWrapper[S], biDirectional bool, t ...*ObjWrapper[T])

Link add a link between s object and all t objects. The biDirectional attribute determine if for t is also connected to s:

 biDirectional == false: s -> t

 biDirectional == true:  s -> t
	                        s <- t

func MakeKey

func MakeKey(tableName, id string) []byte

func MakeLinkKey

func MakeLinkKey(tableName string, id string, targetName string, targetId string) []string

func MakePrefix

func MakePrefix(tableName string) string

func NameOfField

func NameOfField(parent interface{}, field interface{}) (string, error)

func NameOfStruct

func NameOfStruct[T any]() string

NameOfStruct simply reflect the name of the type T.

func RemoveAllTableLink[S IObject, T IObject](t *ObjWrapper[S])

RemoveAllTableLink remove all link between t object and object having the S tableName.

func RemoveLink[S IObject, T IObject](s *ObjWrapper[S], t *ObjWrapper[T]) bool

RemoveLink remove all link between s and t object. Return true if the link s->t are deleted (is at least the link created when isBidirectional == false).

func ToString

func ToString(v any) string

ToString print the name of type and all field name with the corresponding value.

func Type

func Type[T IObject]() *T

Types

type AbstractRelDB

type AbstractRelDB struct{ IRelationalDB }

AbstractRelDB pre-implement IRelationalDB, you need to set value of interface: methods implemented are used in abstract.

func (*AbstractRelDB) CleanUnusedKey

func (db *AbstractRelDB) CleanUnusedKey()

func (*AbstractRelDB) Count

func (db *AbstractRelDB) Count(prefix string) int

func (*AbstractRelDB) DeepDelete

func (db *AbstractRelDB) DeepDelete(tableName string, id string) error

func (*AbstractRelDB) Delete

func (db *AbstractRelDB) Delete(tableName string, id string) error

func (*AbstractRelDB) FindAll

func (db *AbstractRelDB) FindAll(tableName string, predicate func(id string, value *IObject) bool) ([]string, []*IObject)

func (*AbstractRelDB) FindFirst

func (db *AbstractRelDB) FindFirst(tableName string, predicate func(id string, value *IObject) bool) (string, *IObject)

func (*AbstractRelDB) Foreach

func (db *AbstractRelDB) Foreach(tableName string, do func(id string, value *IObject))

func (*AbstractRelDB) FreeKey

func (db *AbstractRelDB) FreeKey(keys ...string) []error

func (*AbstractRelDB) Get

func (db *AbstractRelDB) Get(tableName string, id string) *IObject

func (*AbstractRelDB) GetNextKey

func (db *AbstractRelDB) GetNextKey() string

func (*AbstractRelDB) Insert

func (db *AbstractRelDB) Insert(object IObject) string

func (*AbstractRelDB) Set

func (db *AbstractRelDB) Set(id string, object IObject) error

func (*AbstractRelDB) SetWrp

func (db *AbstractRelDB) SetWrp(objWrp ObjWrapper[IObject]) error

func (*AbstractRelDB) Update

func (db *AbstractRelDB) Update(tableName string, id string, editor func(value IObject) IObject) *IObject

type DBObject

type DBObject struct{ IObject }

func (DBObject) Equals

func (o DBObject) Equals(v IObject) bool

func (DBObject) Hash

func (o DBObject) Hash() string

type IObject

type IObject interface {
	Equals(v IObject) bool
	Hash() string
	ToString() string
	TableName() string
}

func Decode

func Decode(value []byte) *IObject

type IRelationalDB

type IRelationalDB interface {
	// GetNextKey pick a key in tank of key. If tank is empty, it should be filled with unused key.
	GetNextKey() string
	// FreeKey check if key is used (return error otherwise) and make key available again.
	FreeKey(key ...string) []error
	CleanUnusedKey()

	// RawSet set a value in DB. prefix and key are simply concatenated. Don't care about Key is already in used or not.
	RawSet(prefix string, key string, value []byte)
	// RawGet get a value in DB. prefix and key are simply concatenated. If no value corresponding to this Key,
	//empty slice and false should be returned.
	RawGet(prefix string, key string) ([]byte, bool)
	// RawDelete delete a value in DB. prefix and key are simply concatenated. Return true if value is correctly deleted.
	RawDelete(prefix string, key string) bool
	// RawIterKey iterate in DB when prefix match with Key.
	//The action it called for each Key and the key is truncated with the prefix given.
	//The stop boolean defined if iteration should be stopped. No values are prefetched with this iterator.
	RawIterKey(prefix string, action func(key string) (stop bool))
	// RawIterKV iterate in DB when prefix match with Key.
	//The action it called for each Key and the key is truncated with the prefix given.
	//The stop boolean defined if iteration should be stopped. value is the corresponding value of the key.
	RawIterKV(prefix string, action func(key string, value []byte) (stop bool))

	// Insert create a new entry in storage with IObject passed. TableName is inferred with the IObject.
	Insert(object IObject) string
	// Set write a value for a specific id. TableName is inferred with the IObject. If Key not exist, an error is returned.
	Set(id string, object IObject) error
	SetWrp(objWrp ObjWrapper[IObject]) error
	// Get retrieve the value for corresponding TableName and ID. Return nil if nothing found.
	Get(tableName string, id string) *IObject
	// Update retrieve the value for corresponding TableName and ID, call the editor et Set the resulted value.
	Update(tableName string, id string, editor func(value IObject) IObject) *IObject
	// Delete remove the value for corresponding TableName and ID. If Key not exist,
	//an error is returned. The link using the object will be also deleted.
	Delete(tableName string, id string) error
	// DeepDelete remove the value for corresponding TableName and ID. If Key not exist,
	//an error is returned. It also recursively remove all values connected with a link.
	DeepDelete(tableName string, id string) error
	// Exist return true if the for corresponding TableName and ID exist in DB.
	Exist(tableName string, id string) bool

	// Count return the count for matching Key prefixed by TableName that exist in DB.
	Count(tableName string) int
	// Foreach call the do function for each value whose key is prefixed by TableName.
	Foreach(tableName string, do func(id string, value *IObject))
	// FindFirst iterate on values of tableName and apply the predicate: if predicate is true the
	//value is returned.
	FindFirst(tableName string, predicate func(id string, value *IObject) bool) (string, *IObject)
	// FindAll iterate on values of tableName and apply the predicate: all values matched are
	//returned.
	FindAll(tableName string, predicate func(id string, value *IObject) bool) ([]string, []*IObject)

	Print(tableName string) error
}

IRelationalDB is a small interface to define some operation with a storage used like a relational DB.

Key

In first, the underlying storage should be work like a KV DB. In consequence, a key is structured to store a flattened hierarchy.

  • Key: internalTypePrefix, suffix
  • Key for concrete type: internalTypePrefix, tableName, id

To make uniq key, a tank key system is implemented and can be used with GetNextKey, FreeKey. The global AutoKeyBuffer defined the size of this tank. When value is inserted, a key is pick in tank. When entry is deleted, the key become available again via GetNextKey.

Operators

Interface is designed so that all raw operator must be implemented ; other can be but are already implement in the abstraction AbstractRelDB. Raw Operators probably work directly with the db driver and are used by all other operators.

type ObjWrapper

type ObjWrapper[T IObject] struct {
	ID    string
	Value T
	// contains filtered or unexported fields
}

func FindAll

func FindAll[T IObject](db IRelationalDB, predicate func(id string, value *T) bool) []*ObjWrapper[T]

FindAll iterate on the table based on the tableName induced by the T parameter and execute the predicate function on each value. All values matching the predicate are returned.

func FindFirst

func FindFirst[T IObject](db IRelationalDB, predicate func(id string, value *T) bool) *ObjWrapper[T]

FindFirst iterate on the table based on the tableName induced by the T parameter and execute the predicate function on each value until it return true value: the current value is returned.

func Get

func Get[T IObject](db IRelationalDB, id string) *ObjWrapper[T]

Get the value in db based on id and the tableName induced by the T parameter.

func Insert

func Insert[T IObject](db IRelationalDB, value T) *ObjWrapper[T]

Insert in the db the value and return the resulting wrapper.

func LinkNew

func LinkNew[S IObject, T IObject](s *ObjWrapper[S], biDirectional bool, objs ...T) []*ObjWrapper[T]

LinkNew same as Link but take IObject array and wrapping it before the link.

func NewObjWrapper

func NewObjWrapper[T IObject](db IRelationalDB, ID string, value *T) *ObjWrapper[T]

func Set

func Set[T IObject](db IRelationalDB, id string, value T) *ObjWrapper[T]

Set override the value at the id specified with the passed value. The id shall exist.

func SetWrp

func SetWrp[T IObject](db IRelationalDB, objWrp ObjWrapper[T]) *ObjWrapper[T]

SetWrp same as set but take a wrapped object in argument.

func UnlinkAll added in v1.1.1

func UnlinkAll[S IObject, T IObject](s *ObjWrapper[S]) []*ObjWrapper[T]

UnlinkAll delete all links (in both way) concerning the s object.

func Update

func Update[T IObject](db IRelationalDB, id string, editor func(value *T)) *ObjWrapper[T]

Update the value determine with the id and the tableName induced by the T parameter. The result of the editor function is set in the db.

func (t *ObjWrapper[T]) RemoveAllLink()

RemoveAllLink remove all link connected to this object.

func (*ObjWrapper[IObject]) Visit added in v1.1.1

func (t *ObjWrapper[IObject]) Visit(tableName string) []string

Visit iterate on all connected objects and returns all ids.

Jump to

Keyboard shortcuts

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