forked from berty/go-ipfs-log
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_io.go
More file actions
237 lines (190 loc) · 5.33 KB
/
log_io.go
File metadata and controls
237 lines (190 loc) · 5.33 KB
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
package ipfslog // import "berty.tech/go-ipfs-log"
import (
"context"
"errors"
"time"
"berty.tech/go-ipfs-log/entry/sorting"
"berty.tech/go-ipfs-log/entry"
"berty.tech/go-ipfs-log/errmsg"
"berty.tech/go-ipfs-log/io"
"github.com/ipfs/go-cid"
cbornode "github.com/ipfs/go-ipld-cbor"
)
type FetchOptions struct {
Length *int
Exclude []*entry.Entry
ProgressChan chan *entry.Entry
Timeout time.Duration
}
func toMultihash(ctx context.Context, services io.IpfsServices, log *Log) (cid.Cid, error) {
if log.Values().Len() < 1 {
return cid.Cid{}, errors.New(`can't serialize an empty log`)
}
return io.WriteCBOR(ctx, services, log.ToJSON())
}
func fromMultihash(ctx context.Context, services io.IpfsServices, hash cid.Cid, options *FetchOptions) (*Snapshot, error) {
result, err := io.ReadCBOR(ctx, services, hash)
if err != nil {
return nil, err
}
logData := &JSONLog{}
err = cbornode.DecodeInto(result.RawData(), logData)
if err != nil {
return nil, err
}
entries := entry.FetchAll(ctx, services, logData.Heads, &entry.FetchOptions{
Length: options.Length,
Exclude: options.Exclude,
ProgressChan: options.ProgressChan,
})
// Find latest clock
var clock *entry.LamportClock
for _, e := range entries {
if clock == nil || e.Clock.Time > clock.Time {
clock = entry.NewLamportClock(e.Clock.ID, e.Clock.Time)
}
}
sorting.Sort(sorting.Compare, entries)
heads := []*entry.Entry{}
for _, e := range entries {
for _, h := range logData.Heads {
if h.String() == e.Hash.String() {
heads = append(heads, e)
}
}
}
headsCids := []cid.Cid{}
for _, head := range heads {
headsCids = append(headsCids, head.Hash)
}
return &Snapshot{
ID: logData.ID,
Values: entries,
Heads: headsCids,
Clock: clock,
}, nil
}
func fromEntryHash(ctx context.Context, services io.IpfsServices, hashes []cid.Cid, options *FetchOptions) ([]*entry.Entry, error) {
if services == nil {
return nil, errmsg.IPFSNotDefined
}
if options == nil {
return nil, errmsg.FetchOptionsNotDefined
}
// Fetch given length, return size at least the given input entries
length := -1
if options.Length != nil && *options.Length > -1 {
length = maxInt(*options.Length, 1)
}
entries := entry.FetchParallel(ctx, services, hashes, &entry.FetchOptions{
Length: options.Length,
Exclude: options.Exclude,
ProgressChan: options.ProgressChan,
})
sliced := entries
if length > -1 {
sliced = sliced[:-length]
}
return sliced, nil
}
func fromJSON(ctx context.Context, services io.IpfsServices, jsonLog *JSONLog, options *entry.FetchOptions) (*Snapshot, error) {
if services == nil {
return nil, errmsg.IPFSNotDefined
}
if options == nil {
return nil, errmsg.FetchOptionsNotDefined
}
entries := entry.FetchParallel(ctx, services, jsonLog.Heads, &entry.FetchOptions{
Length: options.Length,
Exclude: []*entry.Entry{},
ProgressChan: options.ProgressChan,
Concurrency: 16,
Timeout: options.Timeout,
})
sorting.Sort(sorting.Compare, entries)
return &Snapshot{
ID: jsonLog.ID,
Heads: jsonLog.Heads,
Values: entries,
}, nil
}
func fromEntry(ctx context.Context, services io.IpfsServices, sourceEntries []*entry.Entry, options *entry.FetchOptions) (*Snapshot, error) {
if services == nil {
return nil, errmsg.IPFSNotDefined
}
if options == nil {
return nil, errmsg.FetchOptionsNotDefined
}
// Fetch given length, return size at least the given input entries
length := -1
if options.Length != nil && *options.Length > -1 {
length = maxInt(*options.Length, len(sourceEntries))
}
// Make sure we pass hashes instead of objects to the fetcher function
hashes := []cid.Cid{}
for _, e := range sourceEntries {
hashes = append(hashes, e.Hash)
}
// Fetch the entries
entries := entry.FetchParallel(ctx, services, hashes, &entry.FetchOptions{
Length: &length,
Exclude: options.Exclude,
ProgressChan: options.ProgressChan,
})
// Combine the fetches with the source entries and take only uniques
combined := append(sourceEntries, entries...)
uniques := entry.NewOrderedMapFromEntries(combined).Slice()
sorting.Sort(sorting.Compare, uniques)
// Cap the result at the right size by taking the last n entries
var sliced []*entry.Entry
if length > -1 {
sliced = entrySlice(uniques, -length)
} else {
sliced = uniques
}
missingSourceEntries := entry.Difference(sliced, sourceEntries)
result := append(missingSourceEntries, entrySliceRange(sliced, len(missingSourceEntries), len(sliced))...)
return &Snapshot{
ID: result[len(result)-1].LogID,
Values: result,
}, nil
}
func entrySlice(entries []*entry.Entry, index int) []*entry.Entry {
if len(entries) == 0 || index >= len(entries) {
return []*entry.Entry{}
}
if index == 0 || (index < 0 && -index >= len(entries)) {
return entries
}
if index > 0 {
return entries[index:]
}
return entries[(len(entries) + index):]
}
func entrySliceRange(entries []*entry.Entry, from int, to int) []*entry.Entry {
if len(entries) == 0 {
return []*entry.Entry{}
}
if from < 0 {
from = len(entries) + from
if from < 0 {
from = 0
}
}
if to < 0 {
to = len(entries) + to
}
if from >= len(entries) {
return []*entry.Entry{}
}
if to > len(entries) {
to = len(entries)
}
if from >= to {
return []*entry.Entry{}
}
if from == to {
return entries
}
return entries[from:to]
}