Пример #1
0
 def test_merge_same_set(self):
     crdt1 = GSet()
     crdt2 = GSet()
     crdt1.add('A')
     crdt2.add('A')
     crdt1 = crdt1.merge(crdt2)
     self.assertEqual(list(crdt1.values()), ['A'])
Пример #2
0
 def test_iterator_functionality(self):
     crdt = GSet()
     crdt.add('A')
     crdt.add('B')
     iterator = crdt.values()
     self.assertEqual(next(iterator), 'A')
     self.assertEqual(next(iterator), 'B')
Пример #3
0
 def test_merge(self):
     crdt1 = GSet()
     crdt2 = GSet()
     crdt1.add('A')
     crdt2.add('B')
     crdt2.add('C')
     crdt1 = crdt1.merge(crdt2)
     self.assertEqual(list(crdt1.values()), ['A', 'B', 'C'])
Пример #4
0
def mergeUsers(middlewareName1, middlewareName2):
    middlewareData1 = load_middleware(middlewareName1)
    middlewareData2 = load_middleware(middlewareName2)
    data1 = GSet.loadFromDict(middlewareData1['Users'])
    data2 = GSet.loadFromDict(middlewareData2['Users'])
    data1.merge(data2)
    mergedList = data1.toDict()
    middlewareData1['Users'] = mergedList
    save_middleware(middlewareData1, middlewareName1)
    save_middleware(middlewareData1, middlewareName2)
Пример #5
0
    def test_no_removal(self):
        crdt = GSet()
        crdt.add('A')
        try:
            crdt.discard('A')
        except NotImplementedError as error:
            s = str(error)

        self.assertEqual(s, "This is a grow-only set")
        self.assertEqual(set(crdt.values()), set(['A']))
Пример #6
0
    def test_only_unique_items(self):
        crdt = GSet()
        crdt.add(1)
        crdt.add('A')
        # crdt.add('A')
        crdt.add(1)
        # crdt.add('A')
        obj = {"hello": 'ABC'}
        crdt.add(json.dumps(obj))
        crdt.add(json.dumps(obj))
        crdt.add(json.dumps({"hello": 'ABCD'}))

        expectedResult = [
            'A', 1,
            json.dumps({"hello": 'ABC'}),
            json.dumps({"hello": 'ABCD'})
        ]
        self.assertEqual(set(crdt.values()), set(expectedResult))
Пример #7
0
 def test_adding_multiple(self):
     crdt = GSet()
     crdt.add('A')
     crdt.add('B')
     crdt.add('C')
     self.assertEqual(set(crdt.values()), OrderedSet(['A', 'B', 'C']))
Пример #8
0
 def test_adding_to_crdt(self):
     crdt = GSet()
     crdt.add('A')
     self.assertEqual(set(crdt.values()), OrderedSet(['A']))
Пример #9
0
def addUsers(user, dbName, middlewareName):  
    middlewareData = load_middleware(middlewareName)
    data = GSet.loadFromDict(middlewareData['Users'])  # type(middlewareData['Users']) : GSet
    data.add(user)
    middlewareData['Users'] = data.toDict()
    save_middleware(middlewareData, middlewareName)
Пример #10
0
 def test_creates_set_from_values(self):
     crdt = GSet(['A', 'B'])
     self.assertNotEqual(crdt, None)
     self.assertTrue(isinstance(crdt._payload, OrderedSet))
     self.assertEqual(crdt._payload, set(['A', 'B']))
Пример #11
0
 def test_isCmRDTSet(self):
     crdt = GSet()
     self.assertTrue(isinstance(crdt, StateCRDT))
Пример #12
0
 def test_no_overwrite_on_merge(self):
     a = OrderedSet()
     a.add('A')
     a.add('Z')
     a.add('B')
     print(a)
     crdt1 = GSet()
     crdt2 = GSet()
     crdt1.add('A')
     crdt2.add('B')
     crdt1 = crdt1.merge(crdt2)
     print(crdt1._payload)
     crdt1.add('AA')
     crdt2.add('BB')
     crdt1 = crdt1.merge(crdt2)
     print(crdt1._payload)
     self.assertEqual(crdt1._payload, ['A', 'B', 'AA', 'BB'])
     self.assertEqual(crdt2._payload, ['B', 'BB'])
Пример #13
0
 def test_constructor(self):
     crdt = GSet()
     self.assertNotEqual(crdt, None)
     self.assertNotEqual(crdt._payload, None)
     self.assertTrue(isinstance(crdt._payload, OrderedSet))
Пример #14
0
 def add_log(self, log):
     self.log = GSet()
     self.log = self.log.generate_log(log)
Пример #15
0
class Message(object):
    base_size = 1

    def __init__(self, sender, receiver, sending_time, data=None):
        self.sender = sender
        self.receiver = receiver
        self.data = data
        self.generated = sending_time
        self.time_sent = sending_time
        self.receive_times = {}
        self.latencies = {}
        self.clock = None
        self.id = None
        global temp
        self.temp = temp
        temp += 1
        self.type = None
        self.log = None
        self.dag = None
        self.clock_changed = False

    @property
    def size(self):
        return self.base_size + len(repr(self.data))

    def __repr__(self):
        return "message from " + self.sender.name + " sent to " + str(
            len(self.receiver)) + " nodes, generated at " + str(
                self.generated) + "sent at " + str(
                    self.time_sent) + " : " + str(self.temp)

    def set_latency(self, delay, nodeID, is_managed=False):
        self.latencies[nodeID] = delay
        if is_managed:
            self.receive_times[
                nodeID] = self.time_sent + self.latencies[nodeID]

    def readjust(self, timestamp):
        self.time_sent = timestamp
        receivers = self.get_receivers()
        for r in receivers:
            self.receive_times[
                r.name] = self.time_sent + self.latencies[r.name]

    def get_receivers(self):
        receivers = []
        receiver_keys = list(self.receiver.keys())
        for r in receiver_keys:
            receivers.append(self.receiver[r])
        return receivers

    # def add_clock(self,clock,bloom=None):
    #     self.clock = clock
    #     self.id = self.clock.id

    def set_clock(self, clock):
        if self.clock_changed:
            # print("no")
            exit(0)
        else:
            self.clock = clock
            self.id = self.sender.id
            self.type = clock.type
            if self.type == 'lamport' and clock.dag:
                self.type = 'dag-height'
            self.clock_changed = True

    def add_log(self, log):
        self.log = GSet()
        self.log = self.log.generate_log(log)

    def get_log(self):
        return self.log

    def set_dag(self, dag):
        self.dag = dag

    def get_dag_height(self):
        return self.dag.height

    def __lt__(self, other):
        # print(self.type)
        # print(other.type)
        if self.type == "bloom":
            return self.sort_by_bloom(other)

        elif self.type == "hybrid-bloom":
            return self.sort_by_hybrid_bloom(other)

        elif self.type == "dag-height":
            return self.sort_by_dag_height(other)

        else:
            return self.sort_by_lamport(other)

    def sort_by_lamport(self, other):
        assert isinstance(self.clock, LamportClock)
        assert isinstance(other.clock, LamportClock)
        dist = self.clock.time - other.clock.time

        # if dist < 0:
        #     # print("lamport returning true")

        # print("trying to print some id's: ", self.id,other.id)

        if dist == 0 and self.id != other.id:
            # print("id: ", self.id, other.id)
            return True if self.id < other.id else False

        # print(self.sender.name, " ---- ", other.sender.name)
        # print(self.id, " ---- ", other.id)
        # print(self.temp, " ---- ", other.temp)
        # print(dist)
        return True if dist < 0 else False

    def sort_by_bloom(self, other):
        assert isinstance(self.clock, BloomClock)
        assert isinstance(other.clock, BloomClock)
        assert self.clock is not other.clock

        # print("printing message ids: ",self.id,other.id)

        if self.clock.happened_before(other.clock)[0] != 1:
            if other.clock.happened_before(self.clock)[0] != 1:
                return False
            # print("bloom filter returns true")
            # print(self.sender.id, " before ",other.sender.id)
            else:
                # print(self.clock.happened_before(other.clock)[0])
                return True

        elif self.clock.happened_after(other.clock)[0] != 1:
            # print("bloom filter returns false")
            # print(self.clock.happened_before(other.clock)[0])
            return False

        else:
            # print("bloom filter returns order based on id")
            # print("id: ", self.id,other.id)
            return True if self.id < other.id else False

    def sort_by_hybrid_bloom(self, other):
        assert isinstance(self.clock, HybridLamportBloom)
        assert isinstance(other.clock, HybridLamportBloom)
        dist = self.clock.lamport.time - other.clock.lamport.time

        if self.clock.bloom.happened_before(other.clock.bloom)[0] != 1:
            if other.clock.bloom.happened_before(self.clock.bloom)[0] != 1:
                return False
            return True

        if self.clock.bloom.happened_after(other.clock.bloom)[0] != 1:
            return False

        if dist == 0 and self.id != other.id:
            # print("id: ", self.id, other.id)
            return True if self.id < other.id else False

        # print(self.sender.name, " ---- ", other.sender.name)
        # print(self.id, " ---- ", other.id)
        # print(self.temp, " ---- ", other.temp)
        # print(dist)
        return True if dist < 0 else False

    def sort_by_dag_height(self, other):
        assert isinstance(self.clock, LamportClock)
        assert isinstance(other.clock, LamportClock)
        dist = self.clock.time - other.clock.time

        # if dist < 0:
        #     # print("lamport returning true")

        # print("trying to print some id's: ", self.id,other.id)
        # if self.get_dag_height() > other.get_dag_height():
        #     return True

        if dist == 0 and self.id != other.id:
            if self.get_dag_height() > other.get_dag_height():
                return True
            return True if self.id < other.id else False

        # print(self.sender.name, " ---- ", other.sender.name)
        # print(self.id, " ---- ", other.id)
        # print(self.temp, " ---- ", other.temp)
        # print(dist)
        return True if dist < 0 else False
Пример #16
0
 def test_iterator_type(self):
     crdt = GSet()
     self.assertTrue(
         isinstance(crdt.values(), type(OrderedSet().__iter__())))
Пример #17
0
    def test_merge_four_sets(self):
        crdt1 = GSet()
        crdt2 = GSet()
        crdt3 = GSet()
        crdt4 = GSet()
        crdt1.add('A')
        crdt2.add('B')
        crdt3.add('C')
        crdt4.add('D')

        crdt1 = crdt1.merge(crdt2)
        crdt1 = crdt1.merge(crdt3)
        crdt1 = crdt1.merge(crdt4)

        self.assertEqual(list(crdt1.values()), ['A', 'B', 'C', 'D'])
Пример #18
0
def validate_middleware(middlewareName):
    middlewarePath = middlewareName + '.json'
    # Initialize middleware if doesn't exist
    if not os.path.exists(middlewarePath):
        with open(middlewarePath, 'w+') as mp:
            json.dump({'Users': GSet().toDict()} , mp)