use bytes for config proto

This commit is contained in:
Damien Elmes 2020-04-06 09:50:21 +10:00
parent 991e6bdd4c
commit 164bd29438
4 changed files with 21 additions and 22 deletions

View file

@ -63,7 +63,7 @@ message BackendInput {
int32 get_changed_tags = 51; int32 get_changed_tags = 51;
string get_config_json = 52; string get_config_json = 52;
SetConfigJson set_config_json = 53; SetConfigJson set_config_json = 53;
string set_all_config = 54; bytes set_all_config = 54;
Empty get_all_config = 55; Empty get_all_config = 55;
} }
} }
@ -109,10 +109,10 @@ message BackendOutput {
CanonifyTagsOut canonify_tags = 49; CanonifyTagsOut canonify_tags = 49;
AllTagsOut all_tags = 50; AllTagsOut all_tags = 50;
GetChangedTagsOut get_changed_tags = 51; GetChangedTagsOut get_changed_tags = 51;
string get_config_json = 52; bytes get_config_json = 52;
Empty set_config_json = 53; Empty set_config_json = 53;
Empty set_all_config = 54; Empty set_all_config = 54;
string get_all_config = 55; bytes get_all_config = 55;
BackendError error = 2047; BackendError error = 2047;
} }
@ -466,7 +466,7 @@ message CanonifyTagsOut {
message SetConfigJson { message SetConfigJson {
string key = 1; string key = 1;
oneof op { oneof op {
string val = 2; bytes val = 2;
Empty remove = 3; Empty remove = 3;
} }
} }

View file

@ -19,7 +19,6 @@ As this is less efficient, please use the col.*_config() API in new code.
from __future__ import annotations from __future__ import annotations
import copy import copy
import json
import weakref import weakref
from typing import Any from typing import Any
@ -31,10 +30,7 @@ class ConfigManager:
self.col = col.weakref() self.col = col.weakref()
def get_immutable(self, key: str) -> Any: def get_immutable(self, key: str) -> Any:
s = self.col.backend.get_config_json(key) return self.col.backend.get_config_json(key)
if not s:
raise KeyError
return json.loads(s)
def set(self, key: str, val: Any) -> None: def set(self, key: str, val: Any) -> None:
self.col.backend.set_config_json(key, val) self.col.backend.set_config_json(key, val)

View file

@ -577,13 +577,16 @@ class RustBackend:
).get_changed_tags.tags ).get_changed_tags.tags
) )
def get_config_json(self, key: str) -> str: def get_config_json(self, key: str) -> Any:
return self._run_command(pb.BackendInput(get_config_json=key)).get_config_json 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): def set_config_json(self, key: str, val: Any):
self._run_command( self._run_command(
pb.BackendInput( 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( jstr = self._run_command(
pb.BackendInput(get_all_config=pb.Empty()) pb.BackendInput(get_all_config=pb.Empty())
).get_all_config ).get_all_config
return json.loads(jstr) return orjson.loads(jstr)
def set_all_config(self, conf: Dict[str, Any]): 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( def translate_string_in(

View file

@ -796,12 +796,12 @@ impl Backend {
}) })
} }
fn get_config_json(&self, key: &str) -> Result<String> { fn get_config_json(&self, key: &str) -> Result<Vec<u8>> {
self.with_col(|col| { self.with_col(|col| {
let val: Option<JsonValue> = col.get_config_optional(key); let val: Option<JsonValue> = col.get_config_optional(key);
match val { match val {
None => Ok("".to_string()), None => Ok(vec![]),
Some(val) => Ok(serde_json::to_string(&val)?), Some(val) => Ok(serde_json::to_vec(&val)?),
} }
}) })
} }
@ -813,7 +813,7 @@ impl Backend {
match op { match op {
pb::set_config_json::Op::Val(val) => { pb::set_config_json::Op::Val(val) => {
// ensure it's a well-formed object // 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) col.set_config(&input.key, &val)
} }
pb::set_config_json::Op::Remove(_) => col.remove_config(&input.key), 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<()> { fn set_all_config(&self, conf: &[u8]) -> Result<()> {
let val: HashMap<String, JsonValue> = serde_json::from_str(conf)?; let val: HashMap<String, JsonValue> = serde_json::from_slice(conf)?;
self.with_col(|col| { self.with_col(|col| {
col.transact(None, |col| { col.transact(None, |col| {
col.storage col.storage
@ -835,10 +835,10 @@ impl Backend {
}) })
} }
fn get_all_config(&self) -> Result<String> { fn get_all_config(&self) -> Result<Vec<u8>> {
self.with_col(|col| { self.with_col(|col| {
let conf = col.storage.get_all_config()?; 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)
}) })
} }
} }