本文整理匯總了Python中leveldb.WriteBatch方法的典型用法代碼示例。如果您正苦於以下問題:Python leveldb.WriteBatch方法的具體用法?Python leveldb.WriteBatch怎麽用?Python leveldb.WriteBatch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類leveldb
的用法示例。
在下文中一共展示了leveldb.WriteBatch方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_prefix
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def test_prefix(self):
"""
Test iterator prefixes
"""
batch = leveldb.WriteBatch()
batch.put('a', 'b')
batch.put('b', 'b')
batch.put('cd', 'a')
batch.put('ce', 'a')
batch.put('c', 'a')
batch.put('f', 'b')
db = self.db_class(self.db_path, create_if_missing=True)
db.write(batch)
iterator = db.iterator(prefix="c")
iterator.seekFirst()
self.assertEqual(iterator.next(), ('', 'a'))
self.assertEqual(iterator.next(), ('d', 'a'))
self.assertEqual(iterator.next(), ('e', 'a'))
self.assertRaises(StopIteration, iterator.next)
db.close()
示例2: testApproximateSizes
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def testApproximateSizes(self):
db = self.db_class(self.db_path, create_if_missing=True)
self.assertEqual([0, 0, 0],
db.approximateDiskSizes(("a", "z"), ("0", "9"), ("A", "Z")))
batch = leveldb.WriteBatch()
for i in xrange(100):
batch.put("c%d" % i, os.urandom(4096))
db.write(batch, sync=True)
db.close()
db = self.db_class(self.db_path)
sizes = db.approximateDiskSizes(("0", "9"), ("A", "Z"), ("a", "z"))
self.assertEqual(sizes[0], 0)
self.assertEqual(sizes[1], 0)
self.assertTrue(sizes[2] >= 4096 * 100)
for i in xrange(10):
db.put("3%d" % i, os.urandom(10))
db.close()
db = self.db_class(self.db_path)
sizes = db.approximateDiskSizes(("A", "Z"), ("a", "z"), ("0", "9"))
self.assertEqual(sizes[0], 0)
self.assertTrue(sizes[1] >= 4096 * 100)
self.assertTrue(sizes[2] < 4096 * 100)
self.assertTrue(sizes[2] >= 10 * 10)
db.close()
示例3: testApproximateSizes
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def testApproximateSizes(self):
db = self.db_class(self.db_path, create_if_missing=True)
self.assertEqual([0, 0, 0],
db.approximateDiskSizes(("a", "z"), ("0", "9"), ("A", "Z")))
batch = leveldb.WriteBatch()
for i in xrange(100):
batch.put("c%d" % i, os.urandom(4096))
db.write(batch, sync=True)
db.close()
db = self.db_class(self.db_path)
sizes = db.approximateDiskSizes(("0", "9"), ("A", "Z"), ("a", "z"))
self.assertEqual(sizes[0], 0)
self.assertEqual(sizes[1], 0)
self.assertTrue(sizes[2] >= 4096 * 100)
for i in xrange(10):
db.put("3%d" % i, os.urandom(10))
db.close()
db = self.db_class(self.db_path)
sizes = db.approximateDiskSizes(("A", "Z"), ("a", "z"), ("0", "9"))
self.assertEqual(sizes[0], 0)
self.assertTrue(sizes[1] >= 4096 * 100)
self.assertTrue(sizes[2] < 4096 * 100)
self.assertTrue(sizes[2] >= 10 * 10)
db.close()
示例4: _saveChunk_pre1_0
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def _saveChunk_pre1_0(self, chunk, batch=None, writeOptions=None):
"""
:param chunk: PocketLeveldbChunk
:param batch: WriteBatch
:param writeOptions: WriteOptions
:return: None
"""
cx, cz = chunk.chunkPosition
data = chunk.savedData()
key = struct.pack('<i', cx) + struct.pack('<i', cz)+dimension
if batch is None:
with self.world_db() as db:
wop = self.writeOptions if writeOptions is None else writeOptions
db.Put(wop, key + "0", data[0])
if data[1] is not None:
db.Put(wop, key + "1", data[1])
if data[2] is not None:
db.Put(wop, key + "2", data[2])
else:
batch.Put(key + "0", data[0])
if data[1] is not None:
batch.Put(key + "1", data[1])
if data[2] is not None:
batch.Put(key + "2", data[2])
示例5: saveChunk
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def saveChunk(self, chunk, batch=None, writeOptions=None):
"""
Wrapper for the methods corresponding to the world version.
:param chunk: PocketLeveldbChunk
:param batch: WriteBatch
:param writeOptions: WriteOptions
:return: None
"""
# Check the chunk version, since PE 1.0+ can contain pre 1.0+ chunks
ver = chunk.chunk_version
if ver == "\x02":
self._saveChunk_pre1_0(chunk, batch, writeOptions)
elif ord(ver) >= 3:
self._saveChunk_1plus(chunk, batch, writeOptions)
else:
raise AttributeError("Unknown version %s for chunk %s"%(ver, chunk.chunkPosition()))
示例6: deleteChunk
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def deleteChunk(self, cx, cz, batch=None):
"""
Deletes a chunk at given cx, cz. Deletes using the batch if batch is given, uses world_db() otherwise.
:param cx, cz Coordinates of the chunk
:param batch WriteBatch
:return: None
"""
self.worldFile.deleteChunk(cx, cz, batch=batch)
try:
del self._loadedChunks[(cx, cz)]
except:
pass
try:
self.allChunks.remove((cx, cz))
except:
pass
示例7: test_prefix
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def test_prefix(self):
"""
Test iterator prefixes
"""
batch = leveldb.WriteBatch()
batch.Put('a', 'b')
batch.Put('b', 'b')
batch.Put('cd', 'a')
batch.Put('ce', 'a')
batch.Put('c', 'a')
batch.Put('f', 'b')
db = self.db_class(leveldb.Options(), self.db_path, create_if_missing=True)
db.write(leveldb.WriteOptions(), batch)
iterator = db.NewIterator(prefix="c")
iterator.SeekToFirst()
self.assertEqual(iterator.next(), ('', 'a'))
self.assertEqual(iterator.next(), ('d', 'a'))
self.assertEqual(iterator.next(), ('e', 'a'))
self.assertRaises(StopIteration, iterator.next)
db.close()
示例8: testApproximateSizes
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def testApproximateSizes(self):
db = self.db_class(leveldb.Options(), self.db_path, create_if_missing=True)
self.assertEqual([0, 0, 0],
db.approximateDiskSizes(("a", "z"), ("0", "9"), ("A", "Z")))
batch = leveldb.WriteBatch()
for i in xrange(100):
batch.Put("c%d" % i, os.urandom(4096))
db.write(leveldb.WriteOptions(), batch, sync=True)
db.close()
db = self.db_class(leveldb.Options(), self.db_path)
sizes = db.approximateDiskSizes(("0", "9"), ("A", "Z"), ("a", "z"))
self.assertEqual(sizes[0], 0)
self.assertEqual(sizes[1], 0)
self.assertTrue(sizes[2] >= 4096 * 100)
for i in xrange(10):
db.Put(leveldb.WriteOptions(), "3%d" % i, os.urandom(10))
db.close()
db = self.db_class(leveldb.Options(), self.db_path)
sizes = db.approximateDiskSizes(("A", "Z"), ("a", "z"), ("0", "9"))
self.assertEqual(sizes[0], 0)
self.assertTrue(sizes[1] >= 4096 * 100)
self.assertTrue(sizes[2] < 4096 * 100)
self.assertTrue(sizes[2] >= 10 * 10)
db.close()
示例9: setBatch
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def setBatch(self, batch: Iterable[Tuple]):
b = leveldb.WriteBatch()
for key, value in batch:
key = self.to_byte_repr(key)
value = self.to_byte_repr(value)
b.Put(key, value)
self._db.Write(b, sync=False)
示例10: do_ops_in_batch
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def do_ops_in_batch(self, batch: Iterable[Tuple]):
b = leveldb.WriteBatch()
for op, key, value in batch:
key = self.to_byte_repr(key)
value = self.to_byte_repr(value)
if op == self.WRITE_OP:
b.Put(key, value)
elif op == self.REMOVE_OP:
b.Delete(key)
else:
raise ValueError('Unknown operation')
self._db.Write(b, sync=False)
示例11: commit
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def commit(self):
log.debug('committing', db=self)
batch = leveldb.WriteBatch()
for k, v in self.uncommitted.items():
if v is None:
batch.Delete(k)
else:
batch.Put(k, compress(v))
self.db.Write(batch, sync=False)
self.uncommitted.clear()
示例12: _new_batch
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def _new_batch(self):
return leveldb.WriteBatch()
示例13: WriteBatch
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def WriteBatch(self, sync=False) -> KeyValueStoreWriteBatch:
return _KeyValueStoreWriteBatchLevelDb(self._db, sync)
示例14: __init__
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def __init__(self, path):
self._path = path
self._db = leveldb.LevelDB(path)
self._batch = leveldb.WriteBatch()
self._init_db()
self.x = 0
self.lflush = clock()
示例15: flush
# 需要導入模塊: import leveldb [as 別名]
# 或者: from leveldb import WriteBatch [as 別名]
def flush(self, sync_mode=False):
self._db.Write(self._batch, sync=sync_mode)
self.x = 0
self._batch = leveldb.WriteBatch()
self.lflush = clock()