add immutable entries for size, etc (does nothing for now)

feat/vaults
Tomáš Mládek 2022-01-21 17:59:53 +01:00
parent 3599666a01
commit 0920fec0ce
6 changed files with 36 additions and 9 deletions

View File

@ -29,6 +29,7 @@ CREATE TABLE data
entity BLOB NOT NULL, entity BLOB NOT NULL,
attribute VARCHAR NOT NULL, attribute VARCHAR NOT NULL,
value VARCHAR NOT NULL, value VARCHAR NOT NULL,
immutable BOOLEAN NOT NULL,
UNIQUE (entity, attribute, value) UNIQUE (entity, attribute, value)
); );

View File

@ -20,6 +20,9 @@ pub struct Entry {
pub value: EntryValue, pub value: EntryValue,
} }
#[derive(Debug, Clone)]
pub struct ImmutableEntry(pub Entry);
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InvariantEntry { pub struct InvariantEntry {
pub attribute: String, pub attribute: String,
@ -55,6 +58,21 @@ impl TryFrom<&Entry> for models::Entry {
entity: e.entity.encode()?, entity: e.entity.encode()?,
attribute: e.attribute.clone(), attribute: e.attribute.clone(),
value: e.value.to_string()?, value: e.value.to_string()?,
immutable: false,
})
}
}
impl TryFrom<&ImmutableEntry> for models::Entry {
type Error = anyhow::Error;
fn try_from(e: &ImmutableEntry) -> Result<Self, Self::Error> {
Ok(models::Entry {
identity: e.0.address()?.encode()?,
entity: e.0.entity.encode()?,
attribute: e.0.attribute.clone(),
value: e.0.value.to_string()?,
immutable: true,
}) })
} }
} }

View File

@ -46,4 +46,5 @@ pub struct Entry {
pub entity: Vec<u8>, pub entity: Vec<u8>,
pub attribute: String, pub attribute: String,
pub value: String, pub value: String,
pub immutable: bool,
} }

View File

@ -4,6 +4,7 @@ table! {
entity -> Binary, entity -> Binary,
attribute -> Text, attribute -> Text,
value -> Text, value -> Text,
immutable -> Bool,
} }
} }

View File

@ -11,11 +11,11 @@ pub mod lang;
use crate::addressing::{Address, Addressable}; use crate::addressing::{Address, Addressable};
use crate::database::constants::{IS_OF_TYPE_ATTR, TYPE_ADDR, TYPE_HAS_ATTR, TYPE_INVARIANT}; use crate::database::constants::{IS_OF_TYPE_ATTR, TYPE_ADDR, TYPE_HAS_ATTR, TYPE_INVARIANT};
use crate::database::entry::{Entry, EntryValue}; use crate::database::entry::{Entry, EntryValue, ImmutableEntry};
use crate::database::inner::models; use crate::database::inner::models;
use crate::database::inner::schema::data; use crate::database::inner::schema::data;
use crate::database::lang::Query; use crate::database::lang::Query;
use crate::util::hash::{Hash, Hashable}; use crate::util::hash::Hash;
use crate::util::LoggerSink; use crate::util::LoggerSink;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
@ -336,13 +336,19 @@ impl UpEndConnection {
pub fn insert_entry(&self, entry: Entry) -> Result<Address> { pub fn insert_entry(&self, entry: Entry) -> Result<Address> {
debug!("Inserting: {}", entry); debug!("Inserting: {}", entry);
let entry = models::Entry::try_from(&entry)?;
self._insert_entry(entry)
}
let insert_entry = models::Entry::try_from(&entry)?; pub fn insert_entry_immutable(&self, entry: Entry) -> Result<Address> {
debug!("Inserting immutably: {}", entry);
let entry = Entry::try_from(&insert_entry)?; let entry = models::Entry::try_from(&ImmutableEntry(entry))?;
self._insert_entry(entry)
}
fn _insert_entry(&self, entry: models::Entry) -> Result<Address> {
let result = diesel::insert_into(data::table) let result = diesel::insert_into(data::table)
.values(insert_entry) .values(&entry)
.execute(&self.conn); .execute(&self.conn);
if let Some(error) = result.err() { if let Some(error) = result.err() {
@ -352,7 +358,7 @@ impl UpEndConnection {
} }
} }
Ok(Address::Hash(entry.hash()?)) Ok(Address::Hash(Hash(entry.identity)))
} }
// #[deprecated] // #[deprecated]

View File

@ -363,8 +363,8 @@ fn _process_directory_entry<P: AsRef<Path>>(
connection.transaction::<_, Error, _>(|| { connection.transaction::<_, Error, _>(|| {
connection.insert_file(new_file)?; connection.insert_file(new_file)?;
connection.insert_entry(type_entry)?; connection.insert_entry_immutable(type_entry)?;
connection.insert_entry(size_entry)?; connection.insert_entry_immutable(size_entry)?;
if let Some(mime_entry) = mime_entry { if let Some(mime_entry) = mime_entry {
connection.insert_entry(mime_entry)?; connection.insert_entry(mime_entry)?;
} }