py3crdt package¶
Submodules¶
py3crdt.gcounter module¶
-
class
py3crdt.gcounter.GCounter(id)¶ Bases:
objectGrow Only Counter CRDT Implementation.
- Notes:
It implements an array of nodes where the value of array works as a counter. The value of array is sum of the values of the nodes in the array. Each node is assigned an ID equivalent to the index of the node in the array. The array is an equivalent for a cluster of nodes. Updating involves each node incrementing its own index value in the array. Merging occurs by taking the maximum of every node value in the cluster. Comparison function is included to verify the increments. Internal state is monotonically increased by application of each update function according to compare function.
- Attributes:
payload (dict): Dict of node_key : node_value. id (any_type): ID of the class object.
-
add_new_node(key)¶ The function to add the key to the payload.
- Args:
key (any_type): The key of the node to be added.
- Note:
Initialize the key’s value to 0
-
compare(gc2)¶ The function to compare the payload value with argument’s object’s payload value.
- Args:
gc2 (GCounter): The GCounter object to be compared.
- Returns:
bool: True if sum of payload values is greater than that of argument’s object, False otherwise.
-
display()¶ The function to print the object’s payload.
-
inc(key)¶ The function to increment the key’s value in payload.
- Args:
key (any_type): The key of the node to be added.
-
merge(gc2)¶ The function to merge the GCounter object’s payload with the argument’s payload.
- Args:
gc2 (GCounter): The GCounter object to be compared.
- Note:
Merging occurs on the basis of the max value from the payloads for each key.
-
query()¶ The function to return sum of the payload values.
- Returns:
int: Sum of the payload values.
py3crdt.gset module¶
-
class
py3crdt.gset.GSet(id)¶ Bases:
objectGrow Only Set CRDT Implementation.
- Notes:
A set of elements where elements can only be added and once an element is added, it cannot be removed. Merging returns union of the two G-Sets.
- Attributes:
payload (list): List of elements. id (any_type): ID of the class object.
-
add(elem)¶ The function to add the element to the payload.
- Args:
elem (any_type): The element to be added.
-
compare(gs2)¶ The function to compare two GSet objects.
- Args:
gs2 (GSet): The GSet object to be compared.
- Returns:
bool: True if payloads of both objects are same, False otherwise.
-
display()¶ The function to print the object’s payload.
-
merge(gs2)¶ The function to merge the GSet object’s payload with the argument’s payload.
- Args:
gs2 (GSet): The GSet object to be compared.
-
query(elem)¶ The function to return True if element is present in the payload.
- Args:
elem (any_type): The element to be searched for.
- Returns:
bool: True if element is present in the payload, False otherwise.
py3crdt.lww module¶
-
class
py3crdt.lww.LWWElementSet(id)¶ Bases:
objectLast-Writer-Wins Element Set CRDT Implementation.
- Notes:
Similar to 2P-Set except each element is added/removed with a timestamp. An element is a member of the set if it is in the “add” set but not in the “remove” set, or if it is in both the “add” and “remove” set then timestamp in “remove” set should be less than that of the latest timestamp in “add” set. “Bias” comes into play, if timestamps are equal which can be towards “add” or “remove”. In this set, an element can be reinserted after being removed and thus, it has an advantage over 2P-Set.
- Attributes:
A (list): List of elements added. R (list): List of elements removed. id (any_type): ID of the class object. lwf (LWWFunctions): LWWFunctions object to access the static methods.
-
add(elem)¶ The function to add the element.
- Args:
elem (any_type): The element to be added.
- Note:
‘elem’ is added to payload ‘A’
-
compare(lww)¶ The function to compare the payloads with the argument’s payloads.
- Args:
lww (LWWElementSet): Object to be compared to.
- Note:
Compares payload ‘A’ and payload ‘R’ of the objects
- Returns:
bool: True if payloads of both objects are same, False otherwise.
-
display()¶ The function to print the object’s payloads.
-
merge(lww)¶ The function to merge the payloads with the argument’s payloads.
- Args:
lww (LWWElementSet): Object to be merged from.
-
query(elem)¶ The function to return True if element is present in the payload.
- Args:
elem (any_type): The element to be searched for.
- Returns:
bool: True if element present in the payload ‘A’ with latest timestamp than in payload ‘R’, False otherwise.
-
remove(elem)¶ The function to remove the element.
- Args:
elem (any_type): The element to be removed.
- Note:
‘elem’ is added to payload ‘R’
-
class
py3crdt.lww.LWWFunctions¶ Bases:
objectA class to provide static methods to LWWElementSet Class
-
static
compare(payload1, payload2)¶ The function to compare two LWW objects’ payloads.
- Args:
payload1 (list): payloads to be compared withs. payload2 (list): payloads to be compared tos.
- Returns:
bool: True if payloads of both objects are same, False otherwise.
-
static
display(name, payload)¶ The function to print the object.
- Args:
name (string): payloads types. payload (list): payloads to displays.
-
static
merge(payload1, payload2)¶ The function to merge the payload2 to payload1.
- Args:
payload1 (list): payloads to be merged tos. payload2 (list): payloads to be merged froms.
- Returns:
payload1 (list): payloads merged tos.
-
static
update(payload, elem)¶ The function to add an element to Payload.
- Args:
payload (list): payloads in which element has to be addeds. elem (any_type): The element to be added.
- Returns:
payload (list): payloads in which element is added.
-
static
py3crdt.node module¶
-
class
py3crdt.node.Node(id)¶ Bases:
objectA class to create a node with a id.
- Attributes:
id (any_type): ID of the Node object.
py3crdt.orset module¶
-
class
py3crdt.orset.ORSet(id)¶ Bases:
objectObserved-Removed Set CRDT Implementation.
- Notes:
Similar to LWW-Element-Set, except that it unique tags are used instead of timestamps. For each element, a list of add/remove tags are maintained. An element is added by adding a newly generated unique tag to the add-tag list for the element. Removing an element involves copying all the tags in it’s add-tag list to it’s remove-tag list. An element is a member of the set iff there exists a tag in add-tag list which is not in remove-tag list.
- Attributes:
A (list): List of elements added. R (list): List of elements removed. id (any_type): ID of the class object. orsetf (ORSetFunctions): ORSetFunctions object to access the static methods.
-
add(elem, unique_tag)¶ The function to add the element.
- Args:
elem (any_type): The element to be added. unique_tag (any_type): Tag to identify element.
- Note:
‘elem’ is added to payload ‘A’
-
compare(orset)¶ The function to compare the payloads with the argument’s payloads.
- Args:
orset (ORSet): Object to be compared to.
- Note:
Compares payload ‘A’ and payload ‘R’ of the objects
- Returns:
bool: True if payloads of both objects are same, False otherwise.
-
display()¶ The function to print the object’s payloads.
-
merge(orset)¶ The function to merge the payloads with the argument’s payloads.
- Args:
orset (ORSet): Object to be merged from.
-
query(elem)¶ The function to return True if element is present in the payload.
- Args:
elem (any_type): The element to be searched for.
- Returns:
bool: True if element’s tags present in the payload ‘A’ but not in payload ‘R’, False otherwise.
-
remove(elem)¶ The function to remove the element.
- Args:
elem (any_type): The element to be removed.
- Note:
‘elem’ is added to payload ‘R’
-
class
py3crdt.orset.ORSetFunctions¶ Bases:
objectA class to provide static methods to ORSet Class
-
static
add(payload, elem, unique_tag)¶ The function to add an element with it’s unique tag to ORSet object’s payload.
- Args:
payload (list): Payload in which element has to be added. elem (any_type): The element to be added. unique_tag (any_type): Tag to identify element.
- Returns:
payload (list): Payload in which element is added.
-
static
compare(payload1, payload2)¶ The function to compare two ORSet objects’ payloads.
- Args:
payload1 (list): Payload to be compared with. payload2 (list): Payload to be compared to.
- Returns:
bool: True if payloads of both objects are same, False otherwise.
-
static
display(name, payload)¶ The function to print the object.
- Args:
name (string): Payload type. payload (list): Payload to display.
- Returns:
-1: If no element in the payload
-
static
merge(payload1, payload2)¶ The function to merge the payload2 to payload1.
- Args:
payload1 (list): Payload to be merged to. payload2 (list): Payload to be merged from.
- Returns:
payload1 (list): Payload merged to.
-
static
query(elem, payload)¶ The function to return tags list if element is present in the payload.
- Args:
elem (any_type): The element to be searched for. payload (list): Payload to query from.
- Returns:
list: Tags list if element is present in the payload, Empty list otherwise.
-
static
remove(payloadA, payloadR, elem)¶ The function to remove an element from ORSet object’s payload.
- Args:
payloadA (list): Payload in which elements to be added are added. payloadR (list): Payload in which elements to be removed are added. elem (any_type): The element to be removed.
- Note:
It searches for element in payloadA, and copies it’s tags to payloadR
- Returns:
payloadR (list): Payload in which elements to be removed are added.
-
static
py3crdt.pncounter module¶
-
class
py3crdt.pncounter.PNCounter(id)¶ Bases:
objectPositive-Negative Counter CRDT Implementation.
- Notes:
This counter supports both increment and decrement operations. It combines two G-Counters namely “P” (for incrementing) and “N” (for decrementing) counter. The value of the counter is the value of the P counter minus the value of the N counter. Merging involves merging the P and N counter independently.
- Attributes:
P (PNCounter): PNCounter object to increment counter. N (PNCounter): PNCounter object to deceremnt counter. id (any_type): ID of the class object.
-
add_new_node(key)¶ The function to add the key to the payload.
- Args:
key (any_type): The key of the node to be added.
- Note:
Adds the key to both gcounter objects P and N.
-
compare(pnc2)¶ The function to compare the payload value with argument’s object’s payload value.
- Args:
pnc2 (PNCounter): The PNCounter object to be compared.
- Returns:
bool: True if effective payload value is greater than that of argument’s object, False otherwise.
-
dec(key)¶ The function to decrement the key’s value in payload.
- Args:
key (any_type): The key of the node to be added.
- Note:
Increments the value of gcounter object N.
-
display(name)¶ The function to print the object’s payloads.
-
inc(key)¶ The function to increment the key’s value in payload.
- Args:
key (any_type): The key of the node to be added.
- Note:
Increments the value of gcounter object P.
-
merge(pnc2)¶ The function to merge the PNCounter object’s payload with the argument’s payload.
- Args:
pnc2 (PNCounter): The PNCounter object to be compared.
- Note:
Merging occurs on the basis of the max value from the payloads for each key. Merges both objects P and N.
-
query()¶ The function to return the effective counter value.
- Note:
Returns the difference between gcounter object P and N.
py3crdt.sequence module¶
-
class
py3crdt.sequence.SeqFunctions¶ Bases:
objectA class to provide static methods to Sequence Class
-
static
add(payload, elem, id)¶ The function to add an element with it’s ID to Sequence object’s payload.
- Args:
payload (list): Payload in which element has to be added. elem (any_type): The element to be added. id (any_type): ID of the element.
- Returns:
payload (list): Payload in which element is added.
-
static
display(name, payload)¶ The function to print the object.
- Args:
name (string): Payload type. payload (list): Payload to display.
-
static
get_seq(payload)¶ The function to return a string of elements in the payload.
- Args:
payload (list): Payload to display.
- Returns:
seq (string): String of elements in the payload
-
static
merge(payload1, payload2)¶ The function to merge the payload2 to payload1.
- Args:
payload1 (list): Payload to be merged to. payload2 (list): Payload to be merged from.
- Returns:
payload1 (list): Payload merged to.
-
static
remove(payload, id)¶ The function to remove an element from Sequence object’s payload.
- Args:
payload (list): Payload in which elements to be removed are added. id (any_type): ID of the element to be removed.
- Returns:
payload (list): Payload in which elements to be removed are added.
-
static
-
class
py3crdt.sequence.Sequence(id)¶ Bases:
objectSequence CRDT Implementation.
- Notes:
An ordered set, list or a sequence of elements. This CRDT can be build on top of other Set based CRDTs by sorting them on some basis. We have used this CRDT to build a Collaborative Code/Text Editor.
- Attributes:
elem_list (list): List of elements added. id_remv_list (list): List of IDs removed. id_seq (list): List of IDs in sequence. id_elem_seq (list): List of elements in sequence. id (any_type): ID of the class object. seqf (SeqFunctions): SeqFunctions object to access the static methods.
-
add(elem, id)¶ The function to add the element.
- Args:
elem (any_type): The element to be added. id (any_type): ID of the element.
- Note:
‘elem’ is added to elem_list
-
display()¶ The function to print the object’s payloads.
-
get_seq()¶ The function to get the sequence as string.
-
merge(list, func='na')¶ The function to merge the lists with the argument’s list.
- Args:
- list (
list: List to be merged from, Sequence: Object to be merged from.
) func (
‘na’: Merge both elem_list and id_remv_list, ‘elem’: Merge elem_list, ‘id’: Merge id_remv_list,
)
-
query(id)¶ The function to return True if ID of the element is present in the list.
- Args:
elem (any_type): The element to be searched for.
- Returns:
bool: True if element’s ID present in the elem_list but not in id_remv_list , False otherwise.
-
remove(id)¶ The function to remove the element.
- Args:
id (any_type): The ID of the element to be removed.
- Note:
‘elem’ is added to id_remv_list
-
update_seq()¶
py3crdt.twopset module¶
-
class
py3crdt.twopset.TwoPSet(id)¶ Bases:
objectTwo-Phase Set CRDT Implementation.
- Notes:
A set in which elements can be added as well as removed. It combines two G-Sets namely “add” and “remove” set. For adding/removing an element, it is inserted in the “add”/“remove” set. An element is a member of the set if it is in the “add” set but not in the “remove” set. Query function returns whether the element is a member of the set or not. Hence, if an element is removed, query will never return True for that element, so it cannot be re-added. Merging involves union of the “add”/“remove” sets.
- Attributes:
A (list): List of elements added. R (list): List of elements removed. id (any_type): ID of the class object.
-
add(elem)¶ The function to add the element.
- Args:
elem (any_type): The element to be added.
- Note:
‘elem’ is added to payload ‘A’
-
compare(tps2)¶ The function to compare the payloads with the argument’s payloads.
- Args:
tps2 (TwoPSet): Object to be compared to.
- Note:
Compares payload ‘A’ and payload ‘R’ of the objects
- Returns:
bool: True if payloads of both objects are same, False otherwise.
-
display()¶ The function to print the object’s payloads.
-
merge(tps2)¶ The function to merge the payloads with the argument’s payloads.
- Args:
tps2 (TwoPSet): Object to be merged from.
-
query(elem)¶ The function to return True if element is present in the payload.
- Args:
elem (any_type): The element to be searched for.
- Returns:
bool: True if element’s tags present in the payload ‘A’ but not in payload ‘R’, False otherwise.
-
remove(elem)¶ The function to remove the element.
- Args:
elem (any_type): The element to be removed.
- Note:
‘elem’ is added to payload ‘R’