Beispiel #1
0
 def update(self, filter, values, model=None):
     pkeys = []
     for kv in self.select_r(filter):
         rec = decodes(kv.value)
         if updateFields(rec, values, model):
             pkeys.append(kv.key)
             self.set_r(kv.key, encodes(rec))
     return pkeys
Beispiel #2
0
    def testSerializationErrors(self):
        # NOTE: Proxy storage requires an RPCStorageCtx.
        proxy_enc = encodes(Record('ref', {'path':'p', 'node':'n'}))
        unk_enc = encodes(Record('unknown', 3))
        msg_enc = encodes(Record('@', 'xyz', 35))
        s = Storage({'foo': proxy_enc, 'unk': unk_enc, 'msg': msg_enc})
        #self.assertRaises(SerializationError, s.__getitem__, 'foo')
        self.assertRaises(SerializationError, s.__getitem__, 'unk')

        self.assertEqual(s['msg'], Record('@', 'xyz', 35))

        class NoSer(object):
            pass
        self.assertRaises(SerializationError, s.__setitem__, 'nos', NoSer())
        
        ns = NameStore(s, {})
        self.assertRaises(NoSuchName, ns.getn, 'bimbo')
Beispiel #3
0
 def update(self, filter, values, model=None):
     pkeys = []
     for kv in self.select_r(filter):
         rec = decodes(kv.value)
         if updateFields(rec, values, model):
             pkeys.append(kv.key)
             self.set_r(kv.key, encodes(rec))
     return pkeys
Beispiel #4
0
    def testSerializationErrors(self):
        # NOTE: Proxy storage requires an RPCStorageCtx.
        proxy_enc = encodes(Record('ref', {'path': 'p', 'node': 'n'}))
        unk_enc = encodes(Record('unknown', 3))
        msg_enc = encodes(Record('@', 'xyz', 35))
        s = Storage({'foo': proxy_enc, 'unk': unk_enc, 'msg': msg_enc})
        #self.assertRaises(SerializationError, s.__getitem__, 'foo')
        self.assertRaises(SerializationError, s.__getitem__, 'unk')

        self.assertEqual(s['msg'], Record('@', 'xyz', 35))

        class NoSer(object):
            pass

        self.assertRaises(SerializationError, s.__setitem__, 'nos', NoSer())

        ns = NameStore(s, {})
        self.assertRaises(NoSuchName, ns.getn, 'bimbo')
Beispiel #5
0
    def test(self):
        net = MockNet()
        e = net.addNode('node')
        d = Dispatcher(e)

        hfb = d.addEndpoint('foo/bar')
        lfb = Listener(hfb)

        hf = d.addEndpoint('foo')
        lf = Listener(hf)

        h = d.addEndpoint('')
        l = Listener(h)

        bm_to_fred = {'pcol': 'json', 'message': '{"o": "fred"}'}
        e.notify('message', bm_to_fred)

        self.assertEqual(l.received, [bm_to_fred])
        self.assertEqual(lf.received, [])
        self.assertEqual(lfb.received, [])

        msg_to_ffred = {'pcol': 'serf', 'message': encodes('foo/fred')}
        e.notify('message', msg_to_ffred)

        self.assertEqual(l.received, [bm_to_fred])
        self.assertEqual(lf.received, [msg_to_ffred])
        self.assertEqual(lfb.received, [])

        msg_to_fbb = {'pcol': 'serf', 'message': encodes('foo/bar/baz')}
        e.notify('message', msg_to_fbb)

        self.assertEqual(l.received, [bm_to_fred])
        self.assertEqual(lf.received, [msg_to_ffred])
        self.assertEqual(lfb.received, [msg_to_fbb])

        msg_to_fbark = {'pcol': 'serf', 'message': encodes('foo/bark')}
        e.notify('message', msg_to_fbark)

        self.assertEqual(l.received, [bm_to_fred])
        self.assertEqual(lf.received, [msg_to_ffred, msg_to_fbark])
        self.assertEqual(lfb.received, [msg_to_fbb])
Beispiel #6
0
 def send(self, node, addr, msg, errh=None):
     msg['o'] = addr
     pcol = self._peer_protocol(node)
     if pcol == 'json':
         if self.verbose:
             print getattr(self.node, 'client_ip', ''), 'Out', msg
         enc = JSON_CODEC.encode(msg, self.json_ctx)
     elif pcol == 'local':
         enc = rmap(self.delocalize, msg)
     else:
         enc = encodes(msg, self.remote_ctx)
     self.node.send(node, enc, errh=errh)
Beispiel #7
0
 def custom(self, name, data):
     storage = self.storage()
     if name == 'ref':
         return Ref(storage, data['path'], data.get('facet'))
     if name == 'inst':
         cls = importSymbol(storage.map_class(data['CLS']))
         data['#vat'] = storage
         data.update(storage.resources)
         # Check for version change.
         data_version = data.get('$version', 0)
         cls_version = getattr(cls, '_version', 0)
         if cls_version != data_version:
             cls._upgrade(data, data_version)
         args = [data.get(key.lstrip('_')) for key in cls.serialize]
         inst = cls(*args)
         if cls_version != data_version:
             # Ensure new nested serializables get correct _save hook.
             encodes(inst, self)
         if hasattr(cls, '_save'):
             inst._save = self.save
         return inst
     raise SerializationError(name)
Beispiel #8
0
 def _join1(self, l_pk, left, join, query, func):
     l_col, r_col, type = join.leftCol, join.rightCol, join.type
     if r_col != '#':
         r_col = ':' + r_col
     spec = '%s %s' % (r_col, type)
     limit = join.limit or sys.maxint
     try:
         key = l_pk if l_col == '#' else left[l_col]
     except (KeyError, AttributeError):
         to_join = []
     else:
         filters = [query, Range(0, limit)] if query is not None else [Range(0, limit)]
         to_join = self.select_r([Key(spec, key)] + filters)
     if not to_join and join.nulls:
         to_join.append(KeyValue(0, encodes({})))
     for kv in to_join:
         right = decodes(kv.value)
         func(l_pk, left, kv.key, right)
Beispiel #9
0
 def _join1(self, l_pk, left, join, query, func):
     l_col, r_col, type = join.leftCol, join.rightCol, join.type
     if r_col != '#':
         r_col = ':' + r_col
     spec = '%s %s' % (r_col, type)
     limit = join.limit or sys.maxint
     try:
         key = l_pk if l_col == '#' else left[l_col]
     except (KeyError, AttributeError):
         to_join = []
     else:
         filters = [query, Range(0, limit)
                    ] if query is not None else [Range(0, limit)]
         to_join = self.select_r([Key(spec, key)] + filters)
     if not to_join and join.nulls:
         to_join.append(KeyValue(0, encodes({})))
     for kv in to_join:
         right = decodes(kv.value)
         func(l_pk, left, kv.key, right)
Beispiel #10
0
 def _update(self, pkey, values, vrec):
     rec = self.get(pkey)
     if updateFields(rec, values, vrec):
         self.set_r(pkey, encodes(rec))
Beispiel #11
0
 def set(self, pkey, data, notify=True):
     self.set_r(pkey, encodes(data), notify)
Beispiel #12
0
def convert(v1, v2, value):
    if v1.node_id == v2.node_id:
        return v2.localize(v1.delocalize(value))
    return decodes(encodes(value, v1.encodeRemote), v2.decodeRemote)
Beispiel #13
0
 def setn(self, name, ref_or_value):
     ref = self._asRef(ref_or_value)
     ctx = self.storage.make_context(self.storage)
     self.store[name] = encodes(ref, ctx)
Beispiel #14
0
 def set(self, pkey, data, notify=True):
     self.set_r(pkey, encodes(data), notify)
Beispiel #15
0
 def _update(self, pkey, values, vrec):
     rec = self.get(pkey)
     if updateFields(rec, values, vrec):
         self.set_r(pkey, encodes(rec))
Beispiel #16
0
 def append(self, value):
     index = self.log.append(encodes(value, self.storage_ctx))
     self.obs.add(index, value)
     return index
Beispiel #17
0
 def setKey(self, spec, message, replace=True):
     return self.setKey_r(spec, encodes(message), replace)
Beispiel #18
0
 def buildJoin(s_pk, s_rec, r_pk, r_rec):
     out = mergeRecords(s_pk, s_rec, r_pk, r_rec, merge, {})
     results.append(KeyValue(s_pk, encodes(out)))
Beispiel #19
0
 def setKey(self, spec, message, replace=True):
     return self.setKey_r(spec, encodes(message), replace)
Beispiel #20
0
 def save(self, path, svalue=Unique):
     if svalue is Unique:
         svalue = self.cache[path]
     ctx = self.make_context(self, path)
     self.store[path] = encodes(svalue, ctx)
Beispiel #21
0
 def buildJoin(s_pk, s_rec, r_pk, r_rec):
     out = mergeRecords(s_pk, s_rec, r_pk, r_rec, merge, {})
     results.append(KeyValue(s_pk, encodes(out)))
Beispiel #22
0
 def append(self, value):
     index = self.log.append(encodes(value, self.storage_ctx))
     self.obs.add(index, value)
     return index