structsy 0.5.2

Simple single file structs database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
use crate::{
    desc::DefinitionInfo,
    id::{raw_format, raw_parse},
    internal::Description,
    record::Record,
    snapshot::SnapshotRecordIter,
    transaction::OwnedSytx,
    InternalDescription, Persistent, PersistentEmbedded, RawAccess, RawRead, Ref, SRes, Snapshot, Structsy,
    StructsyConfig, StructsyError, StructsyTx,
};
use persy::{Config, Persy, PersyId, Transaction};
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::io::Cursor;
use std::marker::PhantomData;
use std::path::Path;
use std::sync::{Arc, Mutex};

pub(crate) const INTERNAL_SEGMENT_NAME: &str = "__#internal";

struct Definitions {
    definitions: Mutex<HashMap<String, InternalDescription>>,
}

impl Definitions {
    fn new(definitions: HashMap<String, InternalDescription>) -> Definitions {
        Definitions {
            definitions: Mutex::new(definitions),
        }
    }

    pub(crate) fn check_defined<T: Persistent>(&self) -> SRes<DefinitionInfo> {
        let mut lock = self.definitions.lock()?;
        let name = T::get_name();
        if let Some(x) = lock.get_mut(name) {
            if x.checked {
                Ok(x.info())
            } else {
                let desc = T::get_description();
                if x.desc != desc {
                    Err(StructsyError::StructNotDefined(desc.get_name()))
                } else {
                    x.checked = true;
                    Ok(x.info())
                }
            }
        } else {
            Err(StructsyError::StructNotDefined(String::from(name)))
        }
    }

    pub fn is_defined<T: Persistent>(&self) -> SRes<bool> {
        let lock = self.definitions.lock()?;
        Ok(lock.contains_key(T::get_name()))
    }

    pub fn define_raw<F>(&self, desc: Description, create: F) -> SRes<bool>
    where
        F: Fn(Description) -> SRes<InternalDescription>,
    {
        let mut lock = self.definitions.lock()?;
        match lock.entry(desc.get_name()) {
            Entry::Occupied(x) => {
                if x.get().desc != desc {
                    return Err(StructsyError::StructAlreadyDefined(desc.get_name()));
                }
                Ok(false)
            }
            Entry::Vacant(x) => {
                let desc = create(desc)?;
                x.insert(desc);
                Ok(true)
            }
        }
    }

    pub fn define<T: Persistent, F>(&self, create: F) -> SRes<bool>
    where
        F: Fn(Description) -> SRes<InternalDescription>,
    {
        self.define_raw(T::get_description(), create)
    }

    pub fn drop_defined<T: Persistent>(&self) -> SRes<InternalDescription> {
        let desc = T::get_description();
        let mut lock = self.definitions.lock()?;
        let removed = lock.remove(&desc.get_name());
        if let Some(rem) = removed {
            Ok(rem)
        } else {
            Err(StructsyError::StructNotDefined(desc.get_name()))
        }
    }

    pub fn is_migration_started<T: Persistent>(&self) -> SRes<bool> {
        let name = T::get_name();
        let lock = self.definitions.lock()?;
        Ok(if let Some(to_check) = lock.get(name) {
            to_check.is_migration_started()
        } else {
            false
        })
    }

    pub fn start_migration<T: Persistent>(&self, st: &Arc<StructsyImpl>) -> SRes<()> {
        let name = T::get_name();
        let mut lock = self.definitions.lock()?;
        if let Some(to_change) = lock.get_mut(name) {
            to_change.start_migration();
            to_change.update(st)?;
        }

        Ok(())
    }

    pub fn finish_migration<S: Persistent, D: Persistent>(&self, st: &Arc<StructsyImpl>) -> SRes<()> {
        let name = S::get_name();
        let mut tx = st.begin()?;
        let mut lock = self.definitions.lock()?;
        if let Some(mut to_change) = lock.remove(name) {
            to_change.migrate::<D>(&mut tx)?;
            lock.insert(D::get_name().to_string(), to_change);
        }
        for (_, v) in lock.iter_mut() {
            if v.remap_refer(name, D::get_name()) {
                v.update_tx(&mut tx)?;
            }
        }
        tx.commit()?;
        Ok(())
    }

    pub fn list(&self) -> SRes<impl std::iter::Iterator<Item = Description>> {
        let values = {
            self.definitions
                .lock()?
                .values()
                .map(|v| v.desc.clone())
                .collect::<Vec<_>>()
        };
        Ok(values.into_iter())
    }

    pub(crate) fn full_definition_by_name(&self, name: &str) -> SRes<InternalDescription> {
        let lock = self.definitions.lock()?;
        if let Some(x) = lock.get(name) {
            Ok(x.clone())
        } else {
            Err(StructsyError::StructNotDefined(name.to_owned()))
        }
    }
}

pub(crate) struct StructsyImpl {
    pub(crate) persy: Persy,
    definitions: Arc<Definitions>,
}

impl StructsyImpl {
    pub fn migrate<S, D>(self: &Arc<Self>) -> SRes<()>
    where
        S: Persistent,
        D: Persistent,
        D: From<S>,
    {
        if !self.is_defined::<S>()? {
            return Ok(());
        }
        let info = self.check_defined::<S>()?;
        let migration_batches = format!("--migration-{}-{}", S::get_name(), D::get_name());
        if self.persy.exists_segment(&migration_batches)? && !self.definitions.is_migration_started::<S>()? {
            let mut tx = self.begin()?;
            tx.trans.drop_segment(&migration_batches)?;
            tx.commit()?;
        }
        if !self.persy.exists_segment(&migration_batches)? {
            let mut tx = self.begin()?;
            tx.trans.create_segment(&migration_batches)?;
            tx.commit()?;
            let batch_size = 1000;
            let batch_commit_size = batch_size * 1000;
            let mut count = 0;
            tx = self.begin()?;
            let mut mig_batch = Vec::new();
            for (id, _record) in self.scan::<S>()? {
                mig_batch.push(id);
                count += 1;
                if count % batch_size == 0 {
                    let mut buff = Vec::new();
                    PersistentEmbedded::write(&mig_batch, &mut buff)?;
                    tx.trans.insert(&migration_batches, &buff)?;
                    mig_batch.clear();
                }
                if count % batch_commit_size == 0 {
                    tx.commit()?;
                    tx = self.begin()?;
                }
            }
            if !mig_batch.is_empty() {
                let mut buff = Vec::new();
                PersistentEmbedded::write(&mig_batch, &mut buff)?;
                tx.trans.insert(&migration_batches, &buff)?;
                mig_batch.clear();
            }
            tx.commit()?;
        }
        self.definitions.start_migration::<S>(self)?;
        let mut tx = self.begin()?;
        for (batch_id, record) in self.persy.scan(&migration_batches)? {
            let batch: Vec<Ref<S>> = PersistentEmbedded::read(&mut Cursor::new(record))?;
            for id in batch {
                let mut buff = Vec::new();
                D::from(tx.read(&id)?.unwrap()).write(&mut buff)?;
                tx.trans.update(info.segment_name(), &id.raw_id, &buff)?;
            }
            tx.trans.delete(&migration_batches, &batch_id)?;
            tx.commit()?;
            tx = self.begin()?;
        }
        tx.commit()?;
        self.definitions.finish_migration::<S, D>(self)?;
        Ok(())
    }

    fn init_segment<P: AsRef<Path>>(path: P) -> SRes<()> {
        let persy = Persy::open(path, Config::new())?;
        let mut tx = persy.begin()?;
        tx.create_segment(INTERNAL_SEGMENT_NAME)?;
        tx.prepare()?.commit()?;
        Ok(())
    }

    pub fn memory() -> SRes<StructsyImpl> {
        let persy = persy::OpenOptions::new().memory()?;
        let mut tx = persy.begin()?;
        tx.create_segment(INTERNAL_SEGMENT_NAME)?;
        tx.prepare()?.commit()?;
        let definitions = HashMap::new();
        Ok(StructsyImpl {
            definitions: Arc::new(Definitions::new(definitions)),
            persy,
        })
    }

    pub fn open(config: StructsyConfig) -> SRes<StructsyImpl> {
        if config.create && !config.path.exists() {
            Persy::create(&config.path)?;
            StructsyImpl::init_segment(&config.path)?;
        }
        let persy = Persy::open(&config.path, Config::new())?;
        let definitions = persy
            .scan(INTERNAL_SEGMENT_NAME)?
            .filter_map(|(id, r)| InternalDescription::read(id, &mut Cursor::new(r)).ok())
            .map(|d| (d.desc.get_name(), d))
            .collect();
        Ok(StructsyImpl {
            definitions: Arc::new(Definitions::new(definitions)),
            persy,
        })
    }

    pub fn check_defined<T: Persistent>(&self) -> SRes<DefinitionInfo> {
        self.definitions.check_defined::<T>()
    }

    pub fn is_defined<T: Persistent>(&self) -> SRes<bool> {
        self.definitions.is_defined::<T>()
    }

    pub fn define<T: Persistent>(self: &Arc<StructsyImpl>) -> SRes<bool> {
        self.definitions
            .define::<T, _>(|desc| InternalDescription::create::<T>(desc, self))
    }

    pub fn undefine<T: Persistent>(&self) -> SRes<()> {
        let int_def = self.definitions.drop_defined::<T>()?;
        let mut tx = self.persy.begin()?;
        if let Description::Struct(s) = &int_def.desc {
            for field in &s.fields {
                if field.indexed.is_some() {
                    tx.drop_index(&format!("{}.{}", s.get_name(), field.name))?;
                }
            }
        }
        tx.delete(INTERNAL_SEGMENT_NAME, &int_def.id)?;
        tx.drop_segment(int_def.info().segment_name())?;
        tx.prepare()?.commit()?;
        Ok(())
    }

    pub fn begin(self: &Arc<Self>) -> SRes<OwnedSytx> {
        Ok(OwnedSytx {
            structsy_impl: self.clone(),
            trans: self.persy.begin()?,
        })
    }

    pub fn read<T: Persistent>(&self, sref: &Ref<T>) -> SRes<Option<T>> {
        let def = self.check_defined::<T>()?;
        if let Some(buff) = self.persy.read(def.segment_name(), &sref.raw_id)? {
            Ok(Some(T::read(&mut Cursor::new(buff))?))
        } else {
            Ok(None)
        }
    }

    pub fn commit(&self, tx: OwnedSytx) -> SRes<()> {
        let to_finalize = tx.trans.prepare()?;
        to_finalize.commit()?;
        Ok(())
    }

    pub fn scan<T: Persistent>(&self) -> SRes<RecordIter<T>> {
        let def = self.check_defined::<T>()?;
        Ok(RecordIter {
            iter: self.persy.scan(def.segment_name())?,
            marker: PhantomData,
        })
    }

    pub fn read_snapshot<T: Persistent>(&self, snap: &Snapshot, sref: &Ref<T>) -> SRes<Option<T>> {
        let def = self.check_defined::<T>()?;
        if let Some(buff) = snap.ps.read(def.segment_name(), &sref.raw_id)? {
            Ok(Some(T::read(&mut Cursor::new(buff))?))
        } else {
            Ok(None)
        }
    }

    pub fn scan_snapshot<T: Persistent>(&self, snap: &Snapshot) -> SRes<SnapshotRecordIter<T>> {
        let def = self.check_defined::<T>()?;
        Ok(SnapshotRecordIter::new(snap.ps.scan(def.segment_name())?, snap.clone()))
    }

    pub fn list_defined(&self) -> SRes<impl std::iter::Iterator<Item = Description>> {
        self.definitions.list()
    }
    pub(crate) fn full_definition_by_name(&self, name: &str) -> SRes<InternalDescription> {
        self.definitions.full_definition_by_name(name)
    }
}

impl RawRead for Structsy {
    fn raw_scan(&self, strct_name: &str) -> SRes<RawIter> {
        let definition = self.structsy_impl.definitions.full_definition_by_name(strct_name)?;
        Ok(RawIter {
            iter: self.structsy_impl.persy.scan(&definition.info().segment_name())?,
            description: definition,
        })
    }
    fn raw_read(&self, id: &str) -> SRes<Option<Record>> {
        let (ty, pid) = raw_parse(id)?;
        let definition = self.structsy_impl.definitions.full_definition_by_name(ty)?;
        let rid: PersyId = pid.parse().or(Err(StructsyError::InvalidId))?;
        let raw = self.structsy_impl.persy.read(&definition.info().segment_name(), &rid)?;
        if let Some(data) = raw {
            Ok(Some(Record::read(&mut Cursor::new(data), &definition.desc)?))
        } else {
            Ok(None)
        }
    }
}

impl RawAccess for Structsy {
    fn raw_begin(&self) -> SRes<RawTransaction> {
        Ok(RawTransaction {
            tx: self.structsy_impl.persy.begin()?,
            structsy_impl: self.structsy_impl.clone(),
        })
    }

    fn raw_define(&self, desc: Description) -> SRes<bool> {
        self.structsy_impl
            .definitions
            .define_raw::<_>(desc, |desc| InternalDescription::create_raw(desc, &self.structsy_impl))
    }
}

/// Transaction for save data in a structsy database without the original source
/// code
pub struct RawTransaction {
    tx: persy::Transaction,
    structsy_impl: Arc<StructsyImpl>,
}
impl RawTransaction {
    pub fn raw_insert(&mut self, record: &Record) -> SRes<String> {
        let type_name = record.type_name();
        let definition = self.structsy_impl.definitions.full_definition_by_name(type_name)?;
        let mut data = Vec::new();
        record.write(&mut data, &definition.desc)?;
        let id = self.tx.insert(definition.info().segment_name(), &data)?;
        record.put_indexes(&mut self.tx, &id)?;
        Ok(raw_format(type_name, &id))
    }
    pub fn raw_update(&mut self, id: &str, record: &Record) -> SRes<()> {
        let (_, pid) = raw_parse(id)?;
        let type_name = record.type_name();
        let definition = self.structsy_impl.definitions.full_definition_by_name(type_name)?;
        let ppid = pid.parse()?;
        if let Some(record) = self.raw_read(id)? {
            record.remove_indexes(&mut self.tx, &ppid)?;
        }
        let mut data = Vec::new();
        record.write(&mut data, &definition.desc)?;
        self.tx.update(definition.info().segment_name(), &ppid, &data)?;
        record.put_indexes(&mut self.tx, &ppid)?;
        Ok(())
    }
    pub fn raw_delete(&mut self, id: &str) -> SRes<()> {
        let (type_name, pid) = raw_parse(id)?;
        let definition = self.structsy_impl.definitions.full_definition_by_name(type_name)?;
        let ppid = pid.parse()?;
        if let Some(record) = self.raw_read(id)? {
            record.remove_indexes(&mut self.tx, &ppid)?;
        }
        self.tx.delete(definition.info().segment_name(), &ppid)?;
        Ok(())
    }

    pub fn raw_read(&mut self, id: &str) -> SRes<Option<Record>> {
        let (ty, pid) = raw_parse(id)?;
        let definition = self.structsy_impl.definitions.full_definition_by_name(ty)?;
        let rid: PersyId = pid.parse().or(Err(StructsyError::InvalidId))?;
        let raw = self.tx.read(&definition.info().segment_name(), &rid)?;
        if let Some(data) = raw {
            Ok(Some(Record::read(&mut Cursor::new(data), &definition.desc)?))
        } else {
            Ok(None)
        }
    }

    pub fn prepare(self) -> SRes<RawPrepare> {
        Ok(RawPrepare {
            prepared: self.tx.prepare()?,
        })
    }
}
/// Prepared state of RawTransaction
pub struct RawPrepare {
    prepared: persy::TransactionFinalize,
}
impl RawPrepare {
    pub fn commit(self) -> SRes<()> {
        Ok(self.prepared.commit()?)
    }
}

/// Iterator of raw Records
pub struct RawIter {
    iter: persy::SegmentIter,
    description: InternalDescription,
}
impl RawIter {
    pub(crate) fn new(iter: persy::SegmentIter, description: InternalDescription) -> Self {
        Self { iter, description }
    }
}
impl Iterator for RawIter {
    type Item = (String, Record);
    fn next(&mut self) -> Option<Self::Item> {
        if let Some((id, data)) = self.iter.next() {
            let fid = format!("{}@{}", self.description.desc.get_name(), id);
            let rec = Record::read(&mut Cursor::new(data), &self.description.desc).unwrap();
            Some((fid, rec))
        } else {
            None
        }
    }
}
pub(crate) fn tx_read<T: Persistent>(name: &str, tx: &mut Transaction, id: &PersyId) -> SRes<Option<T>> {
    if let Some(buff) = tx.read(name, id)? {
        Ok(Some(T::read(&mut Cursor::new(buff))?))
    } else {
        Ok(None)
    }
}

/// Iterator for record instances
pub struct RecordIter<T> {
    iter: persy::SegmentIter,
    marker: PhantomData<T>,
}

impl<T: Persistent> Iterator for RecordIter<T> {
    type Item = (Ref<T>, T);
    fn next(&mut self) -> Option<Self::Item> {
        if let Some((id, buff)) = self.iter.next() {
            if let Ok(x) = T::read(&mut Cursor::new(buff)) {
                Some((Ref::new(id), x))
            } else {
                None
            }
        } else {
            None
        }
    }
}