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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
use std::sync::Arc;
use futures::StreamExt;
use futures::TryStreamExt;
use crate::clone::CloneData;
use crate::default_impl;
use crate::errors::NotFoundError;
use crate::id::Group;
use crate::id::Id;
use crate::id::VertexName;
pub use crate::iddag::IdDagAlgorithm;
use crate::namedag::MemNameDag;
use crate::nameset::id_lazy::IdLazySet;
use crate::nameset::id_static::IdStaticSet;
use crate::nameset::NameSet;
use crate::IdSet;
use crate::Result;
use crate::VerLink;
use crate::VertexListWithOptions;
#[async_trait::async_trait]
pub trait DagAlgorithm: Send + Sync {
async fn sort(&self, set: &NameSet) -> Result<NameSet>;
async fn beautify(&self, main_branch: Option<NameSet>) -> Result<MemNameDag> {
default_impl::beautify(self, main_branch).await
}
async fn subdag(&self, set: NameSet) -> Result<MemNameDag> {
default_impl::subdag(self, set).await
}
async fn parent_names(&self, name: VertexName) -> Result<Vec<VertexName>>;
async fn all(&self) -> Result<NameSet>;
async fn master_group(&self) -> Result<NameSet>;
async fn ancestors(&self, set: NameSet) -> Result<NameSet>;
async fn parents(&self, set: NameSet) -> Result<NameSet> {
default_impl::parents(self, set).await
}
async fn first_ancestor_nth(&self, name: VertexName, n: u64) -> Result<Option<VertexName>> {
default_impl::first_ancestor_nth(self, name, n).await
}
async fn first_ancestors(&self, set: NameSet) -> Result<NameSet> {
default_impl::first_ancestors(self, set).await
}
async fn heads(&self, set: NameSet) -> Result<NameSet> {
default_impl::heads(self, set).await
}
async fn children(&self, set: NameSet) -> Result<NameSet>;
async fn roots(&self, set: NameSet) -> Result<NameSet> {
default_impl::roots(self, set).await
}
async fn merges(&self, set: NameSet) -> Result<NameSet> {
default_impl::merges(self, set).await
}
async fn gca_one(&self, set: NameSet) -> Result<Option<VertexName>> {
default_impl::gca_one(self, set).await
}
async fn gca_all(&self, set: NameSet) -> Result<NameSet> {
default_impl::gca_all(self, set).await
}
async fn common_ancestors(&self, set: NameSet) -> Result<NameSet> {
default_impl::common_ancestors(self, set).await
}
async fn is_ancestor(&self, ancestor: VertexName, descendant: VertexName) -> Result<bool> {
default_impl::is_ancestor(self, ancestor, descendant).await
}
async fn heads_ancestors(&self, set: NameSet) -> Result<NameSet> {
default_impl::heads_ancestors(self, set).await
}
async fn range(&self, roots: NameSet, heads: NameSet) -> Result<NameSet>;
async fn only(&self, reachable: NameSet, unreachable: NameSet) -> Result<NameSet> {
default_impl::only(self, reachable, unreachable).await
}
async fn only_both(
&self,
reachable: NameSet,
unreachable: NameSet,
) -> Result<(NameSet, NameSet)> {
default_impl::only_both(self, reachable, unreachable).await
}
async fn descendants(&self, set: NameSet) -> Result<NameSet>;
async fn reachable_roots(&self, roots: NameSet, heads: NameSet) -> Result<NameSet> {
default_impl::reachable_roots(self, roots, heads).await
}
async fn dirty(&self) -> Result<NameSet>;
fn is_vertex_lazy(&self) -> bool;
fn dag_snapshot(&self) -> Result<Arc<dyn DagAlgorithm + Send + Sync>>;
fn id_dag_snapshot(&self) -> Result<Arc<dyn IdDagAlgorithm + Send + Sync>> {
Err(crate::errors::BackendError::Generic(format!(
"id_dag_snapshot() is not supported for {}",
std::any::type_name::<Self>()
))
.into())
}
fn dag_id(&self) -> &str;
fn dag_version(&self) -> &VerLink;
}
#[async_trait::async_trait]
pub trait Parents: Send + Sync {
async fn parent_names(&self, name: VertexName) -> Result<Vec<VertexName>>;
async fn hint_subdag_for_insertion(&self, _heads: &[VertexName]) -> Result<MemNameDag>;
}
#[async_trait::async_trait]
impl Parents for Arc<dyn DagAlgorithm + Send + Sync> {
async fn parent_names(&self, name: VertexName) -> Result<Vec<VertexName>> {
DagAlgorithm::parent_names(self, name).await
}
async fn hint_subdag_for_insertion(&self, heads: &[VertexName]) -> Result<MemNameDag> {
let scope = self.dirty().await?;
default_impl::hint_subdag_for_insertion(self, &scope, heads).await
}
}
#[async_trait::async_trait]
impl Parents for &(dyn DagAlgorithm + Send + Sync) {
async fn parent_names(&self, name: VertexName) -> Result<Vec<VertexName>> {
DagAlgorithm::parent_names(*self, name).await
}
async fn hint_subdag_for_insertion(&self, heads: &[VertexName]) -> Result<MemNameDag> {
let scope = self.dirty().await?;
default_impl::hint_subdag_for_insertion(self, &scope, heads).await
}
}
#[async_trait::async_trait]
impl<'a> Parents for Box<dyn Fn(VertexName) -> Result<Vec<VertexName>> + Send + Sync + 'a> {
async fn parent_names(&self, name: VertexName) -> Result<Vec<VertexName>> {
(self)(name)
}
async fn hint_subdag_for_insertion(&self, _heads: &[VertexName]) -> Result<MemNameDag> {
Ok(MemNameDag::new())
}
}
#[async_trait::async_trait]
impl Parents for std::collections::HashMap<VertexName, Vec<VertexName>> {
async fn parent_names(&self, name: VertexName) -> Result<Vec<VertexName>> {
match self.get(&name) {
Some(v) => Ok(v.clone()),
None => name.not_found(),
}
}
async fn hint_subdag_for_insertion(&self, heads: &[VertexName]) -> Result<MemNameDag> {
let mut keys: Vec<VertexName> = self.keys().cloned().collect();
keys.sort_unstable();
let scope = NameSet::from_static_names(keys);
default_impl::hint_subdag_for_insertion(self, &scope, heads).await
}
}
#[async_trait::async_trait]
pub trait DagAddHeads {
async fn add_heads(
&mut self,
parents: &dyn Parents,
heads: &VertexListWithOptions,
) -> Result<bool>;
}
#[async_trait::async_trait]
pub trait DagStrip {
async fn strip(&mut self, set: &NameSet) -> Result<()>;
}
#[async_trait::async_trait]
pub trait DagImportCloneData {
async fn import_clone_data(&mut self, clone_data: CloneData<VertexName>) -> Result<()>;
}
#[async_trait::async_trait]
pub trait DagImportPullData {
async fn import_pull_data(
&mut self,
clone_data: CloneData<VertexName>,
heads: &VertexListWithOptions,
) -> Result<()>;
}
#[async_trait::async_trait]
pub trait DagExportCloneData {
async fn export_clone_data(&self) -> Result<CloneData<VertexName>>;
}
#[async_trait::async_trait]
pub trait DagExportPullData {
async fn export_pull_data(&self, set: &NameSet) -> Result<CloneData<VertexName>>;
}
#[async_trait::async_trait]
pub trait DagPersistent {
async fn flush(&mut self, master_heads: &VertexListWithOptions) -> Result<()>;
async fn flush_cached_idmap(&self) -> Result<()>;
async fn add_heads_and_flush(
&mut self,
parent_names_func: &dyn Parents,
heads: &VertexListWithOptions,
) -> Result<()>;
async fn import_and_flush(
&mut self,
dag: &dyn DagAlgorithm,
master_heads: NameSet,
) -> Result<()> {
let heads = dag.heads(dag.all().await?).await?;
let non_master_heads = heads - master_heads.clone();
let master_heads: Vec<VertexName> =
master_heads.iter().await?.try_collect::<Vec<_>>().await?;
let non_master_heads: Vec<VertexName> = non_master_heads
.iter()
.await?
.try_collect::<Vec<_>>()
.await?;
let heads = VertexListWithOptions::from(master_heads)
.with_highest_group(Group::MASTER)
.chain(non_master_heads);
self.add_heads_and_flush(&dag.dag_snapshot()?, &heads).await
}
}
pub trait ImportAscii {
fn import_ascii_with_heads(
&mut self,
text: &str,
heads: Option<&[impl AsRef<str>]>,
) -> Result<()>;
fn import_ascii(&mut self, text: &str) -> Result<()> {
self.import_ascii_with_heads(text, <Option<&[&str]>>::None)
}
}
#[async_trait::async_trait]
pub trait PrefixLookup {
async fn vertexes_by_hex_prefix(
&self,
hex_prefix: &[u8],
limit: usize,
) -> Result<Vec<VertexName>>;
}
#[async_trait::async_trait]
pub trait IdConvert: PrefixLookup + Sync {
async fn vertex_id(&self, name: VertexName) -> Result<Id>;
async fn vertex_id_with_max_group(
&self,
name: &VertexName,
max_group: Group,
) -> Result<Option<Id>>;
async fn vertex_name(&self, id: Id) -> Result<VertexName>;
async fn contains_vertex_name(&self, name: &VertexName) -> Result<bool>;
async fn contains_vertex_id_locally(&self, id: &[Id]) -> Result<Vec<bool>>;
async fn contains_vertex_name_locally(&self, name: &[VertexName]) -> Result<Vec<bool>>;
async fn vertex_id_optional(&self, name: &VertexName) -> Result<Option<Id>> {
self.vertex_id_with_max_group(name, Group::NON_MASTER).await
}
async fn vertex_name_batch(&self, ids: &[Id]) -> Result<Vec<Result<VertexName>>> {
let mut names = Vec::with_capacity(ids.len());
for &id in ids {
names.push(self.vertex_name(id).await);
}
Ok(names)
}
async fn vertex_id_batch(&self, names: &[VertexName]) -> Result<Vec<Result<Id>>> {
let mut ids = Vec::with_capacity(names.len());
for name in names {
ids.push(self.vertex_id(name.clone()).await);
}
Ok(ids)
}
fn map_id(&self) -> &str;
fn map_version(&self) -> &VerLink;
}
#[async_trait::async_trait]
pub trait CheckIntegrity {
async fn check_universal_ids(&self) -> Result<Vec<Id>>;
async fn check_segments(&self) -> Result<Vec<String>>;
async fn check_isomorphic_graph(
&self,
other: &dyn DagAlgorithm,
heads: NameSet,
) -> Result<Vec<String>>;
}
impl<T> ImportAscii for T
where
T: DagAddHeads,
{
fn import_ascii_with_heads(
&mut self,
text: &str,
heads: Option<&[impl AsRef<str>]>,
) -> Result<()> {
let parents = drawdag::parse(&text);
let heads: Vec<_> = match heads {
Some(heads) => heads
.iter()
.map(|s| VertexName::copy_from(s.as_ref().as_bytes()))
.collect(),
None => {
let mut heads: Vec<_> = parents
.keys()
.map(|s| VertexName::copy_from(s.as_bytes()))
.collect();
heads.sort();
heads
}
};
let v = |s: String| VertexName::copy_from(s.as_bytes());
let parents: std::collections::HashMap<VertexName, Vec<VertexName>> = parents
.into_iter()
.map(|(k, vs)| (v(k), vs.into_iter().map(v).collect()))
.collect();
nonblocking::non_blocking_result(self.add_heads(&parents, &heads[..].into()))?;
Ok(())
}
}
#[async_trait::async_trait]
pub trait ToIdSet {
async fn to_id_set(&self, set: &NameSet) -> Result<IdSet>;
}
pub trait ToSet {
fn to_set(&self, set: &IdSet) -> Result<NameSet>;
}
pub trait IdMapSnapshot {
fn id_map_snapshot(&self) -> Result<Arc<dyn IdConvert + Send + Sync>>;
}
pub trait Persist {
type Lock: Send + Sync;
fn lock(&mut self) -> Result<Self::Lock>;
fn reload(&mut self, _lock: &Self::Lock) -> Result<()>;
fn persist(&mut self, _lock: &Self::Lock) -> Result<()>;
}
pub trait Open: Clone {
type OpenTarget;
fn open(&self) -> Result<Self::OpenTarget>;
}
pub trait IntVersion {
fn int_version(&self) -> (u64, u64);
}
pub trait TryClone {
fn try_clone(&self) -> Result<Self>
where
Self: Sized;
}
impl<T: Clone> TryClone for T {
fn try_clone(&self) -> Result<Self> {
Ok(self.clone())
}
}
#[async_trait::async_trait]
impl<T: IdConvert + IdMapSnapshot> ToIdSet for T {
async fn to_id_set(&self, set: &NameSet) -> Result<IdSet> {
let version = set.hints().id_map_version();
if let Some(set) = set.as_any().downcast_ref::<IdStaticSet>() {
if None < version && version <= Some(self.map_version()) {
return Ok(set.spans.clone());
}
}
if let Some(set) = set.as_any().downcast_ref::<IdLazySet>() {
if None < version && version <= Some(self.map_version()) {
let set: IdStaticSet = set.to_static()?;
return Ok(set.spans);
}
}
let mut spans = IdSet::empty();
let mut iter = set.iter().await?.chunks(1 << 17);
while let Some(names) = iter.next().await {
let names = names.into_iter().collect::<Result<Vec<_>>>()?;
let ids = self.vertex_id_batch(&names).await?;
for id in ids {
spans.push(id?);
}
}
Ok(spans)
}
}
impl IdMapSnapshot for Arc<dyn IdConvert + Send + Sync> {
fn id_map_snapshot(&self) -> Result<Arc<dyn IdConvert + Send + Sync>> {
Ok(self.clone())
}
}
impl<T: IdMapSnapshot + DagAlgorithm> ToSet for T {
fn to_set(&self, set: &IdSet) -> Result<NameSet> {
NameSet::from_spans_dag(set.clone(), self)
}
}