gods

package module
v0.0.0-...-826a4dd Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2015 License: MIT Imports: 3 Imported by: 0

README

GoDS

Simple lib for text synchronization between server and client based on Differential Synchronization (DS) method by N. Fraser. This is basic implementation of this methods and readme document describe basic information about method. For better understanding of method read link above.

Install

Installation process is standard as any other package installation. Type the following command in your terminal:

go get github.com/lehovec/gods

After it the package is ready to use.

Import package in your project

Add following line in your *.go file:

import "github.com/lehovec/gods"

No more is needed.

Usage:

GoDS is lib for client server communication but DS method is same for both sides. Client or server side of lib is created by:

gods.New()

Every instance of gods synchronize one document. For every document you must create new instance of gods struct.

DS method creates sync cycles. Every initialisation of GoDS can communicate as server or as client at the same time by creating synchronization cycles. Every cycle synchronize document between client and server. You can create cycles by:

diffSync := gods.New()
diffSync.AddClientConnection(<id>)
diffSync.AddServerConnection(<id>)

param identifies synchronization cycle. ID can be any comparable type (see Go comparison operators) Note: You can't create server and client connection with same ID.

GoDS doesn't check connection status, you must remove connections cycles manually:

diffSync := gods.New()
diffSync.RemoveConnection(<id>)

There are two main method of GoDS lib. Fist is GetPatch. This method generate patches for send to client or server. Method receive id of connection cycle.

diffSync := gods.New()
// connection creation
patches, err := diffSync.GetPatch(<id>)

Second method is ApplyPatch. Method receive generate patches and apply them to document. This method then return patched document.

diffSync := gods.New()
// connection creation
document, err := diffSync.ApplyPatch(patches, <id>)

All documents is save to memory ( to variable ) by default. If you want to use another storage, you can control saving and obtaining of documents by impelmenting Storage interface.

type MyStorage struct {
}
func (storage *MyStorage) Get(docType int, id interface{}) (string, error) {
	// Your storage code
}
func (storage *MyStorage) Set(docType int, id interface{}, text string) error {
	// Your storage code
}
myStorage := new(MyStorage)
diffSync := gods.New()
diffSync.SetStorage(myStorage)

Methods Set and Get save three types of documents defined by first param docType. There are three constants defined in GoDS which represents document types

  • DOCTYPE_DOCUMENT
  • DOCTYPE_SHADOW
  • DOCTYPE_BACKUP

Second param is id of connection cycle. Every connection cycle has own unique DOCTYPE_SHADOW, and DOCTYPE_BACKUP. DOCTYPE_DOCUMENT is common for entire GoDS instance. Get function returns document content. Set function last param is saved document content. Note: RemoveConnection method doesn't call Storage methods.

Errors

Methods can send errors, in almost every case is good to remove connection and create new. For list which method return errors, see godoc.

Examples

All examples is available in examples dir. There is only one example but more examples will be added in future

Notes

Lib is not tested and I am beginner at Go. Please criticize my code or my english and creates issues. Thanks. Test will be added later just as errors documentation.

Support

If you do have a contribution for the package feel free to put up a Pull Request or open Issue.

Documentation

Overview

Package implements differential synchronization method by N. Fraser For more info please visit: https://neil.fraser.name/writing/sync/

Index

Constants

View Source
const DOCTYPE_BACKUP = 4

Constant is backup document send to Get or Set method in storage

View Source
const DOCTYPE_DOCUMENT = 1

Constant is standard document type send to Get or Set method in storage

View Source
const DOCTYPE_SHADOW = 2

Constant is shadow type document send to Get or Set method in storage

Variables

This section is empty.

Functions

This section is empty.

Types

type DSPatch

type DSPatch struct {
	Diffs   []diffmatchpatch.Patch // Slice of patches generated by diff patch match algorithm (see http://github.com/sergi/go-diff/diffmatchpatch)
	Version versions               // This struct contains versions of patches
}

Patch object, represents patch uses in Differential Synchronization Patch contains diff generated by diff algoritm and versions of documents uses in synchronization cycle

type GoDS

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

func New

func New() *GoDS

Inicialize new differential synchronization object

func (*GoDS) AddClientConnection

func (gods *GoDS) AddClientConnection(key interface{})

Create new client connection for GoDS object which represent client

func (*GoDS) AddServerConnection

func (gods *GoDS) AddServerConnection(key interface{})

Create new server connection for GoDS object which represent server

func (*GoDS) ApplyPatch

func (gods *GoDS) ApplyPatch(patches []DSPatch, connId interface{}) (string, error)

Receive patches and return patched document First method property receive array of DSPatch object generated by GetPatch method Second method property receive connection id provided in AddClientConnection or AddServerConnection methods

func (*GoDS) GetPatch

func (gods *GoDS) GetPatch(connId interface{}) ([]DSPatch, error)

Method return patch array which receive ApplyPatch method Method property receive connection id provided in AddClientConnection or AddServerConnection methods

func (*GoDS) RemoveConnection

func (gods *GoDS) RemoveConnection(connId interface{})

Remove connection by id

func (*GoDS) SetStorage

func (gods *GoDS) SetStorage(storage Storage) *GoDS

Set synchronization storage

type MemoryStorage

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

Default Storage interface implementation, save document to variables (arrays)

func NewMemoryStorage

func NewMemoryStorage() *MemoryStorage

Create new memory storage

func (*MemoryStorage) Get

func (storage *MemoryStorage) Get(docType int, id interface{}) (string, error)

func (*MemoryStorage) Set

func (storage *MemoryStorage) Set(docType int, id interface{}, text string) error

type Storage

type Storage interface {
	// Get document from storage by id
	Get(docType int, connId interface{}) (string, error)
	// Set document to storage by id
	Set(docType int, connId interface{}, text string) error
}

Storage interface provide methods to save and load documents created by Differential Synchronization You can create different kinds of storages for save and load from file, DB or eg. web... Storage function should return error!

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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