/
remote_hash_tree.go
192 lines (189 loc) · 5.37 KB
/
remote_hash_tree.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
package dhash
import (
"github.com/zond/god/common"
"github.com/zond/god/radix"
)
type remoteHashTree struct {
destination common.Remote
source common.Remote
node *Node
}
func (self remoteHashTree) Configuration() (conf map[string]string, timestamp int64) {
var result common.Conf
if err := self.destination.Call("DHash.Configuration", 0, &result); err != nil {
conf = make(map[string]string)
} else {
conf, timestamp = result.Data, result.Timestamp
}
return
}
func (self remoteHashTree) SubConfiguration(key []byte) (conf map[string]string, timestamp int64) {
var result common.Conf
if err := self.destination.Call("DHash.SubConfiguration", key, &result); err != nil {
conf = make(map[string]string)
} else {
conf, timestamp = result.Data, result.Timestamp
}
return
}
func (self remoteHashTree) Configure(conf map[string]string, timestamp int64) {
var x int
self.destination.Call("HashTree.Configure", common.Conf{
Data: conf,
Timestamp: timestamp,
}, &x)
}
func (self remoteHashTree) SubConfigure(key []byte, conf map[string]string, timestamp int64) {
var x int
self.destination.Call("HashTree.SubConfigure", common.Conf{
TreeKey: key,
Data: conf,
Timestamp: timestamp,
}, &x)
}
func (self remoteHashTree) Hash() (result []byte) {
self.destination.Call("HashTree.Hash", 0, &result)
return
}
func (self remoteHashTree) Finger(key []radix.Nibble) (result *radix.Print) {
result = &radix.Print{}
self.destination.Call("HashTree.Finger", key, result)
return
}
func (self remoteHashTree) GetTimestamp(key []radix.Nibble) (value []byte, timestamp int64, present bool) {
result := HashTreeItem{}
self.destination.Call("HashTree.GetTimestamp", key, &result)
value, timestamp, present = result.Value, result.Timestamp, result.Exists
return
}
func (self remoteHashTree) PutTimestamp(key []radix.Nibble, value []byte, present bool, expected, timestamp int64) (changed bool) {
data := HashTreeItem{
Key: key,
Value: value,
Exists: present,
Expected: expected,
Timestamp: timestamp,
}
op := "HashTree.PutTimestamp"
if self.node.hasCommListeners() {
self.node.triggerCommListeners(Comm{
Key: radix.Stitch(data.Key),
Source: self.source,
Destination: self.destination,
Type: op,
})
}
self.destination.Call(op, data, &changed)
return
}
func (self remoteHashTree) DelTimestamp(key []radix.Nibble, expected int64) (changed bool) {
data := HashTreeItem{
Key: key,
Expected: expected,
}
op := "HashTree.DelTimestamp"
if self.node.hasCommListeners() {
self.node.triggerCommListeners(Comm{
Key: radix.Stitch(data.Key),
Source: self.source,
Destination: self.destination,
Type: op,
})
}
self.destination.Call(op, data, &changed)
return
}
func (self remoteHashTree) SubFinger(key, subKey []radix.Nibble) (result *radix.Print) {
data := HashTreeItem{
Key: key,
SubKey: subKey,
}
result = &radix.Print{}
self.destination.Call("HashTree.SubFinger", data, result)
return
}
func (self remoteHashTree) SubGetTimestamp(key, subKey []radix.Nibble) (value []byte, timestamp int64, present bool) {
data := HashTreeItem{
Key: key,
SubKey: subKey,
}
self.destination.Call("HashTree.SubGetTimestamp", data, &data)
value, timestamp, present = data.Value, data.Timestamp, data.Exists
return
}
func (self remoteHashTree) SubPutTimestamp(key, subKey []radix.Nibble, value []byte, present bool, subExpected, subTimestamp int64) (changed bool) {
data := HashTreeItem{
Key: key,
SubKey: subKey,
Value: value,
Exists: present,
Expected: subExpected,
Timestamp: subTimestamp,
}
op := "HashTree.SubPutTimestamp"
if self.node.hasCommListeners() {
self.node.triggerCommListeners(Comm{
Key: radix.Stitch(data.Key),
SubKey: radix.Stitch(data.SubKey),
Source: self.source,
Destination: self.destination,
Type: op,
})
}
self.destination.Call(op, data, &changed)
return
}
func (self remoteHashTree) SubDelTimestamp(key, subKey []radix.Nibble, subExpected int64) (changed bool) {
data := HashTreeItem{
Key: key,
SubKey: subKey,
Expected: subExpected,
}
op := "HashTree.SubDelTimestamp"
if self.node.hasCommListeners() {
self.node.triggerCommListeners(Comm{
Key: radix.Stitch(data.Key),
SubKey: radix.Stitch(data.SubKey),
Source: self.source,
Destination: self.destination,
Type: op,
})
}
self.destination.Call(op, data, &changed)
return
}
func (self remoteHashTree) SubClearTimestamp(key []radix.Nibble, expected, timestamp int64) (deleted int) {
data := HashTreeItem{
Key: key,
Expected: expected,
Timestamp: timestamp,
}
op := "HashTree.SubClearTimestamp"
if self.node.hasCommListeners() {
self.node.triggerCommListeners(Comm{
Key: radix.Stitch(data.Key),
Source: self.source,
Destination: self.destination,
Type: op,
})
}
self.destination.Call(op, data, &deleted)
return
}
func (self remoteHashTree) SubKillTimestamp(key []radix.Nibble, expected int64) (deleted int) {
data := HashTreeItem{
Key: key,
Expected: expected,
}
op := "HashTree.SubKillTimestamp"
if self.node.hasCommListeners() {
self.node.triggerCommListeners(Comm{
Key: radix.Stitch(data.Key),
Source: self.source,
Destination: self.destination,
Type: op,
})
}
self.destination.Call(op, data, &deleted)
return
}