Module: Mohair
- Defined in:
- lib/mohair.rb,
lib/mohair/version.rb,
lib/mohair/inserter.rb,
lib/mohair/selector.rb,
lib/mohair/sql/tree.rb,
lib/mohair/sql/parser.rb,
lib/mohair/sql/select.rb
Defined Under Namespace
Modules: Sql
Classes: Column, Condition, Delete, From, Function, Group, Insert, Inserter, Order, Select, Update, Where
Constant Summary
collapse
- VERSION =
"0.0.3"
- MapperTemplate =
"function(v){\n var f = function(key, obj){\n var ret = {};\n <% select.each do |c| %>\n <%= c %>\n <% end %>\n ret.__key = key;\n <%= where %>\n };\n var raw_obj = JSON.parse(v.values[0].data);\n if(raw_obj instanceof Array){\n var ret0 = [];\n for(var i in raw_obj){\n ret0 = ret0.concat(f(v.key, raw_obj[i]));\n }\n return ret0;\n }else{\n return f(v.key, raw_obj);\n }\n}\n"
- ReducerTemplate =
"function(values){\n var ret = {};\n // init lines\n <%= agg_init %>\n for(var i in values){\n var v=values[i];\n <%= agg_fun %>\n }\n return [ret];\n}\n"
- GetAllMapper =
if(!!(v.sum_age)){ ret.sum_age += v.sum_age; }
"function(v){\n var f = function(key, obj){\n var ret = obj;\n ret.__key = key;\n <%= where %>\n };\n var raw_obj = JSON.parse(v.values[0].data);\n if(raw_obj instanceof Array){\n var ret0 = [];\n for(var i in raw_obj){\n ret0 = ret0.concat(f(v.key, raw_obj[i]));\n }\n return ret0;\n }else{\n return f(v.key, raw_obj);\n }\n}\n"
- GroupByMapperTemplate =
"function(v){\n ejsLog('/tmp/map_reduce.log', \"startmapper>\")\n var f = function(key, obj){\n var ret = {};\n <% select.each do |c| %>\n <%= c %>\n <% end %>\n ret.__key = key;\n <%= where %>\n };\n var arr = [];\n var raw_obj = JSON.parse(v.values[0].data);\n if(raw_obj instanceof Array){\n var ret0 = [];\n for(var i in raw_obj){\n ret0 = ret0.concat(f(v.key, raw_obj[i]));\n }\n arr = ret0;\n }else{\n arr = f(v.key, raw_obj);\n }\n var ret = {};\n for(var i in arr){\n //ejsLog('/tmp/map_reduce.log', JSON.stringify(arr[i].<%= col %>))\n var col = arr[i].<%= col %>;\n //ejsLog('/tmp/map_reduce.log', JSON.stringify(col))\n if(ret[col]){\n ret[col] = ret[col].push(arr[i]);\n //ejsLog('/tmp/map_reduce.log', JSON.stringify(ret[col]))\n }else{\n ret[col] = [arr[i]];\n //ejsLog('/tmp/map_reduce.log', JSON.stringify(ret[col]))\n }\n }\n ejsLog('/tmp/map_reduce.log', \"<eomapper\")\n return [ret];\n}\n"
- GroupByReducerTemplate =
merge them all
- { k, v }, .…
-
-> v
"function(values){\n ejsLog('/tmp/map_reduce.log', \"start>\");\n var ret = {};\n for(var i in values){\n ejsLog('/tmp/map_reduce.log', JSON.stringify(i))\n for(var a in values[i]){\n if(ret[a]){\n ret[a] = ret[a].concat(values[i][a]);\n }else{\n ret[a] = values[i][a];\n }\n }\n }\n ejsLog('/tmp/map_reduce.log', \"<end\");\n// ejsLog('/tmp/map_reduce.log', JSON.stringify(ret))\n return [ret];\n}\n"
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.build(tree) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/mohair/sql/tree.rb', line 6
def Mohair.build tree
case tree[:op]
when 'select'
Select.new tree
when 'insert'
Insert.new tree
when 'update'
Update.new tree
when 'delete'
Delete.new tree
else
LOG.error "bad :op", tree
end
end
|
.do_dump ⇒ Object
83
84
85
86
|
# File 'lib/mohair.rb', line 83
def self.do_dump
objs = JSON.load(STDIN)
Inserter.new(ARGV[0]).insert_all(objs)
end
|
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/mohair/selector.rb', line 128
def Mohair.format_result results
columns = Set.new
results.each do |r|
r.each do |k,v|
columns.add(k)
end
end
columns.delete('__key')
cols = columns.to_a.join("\t| ")
print "| | #{cols}|\n"
print "+---------------+------------------------------+\n"
results.each do |r|
print "| #{r['__key']}\t| "
columns.to_a.each do |c|
print "#{r[c]} \t| "
end
print "\n"
end
end
|
.main ⇒ Object
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
|
# File 'lib/mohair.rb', line 29
def self.main
q = nil
index = nil
opt = OptionParser.new
opt.on('-q Q'){|v| q = v}
opt.on('-i INDEX'){|v| index = v}
opt.on('-h', '--help'){ usage }
opt.parse!(ARGV)
parser = Sql::Parser.new
sql_syntax_tree = parser.parse (q.strip)
LOG.debug sql_syntax_tree
case sql_syntax_tree[:op]
when 'select'
s = (Mohair.build sql_syntax_tree)
LOG.debug "mapper->\n"
LOG.debug s.mapper
LOG.debug "reducer->\n"
LOG.debug s.reducer
client = Riak::Client.new(:protocol => "http")
bucket = Riak::MapReduce.new(client)
.add(client.bucket(s.bucket))
reducer = s.reducer
result = nil
if reducer.nil? then
result = bucket.map(s.mapper, :keep => true)
.run
else
result = bucket.map(s.mapper, :keep => false)
.reduce(reducer, :keep => true)
.run
end
LOG.info "query result:"
format_result result
when :create
LOG.error "CREATE sentence is unavailable at mohair"
else
LOG.error "bad query: ", result, "\n"
end
end
|
.usage ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/mohair.rb', line 17
def self.usage
print "usage:\n $ mohair -q \"select foo, bar from bucket_name\" [-i INDEX]\n $ mohair_dump <bucket_name> < sample_data.json\n\ninsert, delete sentence is future work\nmohair version \#{Mohair::VERSION}\n"
exit -1
end
|
Instance Method Details
#any(arr, fun) ⇒ Object
84
85
86
87
88
89
90
91
|
# File 'lib/mohair/sql/select.rb', line 84
def any(arr, fun)
arr.each do |e|
if fun(e) then
return true
end
end
return false
end
|