55 releases (32 breaking)
0.116.0 | Feb 13, 2024 |
---|---|
0.114.0 | Feb 6, 2024 |
0.104.0 | Dec 26, 2023 |
0.101.5 | Oct 28, 2023 |
0.51.0 |
|
#127 in Database implementations
29 downloads per month
Used in 9 crates
(via semilattice-database)
52KB
1K
SLoC
versatile-data
This is a small database that is easy to use. It doesn't use SQL. No table.
It has data formatted like key-value, but with rows like data in Table format. Each row can have data in key-value format. In addition, as a field of commonly used concepts, Activity, term_begin, term_end, last_updated is fixed for each row.
No need to design a table scheme like SQL. You can add any column(field) at any time.
All fields are indexed for fast search. You don't have to think about which fields to index. it is done automatically.
Example
use std::{num::NonZeroU32, sync::Arc};
use versatile_data::*;
let dir = "./vd-test/";
if std::path::Path::new(dir).exists() {
std::fs::remove_dir_all(dir).unwrap();
}
let mut data = Data::new(dir, DataOption::default());
let range = 1..=10;
futures::executor::block_on(async {
let field_num = FieldName::new("num".into());
let field_num_by3 = FieldName::new("num_by3".into());
let field_num_mod3 = FieldName::new("num_mod3".into());
let field_hoge = FieldName::new("hoge".into());
for i in range.clone() {
data.insert(
Activity::Active,
Term::Default,
Term::Default,
[
(field_num.clone(), i.to_string().into()),
(field_num_by3.clone(), (i * 3).to_string().into()),
(field_num_mod3.clone(), (i % 3).to_string().into()),
]
.into(),
)
.await;
}
let mut sam = 0.0;
for i in range {
sam += data.field_num(i.try_into().unwrap(), &field_num);
println!(
"{},{},{},{}",
data.serial(i.try_into().unwrap()),
std::str::from_utf8(data.field_bytes(i.try_into().unwrap(), &field_num)).unwrap(),
std::str::from_utf8(data.field_bytes(i.try_into().unwrap(), &field_num_by3))
.unwrap(),
std::str::from_utf8(data.field_bytes(i.try_into().unwrap(), &field_num_mod3))
.unwrap()
);
}
assert_eq!(sam, 55.0);
let r = data
.search_field(
field_num.clone(),
&search::Field::Range(b"3".to_vec(), b"8".to_vec()),
)
.search_default() //Automatic execution of the following two lines
//.search_term(Term::In(SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()))
//.search_activity(Activity::Active)
.result()
.await;
println!("{:?}", r);
let r = data
.search_default()
.search(Condition::Wide(&vec![
Condition::Field(field_num.clone(), &search::Field::Match(b"4".to_vec())),
Condition::Field(field_num.clone(), &search::Field::Match(b"6".to_vec())),
]))
.result()
.await;
println!("Wide test:{:?}", r);
let r = data
.search_default()
.result_with_sort(vec![Order::Asc(OrderKey::Serial)])
.await;
println!("sorted-serial:{:?}", r);
let r=data
.search_default()
.result_with_sort(vec![Order::Desc(OrderKey::Field(field_num.clone()))]).await //natural order
;
println!("sorted-num-desc:{:?}", r);
let r = data
.search_default()
.result_with_sort(vec![Order::Desc(OrderKey::Field(field_num_mod3.clone()))])
.await;
println!("sorted-mod3-desc:{:?}", r);
let r = data
.search_default()
.result_with_sort(vec![
Order::Asc(OrderKey::Field(field_num_mod3.clone())),
Order::Asc(OrderKey::Field(field_num.clone())),
])
.await;
println!("sorted mod3-asc num-asc:{:?}", r);
let r = data
.search_default()
.result_with_sort(vec![
Order::Asc(OrderKey::Field(field_num_mod3.clone())),
Order::Desc(OrderKey::Field(field_num.clone())),
])
.await;
println!("sorted mod3-asc num-desc:{:?}", r);
let r = data
.search_field(
field_num,
&search::Field::Range(b"3".to_vec(), b"8".to_vec()),
)
.search_row(&search::Number::Range(4..=7))
.search_default()
.result()
.await;
println!("{:?}", r);
data.update(
unsafe { NonZeroU32::new_unchecked(2) },
Activity::Active,
Term::Default,
Term::Default,
[(field_hoge.clone(), "HAHA".into())].into(),
)
.await;
data.update(
unsafe { NonZeroU32::new_unchecked(4) },
Activity::Active,
Term::Default,
Term::Default,
[(field_hoge.clone(), "agaba".into())].into(),
)
.await;
data.update(
unsafe { NonZeroU32::new_unchecked(5) },
Activity::Active,
Term::Default,
Term::Default,
[(field_hoge.clone(), "agababi".into())].into(),
)
.await;
data.update(
unsafe { NonZeroU32::new_unchecked(1) },
Activity::Active,
Term::Default,
Term::Default,
[(field_hoge.clone(), "ageabe".into())].into(),
)
.await;
data.update(
unsafe { NonZeroU32::new_unchecked(7) },
Activity::Active,
Term::Default,
Term::Default,
[(field_hoge.clone(), "ageee".into())].into(),
)
.await;
data.update(
unsafe { NonZeroU32::new_unchecked(6) },
Activity::Active,
Term::Default,
Term::Default,
[(field_hoge.clone(), "bebebe".into())].into(),
)
.await;
let r = data
.search_field(field_hoge.clone(), &search::Field::Match(b"HAHA".to_vec()))
.result()
.await;
println!("match:{:?}", r);
let r = data
.search_field(
field_hoge.clone(),
&search::Field::Forward(Arc::new("age".into())),
)
.result()
.await;
println!("forward:{:?}", r);
let r = data
.search_field(
field_hoge.clone(),
&search::Field::Partial(Arc::new("eb".into())),
)
.result()
.await;
println!("partial:{:?}", r);
let r = data
.search_field(field_hoge, &search::Field::Backward(Arc::new("be".into())))
.result()
.await;
println!("backward:{:?}", r);
let r = data.begin_search().result().await;
println!("all:{:?}", r);
});
Dependencies
~3.5–4.5MB
~77K SLoC