-
Notifications
You must be signed in to change notification settings - Fork 46
/
heads.go
189 lines (162 loc) · 4.29 KB
/
heads.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
// Copyright 2022 Democratized Data Foundation
//
// 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 clock
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"sort"
"errors"
"github.com/sourcenetwork/defradb/core"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/logging"
cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
)
// heads manages the current Merkle-CRDT heads.
type heads struct {
store datastore.DSReaderWriter
namespace core.HeadStoreKey
}
func NewHeadSet(store datastore.DSReaderWriter, namespace core.HeadStoreKey) *heads {
return newHeadset(store, namespace)
}
func newHeadset(store datastore.DSReaderWriter, namespace core.HeadStoreKey) *heads {
return &heads{
store: store,
namespace: namespace,
}
}
func (hh *heads) key(c cid.Cid) core.HeadStoreKey {
// /<namespace>/<cid>
return hh.namespace.WithCid(c)
}
func (hh *heads) load(ctx context.Context, c cid.Cid) (uint64, error) {
v, err := hh.store.Get(ctx, hh.key(c).ToDS())
if err != nil {
return 0, err
}
height, n := binary.Uvarint(v)
if n <= 0 {
return 0, errors.New("error decoding height")
}
return height, nil
}
func (hh *heads) write(ctx context.Context, store ds.Write, c cid.Cid, height uint64) error {
buf := make([]byte, binary.MaxVarintLen64)
n := binary.PutUvarint(buf, height)
if n == 0 {
return errors.New("error encoding height")
}
return store.Put(ctx, hh.key(c).ToDS(), buf[0:n])
}
func (hh *heads) delete(ctx context.Context, store ds.Write, c cid.Cid) error {
err := store.Delete(ctx, hh.key(c).ToDS())
if err == ds.ErrNotFound {
return nil
}
return err
}
// IsHead returns if a given cid is among the current heads.
func (hh *heads) IsHead(ctx context.Context, c cid.Cid) (bool, uint64, error) {
height, err := hh.load(ctx, c)
if err == ds.ErrNotFound {
return false, 0, nil
}
return err == nil, height, err
}
func (hh *heads) Len(ctx context.Context) (int, error) {
list, _, err := hh.List(ctx)
return len(list), err
}
// Replace replaces a head with a new cid.
func (hh *heads) Replace(ctx context.Context, h, c cid.Cid, height uint64) error {
log.Info(
ctx,
"Replacing DAG head",
logging.NewKV("Old", h),
logging.NewKV("Cid", c),
logging.NewKV("Height", height))
var store ds.Write = hh.store
var err error
// batchingDs, batching := store.(ds.Batching)
// if batching {
// store, err = batchingDs.Batch()
// if err != nil {
// return err
// }
// }
err = hh.delete(ctx, store, h)
if err != nil {
return err
}
err = hh.write(ctx, store, c, height)
if err != nil {
return err
}
// if batching {
// err := store.(ds.Batch).Commit()
// if err != nil {
// return err
// }
// }
return nil
}
func (hh *heads) Add(ctx context.Context, c cid.Cid, height uint64) error {
log.Info(ctx, "Adding new DAG head",
logging.NewKV("Cid", c),
logging.NewKV("Height", height))
return hh.write(ctx, hh.store, c, height)
}
// List returns the list of current heads plus the max height.
// @todo Document Heads.List function
func (hh *heads) List(ctx context.Context) ([]cid.Cid, uint64, error) {
q := query.Query{
Prefix: hh.namespace.ToString(),
KeysOnly: false,
}
results, err := hh.store.Query(ctx, q)
if err != nil {
return nil, 0, err
}
defer func() {
err := results.Close()
if err != nil {
log.ErrorE(ctx, "Error closing results", err)
}
}()
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", r.Error)
}
headKey, err := core.NewHeadStoreKey(r.Key)
if err != nil {
return nil, 0, err
}
height, n := binary.Uvarint(r.Value)
if n <= 0 {
return nil, 0, errors.New("error decocding height")
}
heads = append(heads, headKey.Cid)
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
}