mirror of https://github.com/zkat/cacache-rs.git
feat(index): implemented find()
This commit is contained in:
parent
322e68ffaa
commit
44eb2acc98
113
src/index.rs
113
src/index.rs
|
|
@ -1,5 +1,5 @@
|
||||||
use std::fs::OpenOptions;
|
use std::fs::{self, OpenOptions};
|
||||||
use std::io::Write;
|
use std::io::{ErrorKind, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
|
@ -16,19 +16,29 @@ use ssri::Integrity;
|
||||||
|
|
||||||
const INDEX_VERSION: &str = "5";
|
const INDEX_VERSION: &str = "5";
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(PartialEq, Debug)]
|
||||||
struct Entry {
|
pub struct Entry {
|
||||||
key: String,
|
key: String,
|
||||||
|
// TODO - implement Serialize for Integrity!
|
||||||
integrity: String,
|
integrity: String,
|
||||||
time: u128,
|
time: u128,
|
||||||
size: u128,
|
size: u128,
|
||||||
metadata: Value,
|
metadata: Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, PartialEq, Debug)]
|
||||||
|
struct SerializableEntry {
|
||||||
|
key: String,
|
||||||
|
integrity: Option<String>,
|
||||||
|
time: u128,
|
||||||
|
size: u128,
|
||||||
|
metadata: Value,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Inserter {
|
pub struct Inserter {
|
||||||
cache: PathBuf,
|
cache: PathBuf,
|
||||||
key: String,
|
key: String,
|
||||||
sri: Integrity,
|
sri: Option<Integrity>,
|
||||||
size: Option<u128>,
|
size: Option<u128>,
|
||||||
time: Option<u128>,
|
time: Option<u128>,
|
||||||
metadata: Option<Value>,
|
metadata: Option<Value>,
|
||||||
|
|
@ -52,6 +62,11 @@ impl Inserter {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn integrity(mut self, integrity: Option<Integrity>) -> Self {
|
||||||
|
self.sri = integrity;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn chown(mut self, uid: Option<Uid>, gid: Option<Gid>) -> Self {
|
pub fn chown(mut self, uid: Option<Uid>, gid: Option<Gid>) -> Self {
|
||||||
self.uid = uid;
|
self.uid = uid;
|
||||||
self.gid = gid;
|
self.gid = gid;
|
||||||
|
|
@ -63,9 +78,9 @@ impl Inserter {
|
||||||
if let Some(path) = mkdirp::mkdirp(bucket.parent().unwrap())? {
|
if let Some(path) = mkdirp::mkdirp(bucket.parent().unwrap())? {
|
||||||
chownr::chownr(path.as_path(), self.uid, self.gid)?;
|
chownr::chownr(path.as_path(), self.uid, self.gid)?;
|
||||||
}
|
}
|
||||||
let stringified = serde_json::to_string(&Entry {
|
let stringified = serde_json::to_string(&SerializableEntry {
|
||||||
key: self.key.to_owned(),
|
key: self.key.to_owned(),
|
||||||
integrity: self.sri.to_string(),
|
integrity: self.sri.map(|x| x.to_string()),
|
||||||
time: self.time.unwrap_or_else(now),
|
time: self.time.unwrap_or_else(now),
|
||||||
size: self.size.unwrap_or(0),
|
size: self.size.unwrap_or(0),
|
||||||
metadata: self.metadata.unwrap_or_else(|| json!(null)),
|
metadata: self.metadata.unwrap_or_else(|| json!(null)),
|
||||||
|
|
@ -86,7 +101,7 @@ pub fn insert(cache: &Path, key: &str, sri: Integrity) -> Inserter {
|
||||||
cache: cache.to_path_buf(),
|
cache: cache.to_path_buf(),
|
||||||
key: String::from(key),
|
key: String::from(key),
|
||||||
size: None,
|
size: None,
|
||||||
sri,
|
sri: Some(sri),
|
||||||
time: None,
|
time: None,
|
||||||
metadata: None,
|
metadata: None,
|
||||||
uid: None,
|
uid: None,
|
||||||
|
|
@ -94,8 +109,25 @@ pub fn insert(cache: &Path, key: &str, sri: Integrity) -> Inserter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find(_cache: &Path, _key: &str) {
|
pub fn find(cache: &Path, key: &str) -> Result<Option<Entry>, Error> {
|
||||||
unimplemented!();
|
let bucket = bucket_path(cache, &key);
|
||||||
|
Ok(bucket_entries(bucket.as_path())?.into_iter().fold(None, |acc, entry| {
|
||||||
|
if entry.key == key {
|
||||||
|
if entry.integrity.is_some() {
|
||||||
|
Some(Entry {
|
||||||
|
key: entry.key,
|
||||||
|
integrity: entry.integrity.unwrap(),
|
||||||
|
size: entry.size,
|
||||||
|
time: entry.time,
|
||||||
|
metadata: entry.metadata
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
acc
|
||||||
|
}
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete(_cache: &Path, _key: &str) {
|
pub fn delete(_cache: &Path, _key: &str) {
|
||||||
|
|
@ -136,10 +168,41 @@ fn now() -> u128 {
|
||||||
.as_millis()
|
.as_millis()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn bucket_entries(bucket: &Path) -> Result<Vec<SerializableEntry>, Error> {
|
||||||
|
let lines = match fs::read_to_string(bucket) {
|
||||||
|
Ok(data) => Ok(data),
|
||||||
|
Err(ref e) if e.kind() == ErrorKind::NotFound => Ok(String::from("")),
|
||||||
|
err => err,
|
||||||
|
}?;
|
||||||
|
Ok(lines.split('\n').fold(vec![], |mut acc, entry: &str| {
|
||||||
|
if entry.is_empty() { return acc }
|
||||||
|
let entry_str = match entry.split('\t').collect::<Vec<&str>>()[..] {
|
||||||
|
[hash, entry_str] => {
|
||||||
|
if hash_entry(entry_str) != hash {
|
||||||
|
// Hash is no good! Corruption or malice? Doesn't matter!
|
||||||
|
// EJECT EJECT
|
||||||
|
return acc
|
||||||
|
} else {
|
||||||
|
entry_str
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// Something's wrong with the entry. Abort.
|
||||||
|
_ => return acc,
|
||||||
|
};
|
||||||
|
if let Ok(entry) = serde_json::from_str::<SerializableEntry>(entry_str) {
|
||||||
|
acc.push(entry)
|
||||||
|
}
|
||||||
|
acc
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use tempfile;
|
use tempfile;
|
||||||
|
|
||||||
|
const MOCK_ENTRY: &str = "\n251d18a2b33264ea8655695fd23c88bd874cdea2c3dc9d8f9b7596717ad30fec\t{\"key\":\"hello\",\"integrity\":\"sha1-deadbeef\",\"time\":1234567,\"size\":0,\"metadata\":null}";
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_basic() {
|
fn insert_basic() {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
|
@ -148,7 +211,35 @@ mod tests {
|
||||||
let time = 1_234_567;
|
let time = 1_234_567;
|
||||||
insert(&dir, "hello", sri).time(time).commit().unwrap();
|
insert(&dir, "hello", sri).time(time).commit().unwrap();
|
||||||
let entry = std::fs::read_to_string(bucket_path(&dir, "hello")).unwrap();
|
let entry = std::fs::read_to_string(bucket_path(&dir, "hello")).unwrap();
|
||||||
|
assert_eq!(entry, MOCK_ENTRY);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn find_basic() {
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
let dir = tmp.path().to_owned();
|
||||||
|
let sri: Integrity = "sha1-deadbeef".parse().unwrap();
|
||||||
|
let time = 1_234_567;
|
||||||
|
let bucket = bucket_path(dir.as_path(), "hello");
|
||||||
|
mkdirp::mkdirp(bucket.parent().unwrap()).unwrap();
|
||||||
|
fs::write(bucket, MOCK_ENTRY).unwrap();
|
||||||
|
let entry = find(&dir, "hello").unwrap().unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
entry, "\n251d18a2b33264ea8655695fd23c88bd874cdea2c3dc9d8f9b7596717ad30fec\t{\"key\":\"hello\",\"integrity\":\"sha1-deadbeef\",\"time\":1234567,\"size\":0,\"metadata\":null}")
|
entry,
|
||||||
|
Entry {
|
||||||
|
key: String::from("hello"),
|
||||||
|
integrity: sri.to_string(),
|
||||||
|
time,
|
||||||
|
size: 0,
|
||||||
|
metadata: json!(null)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn find_none() {
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
let dir = tmp.path().to_owned();
|
||||||
|
assert_eq!(find(&dir, "hello").unwrap(), None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue