remove questionable iterator impl

main
Ziyang Hu 2 years ago
parent 1113e01387
commit e8eef6855c

@ -4,6 +4,7 @@ use bridge::*;
use std::fmt::{Display, Formatter};
use std::fmt::Debug;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use cxx::{let_cxx_string};
pub use cxx::{UniquePtr, SharedPtr};
@ -390,18 +391,21 @@ impl OTxnDBOptionsPtr {
}
}
pub struct IteratorPtr(UniquePtr<IteratorBridge>);
pub struct IteratorPtr<'a> {
inner: UniquePtr<IteratorBridge>,
txn: PhantomData<&'a TransactionPtr>,
}
impl Deref for IteratorPtr {
impl<'a> Deref for IteratorPtr<'a> {
type Target = UniquePtr<IteratorBridge>;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
&self.inner
}
}
impl IteratorPtr {
impl<'a> IteratorPtr<'a> {
#[inline]
pub fn to_first(&self) {
IteratorBridge::seek_to_first(self)
@ -455,49 +459,6 @@ impl IteratorPtr {
pub fn status(&self) -> BridgeStatus {
IteratorBridge::status(self)
}
#[inline]
pub fn iter(&self) -> KVIterator {
KVIterator { it: self, should_next: false }
}
#[inline]
pub fn keys(&self) -> KeyIterator {
KeyIterator { it: self, should_next: false }
}
}
pub struct KVIterator<'a> {
it: &'a IteratorPtr,
should_next: bool,
}
impl Iterator for KVIterator<'_> {
type Item = (SlicePtr, SlicePtr);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.should_next {
self.it.next();
}
self.should_next = true;
self.it.pair()
}
}
pub struct KeyIterator<'a> {
it: &'a IteratorPtr,
should_next: bool,
}
impl Iterator for KeyIterator<'_> {
type Item = SlicePtr;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.should_next {
self.it.next();
}
self.should_next = true;
self.it.key()
}
}
pub struct TransactionPtr(UniquePtr<TransactionBridge>);
@ -613,9 +574,15 @@ impl TransactionPtr {
#[inline]
pub fn iterator(&self, transact: bool, cf: &ColumnFamilyHandle) -> IteratorPtr {
if transact {
IteratorPtr(self.iterator_txn(cf))
IteratorPtr {
inner: self.iterator_txn(cf),
txn: PhantomData
}
} else {
IteratorPtr(self.iterator_raw(cf))
IteratorPtr {
inner: self.iterator_raw(cf),
txn: PhantomData
}
}
}
}

@ -284,7 +284,8 @@ impl<'s> Session<'s> {
let it = self.txn.iterator(true, &self.perm_cf);
it.seek(&prefix);
for val in it.keys() {
while it.is_valid() {
let val = it.key().ok_or_else(|| CozoError::LogicError("Failed to get tuple".to_string()))?;
let cur = Tuple::new(val);
if !cur.starts_with(&prefix) {
break;
@ -295,11 +296,13 @@ impl<'s> Session<'s> {
assocs.push((name.to_string(), data));
}
}
it.next();
}
let it = self.txn.iterator(false, &self.temp_cf);
it.seek(&prefix);
for val in it.keys() {
while it.is_valid() {
let val = it.key().ok_or_else(|| CozoError::LogicError("Failed to get tuple".to_string()))?;
let cur = Tuple::new(val);
if !cur.starts_with(&prefix) {
break;
@ -310,6 +313,7 @@ impl<'s> Session<'s> {
assocs.push((name.to_string(), data));
}
}
it.next();
}
Ok(assocs)

@ -130,7 +130,7 @@ impl<'a> Session<'a> {
pub fn start(&mut self) -> Result<()> {
self.start_with_total_seek(false)
}
pub fn start_with_total_seek(&mut self, total_seek: bool) -> Result<()> {
fn start_with_total_seek(&mut self, total_seek: bool) -> Result<()> {
self.perm_cf = self.engine.db.default_cf();
assert!(!self.perm_cf.is_null());
self.temp_cf = self.engine.db.get_cf(&self.handle.read().map_err(|_| Poisoned)?.cf_ident).ok_or(SessionErr)?;
@ -241,9 +241,11 @@ mod tests {
}
let it = sess.txn.iterator(true, &sess.perm_cf);
it.to_first();
for (key, val) in it.iter() {
while it.is_valid() {
let (key, val) = it.pair().unwrap();
println!("a: {:?} {:?}", key.as_ref(), val.as_ref());
println!("v: {:?} {:?}", Tuple::new(key), Tuple::new(val));
it.next();
}
}
let _ = fs::remove_dir_all("_push_get");

@ -101,7 +101,8 @@ impl<'s> Session<'s> {
let it = self.txn.iterator(false, &self.temp_cf);
it.seek(&prefix);
let mut to_delete = vec![];
for val in it.keys() {
while it.is_valid() {
let val = it.key().ok_or_else(|| CozoError::LogicError("Failed to get tuple".to_string()))?;
let cur = Tuple::new(val);
if cur.starts_with(&prefix) {
if let Some(name) = cur.get(1) {
@ -132,6 +133,7 @@ impl<'s> Session<'s> {
to_delete.push(cur.data.as_ref().to_vec());
to_delete.push(ikey.data.to_vec());
}
it.next();
} else {
break;
}
@ -142,7 +144,8 @@ impl<'s> Session<'s> {
prefix.push_int(self.stack_depth as i64);
let it = self.txn.iterator(false, &self.temp_cf);
it.seek(&prefix);
for val in it.keys() {
while it.is_valid() {
let val = it.key().ok_or_else(|| CozoError::LogicError("Failed to get tuple".to_string()))?;
let cur = Tuple::new(val);
if cur.starts_with(&prefix) {
let mut ikey = Tuple::with_prefix(cur.get_prefix());
@ -155,6 +158,7 @@ impl<'s> Session<'s> {
to_delete.push(cur.data.as_ref().to_vec());
to_delete.push(ikey.data.to_vec());
it.next();
} else {
break;
}

@ -1210,14 +1210,18 @@ mod tests {
let it = env.txn.iterator(false, &env.perm_cf);
it.to_first();
for (k, v) in it.iter() {
while it.is_valid() {
let (k, v) = it.pair().unwrap();
println!("{:?}, {:?}", Tuple::new(k), Tuple::new(v));
it.next();
}
let it = env.txn.iterator(false, &env.temp_cf);
it.to_first();
for (k, v) in it.iter() {
while it.is_valid() {
let (k, v) = it.pair().unwrap();
println!("{:?}, {:?}", Tuple::new(k), Tuple::new(v));
it.next();
}
}
fs::remove_dir_all(db_path).unwrap();

@ -289,8 +289,10 @@ mod tests {
sess.rollback().unwrap();
let it = sess.txn.iterator(true, &sess.perm_cf);
it.to_first();
for (k, v) in it.iter() {
while it.is_valid() {
let (k, v) = it.pair().unwrap();
println!("K: {:?}, V: {:?}", Tuple::new(k), Tuple::new(v));
it.next();
}
}
@ -338,8 +340,10 @@ mod tests {
let it = sess.txn.iterator(true, &sess.perm_cf);
it.to_first();
for (k, v) in it.iter() {
while it.is_valid() {
let (k, v) = it.pair().unwrap();
println!("K: {:?}, V: {:?}", Tuple::new(k), Tuple::new(v));
it.next();
}
println!("Time elapsed {:?}", duration);
}

@ -6,6 +6,7 @@ use crate::db::table::{ColId, TableId, TableInfo};
use crate::relation::value::{StaticValue, Value};
use crate::parser::Rule;
use crate::error::Result;
use crate::relation::tuple::OwnTuple;
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum QueryPlan {
@ -130,6 +131,26 @@ impl<'a> Session<'a> {
fn convert_select_data_to_plan(&self, plan: QueryPlan, select_data: Selection) -> Result<QueryPlan> {
Ok(QueryPlan::Projection { arg: Box::new(plan), projection: select_data })
}
pub fn iter_table(&self, tid: TableId) -> ! {
let it = if tid.in_root {
self.txn.iterator(true, &self.perm_cf)
} else {
self.txn.iterator(false, &self.temp_cf)
};
let prefix = OwnTuple::with_prefix(tid.id as u32);
it.seek(prefix);
// it.iter()
todo!()
}
}
pub struct NodeRowIterator {
}
pub struct EdgeRowIterator {
}
#[cfg(test)]
@ -141,6 +162,7 @@ mod tests {
use crate::parser::{Parser, Rule};
use pest::Parser as PestParser;
use crate::db::query::FromEl;
use crate::db::table::TableId;
use crate::relation::value::Value;
use crate::error::Result;
use crate::relation::tuple::{OwnTuple, Tuple};
@ -211,33 +233,25 @@ mod tests {
println!("{:?}", rel_tbls);
let tbl = rel_tbls.pop().unwrap();
let key_prefix = OwnTuple::with_prefix(tbl.id as u32);
let it = sess.txn.iterator(true, &sess.perm_cf);
it.seek(&key_prefix);
while it.is_valid() {
if let Some((k, v)) = it.pair() {
let k = Tuple::new(k);
// if !k.starts_with(&key_prefix) {
// break;
// }
let v = Tuple::new(v);
let tpair = [(k, v)];
match sess.tuple_eval(&where_vals, &tpair).unwrap() {
Value::Bool(true) => {
let extracted = sess.tuple_eval(&vals, &tpair).unwrap();
println!("{}", extracted);
}
Value::Null |
Value::Bool(_) => {
println!(" Ignore {:?}", &tpair);
}
_ => panic!("Bad type")
}
it.next();
} else {
break;
}
}
// for (k, v) in sess.iter_table(tbl) {
// let k = Tuple::new(k);
// // if !k.starts_with(&key_prefix) {
// // break;
// // }
// let v = Tuple::new(v);
// let tpair = [(k, v)];
// match sess.tuple_eval(&where_vals, &tpair).unwrap() {
// Value::Bool(true) => {
// let extracted = sess.tuple_eval(&vals, &tpair).unwrap();
// println!("{}", extracted);
// }
// Value::Null |
// Value::Bool(_) => {
// println!(" Ignore {:?}", &tpair);
// }
// _ => panic!("Bad type")
// }
// }
let duration = start.elapsed();
let duration2 = start2.elapsed();
println!("Time elapsed {:?} {:?}", duration, duration2);

Loading…
Cancel
Save