@peerbit/native-backbone 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  use js_sys::{Array, Uint8Array};
2
+ use peerbit_log_rust::LogIndexEntry;
2
3
  use wasm_bindgen::prelude::*;
3
4
 
4
5
  use crate::append_tx::{
@@ -9,9 +10,37 @@ use crate::documents::{
9
10
  document_index_cached_projection_append_commit,
10
11
  document_index_cached_projection_plain_put_payload_append_commit, DocumentIndexAppendCommit,
11
12
  };
12
- use crate::js_interop::{optional_bytes_from_js, optional_usize_from_js, strings_to_array};
13
+ use crate::error::BackboneError;
14
+ use crate::js_interop::{
15
+ optional_bytes_from_js, optional_usize_from_js, strings_from_array, strings_to_array,
16
+ };
13
17
  use crate::NativePeerbitBackbone;
14
18
 
19
+ /// Trim rows in the exact shape the log-rust
20
+ /// `prepare_entry_v0_plain_entry_commit_[no_next_]facts_trim_and_put_with_builder`
21
+ /// wrappers returned (`log_trim_entries_to_rows`), so the re-pointed typed
22
+ /// cores keep the frozen row layout byte-for-byte.
23
+ fn log_trim_entries_to_rows(values: Vec<LogIndexEntry>) -> Array {
24
+ let out = Array::new();
25
+ for entry in values {
26
+ let row = Array::new();
27
+ row.push(&JsValue::from_str(&entry.hash));
28
+ row.push(&JsValue::from_str(&entry.gid));
29
+ row.push(&JsValue::from_str(&entry.wall_time.to_string()));
30
+ row.push(&JsValue::from_f64(entry.logical as f64));
31
+ row.push(&JsValue::from_f64(entry.entry_type as f64));
32
+ row.push(&strings_to_array(entry.next));
33
+ row.push(&JsValue::from_f64(entry.payload_size as f64));
34
+ row.push(&JsValue::from_bool(entry.head));
35
+ match entry.data {
36
+ Some(data) => row.push(&Uint8Array::from(data.as_slice())),
37
+ None => row.push(&JsValue::UNDEFINED),
38
+ };
39
+ out.push(&row);
40
+ }
41
+ out
42
+ }
43
+
15
44
  #[wasm_bindgen]
16
45
  impl NativePeerbitBackbone {
17
46
  #[allow(clippy::too_many_arguments)]
@@ -29,58 +58,79 @@ impl NativePeerbitBackbone {
29
58
  let trim_length_to = optional_usize_from_js(trim_length_to, "trimLengthTo")?;
30
59
  let has_no_next = next.length() == 0;
31
60
  match (has_no_next, trim_length_to) {
32
- (true, Some(trim_length_to)) => self
33
- .log
34
- .prepare_entry_v0_plain_entry_commit_no_next_facts_trim_and_put_with_builder(
35
- &self.builder,
36
- &mut self.blocks,
37
- wall_time,
38
- logical,
39
- gid,
40
- entry_type,
41
- meta_data,
42
- payload_data,
43
- trim_length_to,
44
- ),
45
- (true, None) => self
46
- .log
47
- .prepare_entry_v0_plain_entry_commit_no_next_facts_and_put_with_builder(
48
- &self.builder,
49
- &mut self.blocks,
50
- wall_time,
51
- logical,
52
- gid,
53
- entry_type,
54
- meta_data,
55
- payload_data,
56
- ),
57
- (false, Some(trim_length_to)) => self
58
- .log
59
- .prepare_entry_v0_plain_entry_commit_facts_trim_and_put_with_builder(
60
- &self.builder,
61
- &mut self.blocks,
62
- wall_time,
63
- logical,
64
- gid,
65
- next,
66
- entry_type,
67
- meta_data,
68
- payload_data,
69
- trim_length_to,
70
- ),
71
- (false, None) => self
72
- .log
73
- .prepare_entry_v0_plain_entry_commit_facts_and_put_with_builder(
74
- &self.builder,
75
- &mut self.blocks,
76
- wall_time,
77
- logical,
78
- gid,
79
- next,
80
- entry_type,
81
- meta_data,
82
- payload_data,
83
- ),
61
+ (true, Some(trim_length_to)) => {
62
+ let (entry_facts, trimmed_entries) = self
63
+ .log
64
+ .prepare_entry_v0_plain_entry_commit_no_next_facts_core_profiled_and_put_with_builder_trim(
65
+ &self.builder,
66
+ &mut self.blocks,
67
+ wall_time,
68
+ logical,
69
+ gid,
70
+ entry_type,
71
+ optional_bytes_from_js(meta_data, "meta data")?,
72
+ payload_data.to_vec(),
73
+ trim_length_to,
74
+ None,
75
+ )?;
76
+ let out = Array::new();
77
+ out.push(&committed_entry_facts_to_row(&entry_facts, false));
78
+ out.push(&log_trim_entries_to_rows(trimmed_entries));
79
+ Ok(out)
80
+ }
81
+ (true, None) => {
82
+ let entry_facts = self
83
+ .log
84
+ .prepare_entry_v0_plain_entry_commit_no_next_facts_core_profiled_and_put_with_builder(
85
+ &self.builder,
86
+ &mut self.blocks,
87
+ wall_time,
88
+ logical,
89
+ gid,
90
+ entry_type,
91
+ optional_bytes_from_js(meta_data, "meta data")?,
92
+ payload_data.to_vec(),
93
+ None,
94
+ )?;
95
+ Ok(committed_entry_facts_to_row(&entry_facts, false))
96
+ }
97
+ (false, Some(trim_length_to)) => {
98
+ let (entry_facts, trimmed_entries) = self
99
+ .log
100
+ .prepare_entry_v0_plain_entry_commit_facts_core_and_put_with_builder(
101
+ &self.builder,
102
+ &mut self.blocks,
103
+ wall_time,
104
+ logical,
105
+ gid,
106
+ strings_from_array(next)?,
107
+ entry_type,
108
+ optional_bytes_from_js(meta_data, "meta data")?,
109
+ payload_data.to_vec(),
110
+ Some(trim_length_to),
111
+ )?;
112
+ let out = Array::new();
113
+ out.push(&committed_entry_facts_to_row(&entry_facts, true));
114
+ out.push(&log_trim_entries_to_rows(trimmed_entries));
115
+ Ok(out)
116
+ }
117
+ (false, None) => {
118
+ let (entry_facts, _trimmed_entries) = self
119
+ .log
120
+ .prepare_entry_v0_plain_entry_commit_facts_core_and_put_with_builder(
121
+ &self.builder,
122
+ &mut self.blocks,
123
+ wall_time,
124
+ logical,
125
+ gid,
126
+ strings_from_array(next)?,
127
+ entry_type,
128
+ optional_bytes_from_js(meta_data, "meta data")?,
129
+ payload_data.to_vec(),
130
+ None,
131
+ )?;
132
+ Ok(committed_entry_facts_to_row(&entry_facts, true))
133
+ }
84
134
  }
85
135
  }
86
136
 
@@ -216,16 +266,18 @@ impl NativePeerbitBackbone {
216
266
  document_projection_encoded_document,
217
267
  document_projection_signer,
218
268
  )?;
219
- self.prepare_plain_entry_commit_no_next_document_index_compact(
220
- wall_time,
221
- logical,
222
- gid,
223
- entry_type,
224
- meta_data,
225
- payload_data,
226
- document_gid,
227
- payload_size,
228
- document_index_commit,
269
+ Ok(
270
+ self.prepare_plain_entry_commit_no_next_document_index_compact(
271
+ wall_time,
272
+ logical,
273
+ gid,
274
+ entry_type,
275
+ meta_data,
276
+ payload_data,
277
+ document_gid,
278
+ payload_size,
279
+ document_index_commit,
280
+ )?,
229
281
  )
230
282
  }
231
283
 
@@ -257,16 +309,18 @@ impl NativePeerbitBackbone {
257
309
  document_projection_encoded_document,
258
310
  document_projection_signer,
259
311
  )?;
260
- self.prepare_plain_entry_commit_no_next_document_index_compact(
261
- wall_time,
262
- logical,
263
- gid,
264
- entry_type,
265
- meta_data,
266
- payload_data,
267
- document_gid,
268
- payload_size,
269
- document_index_commit,
312
+ Ok(
313
+ self.prepare_plain_entry_commit_no_next_document_index_compact(
314
+ wall_time,
315
+ logical,
316
+ gid,
317
+ entry_type,
318
+ meta_data,
319
+ payload_data,
320
+ document_gid,
321
+ payload_size,
322
+ document_index_commit,
323
+ )?,
270
324
  )
271
325
  }
272
326
 
@@ -307,7 +361,7 @@ impl NativePeerbitBackbone {
307
361
  logical,
308
362
  gid,
309
363
  entry_type,
310
- optional_bytes_from_js(meta_data),
364
+ optional_bytes_from_js(meta_data, "meta data")?,
311
365
  &payload_data,
312
366
  None,
313
367
  )?;
@@ -353,18 +407,20 @@ impl NativePeerbitBackbone {
353
407
  document_projection_encoded_document,
354
408
  document_projection_signer,
355
409
  )?;
356
- self.prepare_plain_entry_commit_no_next_document_index_compact_trim_hashes(
357
- wall_time,
358
- logical,
359
- gid,
360
- entry_type,
361
- meta_data,
362
- payload_data,
363
- trim_length_to,
364
- document_gid,
365
- payload_size,
366
- document_index_commit,
367
- false,
410
+ Ok(
411
+ self.prepare_plain_entry_commit_no_next_document_index_compact_trim_hashes(
412
+ wall_time,
413
+ logical,
414
+ gid,
415
+ entry_type,
416
+ meta_data,
417
+ payload_data,
418
+ trim_length_to,
419
+ document_gid,
420
+ payload_size,
421
+ document_index_commit,
422
+ false,
423
+ )?,
368
424
  )
369
425
  }
370
426
 
@@ -399,18 +455,20 @@ impl NativePeerbitBackbone {
399
455
  document_projection_encoded_document,
400
456
  document_projection_signer,
401
457
  )?;
402
- self.prepare_plain_entry_commit_no_next_document_index_compact_trim_hashes(
403
- wall_time,
404
- logical,
405
- gid,
406
- entry_type,
407
- meta_data,
408
- payload_data,
409
- trim_length_to,
410
- document_gid,
411
- payload_size,
412
- document_index_commit,
413
- true,
458
+ Ok(
459
+ self.prepare_plain_entry_commit_no_next_document_index_compact_trim_hashes(
460
+ wall_time,
461
+ logical,
462
+ gid,
463
+ entry_type,
464
+ meta_data,
465
+ payload_data,
466
+ trim_length_to,
467
+ document_gid,
468
+ payload_size,
469
+ document_index_commit,
470
+ true,
471
+ )?,
414
472
  )
415
473
  }
416
474
 
@@ -443,18 +501,20 @@ impl NativePeerbitBackbone {
443
501
  document_projection_encoded_document,
444
502
  document_projection_signer,
445
503
  )?;
446
- self.prepare_plain_entry_commit_no_next_document_index_compact_trim_hashes(
447
- wall_time,
448
- logical,
449
- gid,
450
- entry_type,
451
- meta_data,
452
- payload_data,
453
- trim_length_to,
454
- document_gid,
455
- payload_size,
456
- document_index_commit,
457
- false,
504
+ Ok(
505
+ self.prepare_plain_entry_commit_no_next_document_index_compact_trim_hashes(
506
+ wall_time,
507
+ logical,
508
+ gid,
509
+ entry_type,
510
+ meta_data,
511
+ payload_data,
512
+ trim_length_to,
513
+ document_gid,
514
+ payload_size,
515
+ document_index_commit,
516
+ false,
517
+ )?,
458
518
  )
459
519
  }
460
520
 
@@ -486,15 +546,17 @@ impl NativePeerbitBackbone {
486
546
  document_projection_encoded_document,
487
547
  document_projection_signer,
488
548
  )?;
489
- self.prepare_plain_entry_commit_latest_document_index_trim_hashes_inner(
490
- wall_time,
491
- logical,
492
- fallback_gid,
493
- entry_type,
494
- meta_data,
495
- payload_data,
496
- trim_length_to,
497
- document_index_commit,
549
+ Ok(
550
+ self.prepare_plain_entry_commit_latest_document_index_trim_hashes_inner(
551
+ wall_time,
552
+ logical,
553
+ fallback_gid,
554
+ entry_type,
555
+ meta_data,
556
+ payload_data,
557
+ trim_length_to,
558
+ document_index_commit,
559
+ )?,
498
560
  )
499
561
  }
500
562
 
@@ -524,15 +586,17 @@ impl NativePeerbitBackbone {
524
586
  document_projection_encoded_document,
525
587
  document_projection_signer,
526
588
  )?;
527
- self.prepare_plain_entry_commit_latest_document_index_trim_hashes_inner(
528
- wall_time,
529
- logical,
530
- fallback_gid,
531
- entry_type,
532
- meta_data,
533
- payload_data,
534
- trim_length_to,
535
- document_index_commit,
589
+ Ok(
590
+ self.prepare_plain_entry_commit_latest_document_index_trim_hashes_inner(
591
+ wall_time,
592
+ logical,
593
+ fallback_gid,
594
+ entry_type,
595
+ meta_data,
596
+ payload_data,
597
+ trim_length_to,
598
+ document_index_commit,
599
+ )?,
536
600
  )
537
601
  }
538
602
 
@@ -565,18 +629,20 @@ impl NativePeerbitBackbone {
565
629
  document_projection_encoded_document,
566
630
  document_projection_signer,
567
631
  )?;
568
- self.prepare_plain_entry_commit_no_next_document_index_compact_trim_hashes(
569
- wall_time,
570
- logical,
571
- gid,
572
- entry_type,
573
- meta_data,
574
- payload_data,
575
- trim_length_to,
576
- document_gid,
577
- payload_size,
578
- document_index_commit,
579
- true,
632
+ Ok(
633
+ self.prepare_plain_entry_commit_no_next_document_index_compact_trim_hashes(
634
+ wall_time,
635
+ logical,
636
+ gid,
637
+ entry_type,
638
+ meta_data,
639
+ payload_data,
640
+ trim_length_to,
641
+ document_gid,
642
+ payload_size,
643
+ document_index_commit,
644
+ true,
645
+ )?,
580
646
  )
581
647
  }
582
648
 
@@ -619,7 +685,7 @@ impl NativePeerbitBackbone {
619
685
  logical,
620
686
  gid,
621
687
  entry_type,
622
- optional_bytes_from_js(meta_data),
688
+ optional_bytes_from_js(meta_data, "meta data")?,
623
689
  &payload_data,
624
690
  trim_length_to,
625
691
  None,
@@ -655,7 +721,7 @@ impl NativePeerbitBackbone {
655
721
  document_gid: String,
656
722
  payload_size: u32,
657
723
  document_index_commit: DocumentIndexAppendCommit,
658
- ) -> Result<Array, JsValue> {
724
+ ) -> Result<Array, BackboneError> {
659
725
  let entry_facts = self
660
726
  .log
661
727
  .prepare_entry_v0_plain_entry_commit_no_next_facts_core_profiled_and_put_with_builder(
@@ -665,7 +731,7 @@ impl NativePeerbitBackbone {
665
731
  logical,
666
732
  gid,
667
733
  entry_type,
668
- optional_bytes_from_js(meta_data),
734
+ optional_bytes_from_js(meta_data, "meta data")?,
669
735
  payload_data.to_vec(),
670
736
  None,
671
737
  )?;
@@ -693,7 +759,7 @@ impl NativePeerbitBackbone {
693
759
  payload_size: u32,
694
760
  document_index_commit: DocumentIndexAppendCommit,
695
761
  compact_row: bool,
696
- ) -> Result<Array, JsValue> {
762
+ ) -> Result<Array, BackboneError> {
697
763
  let delete_trimmed_document_heads = document_index_commit.delete_trimmed_heads;
698
764
  let (entry_facts, trim_hashes) = self
699
765
  .log
@@ -704,7 +770,7 @@ impl NativePeerbitBackbone {
704
770
  logical,
705
771
  gid,
706
772
  entry_type,
707
- optional_bytes_from_js(meta_data),
773
+ optional_bytes_from_js(meta_data, "meta data")?,
708
774
  payload_data.to_vec(),
709
775
  trim_length_to,
710
776
  None,
@@ -743,7 +809,7 @@ impl NativePeerbitBackbone {
743
809
  payload_data: Uint8Array,
744
810
  trim_length_to: JsValue,
745
811
  mut document_index_commit: DocumentIndexAppendCommit,
746
- ) -> Result<Array, JsValue> {
812
+ ) -> Result<Array, BackboneError> {
747
813
  let trim_length_to = optional_usize_from_js(trim_length_to, "trimLengthTo")?;
748
814
  let (previous_context, gid, next_hashes) =
749
815
  self.resolve_latest_document_append_context(&mut document_index_commit, fallback_gid)?;
@@ -759,7 +825,7 @@ impl NativePeerbitBackbone {
759
825
  gid.clone(),
760
826
  next_hashes,
761
827
  entry_type,
762
- optional_bytes_from_js(meta_data),
828
+ optional_bytes_from_js(meta_data, "meta data")?,
763
829
  payload_data.to_vec(),
764
830
  trim_length_to,
765
831
  None,