-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewserver.io
139 lines (113 loc) · 3.8 KB
/
viewserver.io
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
JSON
funs := List clone
mapResults := List clone
output := method(data, File standardOutput writeln(data asJSON))
error := method(id, message,
Map with("error", Map with("id", id, "reason", message))
)
// ----------------------------------------------------------------------------
log := method(message, output(Map with("log", message)))
emit := method(key, value, mapResults append(list(key, value)))
sum := method(values, values reduce(+))
raise := method(message, Exception raise(message))
raiseFatal := method(raise("fatal_error"))
// ----------------------------------------------------------------------------
compile := method(funSource,
compiledCode := Message fromString(funSource unescape)
(compiledCode doInContext type == "Block") ifTrue(
return compiledCode
) ifFalse(
return error(
"compilation_error",
"expression does not eval to a function #{funSource unescape}" interpolate
)
)
)
execute := method(fun,
fun doInContext performWithArgList("call", call evalArgs slice(1))
)
handleCommand := method(cmd,
handlers hasKey(cmd at(0)) ifTrue(
return handlers at(cmd at(0)) performWithArgList("call", cmd slice(1))
) ifFalse(
return error("query_server_error", "unknown command #{cmd at(0)}" interpolate)
)
)
// ----------------------------------------------------------------------------
handlers := Map with(
"reset", method(
funs = list()
true
),
"add_fun", method(fun_source,
ex := try(
compiledCode := compile(fun_source)
)
ex catch(Exception,
error("compilation_error", ex error asMutable strip)
)
funs append(compiledCode)
true
),
"map_doc", method(doc,
result := list()
funs foreach(fun,
ex := try(
execute(fun, doc)
result append(mapResults)
)
ex catch(Exception,
(ex error == "fatal_error") ifTrue(
self return error("map_runtime_error", "function raised fatal exception")
) ifFalse(
log("function raised exception (#{ex error}) with doc._id #{doc at(\"_id\")}" interpolate)
result removeAll
)
)
)
result
),
"reduce", method(reduceFuns, arguments, rereduce,
keys := list()
values := list()
reductions := list()
(rereduce == true) ifFalse(
arguments foreach(kv,
keys append(kv first)
values append(kv second)
)
) ifTrue(
keys = nil
values = arguments
)
reduceFuns foreach(reduceFunSrc,
ex := try(
result := execute(compile(reduceFunSrc), keys, values, rereduce)
reductions append(result)
)
ex catch(Exception,
(ex error != "fatal_error") ifTrue(
log("function raised exception (#{ex error})" interpolate)
reductions removeAll
) ifFalse(
return error("reduce_runtime_error", "function raised fatal exception")
)
)
)
return list(true, reductions)
),
"rereduce", method(reduceFuns, arguments, reduce(reduceFuns, arguments, true))
)
// ----------------------------------------------------------------------------
mainLoop := method(
while (lineFromStdin := File standardInput readLine,
ex := try(
command := JSON fromJSON(lineFromStdin)
)
ex catch(Exception,
output(error("unknown_error", ex error))
)
output(handleCommand(command))
)
)
mainLoop