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;
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;
}
}

View file

@ -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)

View file

@ -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(

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| {
let val: Option<JsonValue> = 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<String, JsonValue> = serde_json::from_str(conf)?;
fn set_all_config(&self, conf: &[u8]) -> Result<()> {
let val: HashMap<String, JsonValue> = 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<String> {
fn get_all_config(&self) -> Result<Vec<u8>> {
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)
})
}
}