use futures::{Stream, StreamExt};
use tonic::metadata::MetadataValue;
use tonic::transport::{Channel, ClientTlsConfig, Certificate};
use tonic::Request;
use crate::pb;
use crate::pb::scriva_client::ScrivaClient;
pub type Error = Box<dyn std::error::Error + Send + Sync>;
#[derive(Debug)]
pub enum ScrivaDbError {
NotFound(tonic::Status),
AlreadyExists(tonic::Status),
Rpc(tonic::Status),
}
impl ScrivaDbError {
pub fn status(&self) -> &tonic::Status {
match self {
ScrivaDbError::NotFound(s)
| ScrivaDbError::AlreadyExists(s)
| ScrivaDbError::Rpc(s) => s,
}
}
}
impl From<tonic::Status> for ScrivaDbError {
fn from(status: tonic::Status) -> Self {
match status.code() {
tonic::Code::NotFound => ScrivaDbError::NotFound(status),
tonic::Code::AlreadyExists => ScrivaDbError::AlreadyExists(status),
_ => ScrivaDbError::Rpc(status),
}
}
}
impl std::fmt::Display for ScrivaDbError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ScrivaDbError::NotFound(s) => write!(f, "not found: {}", s.message()),
ScrivaDbError::AlreadyExists(s) => write!(f, "already exists: {}", s.message()),
ScrivaDbError::Rpc(s) => write!(f, "{}", s),
}
}
}
impl std::error::Error for ScrivaDbError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.status())
}
}
#[derive(Debug, Clone)]
pub struct Record {
pub id: u64,
pub key: String,
pub rev: u64,
pub data: serde_json::Value,
pub date_added: Option<String>,
pub date_modified: Option<String>,
}
#[derive(Debug, Clone)]
pub struct OrderBy {
pub field: String,
pub desc: bool,
}
impl OrderBy {
pub fn asc(field: impl Into<String>) -> Self {
OrderBy { field: field.into(), desc: false }
}
pub fn desc(field: impl Into<String>) -> Self {
OrderBy { field: field.into(), desc: true }
}
}
#[derive(Debug, Clone, Default)]
pub struct FindOptions {
pub filter: Option<FilterInput>,
pub limit: u32,
pub offset: u32,
pub order_by: String,
pub descending: bool,
pub order_by_fields: Vec<OrderBy>,
pub fields: Vec<String>,
pub page_token: String,
}
#[derive(Debug, Clone)]
pub struct UpdateResult {
pub id: u64,
pub key: String,
pub rev: u64,
pub date_modified: Option<String>,
}
#[derive(Debug, Clone)]
pub struct CasResult {
pub swapped: bool,
pub record: Option<Record>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AggregateOp {
Count,
Sum,
Avg,
Min,
Max,
}
#[derive(Debug, Clone, Default)]
pub struct AggregateOptions {
pub aggregations: Vec<AggregateOp>,
pub field: String,
pub group_by: String,
pub filter: Option<FilterInput>,
}
#[derive(Debug, Clone)]
pub struct AggregateGroup {
pub group: serde_json::Value,
pub count: u64,
pub numeric: bool,
pub sum: f64,
pub avg: f64,
pub min: f64,
pub max: f64,
}
#[derive(Debug, Clone)]
pub struct CollectionStats {
pub collection: String,
pub record_count: u64,
pub segment_count: u64,
pub dirty_entries: u64,
pub size_bytes: u64,
}
#[derive(Debug, Clone)]
pub struct WatchEvent {
pub op: WatchOp,
pub collection: String,
pub record: Record,
pub ts: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WatchOp {
Inserted,
Updated,
Deleted,
Overflow,
Unspecified,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FilterOp {
Eq,
Neq,
Gt,
Gte,
Lt,
Lte,
Contains,
Regex,
}
#[derive(Debug, Clone)]
pub enum FilterInput {
Field {
field: String,
op: FilterOp,
value: String,
},
And(Vec<FilterInput>),
Or(Vec<FilterInput>),
}
impl FilterInput {
pub fn field(field: impl Into<String>, op: FilterOp, value: impl ToString) -> Self {
FilterInput::Field {
field: field.into(),
op,
value: value.to_string(),
}
}
pub fn and(filters: Vec<FilterInput>) -> Self {
FilterInput::And(filters)
}
pub fn or(filters: Vec<FilterInput>) -> Self {
FilterInput::Or(filters)
}
}
fn filter_op_to_proto(op: FilterOp) -> i32 {
match op {
FilterOp::Eq => pb::FilterOp::Eq as i32,
FilterOp::Neq => pb::FilterOp::Neq as i32,
FilterOp::Gt => pb::FilterOp::Gt as i32,
FilterOp::Gte => pb::FilterOp::Gte as i32,
FilterOp::Lt => pb::FilterOp::Lt as i32,
FilterOp::Lte => pb::FilterOp::Lte as i32,
FilterOp::Contains => pb::FilterOp::Contains as i32,
FilterOp::Regex => pb::FilterOp::Regex as i32,
}
}
fn agg_op_to_proto(op: AggregateOp) -> i32 {
match op {
AggregateOp::Count => pb::AggregateOp::AggCount as i32,
AggregateOp::Sum => pb::AggregateOp::AggSum as i32,
AggregateOp::Avg => pb::AggregateOp::AggAvg as i32,
AggregateOp::Min => pb::AggregateOp::AggMin as i32,
AggregateOp::Max => pb::AggregateOp::AggMax as i32,
}
}
fn order_by_to_proto(o: &OrderBy) -> pb::OrderBy {
pb::OrderBy { field: o.field.clone(), desc: o.desc }
}
fn filter_to_proto(f: &FilterInput) -> pb::Filter {
let kind = match f {
FilterInput::Field { field, op, value } => {
pb::filter::Kind::Field(pb::FieldFilter {
field: field.clone(),
op: filter_op_to_proto(*op),
value: value.clone(),
})
}
FilterInput::And(children) => {
pb::filter::Kind::And(pb::AndFilter {
filters: children.iter().map(filter_to_proto).collect(),
})
}
FilterInput::Or(children) => {
pb::filter::Kind::Or(pb::OrFilter {
filters: children.iter().map(filter_to_proto).collect(),
})
}
};
pb::Filter { kind: Some(kind) }
}
fn json_to_value(val: serde_json::Value) -> prost_types::Value {
let kind = match val {
serde_json::Value::Null => prost_types::value::Kind::NullValue(0),
serde_json::Value::Bool(b) => prost_types::value::Kind::BoolValue(b),
serde_json::Value::Number(n) => {
prost_types::value::Kind::NumberValue(n.as_f64().unwrap_or(0.0))
}
serde_json::Value::String(s) => prost_types::value::Kind::StringValue(s),
serde_json::Value::Array(arr) => {
prost_types::value::Kind::ListValue(prost_types::ListValue {
values: arr.into_iter().map(json_to_value).collect(),
})
}
serde_json::Value::Object(map) => {
let fields = map
.into_iter()
.map(|(k, v)| (k, json_to_value(v)))
.collect();
prost_types::value::Kind::StructValue(prost_types::Struct { fields })
}
};
prost_types::Value { kind: Some(kind) }
}
fn json_to_struct(val: serde_json::Value) -> prost_types::Struct {
if let serde_json::Value::Object(map) = val {
prost_types::Struct {
fields: map
.into_iter()
.map(|(k, v)| (k, json_to_value(v)))
.collect(),
}
} else {
prost_types::Struct::default()
}
}
fn value_to_json(v: prost_types::Value) -> serde_json::Value {
match v.kind {
None => serde_json::Value::Null,
Some(prost_types::value::Kind::NullValue(_)) => serde_json::Value::Null,
Some(prost_types::value::Kind::BoolValue(b)) => serde_json::Value::Bool(b),
Some(prost_types::value::Kind::NumberValue(n)) => serde_json::Number::from_f64(n)
.map(serde_json::Value::Number)
.unwrap_or(serde_json::Value::Null),
Some(prost_types::value::Kind::StringValue(s)) => serde_json::Value::String(s),
Some(prost_types::value::Kind::ListValue(l)) => {
serde_json::Value::Array(l.values.into_iter().map(value_to_json).collect())
}
Some(prost_types::value::Kind::StructValue(s)) => struct_to_json(s),
}
}
fn struct_to_json(s: prost_types::Struct) -> serde_json::Value {
serde_json::Value::Object(
s.fields
.into_iter()
.map(|(k, v)| (k, value_to_json(v)))
.collect(),
)
}
fn proto_record_to_record(r: pb::Record) -> Record {
Record {
id: r.id,
key: r.key,
rev: r.rev,
data: r.data.map(struct_to_json).unwrap_or(serde_json::Value::Object(Default::default())),
date_added: r.date_added.map(timestamp_to_rfc3339),
date_modified: r.date_modified.map(timestamp_to_rfc3339),
}
}
fn timestamp_to_rfc3339(ts: prost_types::Timestamp) -> String {
let secs = ts.seconds;
let nanos = ts.nanos as u64;
let (year, month, day, hour, min, sec) = epoch_secs_to_parts(secs);
if nanos == 0 {
format!("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z", year, month, day, hour, min, sec)
} else {
let ms = nanos / 1_000_000;
format!("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}Z", year, month, day, hour, min, sec, ms)
}
}
fn epoch_secs_to_parts(secs: i64) -> (i32, u32, u32, u32, u32, u32) {
let sec = secs.rem_euclid(60) as u32;
let mins = secs.div_euclid(60);
let min = mins.rem_euclid(60) as u32;
let hours = mins.div_euclid(60);
let hour = hours.rem_euclid(24) as u32;
let mut days = hours.div_euclid(24);
let mut year = 1970i32;
loop {
let diy = if is_leap_year(year) { 366 } else { 365 };
if days < diy {
break;
}
days -= diy;
year += 1;
}
let month_days: [i64; 12] = [
31, if is_leap_year(year) { 29 } else { 28 }, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31,
];
let mut month = 1u32;
for &md in &month_days {
if days < md {
break;
}
days -= md;
month += 1;
}
(year, month, days as u32 + 1, hour, min, sec)
}
fn is_leap_year(y: i32) -> bool {
(y % 4 == 0 && y % 100 != 0) || y % 400 == 0
}
fn watch_op_from_proto(op: i32) -> WatchOp {
match pb::WatchOp::try_from(op) {
Ok(pb::WatchOp::Inserted) => WatchOp::Inserted,
Ok(pb::WatchOp::Updated) => WatchOp::Updated,
Ok(pb::WatchOp::Deleted) => WatchOp::Deleted,
Ok(pb::WatchOp::Overflow) => WatchOp::Overflow,
_ => WatchOp::Unspecified,
}
}
fn opt_string(s: String) -> Option<String> {
if s.is_empty() {
None
} else {
Some(s)
}
}
pub struct ScrivaDB {
client: ScrivaClient<Channel>,
api_key: String,
}
impl ScrivaDB {
pub async fn connect(host: &str, port: u16, api_key: &str) -> Result<Self, Error> {
let endpoint = format!("http://{}:{}", host, port);
let channel = Channel::from_shared(endpoint)?.connect().await?;
Ok(Self {
client: ScrivaClient::new(channel),
api_key: api_key.to_owned(),
})
}
pub async fn connect_tls(
host: &str,
port: u16,
api_key: &str,
ca_cert_pem: &[u8],
) -> Result<Self, Error> {
let ca = Certificate::from_pem(ca_cert_pem);
let tls = ClientTlsConfig::new()
.ca_certificate(ca)
.domain_name(host);
let endpoint = format!("https://{}:{}", host, port);
let channel = Channel::from_shared(endpoint)?
.tls_config(tls)?
.connect()
.await?;
Ok(Self {
client: ScrivaClient::new(channel),
api_key: api_key.to_owned(),
})
}
fn req<T>(&self, body: T) -> Request<T> {
let mut req = Request::new(body);
let val: MetadataValue<_> = self.api_key.parse().expect("api_key is a valid header value");
req.metadata_mut().insert("x-api-key", val);
req
}
fn build_find_request(&self, collection: &str, opts: FindOptions) -> pb::FindRequest {
#[allow(deprecated)]
pb::FindRequest {
collection: collection.to_owned(),
filter: opts.filter.as_ref().map(filter_to_proto),
limit: opts.limit,
offset: opts.offset,
order_by: opts.order_by,
descending: opts.descending,
fields: opts.fields,
page_token: opts.page_token,
order_by_fields: opts.order_by_fields.iter().map(order_by_to_proto).collect(),
}
}
pub async fn create_collection(&mut self, name: &str) -> Result<String, ScrivaDbError> {
self.create_collection_with_ttl(name, 0).await
}
pub async fn create_collection_with_ttl(
&mut self,
name: &str,
default_ttl_seconds: i64,
) -> Result<String, ScrivaDbError> {
let resp = self
.client
.create_collection(self.req(pb::CreateCollectionRequest {
name: name.to_owned(),
default_ttl_seconds,
}))
.await?
.into_inner();
Ok(resp.name)
}
pub async fn drop_collection(&mut self, name: &str) -> Result<bool, ScrivaDbError> {
let resp = self
.client
.drop_collection(self.req(pb::DropCollectionRequest {
name: name.to_owned(),
}))
.await?
.into_inner();
Ok(resp.ok)
}
pub async fn list_collections(&mut self) -> Result<Vec<String>, ScrivaDbError> {
let resp = self
.client
.list_collections(self.req(pb::ListCollectionsRequest {}))
.await?
.into_inner();
Ok(resp.names)
}
pub async fn insert(
&mut self,
collection: &str,
data: serde_json::Value,
) -> Result<u64, ScrivaDbError> {
self.insert_inner(collection, data, 0, "").await
}
pub async fn insert_with_ttl(
&mut self,
collection: &str,
data: serde_json::Value,
ttl_seconds: i64,
) -> Result<u64, ScrivaDbError> {
self.insert_inner(collection, data, ttl_seconds, "").await
}
pub async fn insert_with_key(
&mut self,
collection: &str,
data: serde_json::Value,
key: &str,
) -> Result<u64, ScrivaDbError> {
self.insert_inner(collection, data, 0, key).await
}
async fn insert_inner(
&mut self,
collection: &str,
data: serde_json::Value,
ttl_seconds: i64,
key: &str,
) -> Result<u64, ScrivaDbError> {
let resp = self
.client
.insert(self.req(pb::InsertRequest {
collection: collection.to_owned(),
data: Some(json_to_struct(data)),
ttl_seconds,
key: key.to_owned(),
}))
.await?
.into_inner();
Ok(resp.id)
}
pub async fn insert_many(
&mut self,
collection: &str,
records: Vec<serde_json::Value>,
) -> Result<Vec<u64>, ScrivaDbError> {
self.insert_many_with_ttl(collection, records, 0).await
}
pub async fn insert_many_with_ttl(
&mut self,
collection: &str,
records: Vec<serde_json::Value>,
ttl_seconds: i64,
) -> Result<Vec<u64>, ScrivaDbError> {
let resp = self
.client
.insert_many(self.req(pb::InsertManyRequest {
collection: collection.to_owned(),
records: records.into_iter().map(json_to_struct).collect(),
ttl_seconds,
}))
.await?
.into_inner();
Ok(resp.ids)
}
pub async fn find_by_id(
&mut self,
collection: &str,
id: u64,
) -> Result<Record, ScrivaDbError> {
self.find_by_id_with_fields(collection, id, &[]).await
}
pub async fn find_by_id_with_fields(
&mut self,
collection: &str,
id: u64,
fields: &[&str],
) -> Result<Record, ScrivaDbError> {
let resp = self
.client
.find_by_id(self.req(pb::FindByIdRequest {
collection: collection.to_owned(),
id,
fields: fields.iter().map(|s| s.to_string()).collect(),
}))
.await?
.into_inner();
Ok(proto_record_to_record(resp.record.unwrap_or_default()))
}
pub async fn find(
&mut self,
collection: &str,
opts: FindOptions,
) -> Result<Vec<Record>, ScrivaDbError> {
let (records, _) = self.find_page(collection, opts).await?;
Ok(records)
}
pub async fn find_page(
&mut self,
collection: &str,
opts: FindOptions,
) -> Result<(Vec<Record>, String), ScrivaDbError> {
let request = self.req(self.build_find_request(collection, opts));
let mut streaming = self.client.find(request).await?.into_inner();
let mut records = Vec::new();
let mut next_token = String::new();
while let Some(resp) = streaming.message().await? {
records.push(proto_record_to_record(resp.record.unwrap_or_default()));
if !resp.page_token.is_empty() {
next_token = resp.page_token;
}
}
Ok((records, next_token))
}
pub async fn find_stream(
&mut self,
collection: &str,
opts: FindOptions,
) -> Result<impl Stream<Item = Result<Record, ScrivaDbError>>, ScrivaDbError> {
let request = self.req(self.build_find_request(collection, opts));
let streaming = self.client.find(request).await?.into_inner();
Ok(streaming.map(|result| {
result
.map(|resp| proto_record_to_record(resp.record.unwrap_or_default()))
.map_err(ScrivaDbError::from)
}))
}
pub async fn update(
&mut self,
collection: &str,
id: u64,
data: serde_json::Value,
) -> Result<u64, ScrivaDbError> {
self.update_with_ttl(collection, id, data, 0).await
}
pub async fn update_with_ttl(
&mut self,
collection: &str,
id: u64,
data: serde_json::Value,
ttl_seconds: i64,
) -> Result<u64, ScrivaDbError> {
let resp = self
.client
.update(self.req(pb::UpdateRequest {
collection: collection.to_owned(),
id,
data: Some(json_to_struct(data)),
ttl_seconds,
}))
.await?
.into_inner();
Ok(resp.id)
}
pub async fn delete(&mut self, collection: &str, id: u64) -> Result<bool, ScrivaDbError> {
let resp = self
.client
.delete(self.req(pb::DeleteRequest {
collection: collection.to_owned(),
id,
}))
.await?
.into_inner();
Ok(resp.ok)
}
pub async fn upsert(
&mut self,
collection: &str,
key: &str,
data: serde_json::Value,
) -> Result<Record, ScrivaDbError> {
let resp = self
.client
.upsert(self.req(pb::UpsertRequest {
collection: collection.to_owned(),
key: key.to_owned(),
data: Some(json_to_struct(data)),
}))
.await?
.into_inner();
Ok(proto_record_to_record(resp.record.unwrap_or_default()))
}
pub async fn find_by_key(
&mut self,
collection: &str,
key: &str,
) -> Result<Record, ScrivaDbError> {
self.find_by_key_with_fields(collection, key, &[]).await
}
pub async fn find_by_key_with_fields(
&mut self,
collection: &str,
key: &str,
fields: &[&str],
) -> Result<Record, ScrivaDbError> {
let resp = self
.client
.find_by_key(self.req(pb::FindByKeyRequest {
collection: collection.to_owned(),
key: key.to_owned(),
fields: fields.iter().map(|s| s.to_string()).collect(),
}))
.await?
.into_inner();
Ok(proto_record_to_record(resp.record.unwrap_or_default()))
}
pub async fn update_by_key(
&mut self,
collection: &str,
key: &str,
data: serde_json::Value,
) -> Result<UpdateResult, ScrivaDbError> {
let resp = self
.client
.update_by_key(self.req(pb::UpdateByKeyRequest {
collection: collection.to_owned(),
key: key.to_owned(),
data: Some(json_to_struct(data)),
}))
.await?
.into_inner();
Ok(UpdateResult {
id: resp.id,
key: resp.key,
rev: resp.rev,
date_modified: opt_string(resp.date_modified),
})
}
pub async fn delete_by_key(
&mut self,
collection: &str,
key: &str,
) -> Result<bool, ScrivaDbError> {
let resp = self
.client
.delete_by_key(self.req(pb::DeleteByKeyRequest {
collection: collection.to_owned(),
key: key.to_owned(),
}))
.await?
.into_inner();
Ok(resp.ok)
}
pub async fn update_if_rev(
&mut self,
collection: &str,
key: &str,
expected_rev: u64,
data: serde_json::Value,
) -> Result<CasResult, ScrivaDbError> {
let resp = self
.client
.update_if_rev(self.req(pb::UpdateIfRevRequest {
collection: collection.to_owned(),
key: key.to_owned(),
expected_rev,
data: Some(json_to_struct(data)),
}))
.await?
.into_inner();
let record = if resp.swapped {
resp.record.map(proto_record_to_record)
} else {
None
};
Ok(CasResult {
swapped: resp.swapped,
record,
})
}
pub async fn ensure_index(
&mut self,
collection: &str,
field: &str,
) -> Result<(), ScrivaDbError> {
self.client
.ensure_index(self.req(pb::EnsureIndexRequest {
collection: collection.to_owned(),
field: field.to_owned(),
}))
.await?;
Ok(())
}
pub async fn drop_index(
&mut self,
collection: &str,
field: &str,
) -> Result<bool, ScrivaDbError> {
let resp = self
.client
.drop_index(self.req(pb::DropIndexRequest {
collection: collection.to_owned(),
field: field.to_owned(),
}))
.await?
.into_inner();
Ok(resp.ok)
}
pub async fn list_indexes(&mut self, collection: &str) -> Result<Vec<String>, ScrivaDbError> {
let resp = self
.client
.list_indexes(self.req(pb::ListIndexesRequest {
collection: collection.to_owned(),
}))
.await?
.into_inner();
Ok(resp.fields)
}
pub async fn begin_tx(&mut self, collection: &str) -> Result<String, ScrivaDbError> {
let resp = self
.client
.begin_tx(self.req(pb::BeginTxRequest {
collection: collection.to_owned(),
}))
.await?
.into_inner();
Ok(resp.tx_id)
}
pub async fn commit_tx(&mut self, tx_id: &str) -> Result<bool, ScrivaDbError> {
let resp = self
.client
.commit_tx(self.req(pb::CommitTxRequest {
tx_id: tx_id.to_owned(),
}))
.await?
.into_inner();
Ok(resp.ok)
}
pub async fn rollback_tx(&mut self, tx_id: &str) -> Result<bool, ScrivaDbError> {
let resp = self
.client
.rollback_tx(self.req(pb::RollbackTxRequest {
tx_id: tx_id.to_owned(),
}))
.await?
.into_inner();
Ok(resp.ok)
}
pub async fn watch(
&mut self,
collection: &str,
filter: Option<FilterInput>,
) -> Result<impl Stream<Item = Result<WatchEvent, ScrivaDbError>>, ScrivaDbError> {
let streaming = self
.client
.watch(self.req(pb::WatchRequest {
collection: collection.to_owned(),
filter: filter.as_ref().map(filter_to_proto),
}))
.await?
.into_inner();
Ok(streaming.map(|result| {
result
.map(|event| WatchEvent {
op: watch_op_from_proto(event.op),
collection: event.collection,
record: proto_record_to_record(event.record.unwrap_or_default()),
ts: event.ts.map(timestamp_to_rfc3339),
})
.map_err(ScrivaDbError::from)
}))
}
pub async fn aggregate(
&mut self,
collection: &str,
opts: AggregateOptions,
) -> Result<Vec<AggregateGroup>, ScrivaDbError> {
let request = self.req(pb::AggregateRequest {
collection: collection.to_owned(),
filter: opts.filter.as_ref().map(filter_to_proto),
group_by: opts.group_by,
field: opts.field,
aggregations: opts.aggregations.iter().map(|o| agg_op_to_proto(*o)).collect(),
});
let mut streaming = self.client.aggregate(request).await?.into_inner();
let mut out = Vec::new();
while let Some(resp) = streaming.message().await? {
out.push(AggregateGroup {
group: resp.group_value.map(value_to_json).unwrap_or(serde_json::Value::Null),
count: resp.count,
numeric: resp.numeric,
sum: resp.sum,
avg: resp.avg,
min: resp.min,
max: resp.max,
});
}
Ok(out)
}
pub async fn count(
&mut self,
collection: &str,
filter: Option<FilterInput>,
) -> Result<u64, ScrivaDbError> {
let groups = self
.aggregate(collection, AggregateOptions { filter, ..Default::default() })
.await?;
Ok(groups.first().map(|g| g.count).unwrap_or(0))
}
pub async fn group_by(
&mut self,
collection: &str,
field: &str,
aggregations: Vec<AggregateOp>,
metric: &str,
filter: Option<FilterInput>,
) -> Result<Vec<AggregateGroup>, ScrivaDbError> {
self.aggregate(
collection,
AggregateOptions {
aggregations,
field: metric.to_owned(),
group_by: field.to_owned(),
filter,
},
)
.await
}
pub async fn stats(&mut self, collection: &str) -> Result<CollectionStats, ScrivaDbError> {
let resp = self
.client
.collection_stats(self.req(pb::CollectionStatsRequest {
collection: collection.to_owned(),
}))
.await?
.into_inner();
Ok(CollectionStats {
collection: resp.collection,
record_count: resp.record_count,
segment_count: resp.segment_count,
dirty_entries: resp.dirty_entries,
size_bytes: resp.size_bytes,
})
}
pub async fn compact(&mut self, collection: &str) -> Result<bool, ScrivaDbError> {
let resp = self
.client
.compact(self.req(pb::CompactRequest {
collection: collection.to_owned(),
}))
.await?
.into_inner();
Ok(resp.ok)
}
pub async fn snapshot(
&mut self,
) -> Result<impl Stream<Item = Result<Vec<u8>, ScrivaDbError>>, ScrivaDbError> {
let streaming = self
.client
.snapshot(self.req(pb::SnapshotRequest {}))
.await?
.into_inner();
Ok(streaming.map(|result| result.map(|chunk| chunk.data).map_err(ScrivaDbError::from)))
}
pub async fn snapshot_to_file(
&mut self,
path: impl AsRef<std::path::Path>,
) -> Result<u64, Error> {
use tokio::io::AsyncWriteExt;
let mut stream = self.snapshot().await?;
let mut file = tokio::fs::File::create(path).await?;
let mut total: u64 = 0;
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
file.write_all(&chunk).await?;
total += chunk.len() as u64;
}
file.flush().await?;
Ok(total)
}
}