db

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ClientDeactivated = "deactivated"
	ClientActivated   = "activated"
)

Below are statuses of the client.

Variables

View Source
var (
	ErrClientNotActivated      = errors.New("client not activated")
	ErrDocumentNotAttached     = errors.New("document not attached")
	ErrDocumentNeverAttached   = errors.New("client has never attached the document")
	ErrDocumentAlreadyAttached = errors.New("document already attached")
)

Below are the errors may occur depending on the document and client status.

View Source
var (
	// ErrClientNotFound is returned when the client could not be found.
	ErrClientNotFound = errors.New("client not found")

	// ErrDocumentNotFound is returned when the document could not be found.
	ErrDocumentNotFound = errors.New("document not found")

	// ErrConflictOnUpdate is returned when a conflict occurs during update.
	ErrConflictOnUpdate = errors.New("conflict on update")
)
View Source
var ErrEncodeOperationFailed = errors.New("encode operation failed")

ErrEncodeOperationFailed is returned when encoding operation failed.

View Source
var (
	// ErrInvalidID is returned when the given ID is not ObjectID.
	ErrInvalidID = errors.New("invalid ID")
)

Functions

func EncodeOperations

func EncodeOperations(operations []operation.Operation) ([][]byte, error)

EncodeOperations encodes the given operations into bytes array.

Types

type ChangeInfo

type ChangeInfo struct {
	ID         ID       `bson:"_id"`
	DocID      ID       `bson:"doc_id"`
	ServerSeq  uint64   `bson:"server_seq"`
	ClientSeq  uint32   `bson:"client_seq"`
	Lamport    uint64   `bson:"lamport"`
	ActorID    ID       `bson:"actor_id"`
	Message    string   `bson:"message"`
	Operations [][]byte `bson:"operations"`
}

ChangeInfo is a structure representing information of a change.

func (*ChangeInfo) ToChange

func (i *ChangeInfo) ToChange() (*change.Change, error)

ToChange creates Change model from this ChangeInfo.

type ClientDocInfo

type ClientDocInfo struct {
	Status    string `bson:"status"`
	ServerSeq uint64 `bson:"server_seq"`
	ClientSeq uint32 `bson:"client_seq"`
}

ClientDocInfo is a structure representing information of the document attached to the client.

type ClientInfo

type ClientInfo struct {
	// ID is the unique ID of the client.
	ID ID `bson:"_id"`

	// Key is the key of the client. It is used to identify the client by users.
	Key string `bson:"key"`

	// Status is the status of the client.
	Status string `bson:"status"`

	// Documents is a map of document which is attached to the client.
	Documents map[ID]*ClientDocInfo `bson:"documents"`

	// CreatedAt is the time when the client was created.
	CreatedAt time.Time `bson:"created_at"`

	// UpdatedAt is the last time the client was accessed.
	// NOTE(hackerwins): The field name is "updated_at" but it is used as
	// "accessed_at".
	UpdatedAt time.Time `bson:"updated_at"`
}

ClientInfo is a structure representing information of a client.

func (*ClientInfo) AttachDocument

func (i *ClientInfo) AttachDocument(docID ID) error

AttachDocument attaches the given document to this client.

func (*ClientInfo) Checkpoint

func (i *ClientInfo) Checkpoint(docID ID) *change.Checkpoint

Checkpoint returns the checkpoint of the given document.

func (*ClientInfo) DeepCopy added in v0.1.11

func (i *ClientInfo) DeepCopy() *ClientInfo

DeepCopy returns a deep copy of this client info.

func (*ClientInfo) DetachDocument

func (i *ClientInfo) DetachDocument(docID ID) error

DetachDocument detaches the given document from this client.

func (*ClientInfo) EnsureDocumentAttached

func (i *ClientInfo) EnsureDocumentAttached(docID ID) error

EnsureDocumentAttached ensures the given document is attached.

func (*ClientInfo) IsAttached

func (i *ClientInfo) IsAttached(docID ID) (bool, error)

IsAttached returns whether the given document is attached to this client.

func (*ClientInfo) UpdateCheckpoint

func (i *ClientInfo) UpdateCheckpoint(
	docID ID,
	cp *change.Checkpoint,
) error

UpdateCheckpoint updates the checkpoint of the given document.

type DB

type DB interface {
	// Close all resources of this database.
	Close() error

	// ActivateClient activates the client of the given key.
	ActivateClient(ctx context.Context, key string) (*ClientInfo, error)

	// DeactivateClient deactivates the client of the given ID.
	DeactivateClient(ctx context.Context, clientID ID) (*ClientInfo, error)

	// FindClientInfoByID finds the client of the given ID.
	FindClientInfoByID(ctx context.Context, clientID ID) (*ClientInfo, error)

	// UpdateClientInfoAfterPushPull updates the client from the given clientInfo
	// after handling PushPull.
	UpdateClientInfoAfterPushPull(ctx context.Context, clientInfo *ClientInfo, docInfo *DocInfo) error

	// FindDeactivateCandidates finds the housekeeping candidates.
	FindDeactivateCandidates(
		ctx context.Context,
		deactivateThreshold gotime.Duration,
		candidatesLimit int,
	) ([]*ClientInfo, error)

	// FindDocInfoByKey finds the document of the given key. If the
	// createDocIfNotExist condition is true, create the document if it does not
	// exist.
	FindDocInfoByKey(
		ctx context.Context,
		clientInfo *ClientInfo,
		bsonDocKey string,
		createDocIfNotExist bool,
	) (*DocInfo, error)

	// CreateChangeInfos stores the given changes then updates the given docInfo.
	CreateChangeInfos(
		ctx context.Context,
		docInfo *DocInfo,
		initialServerSeq uint64,
		changes []*change.Change,
	) error

	// FindChangesBetweenServerSeqs returns the changes between two server sequences.
	FindChangesBetweenServerSeqs(
		ctx context.Context,
		docID ID,
		from uint64,
		to uint64,
	) ([]*change.Change, error)

	// FindChangeInfosBetweenServerSeqs returns the changeInfos between two server sequences.
	FindChangeInfosBetweenServerSeqs(
		ctx context.Context,
		docID ID,
		from uint64,
		to uint64,
	) ([]*ChangeInfo, error)

	// CreateSnapshotInfo stores the snapshot of the given document.
	CreateSnapshotInfo(ctx context.Context, docID ID, doc *document.InternalDocument) error

	// FindLastSnapshotInfo finds the last snapshot of the given document.
	FindLastSnapshotInfo(ctx context.Context, docID ID) (*SnapshotInfo, error)

	// UpdateAndFindMinSyncedTicket updates the given serverSeq of the given client
	// and returns the min synced ticket.
	UpdateAndFindMinSyncedTicket(
		ctx context.Context,
		clientInfo *ClientInfo,
		docID ID,
		serverSeq uint64,
	) (*time.Ticket, error)

	// UpdateSyncedSeq updates the syncedSeq of the given client.
	UpdateSyncedSeq(
		ctx context.Context,
		clientInfo *ClientInfo,
		docID ID,
		serverSeq uint64,
	) error
}

DB represents database which reads or saves Yorkie data.

type DocInfo

type DocInfo struct {
	ID         ID        `bson:"_id"`
	Key        string    `bson:"key"`
	ServerSeq  uint64    `bson:"server_seq"`
	Owner      ID        `bson:"owner"`
	CreatedAt  time.Time `bson:"created_at"`
	AccessedAt time.Time `bson:"accessed_at"`
	UpdatedAt  time.Time `bson:"updated_at"`
}

DocInfo is a structure representing information of the document.

func (*DocInfo) DeepCopy added in v0.1.11

func (info *DocInfo) DeepCopy() *DocInfo

DeepCopy creates a deep copy of this DocInfo.

func (*DocInfo) GetKey

func (info *DocInfo) GetKey() (*key.Key, error)

GetKey creates Key instance of this DocInfo.

func (*DocInfo) IncreaseServerSeq

func (info *DocInfo) IncreaseServerSeq() uint64

IncreaseServerSeq increases server sequence of the document.

type ID

type ID string

ID represents ID of entity.

func IDFromActorID added in v0.2.1

func IDFromActorID(actorID time.ActorID) ID

IDFromActorID returns ID represented by the encoded hexadecimal string from actor ID.

func IDFromBytes added in v0.1.2

func IDFromBytes(bytes []byte) ID

IDFromBytes returns ID represented by the encoded hexadecimal string from bytes.

func (*ID) Bytes added in v0.1.2

func (id *ID) Bytes() ([]byte, error)

Bytes returns bytes of decoded hexadecimal string representation of this ID.

func (*ID) String

func (id *ID) String() string

String returns a string representation of this ID.

func (*ID) Validate added in v0.1.11

func (id *ID) Validate() error

Validate returns error if this ID is invalid.

type SnapshotInfo

type SnapshotInfo struct {
	ID        ID        `bson:"_id"`
	DocID     ID        `bson:"doc_id"`
	ServerSeq uint64    `bson:"server_seq"`
	Snapshot  []byte    `bson:"snapshot"`
	CreatedAt time.Time `bson:"created_at"`
}

SnapshotInfo is a structure representing information of the snapshot.

type SyncedSeqInfo

type SyncedSeqInfo struct {
	ID        ID     `bson:"_id"`
	DocID     ID     `bson:"doc_id"`
	ClientID  ID     `bson:"client_id"`
	Lamport   uint64 `bson:"lamport"`
	ActorID   ID     `bson:"actor_id"`
	ServerSeq uint64 `bson:"server_seq"`
}

SyncedSeqInfo is a structure representing information about the synchronized sequence for each client.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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