From 164bd2943811800c50896092cb8bdc4e3a8a9301 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 6 Apr 2020 09:50:21 +1000 Subject: [PATCH] use bytes for config proto --- proto/backend.proto | 8 ++++---- pylib/anki/config.py | 6 +----- pylib/anki/rsbackend.py | 13 ++++++++----- rslib/src/backend/mod.rs | 16 ++++++++-------- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/proto/backend.proto b/proto/backend.proto index 695f8359b..280f6d6bd 100644 --- a/proto/backend.proto +++ b/proto/backend.proto @@ -63,7 +63,7 @@ message BackendInput { int32 get_changed_tags = 51; string get_config_json = 52; SetConfigJson set_config_json = 53; - string set_all_config = 54; + bytes set_all_config = 54; Empty get_all_config = 55; } } @@ -109,10 +109,10 @@ message BackendOutput { CanonifyTagsOut canonify_tags = 49; AllTagsOut all_tags = 50; GetChangedTagsOut get_changed_tags = 51; - string get_config_json = 52; + bytes get_config_json = 52; Empty set_config_json = 53; Empty set_all_config = 54; - string get_all_config = 55; + bytes get_all_config = 55; BackendError error = 2047; } @@ -466,7 +466,7 @@ message CanonifyTagsOut { message SetConfigJson { string key = 1; oneof op { - string val = 2; + bytes val = 2; Empty remove = 3; } } diff --git a/pylib/anki/config.py b/pylib/anki/config.py index 5892fee73..600d8e7e7 100644 --- a/pylib/anki/config.py +++ b/pylib/anki/config.py @@ -19,7 +19,6 @@ As this is less efficient, please use the col.*_config() API in new code. from __future__ import annotations import copy -import json import weakref from typing import Any @@ -31,10 +30,7 @@ class ConfigManager: self.col = col.weakref() def get_immutable(self, key: str) -> Any: - s = self.col.backend.get_config_json(key) - if not s: - raise KeyError - return json.loads(s) + return self.col.backend.get_config_json(key) def set(self, key: str, val: Any) -> None: self.col.backend.set_config_json(key, val) diff --git a/pylib/anki/rsbackend.py b/pylib/anki/rsbackend.py index 926299dd8..2449fbab8 100644 --- a/pylib/anki/rsbackend.py +++ b/pylib/anki/rsbackend.py @@ -577,13 +577,16 @@ class RustBackend: ).get_changed_tags.tags ) - def get_config_json(self, key: str) -> str: - return self._run_command(pb.BackendInput(get_config_json=key)).get_config_json + def get_config_json(self, key: str) -> Any: + b = self._run_command(pb.BackendInput(get_config_json=key)).get_config_json + if b == b"": + raise KeyError + return orjson.loads(b) def set_config_json(self, key: str, val: Any): self._run_command( pb.BackendInput( - set_config_json=pb.SetConfigJson(key=key, val=json.dumps(val)) + set_config_json=pb.SetConfigJson(key=key, val=orjson.dumps(val)) ) ) @@ -598,10 +601,10 @@ class RustBackend: jstr = self._run_command( pb.BackendInput(get_all_config=pb.Empty()) ).get_all_config - return json.loads(jstr) + return orjson.loads(jstr) def set_all_config(self, conf: Dict[str, Any]): - self._run_command(pb.BackendInput(set_all_config=json.dumps(conf))) + self._run_command(pb.BackendInput(set_all_config=orjson.dumps(conf))) def translate_string_in( diff --git a/rslib/src/backend/mod.rs b/rslib/src/backend/mod.rs index 887388f34..7452a624e 100644 --- a/rslib/src/backend/mod.rs +++ b/rslib/src/backend/mod.rs @@ -796,12 +796,12 @@ impl Backend { }) } - fn get_config_json(&self, key: &str) -> Result { + fn get_config_json(&self, key: &str) -> Result> { self.with_col(|col| { let val: Option = col.get_config_optional(key); match val { - None => Ok("".to_string()), - Some(val) => Ok(serde_json::to_string(&val)?), + None => Ok(vec![]), + Some(val) => Ok(serde_json::to_vec(&val)?), } }) } @@ -813,7 +813,7 @@ impl Backend { match op { pb::set_config_json::Op::Val(val) => { // ensure it's a well-formed object - let val: JsonValue = serde_json::from_str(&val)?; + let val: JsonValue = serde_json::from_slice(&val)?; col.set_config(&input.key, &val) } pb::set_config_json::Op::Remove(_) => col.remove_config(&input.key), @@ -825,8 +825,8 @@ impl Backend { }) } - fn set_all_config(&self, conf: &str) -> Result<()> { - let val: HashMap = serde_json::from_str(conf)?; + fn set_all_config(&self, conf: &[u8]) -> Result<()> { + let val: HashMap = serde_json::from_slice(conf)?; self.with_col(|col| { col.transact(None, |col| { col.storage @@ -835,10 +835,10 @@ impl Backend { }) } - fn get_all_config(&self) -> Result { + fn get_all_config(&self) -> Result> { self.with_col(|col| { let conf = col.storage.get_all_config()?; - serde_json::to_string(&conf).map_err(Into::into) + serde_json::to_vec(&conf).map_err(Into::into) }) } }