예제 #1
0
class StateStoreService(BaseService):
    """
    Service to store and retrieve structured objects. Updating an object
    will modify the object's state but keep the state history in place. It is
    always possible to get value objects
    """

    # Declaration of service
    declare = BaseService.service_declare(name="StateStore", version="0.1.0", dependencies=[])

    @defer.inlineCallbacks
    def slc_init(self):
        # use spawn args to determine backend class, second config file
        # backendcls = self.spawnArgs.get('backend_class', CONF.getValue('backend_class', None))
        # if backendcls:
        #    self.backend = pu.get_class(backendcls)
        # else:
        #    self.backend = Store
        # assert issubclass(self.backend, IStore)
        #
        # self.os = ObjectStore(backend=self.backend)
        self.os = ObjectStore(backend=Store)
        yield self.os.init()
        logging.info("DatastoreService initialized")

    @defer.inlineCallbacks
    def op_put(self, content, headers, msg):
        """
        Service operation: Puts a structured object into the data store.
        Equivalent to a git-push, with an already locally commited object.
        Replies with a result with the identity of the commit value
        """
        logging.info("op_put: " + str(content))
        key = content["key"]
        val = DataObject.from_encoding(content["value"])
        parents = content["parents"] if "parents" in content else None
        commitref = yield self.os.put(key, val, parents, committer=headers["sender"])
        yield self.reply(msg, "result", commitref.identity)

    @defer.inlineCallbacks
    def op_get(self, content, headers, msg):
        """
        Service operation: Gets a structured object from the data store.
        Equivalent to a git-pull.
        """
        logging.info("op_get: " + str(content))
        key = content["key"]

        commit = None
        if "commit" in content:
            commit = content["commit"]

        val = yield self.os.get(key, commit)

        if val:
            val = val.encode()
        yield self.reply(msg, "result", val, {})
예제 #2
0
    def test_CassandraBackend(self):
        backargs = {"cass_host_list": ["amoeba.ucsd.edu:9160"]}
        os = ObjectStore(backend=CassandraStore, backargs=backargs)
        yield os.init()

        r1 = yield os.put("key1", "1")
        self.assertTrue(isinstance(r1, ValueRef))
        self.assertFalse(hasattr(r1, "value"))

        # Check that a value object actually was placed in the values store
        re1 = yield os.vs.exists_value(ValueObject("1").identity)
        self.assertTrue(re1)

        r2 = yield os.put("key2", "2")
예제 #3
0
class ObjectStoreTest(unittest.TestCase):
    """
    Testing object store
    """

    @defer.inlineCallbacks
    def setUp(self):
        self.os = ObjectStore()
        yield self.os.init()

        self.vo1 = ValueObject("1")
        self.vo2 = ValueObject("2")
        self.vo3 = ValueObject("3")
        self.vo4 = ValueObject("4")
        self.vo5 = ValueObject("5")

        self.tv1 = TreeValue((self.vo1, self.vo2))
        self.tv2 = TreeValue((self.vo3, self.vo4))
        self.tv3 = TreeValue(self.vo5)
        self.tv4 = TreeValue(self.tv1)

        # self.cv0 = CommitValue()
        # self.cv1 = CommitValue(vo5)
        # self.cv2 = CommitValue(vo11)
        # self.cv3 = CommitValue()
        # self.cv4 = CommitValue((vo12,vo13))
        # self.cv5 = CommitValue(vo14)

    def _test_ObjectStore_DataObjs(self):
        # Putting data objects into
        pass

    @defer.inlineCallbacks
    def test_ObjectStore_values(self):
        # Check put
        r1 = yield self.os.put("key1", "1")
        print "r1=", r1.__dict__
        self.assertTrue(isinstance(r1, ValueRef))
        self.assertFalse(hasattr(r1, "value"))
        self.assertEqual(self.os._num_entities(), 1)
        self.assertEqual(self.os._num_values(), 3)
        # Check that a value object actually was placed in the values store
        re1 = yield self.os.vs.exists_value(ValueObject("1").identity)
        re1 = yield self.os.vs.exists_value(self.vo1.identity)
        self.assertTrue(re1)

        r2 = yield self.os.put("key2", "2")
        print "r2=", r2.__dict__

        nume = self.os._num_entities()
        numv = self.os._num_values()
        rt1 = yield self.os.put("tree1-2", (r1, r2))
        self.assertEqual(self.os._num_entities() - nume, 1)
        self.assertEqual(self.os._num_values() - numv, 2)

        nume = self.os._num_entities()
        numv = self.os._num_values()
        r3 = yield self.os.put("key3", ("3", rt1))
        re2 = yield self.os.vs.exists_value(ValueObject("3").identity)
        self.assertTrue(re2)
        self.assertEqual(self.os._num_entities() - nume, 1)
        self.assertEqual(self.os._num_values() - numv, 3)

        r4 = yield self.os.put("key4", "4")
        r5 = yield self.os.put("key5", "5")
        rt3 = yield self.os.put("tree4-5", (r4, r5))
        rt4 = yield self.os.put("treet1-t2", (rt3, r3))

        nume = self.os._num_entities()
        numv = self.os._num_values()
        r6 = yield self.os.put("key1", "6")
        re3 = yield self.os.vs.exists_value(ValueObject("6").identity)
        self.assertTrue(re3)
        self.assertEqual(self.os._num_entities() - nume, 0)
        self.assertEqual(self.os._num_values() - numv, 3)

        nume = self.os._num_entities()
        numv = self.os._num_values()
        r7 = yield self.os.put("key1", "1")
        self.assertEqual(self.os._num_entities() - nume, 0)
        self.assertEqual(self.os._num_values() - numv, 1)

        r11 = yield self.os.put("key11", "11", r5)
        r12 = yield self.os.put("key12", "12", r11)
        r13 = yield self.os.put("key13", "13")
        r14 = yield self.os.put("key14", "14", (r12, r13))
        r15 = yield self.os.put("key15", "15", r14)

        # Check get
        fg0 = yield self.os.get("not_exist")
        self.assertEqual(fg0, None)

        fg1 = yield self.os.get("key1")
        print "fg1=", fg1.__dict__
        self.assertEqual(fg1.get_attr("value"), "1")
        self.assertEqual(fg1.get_attr("value"), self.vo1.value)
        self.assertEqual(fg1.identity, self.vo1.identity)

        fg2 = yield self.os.get("key2")
        self.assertEqual(fg2.identity, self.vo2.identity)

        fg3 = yield self.os.get("tree1-2")
        print "fg3=", fg3.__dict__
예제 #4
0
class DatastoreService(BaseService):
    """
    Service to store and retrieve structured objects. Updating an object
    will modify the object's state but keep the state history in place. It is
    always possible to get value objects
    """
    # Declaration of service
    declare = BaseService.service_declare(name='datastore', version='0.1.0', dependencies=[])

    @defer.inlineCallbacks
    def slc_init(self):
        # use spawn args to determine backend class, second config file
        backendcls = self.spawnArgs.get('backend_class', CONF.getValue('backend_class', None))
        backendargs = self.spawnArgs.get('backend_args', CONF.getValue('backend_args', {}))
        if backendcls:
            self.backend = pu.get_class(backendcls)
        else:
            self.backend = Store
        assert issubclass(self.backend, IStore)

        # Provide rest of the spawnArgs to init the store
        self.os = ObjectStore(backend=self.backend, backargs=backendargs)
        yield self.os.init()
        logging.info("DatastoreService initialized")

    @defer.inlineCallbacks
    def op_put(self, content, headers, msg):
        """
        Service operation: Puts a structured object into the data store.
        Equivalent to a git-push, with an already locally commited object.
        Replies with a result with the identity of the commit value
        """
        logging.info("op_put: "+str(content))
        key = content['key']
        val = content['value']
        parents = content['parents'] if 'parents' in content else None
        commitref = yield self.os.put(key, val, parents, committer=headers['sender'])
        yield self.reply(msg, 'result', commitref.identity)

    @defer.inlineCallbacks
    def op_get(self, content, headers, msg):
        """
        Service operation: Gets a structured object from the data store.
        Equivalent to a git-pull.
        """
        logging.info("op_get: "+str(content))
        key = content['key']
        val = yield self.os.get(key)
        yield self.reply(msg, 'result', val.encode(), {})

    @defer.inlineCallbacks
    def op_get_values(self, content, headers, msg):
        """Service operation: Gets values from the object store.
        """
        logging.info("op_get_values: "+str(content))
        keys = content['keys']
        vals = yield self.os.getmult(keys)
        resvals = []
        for val in vals:
            resvals.append(val.encode())
        yield self.reply(msg, 'result', resvals)

    @defer.inlineCallbacks
    def op_get_ancestors(self, content, headers, msg):
        """Service operation: Gets all ancestors of a value.
        """
        logging.info("op_get_ancestors: "+str(content))
        key = str(content['key'])

        resvalues = []
        cref = yield self.os.get_commitref(key)
        ancs = yield self.os.vs.get_ancestors(cref)
        yield self.reply(msg, 'result', ancs)