2022-02-15 03:40:01 +00:00
|
|
|
use crate::dbs::Options;
|
|
|
|
use crate::dbs::Runtime;
|
2022-03-18 19:51:13 +00:00
|
|
|
use crate::dbs::Statement;
|
2022-02-15 03:40:01 +00:00
|
|
|
use crate::dbs::Transaction;
|
|
|
|
use crate::err::Error;
|
2022-03-07 18:11:44 +00:00
|
|
|
use crate::key::thing;
|
2022-02-15 03:40:01 +00:00
|
|
|
use crate::sql::array::Array;
|
2022-03-18 07:24:36 +00:00
|
|
|
use crate::sql::id::Id;
|
2022-02-15 03:40:01 +00:00
|
|
|
use crate::sql::model::Model;
|
|
|
|
use crate::sql::table::Table;
|
|
|
|
use crate::sql::thing::Thing;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
use async_recursion::async_recursion;
|
2022-02-26 23:30:19 +00:00
|
|
|
use tokio::sync::mpsc::Sender;
|
2022-02-15 03:40:01 +00:00
|
|
|
|
|
|
|
impl Value {
|
2022-05-01 22:25:53 +00:00
|
|
|
pub(crate) async fn channel(
|
2022-02-15 03:40:01 +00:00
|
|
|
self,
|
|
|
|
ctx: Runtime,
|
|
|
|
opt: Options,
|
2022-03-18 19:51:13 +00:00
|
|
|
stm: Statement,
|
2022-02-15 03:40:01 +00:00
|
|
|
txn: Transaction,
|
2022-02-26 23:30:19 +00:00
|
|
|
chn: Sender<(Option<Thing>, Value)>,
|
2022-02-15 03:40:01 +00:00
|
|
|
) -> Result<(), Error> {
|
2022-02-25 22:42:14 +00:00
|
|
|
if ctx.is_ok() {
|
|
|
|
match self {
|
2022-03-18 19:51:13 +00:00
|
|
|
Value::Array(v) => v.process(&ctx, &opt, &stm, &txn, &chn).await?,
|
|
|
|
Value::Model(v) => v.process(&ctx, &opt, &stm, &txn, &chn).await?,
|
|
|
|
Value::Thing(v) => v.process(&ctx, &opt, &stm, &txn, &chn).await?,
|
|
|
|
Value::Table(v) => v.process(&ctx, &opt, &stm, &txn, &chn).await?,
|
2022-02-26 23:30:19 +00:00
|
|
|
v => chn.send((None, v)).await?,
|
2022-02-25 22:42:14 +00:00
|
|
|
}
|
2022-02-15 03:40:01 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Array {
|
|
|
|
#[async_recursion]
|
2022-05-01 22:25:53 +00:00
|
|
|
pub(crate) async fn process(
|
2022-02-15 03:40:01 +00:00
|
|
|
self,
|
|
|
|
ctx: &Runtime,
|
|
|
|
opt: &Options,
|
2022-03-18 19:51:13 +00:00
|
|
|
stm: &Statement,
|
2022-02-15 03:40:01 +00:00
|
|
|
txn: &Transaction,
|
2022-02-26 23:30:19 +00:00
|
|
|
chn: &Sender<(Option<Thing>, Value)>,
|
2022-02-15 03:40:01 +00:00
|
|
|
) -> Result<(), Error> {
|
2022-05-04 16:14:40 +00:00
|
|
|
for v in self {
|
2022-02-25 22:42:14 +00:00
|
|
|
if ctx.is_ok() {
|
|
|
|
match v {
|
2022-03-18 19:51:13 +00:00
|
|
|
Value::Array(v) => v.process(ctx, opt, stm, txn, chn).await?,
|
|
|
|
Value::Model(v) => v.process(ctx, opt, stm, txn, chn).await?,
|
|
|
|
Value::Thing(v) => v.process(ctx, opt, stm, txn, chn).await?,
|
|
|
|
Value::Table(v) => v.process(ctx, opt, stm, txn, chn).await?,
|
2022-02-26 23:30:19 +00:00
|
|
|
v => chn.send((None, v)).await?,
|
2022-02-25 22:42:14 +00:00
|
|
|
}
|
2022-02-15 03:40:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Model {
|
2022-05-01 22:25:53 +00:00
|
|
|
pub(crate) async fn process(
|
2022-02-15 03:40:01 +00:00
|
|
|
self,
|
|
|
|
ctx: &Runtime,
|
|
|
|
opt: &Options,
|
2022-03-18 19:51:13 +00:00
|
|
|
stm: &Statement,
|
2022-02-15 03:40:01 +00:00
|
|
|
txn: &Transaction,
|
2022-02-26 23:30:19 +00:00
|
|
|
chn: &Sender<(Option<Thing>, Value)>,
|
2022-02-15 03:40:01 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
if ctx.is_ok() {
|
|
|
|
if let Some(c) = self.count {
|
|
|
|
for _ in 0..c {
|
|
|
|
Thing {
|
|
|
|
tb: self.table.to_string(),
|
2022-03-18 07:24:36 +00:00
|
|
|
id: Id::rand(),
|
2022-02-15 03:40:01 +00:00
|
|
|
}
|
2022-03-18 19:51:13 +00:00
|
|
|
.process(ctx, opt, stm, txn, chn)
|
2022-02-15 03:40:01 +00:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(r) = self.range {
|
2022-03-17 22:26:34 +00:00
|
|
|
for x in r.0..=r.1 {
|
2022-02-15 03:40:01 +00:00
|
|
|
Thing {
|
|
|
|
tb: self.table.to_string(),
|
2022-03-18 07:24:36 +00:00
|
|
|
id: Id::from(x),
|
2022-02-15 03:40:01 +00:00
|
|
|
}
|
2022-03-18 19:51:13 +00:00
|
|
|
.process(ctx, opt, stm, txn, chn)
|
2022-02-15 03:40:01 +00:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Thing {
|
2022-05-01 22:25:53 +00:00
|
|
|
pub(crate) async fn process(
|
2022-02-15 03:40:01 +00:00
|
|
|
self,
|
2022-03-07 18:11:44 +00:00
|
|
|
ctx: &Runtime,
|
|
|
|
opt: &Options,
|
2022-04-01 22:28:54 +00:00
|
|
|
_stm: &Statement,
|
2022-03-07 18:11:44 +00:00
|
|
|
txn: &Transaction,
|
|
|
|
chn: &Sender<(Option<Thing>, Value)>,
|
2022-02-15 03:40:01 +00:00
|
|
|
) -> Result<(), Error> {
|
2022-03-07 18:11:44 +00:00
|
|
|
if ctx.is_ok() {
|
|
|
|
let key = thing::new(opt.ns(), opt.db(), &self.tb, &self.id);
|
|
|
|
let val = txn.clone().lock().await.get(key).await?;
|
|
|
|
let val = match val {
|
|
|
|
Some(v) => Value::from(v),
|
|
|
|
None => Value::None,
|
|
|
|
};
|
|
|
|
chn.send((Some(self), val)).await?;
|
|
|
|
}
|
2022-02-15 03:40:01 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Table {
|
2022-05-01 22:25:53 +00:00
|
|
|
pub(crate) async fn process(
|
2022-02-15 03:40:01 +00:00
|
|
|
self,
|
2022-03-07 18:11:44 +00:00
|
|
|
ctx: &Runtime,
|
|
|
|
opt: &Options,
|
2022-04-01 22:28:54 +00:00
|
|
|
_stm: &Statement,
|
2022-03-07 18:11:44 +00:00
|
|
|
txn: &Transaction,
|
|
|
|
chn: &Sender<(Option<Thing>, Value)>,
|
2022-02-15 03:40:01 +00:00
|
|
|
) -> Result<(), Error> {
|
2022-03-07 18:11:44 +00:00
|
|
|
if ctx.is_ok() {
|
2022-03-18 07:21:22 +00:00
|
|
|
let beg = thing::prefix(opt.ns(), opt.db(), &self.name);
|
|
|
|
let end = thing::suffix(opt.ns(), opt.db(), &self.name);
|
2022-03-07 18:11:44 +00:00
|
|
|
let mut nxt: Option<Vec<u8>> = None;
|
|
|
|
loop {
|
|
|
|
if ctx.is_ok() {
|
|
|
|
let res = match nxt {
|
|
|
|
None => {
|
2022-03-18 07:21:22 +00:00
|
|
|
let min = beg.clone();
|
|
|
|
let max = end.clone();
|
2022-03-07 18:11:44 +00:00
|
|
|
txn.clone().lock().await.scan(min..max, 1000).await?
|
|
|
|
}
|
|
|
|
Some(ref mut beg) => {
|
2022-03-16 17:01:25 +00:00
|
|
|
beg.push(0x00);
|
2022-03-07 18:11:44 +00:00
|
|
|
let min = beg.clone();
|
2022-03-18 07:21:22 +00:00
|
|
|
let max = end.clone();
|
2022-03-07 18:11:44 +00:00
|
|
|
txn.clone().lock().await.scan(min..max, 1000).await?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if !res.is_empty() {
|
|
|
|
// Get total results
|
2022-03-21 13:48:59 +00:00
|
|
|
let n = res.len();
|
|
|
|
// Exit when settled
|
|
|
|
if n == 0 {
|
|
|
|
break;
|
|
|
|
}
|
2022-03-07 18:11:44 +00:00
|
|
|
// Loop over results
|
|
|
|
for (i, (k, v)) in res.into_iter().enumerate() {
|
|
|
|
if ctx.is_ok() {
|
|
|
|
// Ready the next
|
2022-03-21 13:48:59 +00:00
|
|
|
if n == i + 1 {
|
2022-03-07 18:11:44 +00:00
|
|
|
nxt = Some(k.clone());
|
|
|
|
}
|
|
|
|
// Parse the key-value
|
|
|
|
let k: crate::key::thing::Thing = (&k).into();
|
|
|
|
let v: crate::sql::value::Value = (&v).into();
|
|
|
|
let t = Thing::from((k.tb, k.id));
|
|
|
|
// Process the record
|
|
|
|
chn.send((Some(t), v)).await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 03:40:01 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|