Beispiel #1
0
    def dump_lattice(self, value, typ=None, causal_dependencies={}):
        if not typ:
            if isinstance(value, set):
                return self.dump_lattice(value, SetLattice)
            elif isinstance(value, dict):
                return self.dump_lattice(value, MapLattice)
            elif isinstance(value, list):
                return self.dump_lattice(value, OrderedSetLattice)
            else:
                return self.dump_lattice(value, LWWPairLattice)

        if typ == SetLattice:
            result = set()
            for v in value:
                result.add(self.dump(v))

            result = SetLattice(result)
        elif typ == MapLattice:
            result = {}
            for key in value:
                result[key] = self.dump_lattice(value[key])

            result = MapLattice(result)
        elif typ == OrderedSetLattice:
            result = list()
            for v in value:
                result.append(self.dump(v))

            result = OrderedSetLattice(ListBasedOrderedSet(result))
        elif typ == LWWPairLattice:
            result = LWWPairLattice(generate_timestamp(0), self.dump(value))
        elif typ == SingleKeyCausalLattice:
            # We assume that we will use the default vector clock for causal
            # metadata.
            data = SetLattice({self.dump(value)})
            result = SingleKeyCausalLattice(DEFAULT_VC, data)
        elif typ == MultiKeyCausalLattice:
            # We assume that we will use the default vector clock for causal
            # metadata.
            data = SetLattice({self.dump(value)})
            result = MultiKeyCausalLattice(DEFAULT_VC,
                                           MapLattice(causal_dependencies),
                                           data)
        else:
            raise ValueError(f'Unexpected lattice type: {str(typ)}')

        return result
Beispiel #2
0
def create_function(func_create_socket, kvs, consistency=NORMAL):
    func = Function()
    func.ParseFromString(func_create_socket.recv())

    name = sutils.get_func_kvs_name(func.name)
    logging.info('Creating function %s.' % (name))

    if consistency == NORMAL:
        body = LWWPairLattice(sutils.generate_timestamp(0), func.body)
        kvs.put(name, body)
    else:
        skcl = SingleKeyCausalLattice(sutils.DEFAULT_VC,
                                      SetLattice({func.body}))
        kvs.put(name, skcl)

    funcs = utils.get_func_list(kvs, '', fullname=True)
    funcs.append(name)
    utils.put_func_list(kvs, funcs)

    func_create_socket.send(sutils.ok_resp)