commit a7cbc9ca91b7b5b064f0b4ace07fdb4ae61d9de1
parent 81692c3a1ed22bb853de3ab1fa76664c9967ee6e
Author: lash <dev@holbrook.no>
Date: Thu, 4 Jul 2024 20:33:28 +0100
Implement default mem cache
Diffstat:
M | src/lib.rs | | | 16 | ++++++++-------- |
A | src/mem.rs | | | 60 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 68 insertions(+), 8 deletions(-)
diff --git a/src/lib.rs b/src/lib.rs
@@ -14,17 +14,17 @@ use atom_syndication::Feed as OutFeed;
mod meta;
mod io;
+mod mem;
use meta::FeedMetadata;
+use mem::MemCache;
+use mem::CacheWriter;
+use mem::Cache;
#[derive(Debug)]
pub enum Error {
WriteError,
}
-pub trait Cache {
- fn open(&self, id: String) -> &mut dyn Write;
- fn close(&self, id: String) -> usize;
-}
pub struct Sequencer<'a> {
metadata: FeedMetadata,
@@ -32,8 +32,8 @@ pub struct Sequencer<'a> {
item_keys: Vec<u64>,
crsr: usize,
limit: usize,
- default_cache: Vec<u8>,
- cache: Option<&'a dyn Cache>,
+ default_cache: CacheWriter, //HashMap<String, Vec<u8>>,
+ cache: Option<&'a mut dyn Cache>,
}
pub struct SequencerEntry {
@@ -49,7 +49,7 @@ impl<'a> Sequencer<'a> {
crsr: 0,
limit: 0,
item_keys: Vec::new(),
- default_cache: Vec::<u8>::new(),
+ default_cache: CacheWriter::new(), //HashMap::new(),
cache: None,
};
@@ -59,7 +59,7 @@ impl<'a> Sequencer<'a> {
o
}
- pub fn with_cache(&mut self, w: &'a impl Cache) -> &Sequencer<'a> {
+ pub fn with_cache(&mut self, w: &'a mut impl Cache) -> &Sequencer<'a> {
self.cache = Some(w);
return self;
}
diff --git a/src/mem.rs b/src/mem.rs
@@ -0,0 +1,60 @@
+use std::collections::HashMap;
+use std::io::Write;
+use std::io::Error;
+
+pub trait Cache {
+ fn open(&mut self, id: String) -> &mut dyn Write;
+ fn close(&mut self, id: String) -> usize;
+}
+
+pub struct CacheWriter {
+ data: Vec<u8>,
+}
+
+pub struct MemCache {
+ files: HashMap<String, CacheWriter>,
+}
+
+impl Write for CacheWriter {
+ fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
+ self.data.extend_from_slice(buf);
+ Ok(self.data.len())
+ }
+
+ fn flush(&mut self) -> Result<(), Error> {
+ Ok(())
+ }
+}
+
+
+impl CacheWriter {
+ pub fn new() -> CacheWriter {
+ CacheWriter{
+ data: Vec::new(),
+ }
+ }
+}
+
+impl MemCache {
+ pub fn new() -> MemCache {
+ MemCache{
+ files: HashMap::new(),
+ }
+ }
+}
+
+impl Cache for MemCache {
+ fn open(&mut self, id: String) -> &mut dyn Write {
+ let mut w: CacheWriter;
+ let mut r: Option<&CacheWriter>;
+ if !self.files.contains_key(&id) {
+ w = CacheWriter::new();
+ self.files.insert(id.clone(), w);
+ }
+ self.files.get_mut(&id).unwrap()
+ }
+
+ fn close(&mut self, id: String) -> usize {
+ return 0;
+ }
+}