@peerbit/native-backbone 0.1.0

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.
@@ -0,0 +1,1165 @@
1
+ use js_sys::{Array, BigUint64Array, Uint32Array, Uint8Array};
2
+ use peerbit_log_rust::NativeLogAppendProfile;
3
+ use peerbit_shared_log_rust::{
4
+ commit_local_append_for_gid_compact_core, NativeLocalAppendCompactInput,
5
+ };
6
+ use wasm_bindgen::prelude::*;
7
+
8
+ use crate::append_tx::{
9
+ ensure_batch_append_lens, ensure_batch_projection_lens, no_next_compact_entry_row,
10
+ push_optional_trim_result, required_projection_encoded_document,
11
+ LatestCompactBatchPendingAppend,
12
+ };
13
+ use crate::documents::{
14
+ document_index_append_commit, document_index_cached_projection_append_commit,
15
+ document_index_cached_projection_plain_put_payload_append_commit,
16
+ document_index_plain_put_payload_append_commit, DocumentIndexAppendCommit,
17
+ DocumentIndexValuePrefix,
18
+ };
19
+ use crate::js_interop::{
20
+ ensure_same_len, optional_usize_from_js, required_bytes_from_array, strings_from_array,
21
+ strings_to_array,
22
+ };
23
+ use crate::NativePeerbitBackbone;
24
+
25
+ #[wasm_bindgen]
26
+ impl NativePeerbitBackbone {
27
+ #[allow(clippy::too_many_arguments)]
28
+ pub fn prepare_plain_committed_no_next_storage_append_transaction(
29
+ &mut self,
30
+ wall_time: u64,
31
+ logical: u32,
32
+ gid: String,
33
+ entry_type: u8,
34
+ meta_data: JsValue,
35
+ payload_data: Uint8Array,
36
+ replicas: usize,
37
+ role_age_ms: f64,
38
+ now: String,
39
+ self_hash: String,
40
+ self_replicating: bool,
41
+ resolve_trimmed_entries: bool,
42
+ ) -> Result<Array, JsValue> {
43
+ self.prepare_plain_storage_append_transaction_inner(
44
+ wall_time,
45
+ logical,
46
+ gid,
47
+ Vec::new(),
48
+ entry_type,
49
+ meta_data,
50
+ payload_data,
51
+ replicas,
52
+ role_age_ms,
53
+ now,
54
+ self_hash,
55
+ self_replicating,
56
+ resolve_trimmed_entries,
57
+ None,
58
+ true,
59
+ None,
60
+ )
61
+ }
62
+
63
+ #[allow(clippy::too_many_arguments)]
64
+ pub fn prepare_plain_committed_no_next_storage_append_transaction_trim(
65
+ &mut self,
66
+ wall_time: u64,
67
+ logical: u32,
68
+ gid: String,
69
+ entry_type: u8,
70
+ meta_data: JsValue,
71
+ payload_data: Uint8Array,
72
+ replicas: usize,
73
+ role_age_ms: f64,
74
+ now: String,
75
+ self_hash: String,
76
+ self_replicating: bool,
77
+ resolve_trimmed_entries: bool,
78
+ trim_length_to: usize,
79
+ ) -> Result<Array, JsValue> {
80
+ self.prepare_plain_storage_append_transaction_inner(
81
+ wall_time,
82
+ logical,
83
+ gid,
84
+ Vec::new(),
85
+ entry_type,
86
+ meta_data,
87
+ payload_data,
88
+ replicas,
89
+ role_age_ms,
90
+ now,
91
+ self_hash,
92
+ self_replicating,
93
+ resolve_trimmed_entries,
94
+ Some(trim_length_to),
95
+ true,
96
+ None,
97
+ )
98
+ }
99
+
100
+ #[allow(clippy::too_many_arguments)]
101
+ pub fn benchmark_plain_committed_no_next_storage_append_transaction_loop(
102
+ &mut self,
103
+ iterations: u32,
104
+ wall_time_start: u64,
105
+ payload_data: Uint8Array,
106
+ replicas: usize,
107
+ self_hash: String,
108
+ use_document_index: bool,
109
+ document_byte_element_index_limit: usize,
110
+ trim_length_to: JsValue,
111
+ ) -> Result<Array, JsValue> {
112
+ let trim_length_to = optional_usize_from_js(trim_length_to, "trimLengthTo")?;
113
+ let profile_enabled = self.append_profile_enabled;
114
+ let input_copy_started = profile_enabled.then(js_sys::Date::now);
115
+ let payload_template = payload_data.to_vec();
116
+ if let Some(started) = input_copy_started {
117
+ self.append_profile.input_copy_ms += js_sys::Date::now() - started;
118
+ }
119
+ let payload_size = payload_template.len() as u32;
120
+ let now = wall_time_start.to_string();
121
+ let started = js_sys::Date::now();
122
+ for i in 0..iterations {
123
+ let wall_time = wall_time_start + i as u64;
124
+ let logical = i;
125
+ let gid = format!("native-backbone-loop-{wall_time_start}-{i}");
126
+ let storage_append_started = profile_enabled.then(js_sys::Date::now);
127
+ let payload_copy_started = profile_enabled.then(js_sys::Date::now);
128
+ let payload_data = payload_template.clone();
129
+ if let Some(started) = payload_copy_started {
130
+ self.append_profile.input_copy_ms += js_sys::Date::now() - started;
131
+ }
132
+
133
+ let log_started = profile_enabled.then(js_sys::Date::now);
134
+ let mut log_profile = NativeLogAppendProfile::default();
135
+ let (entry_facts, trim_hashes) = self
136
+ .log
137
+ .prepare_entry_v0_plain_entry_commit_facts_core_profiled_and_put_with_builder_trim_hashes(
138
+ &self.builder,
139
+ &mut self.blocks,
140
+ wall_time,
141
+ logical,
142
+ gid.clone(),
143
+ Vec::new(),
144
+ 0,
145
+ None,
146
+ payload_data,
147
+ trim_length_to,
148
+ profile_enabled.then_some(&mut log_profile),
149
+ )?;
150
+ if let Some(started) = log_started {
151
+ self.append_profile.log_total_ms += js_sys::Date::now() - started;
152
+ self.append_profile.add_log_profile(&log_profile);
153
+ }
154
+
155
+ let hash_number = self.hash_number_profiled(&entry_facts.hash_digest_bytes)?;
156
+
157
+ let coordinate_plan_started = profile_enabled.then(js_sys::Date::now);
158
+ let coordinate_facts = commit_local_append_for_gid_compact_core(
159
+ &mut self.shared_log,
160
+ entry_facts.hash.clone(),
161
+ gid.clone(),
162
+ hash_number,
163
+ &[],
164
+ &trim_hashes,
165
+ replicas,
166
+ 0.0,
167
+ &now,
168
+ &self_hash,
169
+ true,
170
+ true,
171
+ true,
172
+ )?;
173
+ if let Some(started) = coordinate_plan_started {
174
+ self.append_profile.coordinate_plan_ms += js_sys::Date::now() - started;
175
+ }
176
+
177
+ let coordinate_core_started = profile_enabled.then(js_sys::Date::now);
178
+ self.commit_coordinate_core_from_compact_facts(
179
+ &coordinate_facts,
180
+ &[],
181
+ &trim_hashes,
182
+ wall_time,
183
+ entry_facts.meta_bytes.clone(),
184
+ );
185
+ if let Some(started) = coordinate_core_started {
186
+ self.append_profile.coordinate_core_ms += js_sys::Date::now() - started;
187
+ }
188
+
189
+ let document_index_started = profile_enabled.then(js_sys::Date::now);
190
+ let document_index_commit = use_document_index.then(|| DocumentIndexAppendCommit {
191
+ key: format!("native-backbone-loop-doc-{wall_time_start}-{i}"),
192
+ value_prefix: DocumentIndexValuePrefix::Bytes(Vec::new()),
193
+ existing_created: None,
194
+ byte_element_index_limit: document_byte_element_index_limit,
195
+ delete_trimmed_heads: false,
196
+ previous_context: None,
197
+ known_existing: false,
198
+ required_previous_signer_public_key: None,
199
+ });
200
+ self.put_document_index_for_append(
201
+ document_index_commit,
202
+ wall_time,
203
+ &entry_facts.hash,
204
+ &gid,
205
+ payload_size,
206
+ )?;
207
+ if let Some(started) = document_index_started {
208
+ self.append_profile.document_index_commit_ms += js_sys::Date::now() - started;
209
+ }
210
+
211
+ if let Some(started) = storage_append_started {
212
+ self.append_profile.storage_append_inner_ms += js_sys::Date::now() - started;
213
+ }
214
+ }
215
+ let row = Array::new();
216
+ row.push(&JsValue::from_f64(js_sys::Date::now() - started));
217
+ row.push(&JsValue::from_f64(self.log.len() as f64));
218
+ row.push(&JsValue::from_f64(self.blocks.len() as f64));
219
+ row.push(&JsValue::from_f64(self.coordinate_index.len() as f64));
220
+ row.push(&JsValue::from_f64(self.document_index.len() as f64));
221
+ Ok(row)
222
+ }
223
+
224
+ #[allow(clippy::too_many_arguments)]
225
+ pub fn prepare_plain_committed_no_next_storage_append_document_index_transaction(
226
+ &mut self,
227
+ wall_time: u64,
228
+ logical: u32,
229
+ gid: String,
230
+ entry_type: u8,
231
+ meta_data: JsValue,
232
+ payload_data: Uint8Array,
233
+ replicas: usize,
234
+ role_age_ms: f64,
235
+ now: String,
236
+ self_hash: String,
237
+ self_replicating: bool,
238
+ resolve_trimmed_entries: bool,
239
+ document_key: String,
240
+ document_value_prefix_bytes: Vec<u8>,
241
+ document_existing_created: String,
242
+ document_byte_element_index_limit: usize,
243
+ document_delete_trimmed_heads: bool,
244
+ document_projection_plan: JsValue,
245
+ document_projection_encoded_document: JsValue,
246
+ document_projection_signer: JsValue,
247
+ ) -> Result<Array, JsValue> {
248
+ self.prepare_plain_storage_append_transaction_inner(
249
+ wall_time,
250
+ logical,
251
+ gid,
252
+ Vec::new(),
253
+ entry_type,
254
+ meta_data,
255
+ payload_data,
256
+ replicas,
257
+ role_age_ms,
258
+ now,
259
+ self_hash,
260
+ self_replicating,
261
+ resolve_trimmed_entries,
262
+ None,
263
+ true,
264
+ Some(document_index_append_commit(
265
+ document_key,
266
+ document_value_prefix_bytes,
267
+ document_existing_created,
268
+ document_byte_element_index_limit,
269
+ document_delete_trimmed_heads,
270
+ document_projection_plan,
271
+ document_projection_encoded_document,
272
+ document_projection_signer,
273
+ )?),
274
+ )
275
+ }
276
+
277
+ #[allow(clippy::too_many_arguments)]
278
+ pub fn prepare_plain_committed_no_next_storage_append_document_index_transaction_trim(
279
+ &mut self,
280
+ wall_time: u64,
281
+ logical: u32,
282
+ gid: String,
283
+ entry_type: u8,
284
+ meta_data: JsValue,
285
+ payload_data: Uint8Array,
286
+ replicas: usize,
287
+ role_age_ms: f64,
288
+ now: String,
289
+ self_hash: String,
290
+ self_replicating: bool,
291
+ resolve_trimmed_entries: bool,
292
+ document_key: String,
293
+ document_value_prefix_bytes: Vec<u8>,
294
+ document_existing_created: String,
295
+ document_byte_element_index_limit: usize,
296
+ document_delete_trimmed_heads: bool,
297
+ document_projection_plan: JsValue,
298
+ document_projection_encoded_document: JsValue,
299
+ document_projection_signer: JsValue,
300
+ trim_length_to: usize,
301
+ ) -> Result<Array, JsValue> {
302
+ self.prepare_plain_storage_append_transaction_inner(
303
+ wall_time,
304
+ logical,
305
+ gid,
306
+ Vec::new(),
307
+ entry_type,
308
+ meta_data,
309
+ payload_data,
310
+ replicas,
311
+ role_age_ms,
312
+ now,
313
+ self_hash,
314
+ self_replicating,
315
+ resolve_trimmed_entries,
316
+ Some(trim_length_to),
317
+ true,
318
+ Some(document_index_append_commit(
319
+ document_key,
320
+ document_value_prefix_bytes,
321
+ document_existing_created,
322
+ document_byte_element_index_limit,
323
+ document_delete_trimmed_heads,
324
+ document_projection_plan,
325
+ document_projection_encoded_document,
326
+ document_projection_signer,
327
+ )?),
328
+ )
329
+ }
330
+
331
+ #[allow(clippy::too_many_arguments)]
332
+ pub fn prepare_plain_committed_no_next_storage_append_document_index_cached_plan_transaction(
333
+ &mut self,
334
+ wall_time: u64,
335
+ logical: u32,
336
+ gid: String,
337
+ entry_type: u8,
338
+ meta_data: JsValue,
339
+ payload_data: Uint8Array,
340
+ replicas: usize,
341
+ role_age_ms: f64,
342
+ now: String,
343
+ self_hash: String,
344
+ self_replicating: bool,
345
+ resolve_trimmed_entries: bool,
346
+ document_key: String,
347
+ document_existing_created: String,
348
+ document_byte_element_index_limit: usize,
349
+ document_delete_trimmed_heads: bool,
350
+ document_projection_plan_id: u32,
351
+ document_projection_encoded_document: JsValue,
352
+ document_projection_signer: JsValue,
353
+ ) -> Result<Array, JsValue> {
354
+ self.prepare_plain_storage_append_transaction_inner(
355
+ wall_time,
356
+ logical,
357
+ gid,
358
+ Vec::new(),
359
+ entry_type,
360
+ meta_data,
361
+ payload_data,
362
+ replicas,
363
+ role_age_ms,
364
+ now,
365
+ self_hash,
366
+ self_replicating,
367
+ resolve_trimmed_entries,
368
+ None,
369
+ true,
370
+ Some(document_index_cached_projection_append_commit(
371
+ document_key,
372
+ document_existing_created,
373
+ document_byte_element_index_limit,
374
+ document_delete_trimmed_heads,
375
+ document_projection_plan_id,
376
+ document_projection_encoded_document,
377
+ document_projection_signer,
378
+ )?),
379
+ )
380
+ }
381
+
382
+ #[allow(clippy::too_many_arguments)]
383
+ pub fn prepare_plain_committed_no_next_storage_append_document_index_cached_plan_transaction_trim(
384
+ &mut self,
385
+ wall_time: u64,
386
+ logical: u32,
387
+ gid: String,
388
+ entry_type: u8,
389
+ meta_data: JsValue,
390
+ payload_data: Uint8Array,
391
+ replicas: usize,
392
+ role_age_ms: f64,
393
+ now: String,
394
+ self_hash: String,
395
+ self_replicating: bool,
396
+ resolve_trimmed_entries: bool,
397
+ document_key: String,
398
+ document_existing_created: String,
399
+ document_byte_element_index_limit: usize,
400
+ document_delete_trimmed_heads: bool,
401
+ document_projection_plan_id: u32,
402
+ document_projection_encoded_document: JsValue,
403
+ document_projection_signer: JsValue,
404
+ trim_length_to: usize,
405
+ ) -> Result<Array, JsValue> {
406
+ self.prepare_plain_storage_append_transaction_inner(
407
+ wall_time,
408
+ logical,
409
+ gid,
410
+ Vec::new(),
411
+ entry_type,
412
+ meta_data,
413
+ payload_data,
414
+ replicas,
415
+ role_age_ms,
416
+ now,
417
+ self_hash,
418
+ self_replicating,
419
+ resolve_trimmed_entries,
420
+ Some(trim_length_to),
421
+ true,
422
+ Some(document_index_cached_projection_append_commit(
423
+ document_key,
424
+ document_existing_created,
425
+ document_byte_element_index_limit,
426
+ document_delete_trimmed_heads,
427
+ document_projection_plan_id,
428
+ document_projection_encoded_document,
429
+ document_projection_signer,
430
+ )?),
431
+ )
432
+ }
433
+
434
+ #[allow(clippy::too_many_arguments)]
435
+ pub fn prepare_plain_committed_no_next_storage_append_document_index_compact_transaction(
436
+ &mut self,
437
+ wall_time: u64,
438
+ logical: u32,
439
+ gid: String,
440
+ entry_type: u8,
441
+ meta_data: JsValue,
442
+ payload_data: Uint8Array,
443
+ replicas: usize,
444
+ role_age_ms: f64,
445
+ now: String,
446
+ self_hash: String,
447
+ self_replicating: bool,
448
+ document_key: String,
449
+ document_value_prefix_bytes: Vec<u8>,
450
+ document_existing_created: String,
451
+ document_byte_element_index_limit: usize,
452
+ document_delete_trimmed_heads: bool,
453
+ trim_length_to: JsValue,
454
+ ) -> Result<Array, JsValue> {
455
+ let trim_length_to = optional_usize_from_js(trim_length_to, "trimLengthTo")?;
456
+ let document_index_commit = document_index_append_commit(
457
+ document_key,
458
+ document_value_prefix_bytes,
459
+ document_existing_created,
460
+ document_byte_element_index_limit,
461
+ document_delete_trimmed_heads,
462
+ JsValue::UNDEFINED,
463
+ JsValue::UNDEFINED,
464
+ JsValue::UNDEFINED,
465
+ )?;
466
+ self.prepare_plain_committed_no_next_storage_append_document_index_compact_transaction_inner(
467
+ wall_time,
468
+ logical,
469
+ gid,
470
+ entry_type,
471
+ meta_data,
472
+ payload_data,
473
+ replicas,
474
+ role_age_ms,
475
+ now,
476
+ self_hash,
477
+ self_replicating,
478
+ document_index_commit,
479
+ trim_length_to,
480
+ )
481
+ }
482
+
483
+ #[allow(clippy::too_many_arguments)]
484
+ pub fn prepare_plain_committed_no_next_storage_append_document_index_compact_plain_put_payload_transaction(
485
+ &mut self,
486
+ wall_time: u64,
487
+ logical: u32,
488
+ gid: String,
489
+ entry_type: u8,
490
+ meta_data: JsValue,
491
+ payload_data: Uint8Array,
492
+ replicas: usize,
493
+ role_age_ms: f64,
494
+ now: String,
495
+ self_hash: String,
496
+ self_replicating: bool,
497
+ document_key: String,
498
+ document_existing_created: String,
499
+ document_byte_element_index_limit: usize,
500
+ document_delete_trimmed_heads: bool,
501
+ trim_length_to: JsValue,
502
+ ) -> Result<Array, JsValue> {
503
+ let trim_length_to = optional_usize_from_js(trim_length_to, "trimLengthTo")?;
504
+ let document_index_commit = document_index_plain_put_payload_append_commit(
505
+ document_key,
506
+ document_existing_created,
507
+ document_byte_element_index_limit,
508
+ document_delete_trimmed_heads,
509
+ )?;
510
+ self.prepare_plain_committed_no_next_storage_append_document_index_compact_transaction_inner(
511
+ wall_time,
512
+ logical,
513
+ gid,
514
+ entry_type,
515
+ meta_data,
516
+ payload_data,
517
+ replicas,
518
+ role_age_ms,
519
+ now,
520
+ self_hash,
521
+ self_replicating,
522
+ document_index_commit,
523
+ trim_length_to,
524
+ )
525
+ }
526
+
527
+ #[allow(clippy::too_many_arguments)]
528
+ pub fn prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction(
529
+ &mut self,
530
+ wall_times: BigUint64Array,
531
+ logicals: Uint32Array,
532
+ gids: Array,
533
+ entry_type: u8,
534
+ meta_datas: Array,
535
+ payload_datas: Array,
536
+ replicas: usize,
537
+ role_age_ms: f64,
538
+ now: String,
539
+ self_hash: String,
540
+ self_replicating: bool,
541
+ document_keys: Array,
542
+ document_value_prefix_bytes: Array,
543
+ document_existing_created: Array,
544
+ document_byte_element_index_limit: usize,
545
+ document_delete_trimmed_heads: bool,
546
+ trim_length_to: JsValue,
547
+ ) -> Result<Array, JsValue> {
548
+ let len = payload_datas.length() as usize;
549
+ ensure_batch_append_lens(
550
+ len,
551
+ &wall_times,
552
+ &logicals,
553
+ &gids,
554
+ "batch gids",
555
+ &meta_datas,
556
+ &document_keys,
557
+ )?;
558
+ ensure_same_len(
559
+ len,
560
+ document_value_prefix_bytes.length() as usize,
561
+ "batch document value prefixes",
562
+ )?;
563
+ ensure_same_len(
564
+ len,
565
+ document_existing_created.length() as usize,
566
+ "batch document existing-created values",
567
+ )?;
568
+ let trim_length_to = optional_usize_from_js(trim_length_to, "trimLengthTo")?;
569
+ let document_keys = strings_from_array(document_keys)?;
570
+ let document_existing_created = strings_from_array(document_existing_created)?;
571
+ let mut document_index_commits = Vec::with_capacity(len);
572
+ for (index, document_key) in document_keys.iter().enumerate() {
573
+ let index_u32 = index as u32;
574
+ document_index_commits.push(document_index_append_commit(
575
+ document_key.clone(),
576
+ required_bytes_from_array(
577
+ &document_value_prefix_bytes,
578
+ index_u32,
579
+ "document value prefix",
580
+ )?
581
+ .to_vec(),
582
+ document_existing_created[index].clone(),
583
+ document_byte_element_index_limit,
584
+ document_delete_trimmed_heads,
585
+ JsValue::UNDEFINED,
586
+ JsValue::UNDEFINED,
587
+ JsValue::UNDEFINED,
588
+ )?);
589
+ }
590
+ self.prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction_inner(
591
+ wall_times,
592
+ logicals,
593
+ gids,
594
+ entry_type,
595
+ meta_datas,
596
+ payload_datas,
597
+ replicas,
598
+ role_age_ms,
599
+ now,
600
+ self_hash,
601
+ self_replicating,
602
+ trim_length_to,
603
+ document_index_commits,
604
+ )
605
+ }
606
+
607
+ #[allow(clippy::too_many_arguments)]
608
+ pub fn prepare_plain_committed_no_next_storage_append_document_index_compact_plain_put_payload_batch_transaction(
609
+ &mut self,
610
+ wall_times: BigUint64Array,
611
+ logicals: Uint32Array,
612
+ gids: Array,
613
+ entry_type: u8,
614
+ meta_datas: Array,
615
+ payload_datas: Array,
616
+ replicas: usize,
617
+ role_age_ms: f64,
618
+ now: String,
619
+ self_hash: String,
620
+ self_replicating: bool,
621
+ document_keys: Array,
622
+ document_existing_created: Array,
623
+ document_byte_element_index_limit: usize,
624
+ document_delete_trimmed_heads: bool,
625
+ trim_length_to: JsValue,
626
+ ) -> Result<Array, JsValue> {
627
+ let len = payload_datas.length() as usize;
628
+ ensure_batch_append_lens(
629
+ len,
630
+ &wall_times,
631
+ &logicals,
632
+ &gids,
633
+ "batch gids",
634
+ &meta_datas,
635
+ &document_keys,
636
+ )?;
637
+ ensure_same_len(
638
+ len,
639
+ document_existing_created.length() as usize,
640
+ "batch document existing-created values",
641
+ )?;
642
+ let trim_length_to = optional_usize_from_js(trim_length_to, "trimLengthTo")?;
643
+ let document_keys = strings_from_array(document_keys)?;
644
+ let document_existing_created = strings_from_array(document_existing_created)?;
645
+ let mut document_index_commits = Vec::with_capacity(len);
646
+ for (index, document_key) in document_keys.iter().enumerate() {
647
+ document_index_commits.push(document_index_plain_put_payload_append_commit(
648
+ document_key.clone(),
649
+ document_existing_created[index].clone(),
650
+ document_byte_element_index_limit,
651
+ document_delete_trimmed_heads,
652
+ )?);
653
+ }
654
+ self.prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction_inner(
655
+ wall_times,
656
+ logicals,
657
+ gids,
658
+ entry_type,
659
+ meta_datas,
660
+ payload_datas,
661
+ replicas,
662
+ role_age_ms,
663
+ now,
664
+ self_hash,
665
+ self_replicating,
666
+ trim_length_to,
667
+ document_index_commits,
668
+ )
669
+ }
670
+
671
+ #[allow(clippy::too_many_arguments)]
672
+ pub fn prepare_plain_committed_no_next_storage_append_document_index_cached_plan_compact_batch_transaction(
673
+ &mut self,
674
+ wall_times: BigUint64Array,
675
+ logicals: Uint32Array,
676
+ gids: Array,
677
+ entry_type: u8,
678
+ meta_datas: Array,
679
+ payload_datas: Array,
680
+ replicas: usize,
681
+ role_age_ms: f64,
682
+ now: String,
683
+ self_hash: String,
684
+ self_replicating: bool,
685
+ document_keys: Array,
686
+ document_existing_created: Array,
687
+ document_byte_element_index_limit: usize,
688
+ document_delete_trimmed_heads: bool,
689
+ document_projection_plan_ids: Uint32Array,
690
+ document_projection_encoded_documents: Array,
691
+ document_projection_signers: Array,
692
+ trim_length_to: JsValue,
693
+ ) -> Result<Array, JsValue> {
694
+ let len = payload_datas.length() as usize;
695
+ ensure_batch_append_lens(
696
+ len,
697
+ &wall_times,
698
+ &logicals,
699
+ &gids,
700
+ "batch gids",
701
+ &meta_datas,
702
+ &document_keys,
703
+ )?;
704
+ ensure_same_len(
705
+ len,
706
+ document_existing_created.length() as usize,
707
+ "batch document existing-created values",
708
+ )?;
709
+ ensure_batch_projection_lens(
710
+ len,
711
+ &document_projection_plan_ids,
712
+ Some(&document_projection_encoded_documents),
713
+ &document_projection_signers,
714
+ )?;
715
+ let trim_length_to = optional_usize_from_js(trim_length_to, "trimLengthTo")?;
716
+ let document_keys = strings_from_array(document_keys)?;
717
+ let document_existing_created = strings_from_array(document_existing_created)?;
718
+ let mut document_index_commits = Vec::with_capacity(len);
719
+ for (index, document_key) in document_keys.iter().enumerate() {
720
+ let index_u32 = index as u32;
721
+ let encoded_document = required_projection_encoded_document(
722
+ &document_projection_encoded_documents,
723
+ index_u32,
724
+ )?;
725
+ document_index_commits.push(document_index_cached_projection_append_commit(
726
+ document_key.clone(),
727
+ document_existing_created[index].clone(),
728
+ document_byte_element_index_limit,
729
+ document_delete_trimmed_heads,
730
+ document_projection_plan_ids.get_index(index_u32),
731
+ encoded_document,
732
+ document_projection_signers.get(index_u32),
733
+ )?);
734
+ }
735
+ self.prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction_inner(
736
+ wall_times,
737
+ logicals,
738
+ gids,
739
+ entry_type,
740
+ meta_datas,
741
+ payload_datas,
742
+ replicas,
743
+ role_age_ms,
744
+ now,
745
+ self_hash,
746
+ self_replicating,
747
+ trim_length_to,
748
+ document_index_commits,
749
+ )
750
+ }
751
+
752
+ #[allow(clippy::too_many_arguments)]
753
+ pub fn prepare_plain_committed_no_next_storage_append_document_index_cached_plan_compact_plain_put_payload_batch_transaction(
754
+ &mut self,
755
+ wall_times: BigUint64Array,
756
+ logicals: Uint32Array,
757
+ gids: Array,
758
+ entry_type: u8,
759
+ meta_datas: Array,
760
+ payload_datas: Array,
761
+ replicas: usize,
762
+ role_age_ms: f64,
763
+ now: String,
764
+ self_hash: String,
765
+ self_replicating: bool,
766
+ document_keys: Array,
767
+ document_existing_created: Array,
768
+ document_byte_element_index_limit: usize,
769
+ document_delete_trimmed_heads: bool,
770
+ document_projection_plan_ids: Uint32Array,
771
+ document_projection_signers: Array,
772
+ trim_length_to: JsValue,
773
+ ) -> Result<Array, JsValue> {
774
+ let len = payload_datas.length() as usize;
775
+ ensure_batch_append_lens(
776
+ len,
777
+ &wall_times,
778
+ &logicals,
779
+ &gids,
780
+ "batch gids",
781
+ &meta_datas,
782
+ &document_keys,
783
+ )?;
784
+ ensure_same_len(
785
+ len,
786
+ document_existing_created.length() as usize,
787
+ "batch document existing-created values",
788
+ )?;
789
+ ensure_batch_projection_lens(
790
+ len,
791
+ &document_projection_plan_ids,
792
+ None,
793
+ &document_projection_signers,
794
+ )?;
795
+ let trim_length_to = optional_usize_from_js(trim_length_to, "trimLengthTo")?;
796
+ let document_keys = strings_from_array(document_keys)?;
797
+ let document_existing_created = strings_from_array(document_existing_created)?;
798
+ let mut document_index_commits = Vec::with_capacity(len);
799
+ for (index, document_key) in document_keys.iter().enumerate() {
800
+ let index_u32 = index as u32;
801
+ document_index_commits.push(
802
+ document_index_cached_projection_plain_put_payload_append_commit(
803
+ document_key.clone(),
804
+ document_existing_created[index].clone(),
805
+ document_byte_element_index_limit,
806
+ document_delete_trimmed_heads,
807
+ document_projection_plan_ids.get_index(index_u32),
808
+ document_projection_signers.get(index_u32),
809
+ )?,
810
+ );
811
+ }
812
+ self.prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction_inner(
813
+ wall_times,
814
+ logicals,
815
+ gids,
816
+ entry_type,
817
+ meta_datas,
818
+ payload_datas,
819
+ replicas,
820
+ role_age_ms,
821
+ now,
822
+ self_hash,
823
+ self_replicating,
824
+ trim_length_to,
825
+ document_index_commits,
826
+ )
827
+ }
828
+
829
+ #[allow(clippy::too_many_arguments)]
830
+ pub fn prepare_plain_committed_no_next_storage_append_document_index_cached_plan_compact_transaction(
831
+ &mut self,
832
+ wall_time: u64,
833
+ logical: u32,
834
+ gid: String,
835
+ entry_type: u8,
836
+ meta_data: JsValue,
837
+ payload_data: Uint8Array,
838
+ replicas: usize,
839
+ role_age_ms: f64,
840
+ now: String,
841
+ self_hash: String,
842
+ self_replicating: bool,
843
+ document_key: String,
844
+ document_existing_created: String,
845
+ document_byte_element_index_limit: usize,
846
+ document_delete_trimmed_heads: bool,
847
+ document_projection_plan_id: u32,
848
+ document_projection_encoded_document: JsValue,
849
+ document_projection_signer: JsValue,
850
+ trim_length_to: JsValue,
851
+ ) -> Result<Array, JsValue> {
852
+ let trim_length_to = optional_usize_from_js(trim_length_to, "trimLengthTo")?;
853
+ let document_index_commit = document_index_cached_projection_append_commit(
854
+ document_key,
855
+ document_existing_created,
856
+ document_byte_element_index_limit,
857
+ document_delete_trimmed_heads,
858
+ document_projection_plan_id,
859
+ document_projection_encoded_document,
860
+ document_projection_signer,
861
+ )?;
862
+ self.prepare_plain_committed_no_next_storage_append_document_index_compact_transaction_inner(
863
+ wall_time,
864
+ logical,
865
+ gid,
866
+ entry_type,
867
+ meta_data,
868
+ payload_data,
869
+ replicas,
870
+ role_age_ms,
871
+ now,
872
+ self_hash,
873
+ self_replicating,
874
+ document_index_commit,
875
+ trim_length_to,
876
+ )
877
+ }
878
+
879
+ #[allow(clippy::too_many_arguments)]
880
+ pub fn prepare_plain_committed_no_next_storage_append_document_index_cached_plan_compact_plain_put_payload_transaction(
881
+ &mut self,
882
+ wall_time: u64,
883
+ logical: u32,
884
+ gid: String,
885
+ entry_type: u8,
886
+ meta_data: JsValue,
887
+ payload_data: Uint8Array,
888
+ replicas: usize,
889
+ role_age_ms: f64,
890
+ now: String,
891
+ self_hash: String,
892
+ self_replicating: bool,
893
+ document_key: String,
894
+ document_existing_created: String,
895
+ document_byte_element_index_limit: usize,
896
+ document_delete_trimmed_heads: bool,
897
+ document_projection_plan_id: u32,
898
+ document_projection_signer: JsValue,
899
+ trim_length_to: JsValue,
900
+ ) -> Result<Array, JsValue> {
901
+ let trim_length_to = optional_usize_from_js(trim_length_to, "trimLengthTo")?;
902
+ let document_index_commit =
903
+ document_index_cached_projection_plain_put_payload_append_commit(
904
+ document_key,
905
+ document_existing_created,
906
+ document_byte_element_index_limit,
907
+ document_delete_trimmed_heads,
908
+ document_projection_plan_id,
909
+ document_projection_signer,
910
+ )?;
911
+ self.prepare_plain_committed_no_next_storage_append_document_index_compact_transaction_inner(
912
+ wall_time,
913
+ logical,
914
+ gid,
915
+ entry_type,
916
+ meta_data,
917
+ payload_data,
918
+ replicas,
919
+ role_age_ms,
920
+ now,
921
+ self_hash,
922
+ self_replicating,
923
+ document_index_commit,
924
+ trim_length_to,
925
+ )
926
+ }
927
+ }
928
+
929
+ impl NativePeerbitBackbone {
930
+ #[allow(clippy::too_many_arguments)]
931
+ fn prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction_inner(
932
+ &mut self,
933
+ wall_times: BigUint64Array,
934
+ logicals: Uint32Array,
935
+ gids: Array,
936
+ entry_type: u8,
937
+ meta_datas: Array,
938
+ payload_datas: Array,
939
+ replicas: usize,
940
+ role_age_ms: f64,
941
+ now: String,
942
+ self_hash: String,
943
+ self_replicating: bool,
944
+ trim_length_to: Option<usize>,
945
+ document_index_commits: Vec<DocumentIndexAppendCommit>,
946
+ ) -> Result<Array, JsValue> {
947
+ let profile_enabled = self.append_profile_enabled;
948
+ let storage_append_started = profile_enabled.then(js_sys::Date::now);
949
+ let batch_len = document_index_commits.len();
950
+ self.reserve_document_batch(batch_len);
951
+ let mut pending_appends = Vec::with_capacity(batch_len);
952
+ let mut coordinate_inputs = Vec::with_capacity(batch_len);
953
+
954
+ for (index, document_index_commit) in document_index_commits.into_iter().enumerate() {
955
+ let index_u32 = index as u32;
956
+ let gid = gids
957
+ .get(index_u32)
958
+ .as_string()
959
+ .ok_or_else(|| JsValue::from_str("Expected batch gid string"))?;
960
+ let payload_bytes = required_bytes_from_array(&payload_datas, index_u32, "payload")?;
961
+ let payload_size = payload_bytes.length();
962
+ let delete_trimmed_document_heads = document_index_commit.delete_trimmed_heads;
963
+
964
+ let (meta_data, payload_data) =
965
+ self.copy_append_inputs_profiled(meta_datas.get(index_u32), &payload_bytes);
966
+
967
+ let (entry_facts, trim_hashes) = self.prepare_no_next_log_append_profiled(
968
+ wall_times.get_index(index_u32),
969
+ logicals.get_index(index_u32),
970
+ gid.clone(),
971
+ entry_type,
972
+ meta_data,
973
+ &payload_data,
974
+ trim_length_to,
975
+ )?;
976
+
977
+ let hash_number = self.hash_number_profiled(&entry_facts.hash_digest_bytes)?;
978
+ let materialization_bytes = trim_length_to
979
+ .is_some()
980
+ .then(|| self.blocks.get_ref(&entry_facts.hash).map(Uint8Array::from))
981
+ .flatten();
982
+
983
+ coordinate_inputs.push(NativeLocalAppendCompactInput {
984
+ entry_hash: entry_facts.hash.clone(),
985
+ gid: gid.clone(),
986
+ entry_hash_number: hash_number,
987
+ next_hashes: Vec::new(),
988
+ delete_hashes: trim_hashes.clone(),
989
+ });
990
+ let plain_put_payload_data = matches!(
991
+ &document_index_commit.value_prefix,
992
+ DocumentIndexValuePrefix::PlainPutPayloadIdentity
993
+ | DocumentIndexValuePrefix::PlainPutPayloadProjection { .. }
994
+ )
995
+ .then_some(payload_data);
996
+ pending_appends.push(LatestCompactBatchPendingAppend {
997
+ wall_time: wall_times.get_index(index_u32),
998
+ payload_size,
999
+ gid,
1000
+ entry_facts,
1001
+ trim_hashes,
1002
+ document_index_commit,
1003
+ previous_document_context: None,
1004
+ delete_trimmed_document_heads,
1005
+ plain_put_payload_data,
1006
+ materialization_bytes,
1007
+ });
1008
+ }
1009
+
1010
+ let coordinate_facts = self.plan_batch_compact_coordinates_profiled(
1011
+ coordinate_inputs,
1012
+ replicas,
1013
+ role_age_ms,
1014
+ &now,
1015
+ &self_hash,
1016
+ self_replicating,
1017
+ pending_appends.len(),
1018
+ "Native no-next compact batch returned mismatched coordinate facts",
1019
+ )?;
1020
+
1021
+ let out = Array::new();
1022
+ for (pending, coordinate_facts) in pending_appends.into_iter().zip(coordinate_facts) {
1023
+ let coordinate_core_started = profile_enabled.then(js_sys::Date::now);
1024
+ self.commit_coordinate_core_from_compact_facts(
1025
+ &coordinate_facts,
1026
+ &[],
1027
+ &pending.trim_hashes,
1028
+ pending.wall_time,
1029
+ pending.entry_facts.meta_bytes.clone(),
1030
+ );
1031
+ if let Some(started) = coordinate_core_started {
1032
+ self.append_profile.coordinate_core_ms += js_sys::Date::now() - started;
1033
+ }
1034
+
1035
+ let document_trimmed_heads_processed = self.commit_append_document_index_profiled(
1036
+ Some(pending.document_index_commit),
1037
+ pending.wall_time,
1038
+ &pending.entry_facts.hash,
1039
+ &pending.gid,
1040
+ pending.payload_size,
1041
+ pending.plain_put_payload_data.as_deref(),
1042
+ pending.delete_trimmed_document_heads,
1043
+ &pending.trim_hashes,
1044
+ )?;
1045
+
1046
+ let result_row_started = profile_enabled.then(js_sys::Date::now);
1047
+ let row = no_next_compact_entry_row(
1048
+ &self.resolution,
1049
+ &pending.entry_facts,
1050
+ &coordinate_facts,
1051
+ );
1052
+ if let Some(bytes) = pending.materialization_bytes.as_ref() {
1053
+ row.push(&strings_to_array(pending.trim_hashes));
1054
+ row.push(&JsValue::from_bool(document_trimmed_heads_processed));
1055
+ row.push(bytes);
1056
+ } else {
1057
+ push_optional_trim_result(
1058
+ &row,
1059
+ pending.trim_hashes,
1060
+ document_trimmed_heads_processed,
1061
+ );
1062
+ }
1063
+ out.push(&row);
1064
+ if let Some(started) = result_row_started {
1065
+ self.append_profile.result_row_ms += js_sys::Date::now() - started;
1066
+ }
1067
+ }
1068
+
1069
+ if let Some(started) = storage_append_started {
1070
+ self.append_profile.storage_append_inner_ms += js_sys::Date::now() - started;
1071
+ }
1072
+ Ok(out)
1073
+ }
1074
+
1075
+ #[allow(clippy::too_many_arguments)]
1076
+ fn prepare_plain_committed_no_next_storage_append_document_index_compact_transaction_inner(
1077
+ &mut self,
1078
+ wall_time: u64,
1079
+ logical: u32,
1080
+ gid: String,
1081
+ entry_type: u8,
1082
+ meta_data: JsValue,
1083
+ payload_data: Uint8Array,
1084
+ replicas: usize,
1085
+ role_age_ms: f64,
1086
+ now: String,
1087
+ self_hash: String,
1088
+ self_replicating: bool,
1089
+ document_index_commit: DocumentIndexAppendCommit,
1090
+ trim_length_to: Option<usize>,
1091
+ ) -> Result<Array, JsValue> {
1092
+ let profile_enabled = self.append_profile_enabled;
1093
+ let storage_append_started = profile_enabled.then(js_sys::Date::now);
1094
+ let payload_size = payload_data.length();
1095
+ let delete_trimmed_document_heads = document_index_commit.delete_trimmed_heads;
1096
+
1097
+ let (meta_data, payload_data) = self.copy_append_inputs_profiled(meta_data, &payload_data);
1098
+
1099
+ let (entry_facts, trim_hashes) = self.prepare_no_next_log_append_profiled(
1100
+ wall_time,
1101
+ logical,
1102
+ gid.clone(),
1103
+ entry_type,
1104
+ meta_data,
1105
+ &payload_data,
1106
+ trim_length_to,
1107
+ )?;
1108
+
1109
+ let hash_number = self.hash_number_profiled(&entry_facts.hash_digest_bytes)?;
1110
+
1111
+ let coordinate_plan_started = profile_enabled.then(js_sys::Date::now);
1112
+ let coordinate_facts = commit_local_append_for_gid_compact_core(
1113
+ &mut self.shared_log,
1114
+ entry_facts.hash.clone(),
1115
+ gid.clone(),
1116
+ hash_number,
1117
+ &[],
1118
+ &trim_hashes,
1119
+ replicas,
1120
+ role_age_ms,
1121
+ &now,
1122
+ &self_hash,
1123
+ self_replicating,
1124
+ true,
1125
+ true,
1126
+ )?;
1127
+ if let Some(started) = coordinate_plan_started {
1128
+ self.append_profile.coordinate_plan_ms += js_sys::Date::now() - started;
1129
+ }
1130
+
1131
+ let coordinate_core_started = profile_enabled.then(js_sys::Date::now);
1132
+ self.commit_coordinate_core_from_compact_facts(
1133
+ &coordinate_facts,
1134
+ &[],
1135
+ &trim_hashes,
1136
+ wall_time,
1137
+ entry_facts.meta_bytes.clone(),
1138
+ );
1139
+ if let Some(started) = coordinate_core_started {
1140
+ self.append_profile.coordinate_core_ms += js_sys::Date::now() - started;
1141
+ }
1142
+
1143
+ let document_trimmed_heads_processed = self.commit_append_document_index_profiled(
1144
+ Some(document_index_commit),
1145
+ wall_time,
1146
+ &entry_facts.hash,
1147
+ &gid,
1148
+ payload_size,
1149
+ Some(&payload_data),
1150
+ delete_trimmed_document_heads,
1151
+ &trim_hashes,
1152
+ )?;
1153
+
1154
+ let result_row_started = profile_enabled.then(js_sys::Date::now);
1155
+ let out = no_next_compact_entry_row(&self.resolution, &entry_facts, &coordinate_facts);
1156
+ push_optional_trim_result(&out, trim_hashes, document_trimmed_heads_processed);
1157
+ if let Some(started) = result_row_started {
1158
+ self.append_profile.result_row_ms += js_sys::Date::now() - started;
1159
+ }
1160
+ if let Some(started) = storage_append_started {
1161
+ self.append_profile.storage_append_inner_ms += js_sys::Date::now() - started;
1162
+ }
1163
+ Ok(out)
1164
+ }
1165
+ }