def insert(db, key, rev, value): info = DocumentInfo(key) info.revSequence = rev # cas, exp, flags info.revMeta = str(struct.pack(REV_META_PACK, 1, 2, 3)) info.deleted = False return db.save(info, value)
def insert_multi(db, keys, values): """Inserts multiple keys / values.""" ids = [] for k in keys: info = DocumentInfo(k) info.revSequence = 1 # cas, exp, flags info.revMeta = str(struct.pack(REV_META_PACK, 1, 2, 3)) info.deleted = False ids.append(info) return db.saveMultiple(ids, values)
def testMetadata(self): info = DocumentInfo("meta") info.revSequence = 23 info.revMeta = "fancy metadata here" info.contentType = DocumentInfo.INVALID_JSON self.store[info] = "the regular non-meta data" gotInfo = self.store.getInfo("meta") self.assertEquals(gotInfo.id, "meta") self.assertEquals(gotInfo.revSequence, info.revSequence) self.assertEquals(gotInfo.revMeta, info.revMeta) self.assertEquals(gotInfo.contentType, info.contentType) self.assertFalse(gotInfo.compressed)
def testBinaryMeta(self): # Make sure binary data, as produced by Python's struct module, works in revMeta. packed = struct.pack(">QII", 0, 1, 2) d = DocumentInfo("bin") d.revMeta = packed self.store[d] = "value" doc_info = self.store.getInfo("bin") self.assertEqual(doc_info.revMeta, packed) i1, i2, i3 = struct.unpack(">QII", doc_info.revMeta) self.assertEqual(i1, 0) self.assertEqual(i2, 1) self.assertEqual(i3, 2)
def testMetadataSave(self): info = DocumentInfo("meta") info.revSequence = 23 info.revMeta = "fancy metadata here" info.contentType = DocumentInfo.INVALID_JSON self.store[info] = "the regular non-meta data" self.store.commit() self.store.close() self.store = CouchStore("/tmp/test.couch", 'r') gotInfo = self.store.getInfo("meta") self.assertEquals(gotInfo.id, "meta") self.assertEquals(gotInfo.revSequence, info.revSequence) self.assertEquals(gotInfo.revMeta, info.revMeta) self.assertEquals(gotInfo.contentType, info.contentType) self.assertFalse(gotInfo.compressed)
def testMultipleMeta(self): k = [] v = [] for i in range(1000): d = DocumentInfo(str(i)) d.revMeta = "hello-%s" % i k.append(d) v.append("world-%s" % i) self.store.saveMultiple(k, v) self.store.commit() self.store.close() self.store = CouchStore("/tmp/test.couch", 'r') for doc_info in self.store.changesSince(0): i = int(doc_info.id) self.assertEqual(doc_info.revMeta, "hello-%s" % i) doc_contents = doc_info.getContents() self.assertEqual(doc_contents, "world-%s" % i)
def testMultipleMetaStruct(self): k = [] v = [] for i in range(1000): d = DocumentInfo(str(i)) d.revMeta = struct.pack(">QII", i * 3, i * 2, i) k.append(d) v.append("world-%s" % i) self.store.saveMultiple(k, v) self.store.commit() self.store.close() self.store = CouchStore("/tmp/test.couch", 'r') for doc_info in self.store.changesSince(0): i = int(doc_info.id) i3, i2, i1 = struct.unpack(">QII", doc_info.revMeta) self.assertEqual(i3, i * 3) self.assertEqual(i2, i * 2) self.assertEqual(i1, i * 1) doc_contents = doc_info.getContents() self.assertEqual(doc_contents, "world-%s" % doc_info.id)