-
Notifications
You must be signed in to change notification settings - Fork 1
/
GenData.ecl
372 lines (335 loc) · 15.1 KB
/
GenData.ecl
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
363
364
365
366
367
368
369
370
371
372
IMPORT $ AS DataMgmt;
IMPORT Std;
EXPORT GenData := MODULE(DataMgmt.Common)
/**
* A convenience method (function macro) that returns the actual data stored
* in a given generation. Note that an underlying assumption here is
* that the data is stored as a flat logical file; it will not work with
* delimited, XML, or JSON data structures, for instance (those types of
* structures are generally supported, just not with this function macro).
*
* @param dataStorePath The full path of the generational data store;
* REQUIRED
* @param recLayout The ECL RECORD structure of the data; REQUIRED
* @param numGeneration An integer indicating which generation of data
* to retrieve; generations are numbered starting
* with 1 and increasing; OPTIONAL, defaults to 1
*
* @return A dataset containing the desired generation of data. If no
* data is found for any reason then an empty dataset with the
* given record structure is returned.
*
* @see GetPath
* @see CurrentPath
* @see CurrentData
*/
EXPORT GetData(dataStorePath, recLayout, numGeneration = 1) := FUNCTIONMACRO
IMPORT DataMgmt;
#UNIQUENAME(path);
LOCAL %path% := DataMgmt.GenData.GetPath(dataStorePath, numGeneration);
RETURN DATASET(%path%, recLayout, FLAT, OPT);
ENDMACRO;
/**
* A convenience method (function macro) that returns the actual data stored
* in the current generation. Note that an underlying assumption here is
* that the data is stored as a flat logical file; it will not work with
* delimited, XML, or JSON data structures, for instance (those types of
* structures are generally supported, just not with this function macro).
* This is the same as calling GetData() and asking for generation 1.
*
* @param dataStorePath The full path of the generational data store;
* REQUIRED
* @param recLayout The ECL RECORD structure of the data; REQUIRED
*
* @return A dataset containing the current generation of data. If no
* data is found for any reason then an empty dataset with the
* given record structure is returned.
*
* @see CurrentPath
* @see GetPath
* @see GetData
*/
EXPORT CurrentData(dataStorePath, recLayout) := FUNCTIONMACRO
IMPORT DataMgmt;
#UNIQUENAME(path);
LOCAL %path% := DataMgmt.GenData.CurrentPath(dataStorePath);
RETURN DATASET(%path%, recLayout, FLAT, OPT);
ENDMACRO;
/**
* Construct a path for a new logical file for the data store. Note that
* the returned value will have time-oriented components in it, therefore
* callers should probably mark the returned value as INDEPENDENT if name
* will be used more than once (say, creating the file via OUTPUT and then
* calling WriteFile() here to store it) to avoid a recomputation of the
* name.
*
* @param dataStorePath The full path of the generational data store;
* REQUIRED
*
* @return String representing a new logical subfile that may be added
* to the data store.
*/
EXPORT NewSubfilePath(STRING dataStorePath) := _NewSubfilePath(dataStorePath);
/**
* Make the given logical file the first generation of data for the data
* store and bump all existing generations of data to the next level. If
* data is stored in the last generation then it will be deleted.
*
* @param dataStorePath The full path of the generational data store;
* REQUIRED
* @param newFilePath The full path of the logical file to insert
* into the data store as the new current
* generation of data; REQUIRED
*
* @return An action that inserts the given logical file into the data
* store. Existing generations of data are bumped to the next
* generation, and any data stored in the last generation will
* be deleted.
*
* @see WriteData
* @see AppendFile
* @see AppendData
*/
EXPORT WriteFile(STRING dataStorePath, STRING newFilePath) := _WriteFile(dataStorePath, newFilePath);
/**
* Convenience method that creates a new logical file from the given data
* and inserts it into the data store, making it the first generation of
* data. All existing generations of data will be bumped to the next level.
* If data is stored in the last generation then it will be deleted.
*
* @param dataStorePath The full path of the generational data store;
* REQUIRED
* @param ds The dataset to insert into the data store;
* REQUIRED
* @param filenameSuffix String suffix to be added to the generated
* logical subfile name; use this if you intend to
* call this method multiple times in a single
* execution run; OPTIONAL, defaults to an
* empty string.
*
* @return An action that creates a new logical subfile and insert it into
* the data store. Existing generations of data are bumped to the
* next generation, and any data stored in the last generation will
* be deleted.
*
* @see WriteFile
* @see AppendFile
* @see AppendData
*/
EXPORT WriteData(dataStorePath, ds, filenameSuffix = '\'\'') := FUNCTIONMACRO
#UNIQUENAME(subfilePath0);
LOCAL %subfilePath0% := #$.GenData.NewSubfilePath((STRING)dataStorePath) : INDEPENDENT;
#UNIQUENAME(subfilePath);
LOCAL %subfilePath% := %subfilePath0% + filenameSuffix;
#UNIQUENAME(createSubfileAction);
LOCAL %createSubfileAction% := OUTPUT(ds, {ds}, %subfilePath%, COMPRESSED);
#UNIQUENAME(allActions);
LOCAL %allActions% := ORDERED
(
%createSubfileAction%;
#$.GenData.WriteFile(dataStorePath, %subfilePath%);
);
RETURN %allActions%;
ENDMACRO;
/**
* Adds the given logical file to the first generation of data for the data
* store. This does not replace any existing data, nor bump any data
* generations to another level. The record structure of this data must
* be the same as other data in the data store.
*
* @param dataStorePath The full path of the generational data store;
* REQUIRED
* @param newFilePath The full path of the logical file to append
* to the current generation of data; REQUIRED
*
* @return An action that appends the given logical file to the current
* generation of data.
*
* @see AppendData
* @see WriteFile
* @see WriteData
*/
EXPORT AppendFile(STRING dataStorePath, STRING newFilePath) := _AppendFile(dataStorePath, newFilePath);
/**
* Convenience method that creates a new logical file from the given data
* and adds it to the first generation of data for the data store. No
* existing data is replaced, nor is any data bumped to the next level.
* The record structure of this data must be the same as other data in
* the data store.
*
* @param dataStorePath The full path of the generational data store;
* REQUIRED
* @param ds The dataset to added into the data store;
* REQUIRED
* @param filenameSuffix String suffix to be added to the generated
* logical subfile name; use this if you intend to
* call this method multiple times in a single
* execution run; OPTIONAL, defaults to an
* empty string.
*
* @return An action that creates a new logical subfile and adds it to
* the first generation of data in the data store.
*
* @see AppendFile
* @see WriteFile
* @see WriteData
*/
EXPORT AppendData(dataStorePath, ds, filenameSuffix = '\'\'') := FUNCTIONMACRO
#UNIQUENAME(subfilePath0);
LOCAL %subfilePath0% := #$.GenData.NewSubfilePath((STRING)dataStorePath) : INDEPENDENT;
#UNIQUENAME(subfilePath);
LOCAL %subfilePath% := %subfilePath0% + filenameSuffix;
#UNIQUENAME(createSubfileAction);
LOCAL %createSubfileAction% := OUTPUT(ds, {ds}, %subfilePath%, COMPRESSED);
#UNIQUENAME(allActions);
LOCAL %allActions% := ORDERED
(
%createSubfileAction%;
#$.GenData.AppendFile(dataStorePath, %subfilePath%);
);
RETURN %allActions%;
ENDMACRO;
/**
* Method promotes all data associated with the first generation into the
* second, promotes the second to the third, and so on. The first
* generation of data will be empty after this method completes.
*
* Note that if you have multiple logical files associated with a generation,
* as via AppendFile() or AppendData(), all of those files will be deleted
* or moved.
*
* @param dataStorePath The full path of the generational data store;
* REQUIRED
*
* @return An action that performs the generational promotion.
*
* @see RollbackGeneration
*/
EXPORT PromoteGeneration(STRING dataStorePath) := _PromoteGeneration(dataStorePath);
/**
* Method deletes all data associated with the current (first) generation of
* data, moves the second generation of data into the first generation, then
* repeats the process for any remaining generations. This functionality
* can be thought of restoring an older version of the data to the current
* generation.
*
* Note that if you have multiple logical files associated with a generation,
* as via AppendFile() or AppendData(), all of those files will be deleted
* or moved.
*
* @param dataStorePath The full path of the generational data store;
* REQUIRED
*
* @return An action that performs the generational rollback.
*
* @see PromoteGeneration
*/
EXPORT RollbackGeneration(STRING dataStorePath) := _RollbackGeneration(dataStorePath);
/**
* Delete all data associated with the data store but leave the
* surrounding superfile structure intact.
*
* @param dataStorePath The full path of the generational data store;
* REQUIRED
*
* @return An action performing the delete operations.
*/
EXPORT ClearAll(STRING dataStorePath) := _ClearAll(dataStorePath);
/**
* Delete all data and structure associated with the data store.
*
* @param dataStorePath The full path of the generational data store;
* REQUIRED
*
* @return An action performing the delete operations.
*
* @see _ClearAll
*/
EXPORT DeleteAll(STRING dataStorePath) := _DeleteAll(dataStorePath);
//--------------------------------------------------------------------------
EXPORT Tests := MODULE
SHARED dataStoreName := '~gendata::test::' + Std.System.Job.WUID();
SHARED numGens := 5;
SHARED subfilePath := NewSubfilePath(dataStoreName) : INDEPENDENT;
SHARED TestRec := {INTEGER1 n};
SHARED testInit := SEQUENTIAL
(
Init(dataStoreName, numGens);
EVALUATE(NumGenerationsAvailable(dataStoreName));
TRUE;
);
SHARED testInsertFile1 := FUNCTION
ds1 := DATASET(10, TRANSFORM(TestRec, SELF.n := RANDOM()));
ds1Path := subfilePath + '-testInsertFile1';
RETURN SEQUENTIAL
(
OUTPUT(ds1,,ds1Path);
WriteFile(dataStoreName, ds1Path);
ASSERT(DataMgmt.Common.NumGenerationsInUse(dataStoreName) = 1);
ASSERT(COUNT(DATASET(DataMgmt.Common.CurrentPath(dataStoreName), TestRec, FLAT, OPT)) = 10)
);
END;
SHARED testInsertFile2 := FUNCTION
ds2 := DATASET(20, TRANSFORM(TestRec, SELF.n := RANDOM()));
ds2Path := subfilePath + '-testInsertFile2';
RETURN SEQUENTIAL
(
OUTPUT(ds2,,ds2Path);
WriteFile(dataStoreName, ds2Path);
ASSERT(DataMgmt.Common.NumGenerationsInUse(dataStoreName) = 2);
ASSERT(COUNT(DATASET(DataMgmt.Common.CurrentPath(dataStoreName), TestRec, FLAT, OPT)) = 20)
);
END;
SHARED testAppendFile1 := FUNCTION
ds3 := DATASET(15, TRANSFORM(TestRec, SELF.n := RANDOM()));
ds3Path := subfilePath + '-testAppendFile1';
RETURN SEQUENTIAL
(
OUTPUT(ds3,,ds3Path);
AppendFile(dataStoreName, ds3Path);
ASSERT(DataMgmt.Common.NumGenerationsInUse(dataStoreName) = 2);
ASSERT(COUNT(DATASET(DataMgmt.Common.CurrentPath(dataStoreName), TestRec, FLAT, OPT)) = 35)
);
END;
SHARED testPromote := SEQUENTIAL
(
PromoteGeneration(dataStoreName);
ASSERT(DataMgmt.Common.NumGenerationsInUse(dataStoreName) = 3);
ASSERT(NOT EXISTS(DATASET(DataMgmt.Common.CurrentPath(dataStoreName), TestRec, FLAT, OPT)))
);
SHARED testRollback1 := SEQUENTIAL
(
RollbackGeneration(dataStoreName);
ASSERT(DataMgmt.Common.NumGenerationsInUse(dataStoreName) = 2);
ASSERT(COUNT(DATASET(DataMgmt.Common.CurrentPath(dataStoreName), TestRec, FLAT, OPT)) = 35)
);
SHARED testRollback2 := SEQUENTIAL
(
RollbackGeneration(dataStoreName);
ASSERT(DataMgmt.Common.NumGenerationsInUse(dataStoreName) = 1);
ASSERT(COUNT(DATASET(DataMgmt.Common.CurrentPath(dataStoreName), TestRec, FLAT, OPT)) = 10)
);
SHARED testClearAll := SEQUENTIAL
(
ClearAll(dataStoreName);
ASSERT(DataMgmt.Common.NumGenerationsInUse(dataStoreName) = 0);
);
SHARED testDeleteAll := SEQUENTIAL
(
DeleteAll(dataStoreName);
ASSERT(NOT Std.File.SuperFileExists(dataStoreName));
);
EXPORT DoAll := SEQUENTIAL
(
testInit;
testInsertFile1;
testInsertFile2;
testAppendFile1;
testPromote;
testRollback1;
testRollback2;
testClearAll;
testDeleteAll;
);
END;
END;