-
Notifications
You must be signed in to change notification settings - Fork 32
/
dag.go
202 lines (171 loc) · 4.59 KB
/
dag.go
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
// Copyright 2020 Source Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package fetcher
import (
"context"
"errors"
"sort"
"strings"
"github.com/sourcenetwork/defradb/core"
"github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
dsq "github.com/ipfs/go-datastore/query"
dshelp "github.com/ipfs/go-ipfs-ds-help"
)
// @todo: Generalize all Fetchers into an shared Fetcher utility
type BlockFetcher struct {
}
// HeadFetcher is a utility to incrementally fetch all the MerkleCRDT
// heads of a given doc/field
type HeadFetcher struct {
// Commented because this code is not used yet according to the linter.
// txn core.Txn
// key core.Key
// curSpanIndex int
spans core.Spans
cid *cid.Cid
kv *core.KeyValue
kvIter dsq.Results
kvEnd bool
}
func (hf *HeadFetcher) Start(ctx context.Context, txn core.Txn, spans core.Spans) error {
numspans := len(spans)
if numspans == 0 {
return errors.New("HeadFetcher must have at least one span")
} else if numspans > 1 {
// if we have multiple spans, we need to sort them by their start position
// so we can do a single iterative sweep
sort.Slice(spans, func(i, j int) bool {
// compare by strings if i < j.
// apply the '!= df.reverse' to reverse the sort
// if we need to
return (strings.Compare(spans[i].Start().String(), spans[j].Start().String()) < 0)
})
}
hf.spans = spans
q := dsq.Query{
Prefix: hf.spans[0].Start().String(),
Orders: []dsq.Order{dsq.OrderByKey{}},
}
var err error
if hf.kvIter != nil {
if err := hf.kvIter.Close(); err != nil {
return err
}
}
hf.kvIter, err = txn.Headstore().Query(ctx, q)
if err != nil {
return err
}
_, err = hf.nextKey()
return err
}
func (hf *HeadFetcher) nextKey() (bool, error) {
var err error
var done bool
done, hf.kv, err = hf.nextKV()
if err != nil {
return false, err
}
hf.kvEnd = done
if hf.kvEnd {
return true, nil
}
return false, nil
}
func (hf *HeadFetcher) nextKV() (iterDone bool, kv *core.KeyValue, err error) {
res, available := hf.kvIter.NextSync()
if !available {
return true, nil, nil
}
if res.Error != nil {
return true, nil, err
}
kv = &core.KeyValue{
Key: core.NewKey(res.Key),
Value: res.Value,
}
return false, kv, nil
}
func (hf *HeadFetcher) processKV(kv *core.KeyValue) error {
// convert Value from KV value to cid.Cid
headKey := ds.NewKey(strings.TrimPrefix(kv.Key.String(), hf.spans[0].Start().String()))
hash, err := dshelp.DsKeyToMultihash(headKey)
if err != nil {
return err
}
headCid := cid.NewCidV1(cid.Raw, hash)
hf.cid = &headCid
return nil
}
func (hf *HeadFetcher) FetchNext() (*cid.Cid, error) {
if hf.kvEnd {
return nil, nil
}
if hf.kv == nil {
return nil, errors.New("Failed to get head, fetcher hasn't been initialized or started")
}
if err := hf.processKV(hf.kv); err != nil {
return nil, err
}
_, err := hf.nextKey()
if err != nil {
return nil, err
}
return hf.cid, nil
}
func (hf *HeadFetcher) Close() error {
if hf.kvIter == nil {
return nil
}
return hf.kvIter.Close()
}
/*
// List returns the list of current heads plus the max height.
// @todo Document Heads.List function
func (hh *heads) List() ([]cid.Cid, uint64, error) {
q := query.Query{
Prefix: hh.namespace.String(),
KeysOnly: false,
}
results, err := hh.store.Query(q)
if err != nil {
return nil, 0, err
}
defer results.Close()
heads := make([]cid.Cid, 0)
var maxHeight uint64
for r := range results.Next() {
if r.Error != nil {
return nil, 0, fmt.Errorf("Failed to get next query result : %w", err)
}
// fmt.Println(r.Key, hh.namespace.String())
headKey := ds.NewKey(strings.TrimPrefix(r.Key, hh.namespace.String()))
headCid, err := dshelp.DsKeyToCid(headKey)
if err != nil {
return nil, 0, fmt.Errorf("Failed to get CID from key : %w", err)
}
height, n := binary.Uvarint(r.Value)
if n <= 0 {
return nil, 0, errors.New("error decocding height")
}
heads = append(heads, headCid)
if height > maxHeight {
maxHeight = height
}
}
sort.Slice(heads, func(i, j int) bool {
ci := heads[i].Bytes()
cj := heads[j].Bytes()
return bytes.Compare(ci, cj) < 0
})
return heads, maxHeight, nil
}
*/