# Path: pycoinnet/util/DictStoreSimple.py
# class DictStoreSimple(collections.abc.MutableMapping):
#     def __init__(self, prefix, parse_f, stream_f=lambda f, obj: obj.stream(f), dir_path=None):
#         self.prefix = prefix
#         self.parse_f = parse_f
#         self.stream_f = stream_f
#         self.dir_path = dir_path
#         self.the_re = re.compile(r"%s[0-9a-f]{64}" % self.prefix)
# 
#     def _path_for_hash(self, key):
#         return os.path.join(self.dir_path, "%s%s" % (self.prefix, b2h_rev(key)))
# 
#     def __setitem__(self, key, v):
#         with open(self._path_for_hash(key), "wb") as f:
#             self.stream_f(f, v)
# 
#     def __getitem__(self, key):
#         try:
#             path = self._path_for_hash(key)
#             with open(path, "rb") as f:
#                 v = self.parse_f(f)
#                 return v
#         except Exception:
#             pass
#         raise KeyError
# 
#     def __delitem__(self, key):
#         try:
#             path = self._path_for_hash(key)
#             os.unlink(path)
#             return
#         except Exception:
#             pass
#         raise KeyError
# 
#     def __len__(self):
#         return len(self.keys())
# 
#     def __iter__(self):
#         paths = os.listdir(self.dir_path)
#         paths.sort()
#         for p in paths:
#             if self.the_re.match(p):
#                 yield bytes(reversed(binascii.unhexlify(p[len(self.prefix):])))
# 
#     def keys(self):
#         return self.__iter__()


import hashlib
from tempfile import TemporaryDirectory
from pycoinnet.util.DictStoreSimple import DictStoreSimple
        db[hash_f(item)] = item

def remove_items_with_hash(db, hashes):
    for h in hashes:
        if h in db:
            del db[h]

def do_test_local_db(local_db):
    assert list(local_db.keys()) == []
    add_items(local_db, [500])
    assert set(local_db.keys()) == {hash_f(500)}
    add_items(local_db, [700])
    assert set(local_db.keys()) == {hash_f(v) for v in [500, 700]}
    add_items(local_db, [900])
    assert set(local_db.keys()) == {hash_f(v) for v in [500, 700, 900]}
    remove_items_with_hash(local_db, [hash_f(600)])
    assert set(local_db.keys()) == {hash_f(v) for v in [500, 700, 900]}
    remove_items_with_hash(local_db, [hash_f(700)])
    assert set(local_db.keys()) == {hash_f(v) for v in [500, 900]}
    add_items(local_db, range(400,450))
    assert set(local_db.keys()) == {hash_f(v) for v in [500, 900] + list(range(400,450))}
    for h in local_db.keys():
        assert h in local_db
        item = local_db.get(h)
        assert hash_f(item) == h
        item = local_db[h]
        assert hash_f(item) == h

def test_localdb_1():
    with TemporaryDirectory() as the_dir:

# Path: pycoinnet/util/DictStoreSimple.py
# class DictStoreSimple(collections.abc.MutableMapping):
#     def __init__(self, prefix, parse_f, stream_f=lambda f, obj: obj.stream(f), dir_path=None):
#         self.prefix = prefix
#         self.parse_f = parse_f
#         self.stream_f = stream_f
#         self.dir_path = dir_path
#         self.the_re = re.compile(r"%s[0-9a-f]{64}" % self.prefix)
# 
#     def _path_for_hash(self, key):
#         return os.path.join(self.dir_path, "%s%s" % (self.prefix, b2h_rev(key)))
# 
#     def __setitem__(self, key, v):
#         with open(self._path_for_hash(key), "wb") as f:
#             self.stream_f(f, v)
# 
#     def __getitem__(self, key):
#         try:
#             path = self._path_for_hash(key)
#             with open(path, "rb") as f:
#                 v = self.parse_f(f)
#                 return v
#         except Exception:
#             pass
#         raise KeyError
# 
#     def __delitem__(self, key):
#         try:
#             path = self._path_for_hash(key)
#             os.unlink(path)
#             return
#         except Exception:
#             pass
#         raise KeyError
# 
#     def __len__(self):
#         return len(self.keys())
# 
#     def __iter__(self):
#         paths = os.listdir(self.dir_path)
#         paths.sort()
#         for p in paths:
#             if self.the_re.match(p):
#                 yield bytes(reversed(binascii.unhexlify(p[len(self.prefix):])))
# 
#     def keys(self):
#         return self.__iter__()


# Path: tests/util/test_DictStoreSimple.py
import hashlib
from tempfile import TemporaryDirectory
from pycoinnet.util.DictStoreSimple import DictStoreSimple

        db[hash_f(item)] = item

def remove_items_with_hash(db, hashes):
    for h in hashes:
        if h in db:
            del db[h]

def do_test_local_db(local_db):
    assert list(local_db.keys()) == []
    add_items(local_db, [500])
    assert set(local_db.keys()) == {hash_f(500)}
    add_items(local_db, [700])
    assert set(local_db.keys()) == {hash_f(v) for v in [500, 700]}
    add_items(local_db, [900])
    assert set(local_db.keys()) == {hash_f(v) for v in [500, 700, 900]}
    remove_items_with_hash(local_db, [hash_f(600)])
    assert set(local_db.keys()) == {hash_f(v) for v in [500, 700, 900]}
    remove_items_with_hash(local_db, [hash_f(700)])
    assert set(local_db.keys()) == {hash_f(v) for v in [500, 900]}
    add_items(local_db, range(400,450))
    assert set(local_db.keys()) == {hash_f(v) for v in [500, 900] + list(range(400,450))}
    for h in local_db.keys():
        assert h in local_db
        item = local_db.get(h)
        assert hash_f(item) == h
        item = local_db[h]
        assert hash_f(item) == h

def test_localdb_1():
    with TemporaryDirectory() as the_dir:

        local_db = DictStoreSimple("t_", parse_f, stream_f, the_dir)