document

package
v0.4.26 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2024 License: Apache-2.0 Imports: 12 Imported by: 4

Documentation

Overview

Package document provides JSON-like document(CRDT) implementation.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDocumentRemoved occurs when the document is removed.
	ErrDocumentRemoved = errors.New("document is removed")
)
View Source
var (
	// ErrUnsupportedPayloadType is returned when the payload is unserializable to JSON.
	ErrUnsupportedPayloadType = errors.New("unsupported payload type")
)

Functions

This section is empty.

Types

type BroadcastRequest added in v0.4.7

type BroadcastRequest struct {
	Topic   string
	Payload []byte
}

BroadcastRequest represents a broadcast request that will be delivered to the client.

type DocEvent added in v0.4.5

type DocEvent struct {
	Type      DocEventType
	Presences map[string]innerpresence.Presence
}

DocEvent represents the event that occurred in the document.

type DocEventType added in v0.4.5

type DocEventType string

DocEventType represents the type of the event that occurred in the document.

const (
	// WatchedEvent means that the client has established a connection with the server,
	// enabling real-time synchronization.
	WatchedEvent DocEventType = "watched"

	// UnwatchedEvent means that the client has disconnected from the server,
	// disabling real-time synchronization.
	UnwatchedEvent DocEventType = "unwatched"

	// PresenceChangedEvent means that the presences of the clients who are editing
	// the document have changed.
	PresenceChangedEvent DocEventType = "presence-changed"
)

type Document

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

Document represents a document accessible to the user.

How document works: The operations are generated by the proxy while executing user's command on the clone. Then the operations will apply the changes into the base json root. This is to protect the base json from errors that may occur while user edit the document.

func New

func New(key key.Key, opts ...Option) *Document

New creates a new instance of Document.

func (*Document) ActorID added in v0.2.2

func (d *Document) ActorID() *time.ActorID

ActorID returns ID of the actor currently editing the document.

func (*Document) AddOnlineClient added in v0.4.5

func (d *Document) AddOnlineClient(clientID string)

AddOnlineClient adds the given client to the online clients.

func (*Document) AllPresences added in v0.4.6

func (d *Document) AllPresences() map[string]innerpresence.Presence

AllPresences returns the presence map of all clients regardless of whether the client is online or not.

func (*Document) ApplyChangePack

func (d *Document) ApplyChangePack(pack *change.Pack) error

ApplyChangePack applies the given change pack into this document.

func (*Document) Broadcast added in v0.4.7

func (d *Document) Broadcast(topic string, payload any) error

Broadcast encodes the given payload and sends a Broadcast request.

func (*Document) BroadcastEventHandlers added in v0.4.7

func (d *Document) BroadcastEventHandlers() map[string]func(
	topic string,
	publisher string,
	payload []byte,
) error

BroadcastEventHandlers returns the registered handlers for broadcast events.

func (*Document) BroadcastRequests added in v0.4.7

func (d *Document) BroadcastRequests() <-chan BroadcastRequest

BroadcastRequests returns the broadcast requests of this document.

func (*Document) BroadcastResponses added in v0.4.7

func (d *Document) BroadcastResponses() chan error

BroadcastResponses returns the broadcast responses of this document.

func (*Document) Checkpoint

func (d *Document) Checkpoint() change.Checkpoint

Checkpoint returns the checkpoint of this document.

func (*Document) CreateChangePack

func (d *Document) CreateChangePack() *change.Pack

CreateChangePack creates pack of the local changes to send to the server.

func (*Document) Events added in v0.4.5

func (d *Document) Events() <-chan DocEvent

Events returns the events of this document.

func (*Document) GarbageCollect

func (d *Document) GarbageCollect(ticket *time.Ticket) int

GarbageCollect purge elements that were removed before the given time.

func (*Document) GarbageLen

func (d *Document) GarbageLen() int

GarbageLen returns the count of removed elements.

func (*Document) HasLocalChanges

func (d *Document) HasLocalChanges() bool

HasLocalChanges returns whether this document has local changes or not.

func (*Document) InternalDocument added in v0.1.11

func (d *Document) InternalDocument() *InternalDocument

InternalDocument returns the internal document.

func (*Document) IsAttached

func (d *Document) IsAttached() bool

IsAttached returns whether this document is attached or not.

func (*Document) Key

func (d *Document) Key() key.Key

Key returns the key of this document.

func (*Document) Marshal

func (d *Document) Marshal() string

Marshal returns the JSON encoding of this document.

func (*Document) MyPresence added in v0.4.5

func (d *Document) MyPresence() innerpresence.Presence

MyPresence returns the presence of the actor.

func (*Document) Presence added in v0.4.5

func (d *Document) Presence(clientID string) innerpresence.Presence

Presence returns the presence of the given client. If the client is not online, it returns nil.

func (*Document) PresenceForTest added in v0.4.6

func (d *Document) PresenceForTest(clientID string) innerpresence.Presence

PresenceForTest returns the presence of the given client regardless of whether the client is online or not.

func (*Document) Presences added in v0.4.5

func (d *Document) Presences() map[string]innerpresence.Presence

Presences returns the presence map of online clients.

func (*Document) RemoveOnlineClient added in v0.4.5

func (d *Document) RemoveOnlineClient(clientID string)

RemoveOnlineClient removes the given client from the online clients.

func (*Document) Root added in v0.1.3

func (d *Document) Root() *json.Object

Root returns the root object of this document.

func (*Document) RootObject

func (d *Document) RootObject() *crdt.Object

RootObject returns the internal root object of this document.

func (*Document) SetActor

func (d *Document) SetActor(actor *time.ActorID)

SetActor sets actor into this document. This is also applied in the local changes the document has.

func (*Document) SetOnlineClients added in v0.4.6

func (d *Document) SetOnlineClients(clientIDs ...string)

SetOnlineClients sets the online clients.

func (*Document) SetStatus added in v0.1.1

func (d *Document) SetStatus(status StatusType)

SetStatus updates the status of this document.

func (*Document) Status added in v0.3.3

func (d *Document) Status() StatusType

Status returns the status of this document.

func (*Document) SubscribeBroadcastEvent added in v0.4.7

func (d *Document) SubscribeBroadcastEvent(
	topic string,
	handler func(topic, publisher string, payload []byte) error,
)

SubscribeBroadcastEvent subscribes to the given topic and registers an event handler.

func (*Document) UnsubscribeBroadcastEvent added in v0.4.7

func (d *Document) UnsubscribeBroadcastEvent(
	topic string,
)

UnsubscribeBroadcastEvent unsubscribes to the given topic and deregisters the event handler.

func (*Document) Update

func (d *Document) Update(
	updater func(root *json.Object, p *presence.Presence) error,
	msgAndArgs ...interface{},
) error

Update executes the given updater to update this document.

type InternalDocument

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

InternalDocument is a document that is used internally. It is not directly exposed to the user.

func NewInternalDocument

func NewInternalDocument(k key.Key) *InternalDocument

NewInternalDocument creates a new instance of InternalDocument.

func NewInternalDocumentFromSnapshot

func NewInternalDocumentFromSnapshot(
	k key.Key,
	serverSeq int64,
	lamport int64,
	snapshot []byte,
) (*InternalDocument, error)

NewInternalDocumentFromSnapshot creates a new instance of InternalDocument with the snapshot.

func (*InternalDocument) ActorID added in v0.2.2

func (d *InternalDocument) ActorID() *time.ActorID

ActorID returns ID of the actor currently editing the document.

func (*InternalDocument) AddOnlineClient added in v0.4.5

func (d *InternalDocument) AddOnlineClient(clientID string)

AddOnlineClient adds the given client to the online clients.

func (*InternalDocument) AllPresences added in v0.4.6

func (d *InternalDocument) AllPresences() map[string]innerpresence.Presence

AllPresences returns the presence map of all clients regardless of whether the client is online or not.

func (*InternalDocument) ApplyChangePack

func (d *InternalDocument) ApplyChangePack(pack *change.Pack, disableGC bool) error

ApplyChangePack applies the given change pack into this document.

func (*InternalDocument) ApplyChanges added in v0.2.3

func (d *InternalDocument) ApplyChanges(changes ...*change.Change) ([]DocEvent, error)

ApplyChanges applies remote changes to the document.

func (*InternalDocument) Checkpoint

func (d *InternalDocument) Checkpoint() change.Checkpoint

Checkpoint returns the checkpoint of this document.

func (*InternalDocument) CreateChangePack

func (d *InternalDocument) CreateChangePack() *change.Pack

CreateChangePack creates pack of the local changes to send to the server.

func (*InternalDocument) GarbageCollect

func (d *InternalDocument) GarbageCollect(ticket *time.Ticket) (int, error)

GarbageCollect purge elements that were removed before the given time.

func (*InternalDocument) GarbageLen

func (d *InternalDocument) GarbageLen() int

GarbageLen returns the count of removed elements.

func (*InternalDocument) HasLocalChanges

func (d *InternalDocument) HasLocalChanges() bool

HasLocalChanges returns whether this document has local changes or not.

func (*InternalDocument) IsAttached

func (d *InternalDocument) IsAttached() bool

IsAttached returns whether this document is attached or not.

func (*InternalDocument) Key

func (d *InternalDocument) Key() key.Key

Key returns the key of this document.

func (*InternalDocument) Lamport added in v0.2.4

func (d *InternalDocument) Lamport() int64

Lamport returns the Lamport clock of this document.

func (*InternalDocument) Marshal

func (d *InternalDocument) Marshal() string

Marshal returns the JSON encoding of this document.

func (*InternalDocument) MyPresence added in v0.4.5

func (d *InternalDocument) MyPresence() innerpresence.Presence

MyPresence returns the presence of the actor currently editing the document.

func (*InternalDocument) Presence added in v0.4.5

func (d *InternalDocument) Presence(clientID string) innerpresence.Presence

Presence returns the presence of the given client. If the client is not online, it returns nil.

func (*InternalDocument) PresenceForTest added in v0.4.6

func (d *InternalDocument) PresenceForTest(clientID string) innerpresence.Presence

PresenceForTest returns the presence of the given client regardless of whether the client is online or not.

func (*InternalDocument) Presences added in v0.4.5

func (d *InternalDocument) Presences() map[string]innerpresence.Presence

Presences returns the presence map of online clients.

func (*InternalDocument) RemoveOnlineClient added in v0.4.5

func (d *InternalDocument) RemoveOnlineClient(clientID string)

RemoveOnlineClient removes the given client from the online clients.

func (*InternalDocument) Root added in v0.2.1

func (d *InternalDocument) Root() *crdt.Root

Root returns the root of this document.

func (*InternalDocument) RootObject

func (d *InternalDocument) RootObject() *crdt.Object

RootObject returns the root object.

func (*InternalDocument) SetActor

func (d *InternalDocument) SetActor(actor *time.ActorID)

SetActor sets actor into this document. This is also applied in the local changes the document has.

func (*InternalDocument) SetOnlineClients added in v0.4.6

func (d *InternalDocument) SetOnlineClients(ids ...string)

SetOnlineClients sets the online clients.

func (*InternalDocument) SetStatus added in v0.1.1

func (d *InternalDocument) SetStatus(status StatusType)

SetStatus sets the status of this document.

type Option added in v0.4.20

type Option func(*Options)

Option configures Options.

func WithDisableGC added in v0.4.20

func WithDisableGC() Option

WithDisableGC configures the document to disable garbage collection.

type Options added in v0.4.20

type Options struct {
	// DisableGC disables garbage collection.
	DisableGC bool
}

Options configures how we set up the document.

type StatusType added in v0.3.3

type StatusType int

StatusType represents the status of the document.

const (
	// StatusDetached means that the document is not attached to the client.
	// The actor of the ticket is created without being assigned.
	StatusDetached StatusType = iota

	// StatusAttached means that this document is attached to the client.
	// The actor of the ticket is created with being assigned by the client.
	StatusAttached

	// StatusRemoved means that this document is removed. If the document is removed,
	// it cannot be edited.
	StatusRemoved
)

Directories

Path Synopsis
Package change provides the implementation of Change.
Package change provides the implementation of Change.
Package crdt provides the implementation of the CRDT data structure.
Package crdt provides the implementation of the CRDT data structure.
Package innerpresence provides the implementation of Presence.
Package innerpresence provides the implementation of Presence.
Package json provides the JSON document implementation.
Package json provides the JSON document implementation.
Package key provides the key implementation of the document.
Package key provides the key implementation of the document.
Package operations implements the operations that can be executed on the document.
Package operations implements the operations that can be executed on the document.
Package presence provides the proxy for the innerpresence.Presence to be manipulated from the outside.
Package presence provides the proxy for the innerpresence.Presence to be manipulated from the outside.
Package time provides logical clock and ticket for document.
Package time provides logical clock and ticket for document.

Jump to

Keyboard shortcuts

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