<commit_before>use prelude::*;

pub fn get_inode(fs: &mut Filesystem, ino: u64) -> Result<Inode> {
  if let Some(inode) = fs.inode_cache.get(&ino) {
    fs.reused_inos.insert(ino);
    return Ok(inode.clone())
  }

  let inode = try!(read_inode(fs, ino));
  fs.inode_cache.insert(ino, inode.clone());
  fs.cache_queue.push_back(ino);
  try!(refit_inode_cache(fs));
  Ok(inode)
}

pub fn update_inode(fs: &mut Filesystem, inode: &Inode) -> Result<()> {
  use std::collections::hash_map::Entry;

  fs.dirty_inos.insert(inode.ino);
  match fs.inode_cache.entry(inode.ino) {
    Entry::Occupied(mut occupied) => {
      occupied.insert(inode.clone());
      fs.reused_inos.insert(inode.ino);
      return Ok(())
    },
    Entry::Vacant(vacant) => {
      vacant.insert(inode.clone());
      fs.cache_queue.push_back(inode.ino);
    },
  }

  refit_inode_cache(fs)
}

fn refit_inode_cache(fs: &mut Filesystem) -> Result<()> {
  while fs.inode_cache.len() > 10 {
    let mut flushed = false;

    while let Some(used_ino) = fs.cache_queue.pop_front() {
      if fs.reused_inos.remove(&used_ino) {
        try!(flush_ino(fs, used_ino));
        flushed = true;
        break;
      }
    }

    if !flushed {
      let random_ino = *fs.inode_cache.iter().next().unwrap().0;
      try!(flush_ino(fs, random_ino));
    }
  }
  Ok(())
}

pub fn flush_ino(fs: &mut Filesystem, ino: u64) -> Result<()> {
  if let Some(inode) = fs.inode_cache.remove(&ino) {
    fs.reused_inos.remove(&ino);
    if fs.dirty_inos.remove(&ino) {
      return write_inode(fs, &inode);
    }
  }
  Ok(())<commit_msg>Make the cache actually work<commit_after>use prelude::*;

pub fn get_inode(fs: &mut Filesystem, ino: u64) -> Result<Inode> {
  if let Some(inode) = fs.inode_cache.get(&ino) {
    fs.reused_inos.insert(ino);
    return Ok(inode.clone())
  }

  let inode = try!(read_inode(fs, ino));
  fs.inode_cache.insert(ino, inode.clone());
  fs.cache_queue.push_back(ino);
  try!(refit_inode_cache(fs));
  Ok(inode)
}

pub fn update_inode(fs: &mut Filesystem, inode: &Inode) -> Result<()> {
  use std::collections::hash_map::Entry;

  fs.dirty_inos.insert(inode.ino);
  match fs.inode_cache.entry(inode.ino) {
    Entry::Occupied(mut occupied) => {
      occupied.insert(inode.clone());
      fs.reused_inos.insert(inode.ino);
      return Ok(())
    },
    Entry::Vacant(vacant) => {
      vacant.insert(inode.clone());
      fs.cache_queue.push_back(inode.ino);
    },
  }

  refit_inode_cache(fs)
}

fn refit_inode_cache(fs: &mut Filesystem) -> Result<()> {
  while fs.inode_cache.len() > 10 {
    let mut flushed = false;

    while let Some(used_ino) = fs.cache_queue.pop_front() {
      if !fs.reused_inos.remove(&used_ino) {
        try!(flush_ino(fs, used_ino));
        flushed = true;
        break;
      } else {
        fs.cache_queue.push_back(used_ino);
      }
    }

    if !flushed {
      let random_ino = *fs.inode_cache.iter().next().unwrap().0;
      try!(flush_ino(fs, random_ino));
    }
  }
  Ok(())
}

pub fn flush_ino(fs: &mut Filesystem, ino: u64) -> Result<()> {
  if let Some(inode) = fs.inode_cache.remove(&ino) {
    fs.reused_inos.remove(&ino);
    if fs.dirty_inos.remove(&ino) {
      return write_inode(fs, &inode);
    }
  }
  Ok(())
{'fraction_non_alphanumeric': 0.17155199012650416, 'fraction_numerical': 0.004319654427645789, 'mean_word_length': 3.226857887874837, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '172280', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1226, 'n_words': 203}