def test_insert_get(self, tmpdir, sh_nums): db = Database(str(tmpdir) + "/db") db.create(with_id_index=False) n = globals()["ShardedUniqueHashIndex%d" % sh_nums] db.add_index(n(db.path, "id")) l = [] for x in range(10000): l.append(db.insert(dict(x=x))["_id"]) for curr in l: assert db.get("id", curr)["_id"] == curr
def main(): db = Database("/tmp/tut2") db.create() x_ind = WithXIndex(db.path, "x") db.add_index(x_ind) for x in xrange(100): db.insert(dict(x=x)) for y in xrange(100): db.insert(dict(y=y)) print(db.get("x", 10, with_doc=True))
def main(): db = Database("/tmp/tut4") db.create() x_ind = WithXIndex(db.path, "x") db.add_index(x_ind) for x in xrange(100): db.insert(dict(x=x)) for y in xrange(100): db.insert(dict(y=y)) print(db.get("x", 10, with_doc=True)) for curr in db.get_many("x", start=15, end=25, limit=-1, with_doc=True): print(curr)
class MySharded(ShardedHashIndex): custom_header = "from codernitydb3.sharded_hash import ShardedHashIndex" def __init__(self, *args, **kwargs): kwargs["sh_nums"] = 10 kwargs["key_format"] = "I" kwargs["use_make_keys"] = True super(MySharded, self).__init__(*args, **kwargs) def make_key_value(self, data): return data["x"], None def calculate_shard(self, key): return key % self.sh_nums y = 1500 * "y" db = Database("/tmp/shard") db.create(with_id_index=False) db.add_index(CustomIdSharded(db.path, "id")) db.add_index(MySharded(db.path, "x")) # it makes non sense to use sharding with such small number of records for x in xrange(10**4): db.insert({"x": x, "y": y}) print(db.get("x", random.randint(0, 10**4))["_id"])