Skip to content

Commit

Permalink
feat: pond cascade
Browse files Browse the repository at this point in the history
  • Loading branch information
i007c committed Nov 25, 2024
1 parent 2e5dfdd commit 6b42b7f
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions shah/src/db/pond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ impl<T: Default + Entity + Debug + Clone + Copy + Binary + Duck> PondDb<T> {
pond.stack
};

self.live += 1;
self.file.write_all_at(buf.as_binary(), stack * T::N)?;
self.index.set(&pond)?;
self.origins.set(&origin)?;
Expand Down Expand Up @@ -358,6 +359,8 @@ impl<T: Default + Entity + Debug + Clone + Copy + Binary + Duck> PondDb<T> {

entity.set_alive(false);

self.live -= 1;

self.file.seek_relative(-(T::N as i64))?;
self.file.write_all(entity.as_binary())?;

Expand Down Expand Up @@ -407,4 +410,54 @@ impl<T: Default + Entity + Debug + Clone + Copy + Binary + Duck> PondDb<T> {

Ok(())
}

pub fn pond_free(&mut self, pond: &mut Pond) -> Result<(), SystemError> {
let mut buf = [T::default(); PAGE_SIZE];

self.seek_id(pond.stack)?;
self.file.read_exact(buf.as_binary_mut())?;

pond.empty = 0;
for item in buf.iter_mut() {
if item.is_alive() {
item.set_alive(false);
self.live -= 1;
}
if item.gene().iter < ITER_EXHAUSTION {
pond.empty += 1;
}
}

self.seek_id(pond.stack)?;
self.file.write_all(buf.as_binary())?;

pond.set_free(true);
pond.alive = 0;

self.index.set(&pond)?;
self.free_list.push(pond.gene);

Ok(())
}

pub fn cascade(&mut self, origene: &Gene) -> Result<(), SystemError> {
let mut origin = Origin::default();
self.origins.get(origene, &mut origin)?;

let mut pond_gene = origin.first;
let mut pond = Pond::default();
loop {
if pond_gene.is_none() {
break;
}

self.index.get(&pond_gene, &mut pond)?;
pond_gene = pond.next;
self.pond_free(&mut pond)?;
}

self.origins.del(origene, &mut origin)?;

Ok(())
}
}

0 comments on commit 6b42b7f

Please sign in to comment.