Exemplo n.º 1
0
    def prefixed_iter(
        self,
        prefixes: Optional[Union[str, Iterable[str]]] = None,
        starting_by: Optional[str] = None,
        include_key=True,
        include_value=True,
    ) -> Iterable:
        prefixes = (normalize_strings(self.key_encoder, prefixes)
                    if prefixes is not None else ())
        starting_by = b"".join(
            serializer.normalize_strings(self.key_encoder, starting_by
                                         ) if starting_by is not None else ())
        subdb = self.prefixed_db(prefixes=prefixes)
        for x in subdb.db.iterator(prefix=starting_by,
                                   include_key=include_key,
                                   include_value=include_value):
            if isinstance(x, bytes):
                if include_key:
                    yield self.key_decoder(x)
                else:
                    yield self.value_decoder(x)

            else:
                try:
                    key, value = x
                    yield self.key_decoder(key), self.value_decoder(value)
                except TypeError:
                    yield None
Exemplo n.º 2
0
    def __delitem__(self, key: Union[str, Iterable[str]]) -> Response:
        *prefixes, key = normalize_strings(lambda x: x, key)

        res = requests.delete(
            url=self.server_address + "/delitem",
            params={"dbpath": self.db_path, "key": key, "prefixes": prefixes},
        )
        return res
Exemplo n.º 3
0
def _parse_and_get_prefixed_db() -> plyvel.DB:
    dbpath = request.args.get("dbpath")
    prefixes = request.args.getlist("prefixes")
    prefixes = (
        serializer.normalize_strings(DecodeType.STR.pure_encode_fun, prefixes)
        if prefixes is not None
        else ()
    )
    return get_prefixed_db(get_db(dbpath), prefixes)
Exemplo n.º 4
0
 def __getitem__(
         self, key: Union[str, Iterable[Union[str,
                                              Ellipsis.__class__]]]) -> Any:
     *prefixes, key = normalize_strings(self.key_encoder, key)
     ldb = self.prefixed_db(prefixes)
     if key is Ellipsis:
         return ldb
     out = ldb.db.get(key)
     return self.value_decoder(out) if out else None
Exemplo n.º 5
0
    def __setitem__(self, key: Union[str, Iterable[str]], value: Any) -> None:
        *prefixes, key = normalize_strings(self.key_encoder, key)

        if key is Ellipsis:
            raise TypeError(
                f"str prefix or key expected, got {type(key).__name__}")

        ldb = self.prefixed_db(prefixes)
        ldb.db.put(key, self.value_encoder(value))
Exemplo n.º 6
0
    def __setitem__(self, key: Union[str, Iterable[str]], value: Any) -> Response:
        *prefixes, key = normalize_strings(lambda x: x, key)

        if key is Ellipsis:
            raise TypeError(f"str prefix or key expected, got {type(key).__name__}")

        res = requests.post(
            url=self.server_address + "/setitem",
            data=encode(value),
            headers={"Content-Type": "application/octet-stream"},
            params={"dbpath": self.db_path, "key": key, "prefixes": prefixes},
        )
        return res
Exemplo n.º 7
0
    def __getitem__(
        self, key: Union[str, Iterable[Union[str, Ellipsis.__class__]]]
    ) -> Any:
        *prefixes, key = normalize_strings(lambda x: x, key)

        if key is Ellipsis:
            raise NotImplementedError

        res = requests.get(
            url=self.server_address + "/getitem",
            params={"dbpath": self.db_path, "key": key, "prefixes": prefixes},
        )

        return self.value_decoder(res.content) if res.content else None
Exemplo n.º 8
0
def dblen() -> str:
    db = _parse_and_get_prefixed_db()

    starting_by = request.args.get("starting_by")
    starting_by = b"".join(
        serializer.normalize_strings(DecodeType.STR.pure_encode_fun, starting_by)
        if starting_by is not None
        else ()
    )
    out = serializer.encode(
        sum(
            1
            for _ in db.iterator(
                include_key=True, include_value=False, prefix=starting_by
            )
        )
    )
    return Response(out, content_type="application/octet-stream")
Exemplo n.º 9
0
def iterator() -> Iterable[Union[bytes, Tuple[bytes, bytes]]]:
    db = _parse_and_get_prefixed_db()

    starting_by = request.args.get("starting_by")
    starting_by = b"".join(
        serializer.normalize_strings(DecodeType.STR.pure_encode_fun, starting_by)
        if starting_by is not None
        else ()
    )

    include_key = request.args.get("include_key") == "True"
    include_value = request.args.get("include_value") == "True"

    out = pickle.dumps(
        list(
            db.iterator(
                prefix=starting_by, include_key=include_key, include_value=include_value
            )
        )
    )
    return Response(out, content_type="application/octet-stream")
Exemplo n.º 10
0
 def __delitem__(self, key: Union[str, Iterable[str]]) -> None:
     *prefixes, key = normalize_strings(self.key_encoder, key)
     ldb = self.prefixed_db(prefixes)
     ldb.db.delete(key)