-
Notifications
You must be signed in to change notification settings - Fork 0
/
IdentifiableMemoryPersistence.go
362 lines (307 loc) · 10.3 KB
/
IdentifiableMemoryPersistence.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package persistence
import (
"reflect"
"github.com/pip-services3-go/pip-services3-commons-go/config"
cdata "github.com/pip-services3-go/pip-services3-commons-go/data"
refl "github.com/pip-services3-go/pip-services3-commons-go/reflect"
"github.com/pip-services3-go/pip-services3-components-go/log"
)
/*
Abstract persistence component that stores data in memory
and implements a number of CRUD operations over data items with unique ids.
The data items must have Id field.
In basic scenarios child structs shall only override GetPageByFilter,
GetListByFilter or DeleteByFilter operations with specific filter function.
All other operations can be used out of the box.
In complex scenarios child structes can implement additional operations by
accessing cached items via c.Items property and calling Save method
on updates.
See MemoryPersistence
Configuration parameters
- options:
- max_page_size: Maximum number of items returned in a single page (default: 100)
References
- *:logger:*:*:1.0 (optional) ILogger components to pass log messages
Examples
type MyMemoryPersistence struct{
IdentifiableMemoryPersistence
}
func composeFilter(filter: FilterParams) (func (item interface{}) bool ) {
if &filter == nil {
filter = NewFilterParams()
}
name := filter.getAsNullableString("Name");
return func(item interface{}) bool {
dummy, ok := item.(MyData)
if (*name != "" && ok && item.Name != *name)
return false;
return true;
};
}
func (mmp * MyMemoryPersistence) GetPageByFilter(correlationId string, filter FilterParams, paging PagingParams) (page DataPage, err error) {
tempPage, err := c.GetPageByFilter(correlationId, composeFilter(filter), paging, nil, nil)
dataLen := int64(len(tempPage.Data))
data := make([]MyData, dataLen)
for i, v := range tempPage.Data {
data[i] = v.(MyData)
}
page = *NewMyDataPage(&dataLen, data)
return page, err}
persistence := NewMyMemoryPersistence();
item, err := persistence.Create("123", { Id: "1", Name: "ABC" })
...
page, err := persistence.GetPageByFilter("123", NewFilterParamsFromTuples("Name", "ABC"), nil)
if err != nil {
panic("Error can't get data")
}
fmt.Prnitln(page.data) // Result: { Id: "1", Name: "ABC" }
item, err := persistence.DeleteById("123", "1")
...
*/
// extends MemoryPersistence implements IConfigurable, IWriter, IGetter, ISetter
type IdentifiableMemoryPersistence struct {
MemoryPersistence
}
// Creates a new empty instance of the persistence.
// Parameters:
// - prototype reflect.Type
// data type of contains items
// Return * IdentifiableMemoryPersistence
// created empty IdentifiableMemoryPersistence
func NewIdentifiableMemoryPersistence(prototype reflect.Type) (c *IdentifiableMemoryPersistence) {
c = &IdentifiableMemoryPersistence{}
c.MemoryPersistence = *NewMemoryPersistence(prototype)
c.Logger = log.NewCompositeLogger()
c.MaxPageSize = 100
return c
}
// Configures component by passing configuration parameters.
// Parameters:
// - config *config.ConfigParams
// configuration parameters to be set.
func (c *IdentifiableMemoryPersistence) Configure(config *config.ConfigParams) {
c.MaxPageSize = config.GetAsIntegerWithDefault("options.max_page_size", c.MaxPageSize)
}
// Gets a list of data items retrieved by given unique ids.
// Parameters:
// - correlationId string
// (optional) transaction id to trace execution through call chain.
// - ids []interface{}
// ids of data items to be retrieved
// Returns []interface{}, error
// data list or error.
func (c *IdentifiableMemoryPersistence) GetListByIds(correlationId string, ids []interface{}) (result []interface{}, err error) {
filter := func(item interface{}) bool {
exist := false
id := GetObjectId(item)
for _, v := range ids {
vId := refl.ObjectReader.GetValue(v)
if CompareValues(id, vId) {
exist = true
break
}
}
return exist
}
return c.GetListByFilter(correlationId, filter, nil, nil)
}
// Gets a data item by its unique id.
// Parameters:
// - correlationId string
// (optional) transaction id to trace execution through call chain.
// - id interface{}
// an id of data item to be retrieved.
// Returns: interface{}, error
// data item or error.
func (c *IdentifiableMemoryPersistence) GetOneById(correlationId string, id interface{}) (result interface{}, err error) {
c.Lock.RLock()
defer c.Lock.RUnlock()
var items []interface{}
for _, v := range c.Items {
vId := GetObjectId(v)
if CompareValues(vId, id) {
items = append(items, v)
}
}
var item interface{} = nil
if len(items) > 0 {
//item = CloneObject(items[0])
item = CloneObjectForResult(items[0], c.Prototype)
}
if item != nil {
c.Logger.Trace(correlationId, "Retrieved item %s", id)
} else {
c.Logger.Trace(correlationId, "Cannot find item by %s", id)
}
return item, err
}
// Get index by "Id" field
// return index number
func (c *IdentifiableMemoryPersistence) GetIndexById(id interface{}) int {
var index int = -1
for i, v := range c.Items {
vId := GetObjectId(v)
if CompareValues(vId, id) {
index = i
break
}
}
return index
}
// Creates a data item.
// Returns:
// - correlation_id string
// (optional) transaction id to trace execution through call chain.
// - item string
// an item to be created.
// Returns: interface{}, error
// created item or error.
func (c *IdentifiableMemoryPersistence) Create(correlationId string, item interface{}) (result interface{}, err error) {
c.Lock.Lock()
newItem := CloneObject(item, c.Prototype)
GenerateObjectId(&newItem)
id := GetObjectId(newItem)
c.Items = append(c.Items, newItem)
c.Lock.Unlock()
c.Logger.Trace(correlationId, "Created item %s", id)
errsave := c.Save(correlationId)
result = CloneObjectForResult(newItem, c.Prototype)
return result, errsave
}
// Sets a data item. If the data item exists it updates it,
// otherwise it create a new data item.
// Parameters:
// - correlation_id string
// (optional) transaction id to trace execution through call chain.
// - item interface{}
// a item to be set.
// Returns: interface{}, error
// updated item or error.
func (c *IdentifiableMemoryPersistence) Set(correlationId string, item interface{}) (result interface{}, err error) {
c.Lock.Lock()
newItem := CloneObject(item, c.Prototype)
GenerateObjectId(&newItem)
id := GetObjectId(item)
index := c.GetIndexById(id)
if index < 0 {
c.Items = append(c.Items, newItem)
} else {
c.Items[index] = newItem
}
c.Lock.Unlock()
c.Logger.Trace(correlationId, "Set item %s", id)
errsav := c.Save(correlationId)
result = CloneObjectForResult(newItem, c.Prototype)
return result, errsav
}
// Updates a data item.
// Parameters:
// - correlation_id string
// (optional) transaction id to trace execution through call chain.
// - item interface{}
// an item to be updated.
// Returns: interface{}, error
// updated item or error.
func (c *IdentifiableMemoryPersistence) Update(correlationId string, item interface{}) (result interface{}, err error) {
c.Lock.Lock()
id := GetObjectId(item)
index := c.GetIndexById(id)
if index < 0 {
c.Logger.Trace(correlationId, "Item %s was not found", id)
c.Lock.Unlock()
return nil, nil
}
newItem := CloneObject(item, c.Prototype)
c.Items[index] = newItem
c.Lock.Unlock()
c.Logger.Trace(correlationId, "Updated item %s", id)
errsave := c.Save(correlationId)
result = CloneObjectForResult(newItem, c.Prototype)
return result, errsave
}
// Updates only few selectFuncected fields in a data item.
// Parameters:
// - correlation_id string
// (optional) transaction id to trace execution through call chain.
// - id interface{}
// an id of data item to be updated.
// - data cdata.AnyValueMap
// a map with fields to be updated.
// Returns: interface{}, error
// updated item or error.
func (c *IdentifiableMemoryPersistence) UpdatePartially(correlationId string, id interface{}, data *cdata.AnyValueMap) (result interface{}, err error) {
c.Lock.Lock()
index := c.GetIndexById(id)
if index < 0 {
c.Logger.Trace(correlationId, "Item %s was not found", id)
c.Lock.Unlock()
return nil, nil
}
newItem := CloneObject(c.Items[index], c.Prototype)
if reflect.ValueOf(newItem).Kind() == reflect.Map {
refl.ObjectWriter.SetProperties(newItem, data.Value())
} else {
objPointer := reflect.New(reflect.TypeOf(newItem))
objPointer.Elem().Set(reflect.ValueOf(newItem))
intPointer := objPointer.Interface()
refl.ObjectWriter.SetProperties(intPointer, data.Value())
newItem = reflect.ValueOf(intPointer).Elem().Interface()
}
c.Items[index] = newItem
c.Lock.Unlock()
c.Logger.Trace(correlationId, "Partially updated item %s", id)
errsave := c.Save(correlationId)
result = CloneObjectForResult(newItem, c.Prototype)
return result, errsave
}
// Deleted a data item by it's unique id.
// Parameters:
// - correlation_id string
// (optional) transaction id to trace execution through call chain.
// - id interface{}
// an id of the item to be deleted
// Retruns: interface{}, error
// deleted item or error.
func (c *IdentifiableMemoryPersistence) DeleteById(correlationId string, id interface{}) (result interface{}, err error) {
c.Lock.Lock()
index := c.GetIndexById(id)
if index < 0 {
c.Logger.Trace(correlationId, "Item %s was not found", id)
c.Lock.Unlock()
return nil, nil
}
oldItem := c.Items[index]
if index == len(c.Items) {
c.Items = c.Items[:index-1]
} else {
c.Items = append(c.Items[:index], c.Items[index+1:]...)
}
c.Lock.Unlock()
c.Logger.Trace(correlationId, "Deleted item by %s", id)
errsave := c.Save(correlationId)
//result = CloneObject(oldItem)
result = CloneObjectForResult(oldItem, c.Prototype)
return result, errsave
}
// Deletes multiple data items by their unique ids.
// Parameters:
// - correlationId string
// (optional) transaction id to trace execution through call chain.
// - ids []interface{}
// ids of data items to be deleted.
// Returns: error
// error or null for success.
func (c *IdentifiableMemoryPersistence) DeleteByIds(correlationId string, ids []interface{}) (err error) {
filterFunc := func(item interface{}) bool {
exist := false
itemId := GetObjectId(item)
for _, v := range ids {
if CompareValues(v, itemId) {
exist = true
break
}
}
return exist
}
return c.DeleteByFilter(correlationId, filterFunc)
}