Documentation ¶
Overview ¶
Package rdoc is a native go implementation of a conflict-free replicated JSON data structure (JSON CRDT). A JSON CRDT is a data structure that automatically resolves concurrent modifications such that no updates are lost, and such that all replicas converge towards the same state.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Doc ¶
type Doc struct {
// contains filtered or unexported fields
}
Doc represents a JSON CRDT document; It maintains the metadata necessary to guarantee that the state of the document replicas converge over time without losing data.
func Init ¶
Init initiates and returns a new JSON CRDT document. The input is a string encoding an unique ID of the replica. The application logic *must* ensure that the replica IDs are unique within its network
func (*Doc) Apply ¶
Apply applies a valid operation represented as a JSON patch (https://tools.ietf.org/html/rfc6902) on the document. Apply handles both local and remote operations.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/gpestana/rdoc" ) func main() { // Initiates a new document with an unique ID (not enforced by underlying library) doc := rdoc.Init("document_id") remotePatch := []byte(`[ {"op": "add", "path": "/", "value": "user", "id":"1.380503024", "deps": [] }, {"op": "add", "path": "/name", "value": "Jane", "id":"2.1", "deps": ["1.1"] }, {"op": "add", "path": "/name", "value": "Jane", "id":"2.380503024", "deps": ["1.380503024"] } ]`) localPatch := []byte(`[ {"op": "add", "path": "/", "value": "user" }, {"op": "add", "path": "/age", "value": 20 } ]`) err := doc.Apply(remotePatch) if err != nil { // handle error, most likely reason is malformed patch } err = doc.Apply(localPatch) if err != nil { // handle error, most likely reason is malformed patch } // Marshals applied document into buffer buffer, err := json.Marshal(*doc) if err != nil { // handle error } fmt.Println(buffer) }
Output:
func (Doc) MarshalFullJSON ¶
MarshalFullJSON marshals a Doc into a buffer, including the dependencies field of each operation.
func (Doc) MarshalJSON ¶
MarshalJSON marshals a Doc into a buffer, excluding the deps field of each operation. The returned buffer *contains only the applied operations* that mutated the document state. The buffered operations as not included in the document serialization.
func (Doc) Operations ¶ added in v1.0.1
Operations returns the encoded operations applied to the document. The output of this function can be sent over the wire to other replicas, in order to achieve convergence.
Directories ¶
Path | Synopsis |
---|---|
Package idset implements a idempotent set of string IDs and helpers to use in the context of `rdoc`
|
Package idset implements a idempotent set of string IDs and helpers to use in the context of `rdoc` |
Package lclock implements Lamport logical clocks, with helps to be used in the context of rdocEvery operation has an unique ID in the network.
|
Package lclock implements Lamport logical clocks, with helps to be used in the context of rdocEvery operation has an unique ID in the network. |