Documentation ¶
Index ¶
- Constants
- Variables
- func Close() error
- func Create[T IObject](typeKey TypeKey) (T, error)
- func Delete[T IObject](tid TypeId) (T, error)
- func Get[T IObject](tid TypeId) (T, error)
- func GetAll[T IObject](typeKey TypeKey) ([]T, error)
- func GetFirst[T IObject](typeKey TypeKey, selector func(obj T) bool) (T, error)
- func GetRef[T IObject](ref *ObjRef) (T, error)
- func Init(dbFile string)
- func List[T IObject](ctx context.Context, typ TypeKey, pageSize int32, pageToken string) (result []T, nextPageToken string, err error)
- func MustNew[T IObject](typeKey TypeKey) T
- func New[T IObject](typeKey TypeKey) (obj T, err error)
- func Put[T IObject](val ...T) error
- func Query[T IObject](ctx context.Context, typ TypeKey, selectFn func(obj T) (bool, error), ...) (result []T, nextPageToken string, err error)
- func Register[T IObject](tmpl T)
- func RemoveWatcher(watcherId string) error
- func SearchProto(m proto.Message, query func(s string) bool) (hits []string, err error)
- func Unmarshal[T IObject](kv KeyVal) (T, error)
- func UnmarshalMany[T IObject](kvs []KeyVal) ([]T, error)
- func UnwatchType(watcherId string, typeKeys ...TypeKey) error
- func UnwatchTypeId(watcherId string, tids ...TypeId) error
- func Update[T IObject](tid TypeId, updater func(obj T) (T, error)) (t T, err error)
- func WatchType(watcherId string, eventCh chan *EventInfo, typeKeys ...TypeKey) (string, error)
- func WatchTypeId(watcherId string, eventCh chan *EventInfo, tids ...TypeId) (string, error)
- type EventInfo
- type IObject
- type KeyVal
- type Metadata
- func (*Metadata) Descriptor() ([]byte, []int)deprecated
- func (m *Metadata) Fill() error
- func (x *Metadata) GetCreatedAt() *timestamppb.Timestamp
- func (x *Metadata) GetDescription() string
- func (x *Metadata) GetLabels() []string
- func (x *Metadata) GetType() []byte
- func (x *Metadata) GetUpdatedAt() *timestamppb.Timestamp
- func (x *Metadata) GetUuid() []byte
- func (m *Metadata) GetUuidAsString() (string, error)
- func (m *Metadata) GetUuidAsUUID() (uuid.UUID, error)
- func (*Metadata) ProtoMessage()
- func (x *Metadata) ProtoReflect() protoreflect.Message
- func (m *Metadata) Ref() *ObjRef
- func (x *Metadata) Reset()
- func (x *Metadata) String() string
- func (m *Metadata) TypeId() TypeId
- type ObjRef
- func (*ObjRef) Descriptor() ([]byte, []int)deprecated
- func (x *ObjRef) GetType() []byte
- func (x *ObjRef) GetUuid() []byte
- func (r *ObjRef) Marshal() []byte
- func (*ObjRef) ProtoMessage()
- func (x *ObjRef) ProtoReflect() protoreflect.Message
- func (x *ObjRef) Reset()
- func (x *ObjRef) String() string
- func (r *ObjRef) TypeId() *TypeId
- type SearchHit
- type SearchResult
- type TObject
- func (*TObject) Descriptor() ([]byte, []int)deprecated
- func (x *TObject) GetMetadata() *Metadata
- func (x *TObject) GetMyField() string
- func (x *TObject) GetMyInt() uint64
- func (*TObject) ProtoMessage()
- func (x *TObject) ProtoReflect() protoreflect.Message
- func (x *TObject) Reset()
- func (x *TObject) String() string
- type TypeId
- func (k *TypeId) Equal(other *TypeId) bool
- func (k *TypeId) Key() []byte
- func (k *TypeId) SetType(keyType TypeKey)
- func (k *TypeId) SetUuid(id uuid.UUID)
- func (k *TypeId) SetUuidBytes(id []byte)
- func (k *TypeId) String() string
- func (k TypeId) TypeKey() TypeKey
- func (k TypeId) Uuid() uuid.UUID
- func (k TypeId) UuidBytes() []byte
- type TypeKey
Examples ¶
Constants ¶
const ( InvalidWatchEvent = 0 EventCreated = 1 EventUpdated = 2 EventDeleted = 3 )
Events reflecting life-cycle changes to an object
Variables ¶
var ( ErrNotAnObject = errors.New("not an object type") ErrInvalidType = errors.New("invalid type") ErrNotFound = errors.New("not found") ErrSessionInvalid = errors.New("session invalid") ErrContextCancelled = errors.New("context cancelled") )
var File_shdb_proto protoreflect.FileDescriptor
Functions ¶
func Delete ¶
Delete an object from the database based on the type and id. The old value is returned
func GetFirst ¶
GetOne returns one of the objects in the database with the specified type that matches the selector function. The selector should return true for a match and false otherwise when presented with an object.
func List ¶
func List[T IObject](ctx context.Context, typ TypeKey, pageSize int32, pageToken string) (result []T, nextPageToken string, err error)
List all objects pertaining to a specific type. For arguments and paging see `Query` method.
func Put ¶
Put creates objects in the database. If the object already existed it will be overwritten.
func Query ¶
func Query[T IObject](ctx context.Context, typ TypeKey, selectFn func(obj T) (bool, error), pageSize int32, pageToken string) (result []T, nextPageToken string, err error)
Query returns all objects of a specific type matching a selector function. Paging of the results is implemented using a pageSize and a token. If there are more results available after pageSize items have been returned a non-empty nextPageToken is returned that can be used to retrieve a new page of results. If nextPageToken is the empty string, no more results are available.
Example ¶
Init(path.Join(os.TempDir(), "example_query.db")) Register(&TObject{ Metadata: &Metadata{Type: TObj[:]}, MyField: "The flying duck is flying low"}) count := 100 pageSize := 10 list := []*TObject{} for k := 0; k < count; k++ { tObj := MustNew[*TObject](TObj) tObj.MyInt = uint64(k) list = append(list, tObj) } if err := Put(list...); err != nil { panic(err) } defer func() { Close() os.Remove(path.Join(os.TempDir(), "example_query.db")) }() var ( nextPageToken string = "" partList []*TObject err error ) ctx := context.Background() selectorFn := func(obj *TObject) (bool, error) { return strings.Contains(obj.MyField, "flying"), nil } collected := []*TObject{} for { partList, nextPageToken, err = Query(ctx, TObj, selectorFn, int32(count/pageSize), nextPageToken) if err != nil { panic(err) } collected = append(collected, partList...) if nextPageToken == "" { break } } for idx, obj := range collected { log.Printf("%d: [%v]\n", idx, obj) }
Output:
func Register ¶
func Register[T IObject](tmpl T)
Register an IObject and it's type in the type table
func RemoveWatcher ¶
RemoveWatcher closes the eventCh for the watcher and removes the watcher.
func SearchProto ¶
SearchProto searches within the fields of the proto message forthe string provided. The result will contain a number of hits in the form - /[fieldName|@idx]/... Examples:
- / - The object contained only one item and it matched
- /myField - {"myField": <match>}
- /field1/field2/@3 {"field1": {"field2": [1,2,<match>]}}
func UnmarshalMany ¶
UnmarshalMany unmarshals a list of KeyVal binary representations
func UnwatchType ¶
UnwatchType removes a list of TypeKeys from a watcher
func UnwatchTypeId ¶
UnwatchTypeId removes a list of TypeIds from a watcher
func Update ¶
Update an object in the database by using an updater function. The updated object is returned.
func WatchType ¶
WatchType creates or updates a watcher by adding watches to new TypeKeys If the provided watcherId is the empty string, a new watcher is created and the eventCh must be specified. If watcherId is non-empty, then the eventCh can be set to nil The watcherId is returned.
func WatchTypeId ¶
WatchType creates or updates a watcher by adding watches to new TypeIds If the provided watcherId is the empty string, a new watcher is created and the eventCh must be specified. If watcherId is non-empty, then the eventCh can be set to nil The watcherId is returned.
Types ¶
type IObject ¶
IObject is the interface for all objects in the dataabase. They all should stem from a protobuf message that looks like this:
message TObject { shdb.Metadata metadata = 1; string my_field = 2; uint64 my_int = 3; }
type KeyVal ¶
KeyVal is the binary representation of an IObject as it is stored in the database
type Metadata ¶
type Metadata struct { Type []byte `protobuf:"bytes,1,opt,name=type,proto3" json:"type,omitempty"` Uuid []byte `protobuf:"bytes,2,opt,name=uuid,proto3" json:"uuid,omitempty"` Labels []string `protobuf:"bytes,3,rep,name=labels,proto3" json:"labels,omitempty"` Description string `protobuf:"bytes,4,opt,name=description,proto3" json:"description,omitempty"` CreatedAt *timestamppb.Timestamp `protobuf:"bytes,5,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"` UpdatedAt *timestamppb.Timestamp `protobuf:"bytes,6,opt,name=updated_at,json=updatedAt,proto3" json:"updated_at,omitempty"` // contains filtered or unexported fields }
func (*Metadata) Descriptor
deprecated
func (*Metadata) GetCreatedAt ¶
func (x *Metadata) GetCreatedAt() *timestamppb.Timestamp
func (*Metadata) GetDescription ¶
func (*Metadata) GetUpdatedAt ¶
func (x *Metadata) GetUpdatedAt() *timestamppb.Timestamp
func (*Metadata) GetUuidAsString ¶ added in v1.0.1
GetUuidAsString returns the UUID as a string
func (*Metadata) GetUuidAsUUID ¶ added in v1.0.1
GetUuidAsUUID returns the UUID as a uuid.UUID
func (*Metadata) ProtoMessage ¶
func (*Metadata) ProtoMessage()
func (*Metadata) ProtoReflect ¶
func (x *Metadata) ProtoReflect() protoreflect.Message
type ObjRef ¶ added in v1.0.1
type ObjRef struct { Type []byte `protobuf:"bytes,1,opt,name=type,proto3" json:"type,omitempty"` Uuid []byte `protobuf:"bytes,2,opt,name=uuid,proto3" json:"uuid,omitempty"` // contains filtered or unexported fields }
func UnmarshalObjRef ¶ added in v1.0.1
UnmarshalObjRef a byte slice into ObjRef
func (*ObjRef) Descriptor
deprecated
added in
v1.0.1
func (*ObjRef) ProtoMessage ¶ added in v1.0.1
func (*ObjRef) ProtoMessage()
func (*ObjRef) ProtoReflect ¶ added in v1.0.1
func (x *ObjRef) ProtoReflect() protoreflect.Message
type SearchHit ¶
type SearchHit struct { Metadata *Metadata `protobuf:"bytes,1,opt,name=metadata,proto3" json:"metadata,omitempty"` Hits []string `protobuf:"bytes,2,rep,name=hits,proto3" json:"hits,omitempty"` // contains filtered or unexported fields }
func (*SearchHit) Descriptor
deprecated
func (*SearchHit) GetMetadata ¶
func (*SearchHit) ProtoMessage ¶
func (*SearchHit) ProtoMessage()
func (*SearchHit) ProtoReflect ¶
func (x *SearchHit) ProtoReflect() protoreflect.Message
type SearchResult ¶
type SearchResult struct { Hits []*SearchHit `protobuf:"bytes,1,rep,name=hits,proto3" json:"hits,omitempty"` // contains filtered or unexported fields }
func Search ¶
func Search(ctx context.Context, typ TypeKey, selector func(string) bool, pageSize int32, pageToken string) (result *SearchResult, nextPageToken string, err error)
Search searches the values of the fields of objects pertaining to a type by calling a selector function for each field in all objects. For paging functionality see `Query` method.
func (*SearchResult) Descriptor
deprecated
func (*SearchResult) Descriptor() ([]byte, []int)
Deprecated: Use SearchResult.ProtoReflect.Descriptor instead.
func (*SearchResult) GetHits ¶
func (x *SearchResult) GetHits() []*SearchHit
func (*SearchResult) ProtoMessage ¶
func (*SearchResult) ProtoMessage()
func (*SearchResult) ProtoReflect ¶
func (x *SearchResult) ProtoReflect() protoreflect.Message
func (*SearchResult) Reset ¶
func (x *SearchResult) Reset()
func (*SearchResult) String ¶
func (x *SearchResult) String() string
type TObject ¶
type TObject struct { Metadata *Metadata `protobuf:"bytes,1,opt,name=metadata,proto3" json:"metadata,omitempty"` MyField string `protobuf:"bytes,2,opt,name=my_field,json=myField,proto3" json:"my_field,omitempty"` MyInt uint64 `protobuf:"varint,3,opt,name=my_int,json=myInt,proto3" json:"my_int,omitempty"` // contains filtered or unexported fields }
func (*TObject) Descriptor
deprecated
func (*TObject) GetMetadata ¶
func (*TObject) GetMyField ¶
func (*TObject) ProtoMessage ¶
func (*TObject) ProtoMessage()
func (*TObject) ProtoReflect ¶
func (x *TObject) ProtoReflect() protoreflect.Message
type TypeId ¶
type TypeId struct {
// contains filtered or unexported fields
}
TypeId is the key in the key-value database that is used to store IObjects. It has a memory layout like:
[b0 .. b3] TypeKey [b4 .. b20] Binary representation of an UUID
func MarshalTypeId ¶
MarshalTypeId creates a TypeId from a []byte slice
func TypeIdFromString ¶
TypeIdFromString returns a TypeId from an URL-encoded string
func (*TypeId) SetUuidBytes ¶
SetUuidBytes sets the Id (as bytes) of a TypeId