This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_ops.go
360 lines (290 loc) · 8.68 KB
/
stream_ops.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
// +build !app_engine
package luaLib
const LibStreamOps = `
-- Lua Interface for Aerospike Record Stream Support
--
-- ======================================================================
-- Copyright [2014] Aerospike, Inc.. Portions may be licensed
-- to Aerospike, Inc. under one or more contributor license agreements.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-- ======================================================================
local function check_limit(v)
return type(v) == 'number' and v >= 1000
end
--
-- clone a table. creates a shallow copy of the table.
--
local function clone_table(t)
local out = {}
for k,v in pairs(t) do
out[k] = v
end
return out
end
--
-- Clone a value.
--
local function clone(v)
local t = type(v)
if t == 'number' then
return v
elseif t == 'string' then
return v
elseif t == 'boolean' then
return v
elseif t == 'table' then
return clone_table(v)
elseif t == 'userdata' then
if v.__index == Map then
return map.clone(v)
elseif v.__index == List then
return list.clone(v)
end
return nil
end
return v
end
--
-- Filter values
-- @param next - a generator that produces the next value from a stream
-- @param f - the function to transform each value
--
function filter( next, p )
-- done indicates if we exhausted the 'next' stream
local done = false
-- return a closure which the caller can use to get the next value
return function()
-- we bail if we already exhausted the stream
if done then return nil end
-- find the first value which satisfies the predicate
for a in next do
if p(a) then
return a
end
end
done = true
return nil
end
end
--
-- Transform values
-- @param next - a generator that produces the next value from a stream
-- @param f - the tranfomation operation
--
function transform( next, f )
-- done indicates if we exhausted the 'next' stream
local done = false
-- return a closure which the caller can use to get the next value
return function()
-- we bail if we already exhausted the stream
if done then return nil end
-- get the first value
local a = next()
-- apply the transformation
if a ~= nil then
return f(a)
end
done = true;
return nil
end
end
--
-- Combines two values from an istream into a single value.
-- @param next - a generator that produces the next value from a stream
-- @param f - the reduction operation
--
function reduce( next, f )
-- done indicates if we exhausted the 'next' stream
local done = false
-- return a closure which the caller can use to get the next value
return function()
-- we bail if we already exhausted the stream
if done then return nil end
-- get the first value
local a = next()
if a ~= nil then
-- get each subsequent value and reduce them
for b in next do
a = f(a,b)
end
end
-- we are done!
done = true
return a
end
end
--
-- Aggregate values into a single value.
-- @param next - a generator that produces the next value from a stream
-- @param f - the aggregation operation
--
function aggregate( next, init, f )
-- done indicates if we exhausted the 'next' stream
local done = false
-- return a closure which the caller can use to get the next value
return function()
-- we bail if we already exhausted the stream
if done then return nil end
-- get the initial value
local a = clone(init)
-- get each subsequent value and aggregate them
for b in next do
a = f(a,b)
-- check the size limit, if it is exceeded,
-- then return the value
if check_limit(a) then
return a
end
end
-- we are done!
done = true
return a
end
end
--
-- as_stream iterator
--
function stream_iterator(s)
local done = false
return function()
if done then return nil end
local v = stream.read(s)
if v == nil then
done = true
end
return v;
end
end
-- ######################################################################################
--
-- StreamOps
-- Builds a sequence of operations to be applied to a stream of values.
--
-- ######################################################################################
StreamOps = {}
StreamOps_mt = { __index = StreamOps }
-- Op only executes on server
local SCOPE_SERVER = 1
-- Op only executes on client
local SCOPE_CLIENT = 2
-- Op can execute on either client or server
local SCOPE_EITHER = 3
-- Op executes on both client and server
local SCOPE_BOTH = 4
--
-- Creates a new StreamOps using an array of ops
--
-- @param ops an array of operations
--
function StreamOps_create()
local self = {}
setmetatable(self, StreamOps_mt)
self.ops = {}
return self
end
function StreamOps_apply(stream, ops, i, n)
-- if nil, then use default values
i = i or 1
n = n or #ops
-- if index in list > size of list, then return the stream
if i > n then return stream end
-- get the current operation
local op = ops[i]
-- apply the operation and get a stream or use provided stream
local s = op.func(stream, unpack(op.args)) or stream
-- move to the next operation
return StreamOps_apply(s, ops, i + 1, n)
end
--
-- This selects the operations appropriate for a given scope.
-- For the SERVER scope, it will select the first n ops until one of the ops
-- is a CLIENT scope op.
-- For the CLIENT scope, it will skip the first n ops that are SERVER scope
-- ops, then it will take the remaining ops, including SERVER scoped ops.
--
function StreamOps_select(stream_ops, scope)
local server_ops = {}
local client_ops = {}
local phase = SCOPE_SERVER
for i,op in ipairs(stream_ops) do
if phase == SCOPE_SERVER then
if op.scope == SCOPE_SERVER then
table.insert(server_ops, op)
elseif op.scope == SCOPE_EITHER then
table.insert(server_ops, op)
elseif op.scope == SCOPE_BOTH then
table.insert(server_ops, op)
table.insert(client_ops, op)
phase = SCOPE_CLIENT
end
elseif phase == SCOPE_CLIENT then
table.insert(client_ops, op)
end
end
if scope == SCOPE_CLIENT then
return client_ops
else
return server_ops
end
end
--
-- OPS: [ OP, ... ]
-- OP: {scope=SCOPE, name=NAME, func=FUNC, args=ARGS}
-- SCOPE: ANY(0) | SERVER(1) | CLIENT(2) |
-- NAME: FUNCTION NAME
-- FUNC: FUNCTION POINTER
-- ARGS: ARRAY OF ARGUMENTS
--
function StreamOps:aggregate(...)
table.insert(self.ops, { scope = SCOPE_SERVER, name = "aggregate", func = aggregate, args = {...}})
return self
end
function StreamOps:reduce(...)
table.insert(self.ops, { scope = SCOPE_BOTH, name = "reduce", func = reduce, args = {...}})
return self
end
function StreamOps:map(...)
table.insert(self.ops, { scope = SCOPE_EITHER, name = "map", func = transform, args = {...}})
return self
end
function StreamOps:filter(...)
table.insert(self.ops, { scope = SCOPE_EITHER, name = "filter", func = filter, args = {...}})
return self
end
-- stream : group(f)
--
-- Group By will return a Map of keys to a list of values. The key is determined by applying the
-- function 'f' to each element in the stream.
--
function StreamOps:groupby(f)
local function _aggregate(m, v)
local k = f and f(v) or nil;
local l = m[k] or list()
list.append(l, v)
m[k] = l;
return m;
end
local function _merge(l1, l2)
local l = list.clone(l1)
for v in list.iterator(l2) do
list.append(l, v)
end
return l
end
function _reduce(m1, m2)
return map.merge(m1, m2, _merge)
end
return self : aggregate(map(), _aggregate) : reduce(_reduce)
end
`