Start on an iterator interface

This commit is contained in:
Nicole Tietz-Sokolskaya 2024-05-24 07:01:03 -04:00
parent 609b341a2b
commit bd4cda4502
4 changed files with 407 additions and 377 deletions

710
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -17,7 +17,7 @@ bincode = "1.3.3"
clap = { version = "4.5.3", features = ["derive", "env"] }
dotenvy = "0.15.7"
env_logger = "0.11.3"
fjall = "0.6.5"
fjall = "1"
free-icons = "0.7.0"
minijinja = { version = "1.0.14", features = ["loader", "json", "builtins"] }
minijinja-autoreload = "1.0.14"

View file

@ -1,3 +1,4 @@
#![feature(trait_alias)]
pub mod config;
pub mod context;
pub mod db;

View file

@ -1,24 +1,89 @@
use std::{collections::{HashMap, HashSet}, path::Path, sync::Arc};
use anyhow::Result;
use uuid::Uuid;
use crate::models::{Document, Project};
pub type ItemId = Uuid;
pub trait Filter = Fn(&Item) -> bool;
pub trait Map<T> = Fn(&Item) -> T;
pub enum Item {
Project(Box<Project>),
Document(Box<Document>),
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct ItemId(Uuid);
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct IndexId(String);
pub type Index = HashSet<ItemId>;
pub struct Store {
items: HashMap<ItemId, Arc<Item>>,
indexes: HashMap<IndexId, Index>,
keyspace: fjall::Keyspace,
persistent_items: fjall::PartitionHandle,
}
impl Store {
pub fn save(_item: &Item) -> Result<()> {
pub fn new<P: AsRef<Path>>(path: P) -> Result<Store> {
let items = HashMap::new();
let indexes = HashMap::new();
let kv_config = fjall::Config::new(path)
.flush_workers(4)
.compaction_workers(4);
let keyspace = fjall::Keyspace::open(kv_config)?;
let persistent_items =
keyspace.open_partition("items", fjall::PartitionCreateOptions::default())?;
Ok(Store {
items,
indexes,
keyspace,
persistent_items,
})
}
pub fn set(&mut self, _item: &Item) -> Result<()> {
todo!()
}
pub fn load(_id: ItemId) -> Result<Option<ItemId>> {
/// Retrieves an item from the store by id. This is always an in-memory
/// operation and cannot fail.
pub fn get(&self, id: ItemId) -> Option<Arc<Item>> {
self.items.get(&id).cloned()
}
pub fn filter<F: Filter>(&self, _filter: F) -> Result<Vec<ItemId>> {
todo!()
}
pub fn map<T, M: Map<T>>(&self, _transform: M) -> Result<Vec<T>> {
todo!()
}
}
// TODO: can filter and map be done in terms of an iterator?
// like, the real Rust iterator trait?
pub struct QuerySet {
indexes: Vec<Index>, // TODO: but use a ref, once this is figured out
}
impl QuerySet {
}
impl Iterator for QuerySet {
type Item = Item;
fn next(&mut self) -> Option<Self::Item> {
todo!()
}
}