Skip to main content

TangledHub Thcrdt Library

Project description

Build Status Stable Version Coverage Python License

THCRDT

Overview

TangledHub library that handles Conflict-free Replicated Data types.

Licencing

thcrdt is licensed under the BSD license. Check the LICENSE for details.


Installation

pip install thcrdt

Testing

docker-compose build thcrdt-test ; docker-compose run --rm thcrdt-test

Building

docker-compose build thcrdt-builning ; docker-compose run --rm thcrdt-builning

Publish

docker-compose build thcrdt-publish ; docker-compose run --rm thcrdt-publish

CRDT supported in this library

CRDT_Dict

doc: dict = crdt.from_({'cards': [{'x': 0, 'y': 10, 'z': 20}]}).unwrap()

CRDT_List

doc: dict = crdt.from_({'cards': [[1, 2, 3]]}).unwrap()

CRDT_Counter

doc: dict = crdt.from_({'cards': [Counter(1)]}).unwrap()

increment(int)

Increase counter value

doc['cards'][0].increment(2)

decrement(int)

Decrease counter value

doc['cards'][0].decrement(1)

API

thcrdt api is using the thresult library, so all api functions are returning result wrapped in Ok or Err object.
Therefore, in order to reach into the result object, you should use .unwrap() as in examples.

crdt = CRDT()

crdt.from_(self, o: Any)

Creates a new CRDT object and populates it with the contents of the passed object

Example:

Create document with initial state

doc_s0: dict = crdt.from_({'cards': [{'x': 0}]}).unwrap()

crdt.clone(self, Any)

Creates a new copy of the CRDT instance

Example:

doc_clone = crdt.clone(doc_s0)

crdt.change(self, doc: Any, fn: Callable)

Modify an CRDT object, returning an updated copy

Example:

# CLIENT A
# changes on client A
def doc_ca(doc: dict):
    doc['cards'][0]['x'] = 5
    doc['cards'][0]['y'] = 10
    doc['cards'][0]['z'] = 20
    
# The doc_a0 object is treated as immutable, you must never change it directly, create doc_a0 clone using crdt.clone().
# In order to update doc_a0 you should use crdt.change() instead.
doc_a1: dict = crdt.change(crdt.clone(doc_s0).unwrap(), doc_ca).unwrap()

crdt.merge(self, a: Any, b: Any)

Example:

Merges the two CRDT instances

# CLIENT B
# Create initial document on client B
doc_b0: dict = crdt.from_({'cards': [{}]}).unwrap()

# Now merge this two documents
doc_b1: dict = crdt.merge(doc_b0, doc_a1).unwrap()
# changes on client B
def doc_cb(doc: dict):
    doc['cards'][0]['x'] = -5
    doc['cards'][0]['y'] = -10
    doc['cards'][0]['z'] = -20
doc_b2: dict = crdt.change(crdt.clone(doc_s0).unwrap(), doc_cb).unwrap()
# Now merge the changes from client B back into client A. You can also
# do the merge the other way round, and you'll get the same result.
final_doc: dict = crdt.merge(doc_b2, doc_a1).unwrap()

crdt.get_changes(self, root: Any, doc: Any)

Returns a list of all the changes that were made in the document

Example:

# Create document with initial state
doc1s: dict = crdt.from_({'cards': [{}]}).unwrap()
# CLIENT A
# changes on client A
def doc_ca(doc: dict):
    doc['cards'][0]['x'] = 5
    doc['cards'][0]['y'] = 10
    doc['cards'][0]['z'] = 20
# In order to update doc1s you should use crdt.change().
doc_a1: dict = crdt.change(crdt.clone(doc1s).unwrap(), doc_ca).unwrap()
# Get changes made on client A. These changes are encoded as byte arrays (Uint8Array)
doc_a1_changes: list = crdt.get_changes(doc1s, doc_a1).unwrap()

crdt.apply_changes

Applies the list of changes to the given document, and returns a new document with those changes applied

Example:

# CLIENT B
# Now apply changes on client B
doc_b1, patch = crdt.apply_changes(doc1s, doc_a1_changes).unwrap()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thcrdt-0.9.2.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

thcrdt-0.9.2-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file thcrdt-0.9.2.tar.gz.

File metadata

  • Download URL: thcrdt-0.9.2.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.1 Linux/5.4.109+

File hashes

Hashes for thcrdt-0.9.2.tar.gz
Algorithm Hash digest
SHA256 d36191157e2cd0cfd34f92a53f9901d78c53ce6794fa25a5aebb1830a1548b19
MD5 711b7e5583c2bec036fe275d7452b60c
BLAKE2b-256 c8f48e48c293a0c55d0adc7a2c87f64cb5ed32de7753b1ebcfd08967a8ca08d5

See more details on using hashes here.

File details

Details for the file thcrdt-0.9.2-py3-none-any.whl.

File metadata

  • Download URL: thcrdt-0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.1 Linux/5.4.109+

File hashes

Hashes for thcrdt-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 89e4027b2f442027609caff4e451f8f4ef33d9d94ff0a4bd05a818d72adce010
MD5 21e51c13949a4d4e3a11c6d0be6ed0db
BLAKE2b-256 9b615c8a44d625d3d27ec85cd0d56f29d59fb3874159ebe92289c23da7f89d7e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page