guid

package
v0.0.0-...-692e47d Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package guid implements a 64-bit global unique k-sortable ID suitable for use as the primary key of a database where entities may be generated on different hosts and merged later. The uid has 31 bits of a timestamp and 32 bits of a hashed value of a pseudorandom number and the content of the entity.

General usage would look like this, (with field_1 and field_2 being two different aspects of the content, such as Title and Text):

guid, err := guid.NewGuidGenerator().
    Content(field_1).
    Content(field_2).
    Guid()

The resulting guid implements both sql.Scanner and driver.Valuer interfaces (mapping to a 64-bit integer), so can be used directly in database calls.

Index

Constants

This section is empty.

Variables

View Source
var EpochString = "2022 Jan 1"
View Source
var NullGuid = Guid{/* contains filtered or unexported fields */}

Functions

func Epoch

func Epoch() time.Time

Epoch returns the beginning of the current epoch used to calculate the Unix-like timestamp.

Types

type Guid

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

Guid implements the following interfaces: * database/sql/driver.Valuer * databse/sql.Scanner * encoding.TextMarshaler * encoding.TextUnmarshaler * encoding/json.Marshaler * encoding/json.Unmarshaler

The Scanner / Valuer interpret it as an int64, allowing it to be used as a primary key.

func (Guid) Equal

func (g Guid) Equal(h Guid) bool

func (*Guid) IsNull

func (g *Guid) IsNull() bool

func (Guid) Less

func (g Guid) Less(j Guid) bool

Less returns true if i < j

func (Guid) MarshalJSON

func (g Guid) MarshalJSON() ([]byte, error)

func (Guid) MarshalText

func (g Guid) MarshalText() ([]byte, error)

func (*Guid) Scan

func (g *Guid) Scan(src interface{}) error

func (Guid) String

func (g Guid) String() string

func (*Guid) UnmarshalJSON

func (g *Guid) UnmarshalJSON(b []byte) error

func (*Guid) UnmarshalText

func (g *Guid) UnmarshalText(text []byte) error

func (Guid) Value

func (g Guid) Value() (driver.Value, error)

type GuidGenerator

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

GuidGenerator is used to store information necessary to genreate a Guid. This should always be created using GuidGenerator; then all content added using the Content() methods; and then finally the Guid() method called to get the resulting compact instance of the guid.

func NewGuidGenerator

func NewGuidGenerator() *GuidGenerator

func (*GuidGenerator) Content

func (g *GuidGenerator) Content(e any) *GuidGenerator

Content hashes in the content to the hash portion of the Guid. Internally "encoding/gob" is used to translate objects into bytestreams, so any content must be accessible to that package. Additionally, a Guid type may be passed in.

If an error has already occured in the generation pipeline, this will be ignored. If an error occurs during the encodring, the error will be set, Errors can be read by calling the Guid() method.

If the GuidGenerator has already been evaluated (by calling the Guid() method), this will cause the error to be set.

func (*GuidGenerator) Guid

func (g *GuidGenerator) Guid() (Guid, error)

Guid evaluates the content of GuidGenerator into a Guid type. The result is cached, so that repeated calls to Guid will not do a complete recalculation.

If any errors have happened during the generation pipeline, NullGuid will be returned, along with the error.

Once a GuidGenerator is evaluated with this method, no further content can be added; doing so will cause the error to be set.

Jump to

Keyboard shortcuts

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