Skip to content

Commit

Permalink
updates to changedmarker, no longer serialised to cbor + fixed writin…
Browse files Browse the repository at this point in the history
…g stand-off files

minicbor has a skip option now we can use
  • Loading branch information
proycon committed May 6, 2024
1 parent 9b0b100 commit 0410079
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ regex = "1.10.3"
nanoid = "0.4.0"
csv = "1.3.0"
datasize = { version = "0.2.15", features = ["smallvec-types"] }
minicbor = { version = "0.21.0", features = ["derive","std" ] }
minicbor = { version = "0.24.0", features = ["derive","std" ] }
rayon = "1.9.0"

[features]
Expand Down
7 changes: 1 addition & 6 deletions src/annotationdataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use serde::Serialize;

use crate::annotationdata::{AnnotationData, AnnotationDataBuilder, AnnotationDataHandle};
use crate::annotationstore::AnnotationStore;
use crate::cbor::*;
use crate::config::{Config, Configurable, SerializeMode};
#[cfg(feature = "csv")]
use crate::csv::FromCsv;
Expand Down Expand Up @@ -66,11 +65,7 @@ pub struct AnnotationDataSet {
filename: Option<String>,

/// Flags if set has changed, if so, they need to be reserialised if stored via the include mechanism
#[n(5)]
#[cbor(
encode_with = "cbor_encode_changed",
decode_with = "cbor_decode_changed"
)]
#[cbor(skip)] // this used to be n(5)
changed: Arc<RwLock<bool>>, //this is modified via internal mutability

/// Maps public IDs to internal IDs for
Expand Down
13 changes: 13 additions & 0 deletions src/annotationstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use smallvec::{smallvec, SmallVec};
use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};

use crate::annotation::{Annotation, AnnotationBuilder, AnnotationHandle, AnnotationsJson};
use crate::annotationdata::{AnnotationData, AnnotationDataHandle};
Expand Down Expand Up @@ -137,6 +138,10 @@ pub struct AnnotationStore {
#[n(52)]
pub(crate) dataset_idmap: IdMap<AnnotationDataSetHandle>,

/// Flags if the store has changed
#[cbor(skip)]
changed: Arc<RwLock<bool>>, //this is modified via internal mutability

// reverse indices:
// ---------------------------------------------------------------------------------
/// Reverse index for AnnotationDataSet => AnnotationData => Annotation. Stores handles.
Expand Down Expand Up @@ -759,6 +764,7 @@ impl AnnotationStore {
key_annotation_metamap: TripleRelationMap::new(), //MAYBE TODO: sparse arrays, maybe better with BTreeMap variant?
data_annotation_metamap: TripleRelationMap::new(),
textrelationmap: TripleRelationMap::new(),
changed: Arc::new(RwLock::new(false)),
config,
filename: None,
annotations_filename: None,
Expand Down Expand Up @@ -2353,3 +2359,10 @@ impl<'de> serde::de::Visitor<'de> for AnnotationDataSetsVisitor<'_> {
Ok(())
}
}

#[sealed]
impl ChangeMarker for AnnotationStore {
fn change_marker(&self) -> &Arc<RwLock<bool>> {
&self.changed
}
}
17 changes: 0 additions & 17 deletions src/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,6 @@ pub(crate) fn cbor_len_positionitem_smallvec<Ctx, W: minicbor::encode::Write>(
}
*/

// minicbor has no skip property unfortunately, we have to fake it for the 'changed' fields on AnnotationDataSet and TextResource:

pub(crate) fn cbor_decode_changed<'b, Ctx>(
_d: &mut minicbor::decode::Decoder<'b>,
_ctx: &mut Ctx,
) -> Result<Arc<RwLock<bool>>, minicbor::decode::Error> {
Ok(Arc::new(RwLock::new(false)))
}

pub(crate) fn cbor_encode_changed<Ctx, W: minicbor::encode::Write>(
_v: &Arc<RwLock<bool>>,
_e: &mut minicbor::encode::Encoder<W>,
_ctx: &mut Ctx,
) -> Result<(), minicbor::encode::Error<W::Error>> {
Ok(())
}

pub(crate) fn cbor_decode_serialize_mode<'b, Ctx>(
_d: &mut minicbor::decode::Decoder<'b>,
_ctx: &mut Ctx,
Expand Down
3 changes: 2 additions & 1 deletion src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ pub(crate) fn filename_without_workdir<'a>(filename: &'a str, config: &Config) -
}

#[sealed(pub(crate))] //<-- this ensures nobody outside this crate can implement the trait
pub trait AssociatedFile: Configurable {
#[allow(private_bounds)]
pub trait AssociatedFile: Configurable + ChangeMarker {
fn filename(&self) -> Option<&str>;

//Set the associated filename for this structure.
Expand Down
18 changes: 10 additions & 8 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use serde::{Deserialize, Serialize};
use smallvec::smallvec;

use crate::annotationstore::AnnotationStore;
use crate::cbor::*;
use crate::config::{Config, Configurable, SerializeMode};
use crate::error::StamError;
use crate::file::*;
Expand Down Expand Up @@ -67,11 +66,7 @@ pub struct TextResource {
textlen: usize,

/// Flags if the text contents have changed, if so, they need to be reserialised if stored via the include mechanism
#[n(5)]
#[cbor(
encode_with = "cbor_encode_changed",
decode_with = "cbor_decode_changed"
)]
#[cbor(skip)] // this used to be n(5)
changed: Arc<RwLock<bool>>, //this is modified via internal mutability

/// A store of text selections (not in textual order)
Expand Down Expand Up @@ -152,6 +147,7 @@ impl TryFrom<TextResourceBuilder> for TextResource {
} else {
0
};
let changed = builder.text.is_some() && builder.filename.is_some(); //we supplied text but also have a filename, that means the file will be new
Ok(Self {
intid: None,
id: if let Some(id) = builder.id {
Expand Down Expand Up @@ -179,7 +175,7 @@ impl TryFrom<TextResourceBuilder> for TextResource {
textselections: Store::default(),
config: builder.config,
filename: builder.filename,
changed: Arc::new(RwLock::new(false)),
changed: Arc::new(RwLock::new(changed)),
})
}
}
Expand Down Expand Up @@ -968,7 +964,13 @@ impl AssociatedFile for TextResource {

/// Get the filename for stand-off file specified using @include (if any)
fn set_filename(&mut self, filename: &str) -> &mut Self {
self.filename = Some(filename.into());
if self.filename.as_ref().map(|s| s.as_str()) != Some(filename) {
self.filename = Some(filename.into());
if !self.text.is_empty() {
//text is already loaded (not empty) and the filename changed
self.mark_changed();
}
}
self
}
}
Expand Down

0 comments on commit 0410079

Please sign in to comment.