entity

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: May 5, 2025 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const HumanIdLength = 7
View Source
const UnsetCombinedId = CombinedId("unset")
View Source
const UnsetId = Id("unset")

Variables

This section is empty.

Functions

func IsErrMultipleMatch

func IsErrMultipleMatch(err error) bool

func IsErrNotFound

func IsErrNotFound(err error) bool

func Resolve

func Resolve[T Resolved](rs Resolvers, id Id) (T, error)

Resolve use the appropriate sub-resolver for the given type and find the Entity matching the Id.

func SeparateIds

func SeparateIds(prefix string) (primaryPrefix string, secondaryPrefix string)

SeparateIds extract primary and secondary prefix from an arbitrary length prefix of an Id created with CombineIds.

Types

type CachedResolver

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

CachedResolver is a resolver ensuring that loading is done only once through another Resolver.

func NewCachedResolver

func NewCachedResolver(resolver Resolver) *CachedResolver

func (*CachedResolver) Resolve

func (c *CachedResolver) Resolve(id Id) (Resolved, error)

type CombinedId

type CombinedId string

CombinedId is an Id holding information from both a primary Id and a secondary Id. While it looks like a regular Id, do not just cast from one to another. Instead, use CombineIds and SeparateIds to create it and split it.

func CombineIds

func CombineIds(primary Id, secondary Id) CombinedId

CombineIds compute a merged Id holding information from both the primary Id and the secondary Id.

This allows to later find efficiently a secondary element because we can access the primary one directly instead of searching for a primary that has a secondary matching the Id.

An example usage is Comment in a Bug. The interleaved Id will hold part of the Bug Id and part of the Comment Id.

To allow the use of an arbitrary length prefix of this Id, Ids from primary and secondary are interleaved with this irregular pattern to give the best chance to find the secondary even with a 7 character prefix.

Format is: PSPSPSPPPSPPPPSPPPPSPPPPSPPPPSPPPPSPPPPSPPPPSPPPPSPPPPSPPPPSPPPP

A complete interleaved Id hold 50 characters for the primary and 14 for the secondary, which give a key space of 36^50 for the primary (~6 * 10^77) and 36^14 for the secondary (~6 * 10^21). This asymmetry assumes a reasonable number of secondary within a primary Entity, while still allowing for a vast key space for the primary (that is, a globally merged database) with a low risk of collision.

Here is the breakdown of several common prefix length:

5: 3P, 2S 7: 4P, 3S 10: 6P, 4S 16: 11P, 5S

func (CombinedId) HasPrefix

func (ci CombinedId) HasPrefix(prefix string) bool

func (CombinedId) Human

func (ci CombinedId) Human() string

Human return the identifier, shortened for human consumption

func (CombinedId) MarshalGQL

func (ci CombinedId) MarshalGQL(w io.Writer)

MarshalGQL implement the Marshaler interface for gqlgen

func (CombinedId) PrimaryPrefix

func (ci CombinedId) PrimaryPrefix() string

PrimaryPrefix is a helper to extract the primary prefix. If practical, use SeparateIds instead.

func (CombinedId) SecondaryPrefix

func (ci CombinedId) SecondaryPrefix() string

SecondaryPrefix is a helper to extract the secondary prefix. If practical, use SeparateIds instead.

func (CombinedId) String

func (ci CombinedId) String() string

String return the identifier as a string

func (*CombinedId) UnmarshalGQL

func (ci *CombinedId) UnmarshalGQL(v interface{}) error

UnmarshalGQL implement the Unmarshaler interface for gqlgen

func (CombinedId) Validate

func (ci CombinedId) Validate() error

Validate tell if the Id is valid

type ErrInvalidFormat

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

ErrInvalidFormat is to be returned when reading on-disk data with an unexpected format or version.

func NewErrInvalidFormat

func NewErrInvalidFormat(version uint, expected uint) *ErrInvalidFormat

func NewErrUnknownFormat

func NewErrUnknownFormat(expected uint) *ErrInvalidFormat

func (ErrInvalidFormat) Error

func (e ErrInvalidFormat) Error() string

type ErrMultipleMatch

type ErrMultipleMatch struct {
	Matching []Id
	// contains filtered or unexported fields
}

ErrMultipleMatch is to be returned when more than one entity, item, element is found, where only one was expected.

func NewErrMultipleMatch

func NewErrMultipleMatch(typename string, matching []Id) *ErrMultipleMatch

func (ErrMultipleMatch) Error

func (e ErrMultipleMatch) Error() string

type ErrNotFound

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

ErrNotFound is to be returned when an entity, item, element is not found.

func NewErrNotFound

func NewErrNotFound(typename string) *ErrNotFound

func (ErrNotFound) Error

func (e ErrNotFound) Error() string

type Id

type Id string

Id is an identifier for an entity or part of an entity

func DeriveId

func DeriveId(data []byte) Id

DeriveId generate an Id from the serialization of the object or part of the object.

func RefToId

func RefToId(ref string) Id

RefToId parse a git reference and return the corresponding Entity's Id.

func RefsToIds

func RefsToIds(refs []string) []Id

RefsToIds parse a slice of git references and return the corresponding Entity's Id.

func (Id) HasPrefix

func (i Id) HasPrefix(prefix string) bool

func (Id) Human

func (i Id) Human() string

Human return the identifier, shortened for human consumption

func (Id) MarshalGQL

func (i Id) MarshalGQL(w io.Writer)

MarshalGQL implement the Marshaler interface for gqlgen

func (Id) String

func (i Id) String() string

String return the identifier as a string

func (*Id) UnmarshalGQL

func (i *Id) UnmarshalGQL(v interface{}) error

UnmarshalGQL implement the Unmarshaler interface for gqlgen

func (Id) Validate

func (i Id) Validate() error

Validate tell if the Id is valid

type Interface

type Interface interface {
	// Id return the Entity identifier
	//
	// This Id need to be immutable without having to store the entity somewhere (ie, an entity only in memory
	// should have a valid Id, and it should not change if further edit are done on this entity).
	// How to achieve that is up to the entity itself. A common way would be to take a hash of an immutable data at
	// the root of the entity.
	// It is acceptable to use such a hash and keep mutating that data as long as Id() is not called.
	Id() Id
	// Validate check if the Entity data is valid
	Validate() error
}

type MergeResult

type MergeResult struct {
	// Err is set when a terminal error occur in the process
	Err error

	Id     Id
	Status MergeStatus

	// Only set for Invalid status
	Reason string

	// Only set for New or Updated status
	Entity Interface
}

MergeResult hold the result of a merge operation on an Entity.

func NewMergeError

func NewMergeError(err error, id Id) MergeResult

func NewMergeInvalidStatus

func NewMergeInvalidStatus(id Id, reason string) MergeResult

func NewMergeNewStatus

func NewMergeNewStatus(id Id, entity Interface) MergeResult

func NewMergeNothingStatus

func NewMergeNothingStatus(id Id) MergeResult

func NewMergeUpdatedStatus

func NewMergeUpdatedStatus(id Id, entity Interface) MergeResult

func (MergeResult) String

func (mr MergeResult) String() string

type MergeStatus

type MergeStatus int

MergeStatus represent the result of a merge operation of an entity

const (
	MergeStatusNew     MergeStatus // a new Entity was created locally
	MergeStatusInvalid             // the remote data is invalid
	MergeStatusUpdated             // a local Entity has been updated
	MergeStatusNothing             // no changes were made to a local Entity (already up to date)
	MergeStatusError               // a terminal error happened
)

type Resolved

type Resolved interface {
	// Id returns the object identifier.
	Id() Id
}

Resolved is a minimal interface on which Resolver operates on. Notably, this operates on Entity and Excerpt in the cache.

type Resolver

type Resolver interface {
	Resolve(id Id) (Resolved, error)
}

Resolver is an interface to find an Entity from its Id

func MakeResolver

func MakeResolver(entities ...Resolved) Resolver

MakeResolver create a resolver able to return the given entities.

type ResolverFunc

type ResolverFunc[EntityT Resolved] func(id Id) (EntityT, error)

ResolverFunc is a helper to morph a function resolver into a Resolver

func (ResolverFunc[EntityT]) Resolve

func (fn ResolverFunc[EntityT]) Resolve(id Id) (Resolved, error)

type Resolvers

type Resolvers map[Resolved]Resolver

Resolvers is a collection of Resolver, for different type of Entity

type StreamedEntity

type StreamedEntity[EntityT Interface] struct {
	Err    error
	Entity EntityT

	// CurrentEntity is the index of the current entity being streamed, to express progress.
	CurrentEntity int64
	// TotalEntities is the total count of expected entities, if known.
	TotalEntities int64
}

Directories

Path Synopsis
Package dag contains the base common code to define an entity stored in a chain of git objects, supporting actions like Push, Pull and Merge.
Package dag contains the base common code to define an entity stored in a chain of git objects, supporting actions like Push, Pull and Merge.

Jump to

Keyboard shortcuts

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