Documentation ¶
Index ¶
- Variables
- func Count[T IObject](db IRelationalDB) int
- func DeepDelete[T IObject](db IRelationalDB, id string) error
- func DeepDeleteWrp[T IObject](objWrp *ObjWrapper[T]) error
- func Delete[T IObject](db IRelationalDB, id string) error
- func DeleteWrp[T IObject](objWrp *ObjWrapper[T]) error
- func Encode(obj *IObject) []byte
- func Exist[T IObject](db IRelationalDB, id string) bool
- func ExistWrp[T IObject](objWrp *ObjWrapper[T]) bool
- func Foreach[T IObject](db IRelationalDB, do func(id string, value *T))
- func Link[S IObject, T IObject](s *ObjWrapper[S], biDirectional bool, targets ...*ObjWrapper[T])
- func MakeKey(tableName, id string) []byte
- func MakeLinkKey(tableName string, id string, targetName string, targetId string) []string
- func MakePrefix(tableName string) string
- func NameOfField(parent interface{}, field interface{}) (string, error)
- func NameOfStruct[T any]() string
- func RemoveAllLink[T IObject](db IRelationalDB, id string)
- func RemoveAllLinkWrp[T IObject](t *ObjWrapper[T])
- func RemoveAllTableLink[S IObject, T IObject](db IRelationalDB, id string)
- func RemoveAllTableLinkWrp[S IObject, T IObject](s *ObjWrapper[S])
- func RemoveLink[S IObject, T IObject](db IRelationalDB, idOfS string, idOfT string) bool
- func RemoveLinkWrp[S IObject, T IObject](s *ObjWrapper[S], t *ObjWrapper[T]) bool
- func TableName[T IObject]() string
- func ToString(v any) string
- func Type[T IObject]() *T
- func Visit[S IObject, T IObject](db IRelationalDB, id string) []string
- func VisitWrp[S IObject, T IObject](s *ObjWrapper[S]) []string
- type AbstractRelDB
- func (db *AbstractRelDB) CleanUnusedKey()
- func (db *AbstractRelDB) Count(prefix string) int
- func (db *AbstractRelDB) DeepDelete(tableName string, id string) error
- func (db *AbstractRelDB) Delete(tableName string, id string) error
- func (db *AbstractRelDB) FindAll(tableName string, predicate func(id string, value *IObject) bool) ([]string, []*IObject)
- func (db *AbstractRelDB) FindFirst(tableName string, predicate func(id string, value *IObject) bool) (string, *IObject)
- func (db *AbstractRelDB) Foreach(tableName string, do func(id string, value *IObject))
- func (db *AbstractRelDB) FreeKey(keys ...string) []error
- func (db *AbstractRelDB) Get(tableName string, id string) *IObject
- func (db *AbstractRelDB) GetNextKey() string
- func (db *AbstractRelDB) Insert(object IObject) string
- func (db *AbstractRelDB) Set(id string, object IObject) error
- func (db *AbstractRelDB) SetWrp(objWrp ObjWrapper[IObject]) error
- func (db *AbstractRelDB) Update(tableName string, id string, editor func(value IObject) IObject) *IObject
- type Collection
- func (c *Collection[T]) Distinct() *Collection[T]
- func (c *Collection[T]) Filter(predicate func(objWrp *ObjWrapper[T]) bool) *Collection[T]
- func (c *Collection[T]) GetArray() []*ObjWrapper[T]
- func (c *Collection[T]) Len() int
- func (c *Collection[T]) Less(i, j int) bool
- func (c *Collection[T]) Sort(comparator func(x, y *ObjWrapper[T]) bool) *Collection[T]
- func (c *Collection[T]) Swap(i, j int)
- type DBObject
- type IObject
- type IRelationalDB
- type ObjWrapper
- func AllFromLink[S IObject, T IObject](db IRelationalDB, idOfS string) []*ObjWrapper[T]
- func AllFromLinkWrp[S IObject, T IObject](s *ObjWrapper[S]) []*ObjWrapper[T]
- func FindAll[T IObject](db IRelationalDB, predicate func(id string, value *T) bool) []*ObjWrapper[T]
- func FindFirst[T IObject](db IRelationalDB, predicate func(id string, value *T) bool) *ObjWrapper[T]
- func Get[T IObject](db IRelationalDB, id string) *ObjWrapper[T]
- func Insert[T IObject](db IRelationalDB, value T) *ObjWrapper[T]
- func LinkNew[S IObject, T IObject](s *ObjWrapper[S], biDirectional bool, objs ...T) []*ObjWrapper[T]
- func NewObjWrapper[T IObject](db IRelationalDB, ID string, value *T) *ObjWrapper[T]
- func Set[T IObject](db IRelationalDB, id string, value T) *ObjWrapper[T]
- func SetWrp[T IObject](db IRelationalDB, objWrp ObjWrapper[T]) *ObjWrapper[T]
- func Update[T IObject](db IRelationalDB, id string, editor func(value *T)) *ObjWrapper[T]
Constants ¶
This section is empty.
Variables ¶
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 DeepDeleteWrp ¶
func DeepDeleteWrp[T IObject](objWrp *ObjWrapper[T]) error
DeepDeleteWrp 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 DeleteWrp ¶
func DeleteWrp[T IObject](objWrp *ObjWrapper[T]) error
DeleteWrp the object determine with the id and the tableName induced by the T parameter. id is released and related link are deleted.
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 ExistWrp ¶
func ExistWrp[T IObject](objWrp *ObjWrapper[T]) bool
ExistWrp 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 ¶
func Link[S IObject, T IObject](s *ObjWrapper[S], biDirectional bool, targets ...*ObjWrapper[T])
Link add a link between s object and all targets objects. The biDirectional attribute determine if for target is also connected to s:
biDirectional == false: s -> t biDirectional == true: s -> t s <- t
func MakeLinkKey ¶
func MakePrefix ¶
func NameOfField ¶
func NameOfStruct ¶
NameOfStruct simply reflect the name of the type T.
func RemoveAllLink ¶
func RemoveAllLink[T IObject](db IRelationalDB, id string)
RemoveAllLink remove all link connected to this object.
func RemoveAllLinkWrp ¶
func RemoveAllLinkWrp[T IObject](t *ObjWrapper[T])
RemoveAllLinkWrp remove all link connected to this object.
func RemoveAllTableLink ¶
func RemoveAllTableLink[S IObject, T IObject](db IRelationalDB, id string)
RemoveAllTableLink remove all link between t object and object having the S tableName.
func RemoveAllTableLinkWrp ¶
func RemoveAllTableLinkWrp[S IObject, T IObject](s *ObjWrapper[S])
RemoveAllTableLinkWrp remove all link between t object and object having the S tableName.
func RemoveLink ¶
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 RemoveLinkWrp ¶
func RemoveLinkWrp[S IObject, T IObject](s *ObjWrapper[S], t *ObjWrapper[T]) bool
RemoveLinkWrp 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).
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) 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) GetNextKey ¶
func (db *AbstractRelDB) GetNextKey() string
func (*AbstractRelDB) Insert ¶
func (db *AbstractRelDB) Insert(object IObject) string
func (*AbstractRelDB) SetWrp ¶
func (db *AbstractRelDB) SetWrp(objWrp ObjWrapper[IObject]) error
type Collection ¶
type Collection[T IObject] struct { // contains filtered or unexported fields }
func NewCollection ¶
func NewCollection[T IObject](db IRelationalDB) *Collection[T]
func Where ¶
func Where[T IObject, K IObject]( firstOnly bool, collection *Collection[T], predicate func(objWrp1 *ObjWrapper[T], objWrp2 *ObjWrapper[K]) bool, ) *Collection[T]
Where ; this function consider a collection and the connected collection induced by Link ( probably used in a same way for each element of the collection).
For each object in collection the predicate is applied with the current object and all connected object given by an AllFromLink operation. If the predicate returns true, the current object is retained.
If the option firstOnly is set to true, only the first result of AllFromLink is retained.
Obviously, the result of unlink can be nil and predicate function must manage this case.
func (*Collection[T]) Distinct ¶
func (c *Collection[T]) Distinct() *Collection[T]
Distinct ; simply eliminate all duplicate from the collection.
The underlying ObjWrapper array is modified with this operation.
func (*Collection[T]) Filter ¶
func (c *Collection[T]) Filter( predicate func(objWrp *ObjWrapper[T]) bool, ) *Collection[T]
Filter iterate on the collection and for each object apply the predicate. If the result is true, the object is retained.
Note: technically you can use this function to get the same result as Where operation.
The underlying ObjWrapper array is modified with this operation.
func (*Collection[T]) GetArray ¶
func (c *Collection[T]) GetArray() []*ObjWrapper[T]
GetArray give the underlying ObjWrapper array of the current collection.
func (*Collection[T]) Len ¶
func (c *Collection[T]) Len() int
func (*Collection[T]) Less ¶
func (c *Collection[T]) Less(i, j int) bool
func (*Collection[T]) Sort ¶
func (c *Collection[T]) Sort( comparator func(x, y *ObjWrapper[T]) bool, ) *Collection[T]
func (*Collection[T]) Swap ¶
func (c *Collection[T]) Swap(i, j int)
type 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) }
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 ¶
func AllFromLink ¶
func AllFromLink[S IObject, T IObject](db IRelationalDB, idOfS string) []*ObjWrapper[T]
AllFromLink returned all object, with the tableName induced by T, connected to the S object.
func AllFromLinkWrp ¶
func AllFromLinkWrp[S IObject, T IObject](s *ObjWrapper[S]) []*ObjWrapper[T]
AllFromLinkWrp returned all object, with the tableName induced by T, connected to the S object.
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 insert them in the db then return the resulting wrapping.
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 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 (*ObjWrapper[T]) Unwrap ¶
func (w *ObjWrapper[T]) Unwrap() (string, T)