remove InnerEntry, consolidate into Entry
parent
2b42f4dc1a
commit
99f6c6c052
|
@ -25,14 +25,6 @@ use std::time::Duration;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Entry {
|
pub struct Entry {
|
||||||
pub identity: Hash,
|
|
||||||
pub target: Address,
|
|
||||||
pub key: String,
|
|
||||||
pub value: EntryValue,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct InnerEntry {
|
|
||||||
pub target: Address,
|
pub target: Address,
|
||||||
pub key: String,
|
pub key: String,
|
||||||
pub value: EntryValue,
|
pub value: EntryValue,
|
||||||
|
@ -64,7 +56,6 @@ impl TryFrom<models::Entry> for Entry {
|
||||||
|
|
||||||
fn try_from(e: models::Entry) -> Result<Self, Self::Error> {
|
fn try_from(e: models::Entry) -> Result<Self, Self::Error> {
|
||||||
Ok(Entry {
|
Ok(Entry {
|
||||||
identity: Hash(e.identity),
|
|
||||||
target: Address::decode(&e.target)?,
|
target: Address::decode(&e.target)?,
|
||||||
key: e.key,
|
key: e.key,
|
||||||
value: e.value.parse().unwrap(),
|
value: e.value.parse().unwrap(),
|
||||||
|
@ -72,14 +63,14 @@ impl TryFrom<models::Entry> for Entry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for InnerEntry {
|
impl std::fmt::Display for Entry {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "{} | {} | {}", self.target, self.key, self.value)
|
write!(f, "{} | {} | {}", self.target, self.key, self.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hashable for InnerEntry {
|
impl Hashable for Entry {
|
||||||
fn hash(self: &InnerEntry) -> Result<Hash> {
|
fn hash(self: &Entry) -> Result<Hash> {
|
||||||
let mut result = Cursor::new(vec![0u8; 0]);
|
let mut result = Cursor::new(vec![0u8; 0]);
|
||||||
result.write_all(self.target.encode()?.as_slice())?;
|
result.write_all(self.target.encode()?.as_slice())?;
|
||||||
result.write_all(self.key.as_bytes())?;
|
result.write_all(self.key.as_bytes())?;
|
||||||
|
@ -618,7 +609,7 @@ pub fn query_entries<C: Connection<Backend = Sqlite>>(
|
||||||
|
|
||||||
pub fn insert_entry<C: Connection<Backend = Sqlite>>(
|
pub fn insert_entry<C: Connection<Backend = Sqlite>>(
|
||||||
connection: &C,
|
connection: &C,
|
||||||
entry: InnerEntry,
|
entry: Entry,
|
||||||
) -> Result<usize> {
|
) -> Result<usize> {
|
||||||
debug!("Inserting: {}", entry);
|
debug!("Inserting: {}", entry);
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
use crate::addressing::Address;
|
use crate::addressing::Address;
|
||||||
use crate::database::{
|
use crate::database::{
|
||||||
bulk_retrieve_objects, file_set_valid, insert_entry, insert_file, query_entries,
|
bulk_retrieve_objects, file_set_valid, insert_entry, insert_file, query_entries,
|
||||||
retrieve_all_files, DbPool, Entry, EntryQuery, EntryValue, InnerEntry, QueryComponent,
|
retrieve_all_files, DbPool, Entry, EntryQuery, EntryValue, QueryComponent, DATABASE_FILENAME,
|
||||||
DATABASE_FILENAME,
|
|
||||||
};
|
};
|
||||||
use crate::hash::Hashable;
|
use crate::hash::Hashable;
|
||||||
use crate::models;
|
use crate::models;
|
||||||
|
@ -234,7 +233,7 @@ pub fn fetch_or_create_dir<C: Connection<Backend = Sqlite>>(
|
||||||
0 => {
|
0 => {
|
||||||
if create {
|
if create {
|
||||||
let new_directory_address = Address::UUID(Uuid::new_v4());
|
let new_directory_address = Address::UUID(Uuid::new_v4());
|
||||||
let directory_entry = InnerEntry {
|
let directory_entry = Entry {
|
||||||
target: new_directory_address.clone(),
|
target: new_directory_address.clone(),
|
||||||
key: String::from(DIR_KEY),
|
key: String::from(DIR_KEY),
|
||||||
value: dir_value,
|
value: dir_value,
|
||||||
|
@ -242,7 +241,7 @@ pub fn fetch_or_create_dir<C: Connection<Backend = Sqlite>>(
|
||||||
let _ = insert_entry(connection, directory_entry)?;
|
let _ = insert_entry(connection, directory_entry)?;
|
||||||
|
|
||||||
if let Some(parent_addr) = parent {
|
if let Some(parent_addr) = parent {
|
||||||
let has_entry = InnerEntry {
|
let has_entry = Entry {
|
||||||
target: parent_addr,
|
target: parent_addr,
|
||||||
key: String::from(DIR_HAS_KEY),
|
key: String::from(DIR_HAS_KEY),
|
||||||
value: EntryValue::Address(new_directory_address.clone()),
|
value: EntryValue::Address(new_directory_address.clone()),
|
||||||
|
@ -434,7 +433,7 @@ fn _process_directory_entry<P: AsRef<Path>>(
|
||||||
connection.transaction::<_, Error, _>(|| {
|
connection.transaction::<_, Error, _>(|| {
|
||||||
let file_address = Address::UUID(Uuid::new_v4());
|
let file_address = Address::UUID(Uuid::new_v4());
|
||||||
|
|
||||||
let name_entry = InnerEntry {
|
let name_entry = Entry {
|
||||||
target: file_address.clone(),
|
target: file_address.clone(),
|
||||||
key: FILENAME_KEY.to_string(),
|
key: FILENAME_KEY.to_string(),
|
||||||
value: EntryValue::Value(Value::String(
|
value: EntryValue::Value(Value::String(
|
||||||
|
@ -443,7 +442,7 @@ fn _process_directory_entry<P: AsRef<Path>>(
|
||||||
};
|
};
|
||||||
let _ = insert_entry(&connection, name_entry)?;
|
let _ = insert_entry(&connection, name_entry)?;
|
||||||
|
|
||||||
let identity_entry = InnerEntry {
|
let identity_entry = Entry {
|
||||||
target: file_address.clone(),
|
target: file_address.clone(),
|
||||||
key: FILE_IDENTITY_KEY.to_string(),
|
key: FILE_IDENTITY_KEY.to_string(),
|
||||||
value: EntryValue::Address(Address::Hash(digest.clone())),
|
value: EntryValue::Address(Address::Hash(digest.clone())),
|
||||||
|
@ -451,7 +450,7 @@ fn _process_directory_entry<P: AsRef<Path>>(
|
||||||
|
|
||||||
let _ = insert_entry(&connection, identity_entry)?;
|
let _ = insert_entry(&connection, identity_entry)?;
|
||||||
|
|
||||||
let dir_has_entry = InnerEntry {
|
let dir_has_entry = Entry {
|
||||||
target: parent_dir.clone(),
|
target: parent_dir.clone(),
|
||||||
key: DIR_HAS_KEY.to_string(),
|
key: DIR_HAS_KEY.to_string(),
|
||||||
value: EntryValue::Address(file_address),
|
value: EntryValue::Address(file_address),
|
||||||
|
|
|
@ -7,6 +7,12 @@ use tiny_keccak::{Hasher, KangarooTwelve};
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Hash(pub Vec<u8>);
|
pub struct Hash(pub Vec<u8>);
|
||||||
|
|
||||||
|
impl AsRef<[u8]> for Hash {
|
||||||
|
fn as_ref(&self) -> &[u8] {
|
||||||
|
self.0.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// impl Hash {
|
// impl Hash {
|
||||||
// pub fn encode(&self) -> String {
|
// pub fn encode(&self) -> String {
|
||||||
// encode(self.0.clone()).into_string()
|
// encode(self.0.clone()).into_string()
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::addressing::Address;
|
use crate::addressing::Address;
|
||||||
use crate::database::{query, remove_object, retrieve_file, retrieve_object, DbPool, Entry, Query};
|
use crate::database::{query, remove_object, retrieve_file, retrieve_object, DbPool, Entry, Query};
|
||||||
use crate::filesystem::{list_directory, lookup_by_filename, UPath};
|
use crate::filesystem::{list_directory, lookup_by_filename, UPath};
|
||||||
use crate::hash::{decode, encode};
|
use crate::hash::{decode, encode, Hashable};
|
||||||
use actix_files::NamedFile;
|
use actix_files::NamedFile;
|
||||||
use actix_web::error::{ErrorBadRequest, ErrorInternalServerError, ErrorNotFound};
|
use actix_web::error::{ErrorBadRequest, ErrorInternalServerError, ErrorNotFound};
|
||||||
use actix_web::{delete, error, get, post, web, Error, HttpResponse};
|
use actix_web::{delete, error, get, post, web, Error, HttpResponse};
|
||||||
|
@ -55,7 +55,10 @@ pub async fn get_object(
|
||||||
|
|
||||||
let mut result: HashMap<String, serde_json::Value> = HashMap::new();
|
let mut result: HashMap<String, serde_json::Value> = HashMap::new();
|
||||||
for entry in response.map_err(error::ErrorInternalServerError)? {
|
for entry in response.map_err(error::ErrorInternalServerError)? {
|
||||||
result.insert(encode(&entry.identity.0), entry.as_json());
|
result.insert(
|
||||||
|
encode(entry.hash().map_err(ErrorInternalServerError)?),
|
||||||
|
entry.as_json(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Ok(HttpResponse::Ok().json(result))
|
Ok(HttpResponse::Ok().json(result))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue