-
Notifications
You must be signed in to change notification settings - Fork 16
/
document_session_cluster_transaction.go
314 lines (243 loc) · 8.31 KB
/
document_session_cluster_transaction.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
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
package ravendb
import (
"reflect"
)
type ClusterTransactionOperations struct {
session *InMemoryDocumentSessionOperations
state map[string]*CompareExchangeSessionValue
}
func (cto *ClusterTransactionOperations) GetNumberOfTrackedCompareExchangeValues() int {
if nil == cto.state {
return 0
}
return len(cto.state)
}
func (cto *ClusterTransactionOperations) IsTracked(key string) bool {
_, exists := cto.tryGetCompareExchangeValueFromSession(key)
return exists
}
func (cto *ClusterTransactionOperations) tryGetCompareExchangeValueFromSession(key string) (*CompareExchangeSessionValue, bool) {
value, exists := cto.state[key]
return value, exists
}
func (cto *ClusterTransactionOperations) updateState(key string, index int64) (*CompareExchangeSessionValue, bool) {
value, exists := cto.tryGetCompareExchangeValueFromSession(key)
if exists == false {
return nil, false
}
value.UpdateState(index)
return value, true
}
func (cto *ClusterTransactionOperations) Clear() {
cto.state = make(map[string]*CompareExchangeSessionValue)
}
func (cto *ClusterTransactionOperations) CreateCompareExchangeValue(key string, item interface{}) (interface{}, error) {
if len(key) == 0 {
return nil, newIllegalArgumentError("Key cannot be null or empty")
}
var exists bool
var value *CompareExchangeSessionValue
value, exists = cto.tryGetCompareExchangeValueFromSession(key)
if exists == false {
var err error
value, err = NewCompareExchangeSessionValue(key, 0, compareExchangeValueStateNone)
if err != nil {
return nil, err
}
cto.state[key] = value
}
return value.Create(item)
}
func (cto *ClusterTransactionOperations) prepareCompareExchangeEntities(result *saveChangesData) error {
if len(cto.state) == 0 {
return nil
}
for _, value := range cto.state {
command, err := value.GetCommand(cto.session)
if err != nil {
return err
}
if command == nil {
continue
}
result.addSessionCommandData(command)
}
return nil
}
func (cto *ClusterTransactionOperations) GetCompareExchangeValue(clazz reflect.Type, key string) (*CompareExchangeValue, error) {
return cto.getCompareExchangeValueInternal(clazz, key)
}
func (cto *ClusterTransactionOperations) GetCompareExchangeValuesWithKeys(clazz reflect.Type, keys []string) (map[string]*CompareExchangeValue, error) {
return cto.getCompareExchangeValuesInternalWithKeys(clazz, keys)
}
func (cto *ClusterTransactionOperations) GetCompareExchangeValues(clazz reflect.Type, startsWith string, start int, pageSize int) (map[string]*CompareExchangeValue, error) {
return cto.getCompareExchangeValuesInternal(clazz, startsWith, start, pageSize)
}
func (cto *ClusterTransactionOperations) getCompareExchangeValuesInternal(clazz reflect.Type, startsWith string, start int, pageSize int) (map[string]*CompareExchangeValue, error) {
cto.session.incrementRequestCount()
operation, err := NewGetCompareExchangeValuesOperation(clazz, startsWith, start, pageSize)
if err != nil {
return nil, err
}
err = cto.session.GetOperations().Send(operation, cto.session.sessionInfo)
if err != nil {
return nil, err
}
operationResult := operation.Command.Result
results := make(map[string]*CompareExchangeValue)
for _, value := range operationResult {
sessionValue, err := cto.registerCompareExchangeValue(value)
if err != nil {
return nil, err
}
resultValue, err := sessionValue.GetValue(clazz, cto.session)
if err != nil {
return nil, err
}
results[value.GetKey()] = resultValue
}
return results, nil
}
func (cto *ClusterTransactionOperations) getCompareExchangeValuesInternalWithKeys(clazz reflect.Type, keys []string) (map[string]*CompareExchangeValue, error) {
results, notTracked, err := cto.getCompareExchangeValuesWithKeysFromSessionInternal(clazz, keys)
if err != nil {
return nil, err
}
if notTracked == nil || len(notTracked) == 0 {
return results, nil
}
cto.session.incrementRequestCount()
operation, err := NewGetCompareExchangeValuesOperationWithKeys(clazz, notTracked)
if err != nil {
return nil, err
}
err = cto.session.GetOperations().Send(operation, cto.session.sessionInfo)
if err != nil {
return nil, err
}
operationResult := operation.Command.Result
for _, key := range notTracked {
value, exists := operationResult[key]
if exists == false || value == nil {
cto.registerMissingCompareExchangeValue(key)
results[key] = nil
continue
}
sessionValue, err := cto.registerCompareExchangeValue(value)
if err != nil {
return nil, err
}
resultValue, err := sessionValue.GetValue(clazz, cto.session)
if err != nil {
return nil, err
}
results[value.GetKey()] = resultValue
}
return results, nil
}
func (cto *ClusterTransactionOperations) getCompareExchangeValueInternal(clazz reflect.Type, key string) (*CompareExchangeValue, error) {
v, notTracked := cto.getCompareExchangeValueFromSessionInternal(clazz, key)
if notTracked == false {
return v, nil
}
cto.session.incrementRequestCount()
operation, err := NewGetCompareExchangeValueOperation(clazz, key)
if err != nil {
return nil, err
}
err = cto.session.GetOperations().Send(operation, cto.session.sessionInfo)
if err != nil {
return nil, err
}
value := operation.Command.Result
if value == nil {
cto.registerMissingCompareExchangeValue(key)
return nil, nil
}
sessionValue, err := cto.registerCompareExchangeValue(value)
if err == nil && sessionValue != nil {
return sessionValue.GetValue(clazz, cto.session)
}
return nil, err
}
func (cto *ClusterTransactionOperations) registerCompareExchangeValue(value *CompareExchangeValue) (*CompareExchangeSessionValue, error) {
if cto.session.noTracking {
return NewCompareExchangeSessionValueWithValue(value)
}
var err error
sesionValue, exists := cto.state[value.GetKey()]
if exists == false || sesionValue == nil {
sesionValue, err = NewCompareExchangeSessionValueWithValue(value)
if err != nil {
return nil, err
}
cto.state[value.GetKey()] = sesionValue
return sesionValue, nil
}
err = sesionValue.UpdateValue(value)
return sesionValue, err
}
func (cto *ClusterTransactionOperations) registerMissingCompareExchangeValue(key string) (*CompareExchangeSessionValue, error) {
value, err := NewCompareExchangeSessionValue(key, -1, exchangeValueStateMissing)
if err != nil {
return nil, err
}
if cto.session.noTracking {
return value, nil
}
cto.state[key] = value
return value, nil
}
func (cto *ClusterTransactionOperations) getCompareExchangeValueFromSessionInternal(clazz reflect.Type, key string) (compareExchangeValue *CompareExchangeValue, notTracked bool) {
result, exist := cto.tryGetCompareExchangeValueFromSession(key)
if exist == false {
return nil, true
}
//we've already deserialized, maybe except situation when user wants get deriative type?
return result.value, false
}
func (cto *ClusterTransactionOperations) getCompareExchangeValuesWithKeysFromSessionInternal(clazz reflect.Type, keys []string) (map[string]*CompareExchangeValue, []string, error) {
var results map[string]*CompareExchangeValue
results = make(map[string]*CompareExchangeValue)
var notTracked []string
if keys == nil || len(keys) == 0 {
return results, nil, nil
}
for _, key := range keys {
cev, exists := cto.tryGetCompareExchangeValueFromSession(key)
if exists {
val, err := cev.GetValue(clazz, cto.session)
if err != nil {
return results, nil, err
}
results[key] = val
continue
}
notTracked = append(notTracked, key)
}
return results, notTracked, nil
}
func (cto *ClusterTransactionOperations) DeleteCompareExchangeValue(item *CompareExchangeValue) error {
if item == nil {
return newIllegalArgumentError("Item cannot be null")
}
sessionValue, exists := cto.tryGetCompareExchangeValueFromSession(item.GetKey())
if exists == false {
sessionValue, _ = NewCompareExchangeSessionValue(item.GetKey(), 0, compareExchangeValueStateNone)
cto.state[item.GetKey()] = sessionValue
}
sessionValue.Delete(item.GetIndex())
return nil
}
func (cto *ClusterTransactionOperations) DeleteCompareExchangeValueByKey(key string, index int64) error {
if len(key) == 0 {
return newIllegalStateError("Key cannot be null nor empty")
}
sessionValue, exists := cto.tryGetCompareExchangeValueFromSession(key)
if exists == false {
sessionValue, _ = NewCompareExchangeSessionValue(key, 0, compareExchangeValueStateNone)
cto.state[key] = sessionValue
}
sessionValue.Delete(index)
return nil
}