bitgirder-platform 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. data/LICENSE.txt +176 -0
  2. data/bin/ensure-test-db +117 -0
  3. data/bin/install-mysql +375 -0
  4. data/bin/tomcat7 +569 -0
  5. data/lib/bitgirder/concurrent.rb +400 -0
  6. data/lib/bitgirder/core.rb +1235 -0
  7. data/lib/bitgirder/etl.rb +58 -0
  8. data/lib/bitgirder/event/file.rb +485 -0
  9. data/lib/bitgirder/event/logger/testing.rb +140 -0
  10. data/lib/bitgirder/event/logger.rb +137 -0
  11. data/lib/bitgirder/event/testing.rb +88 -0
  12. data/lib/bitgirder/http.rb +255 -0
  13. data/lib/bitgirder/io/testing.rb +33 -0
  14. data/lib/bitgirder/io.rb +959 -0
  15. data/lib/bitgirder/irb.rb +35 -0
  16. data/lib/bitgirder/mysql.rb +60 -0
  17. data/lib/bitgirder/ops/java.rb +117 -0
  18. data/lib/bitgirder/ops/ruby.rb +235 -0
  19. data/lib/bitgirder/testing.rb +152 -0
  20. data/lib/doc-gen0.rb +0 -0
  21. data/lib/doc-gen1.rb +0 -0
  22. data/lib/doc-gen10.rb +0 -0
  23. data/lib/doc-gen11.rb +0 -0
  24. data/lib/doc-gen12.rb +0 -0
  25. data/lib/doc-gen13.rb +0 -0
  26. data/lib/doc-gen14.rb +0 -0
  27. data/lib/doc-gen15.rb +0 -0
  28. data/lib/doc-gen16.rb +0 -0
  29. data/lib/doc-gen17.rb +14 -0
  30. data/lib/doc-gen18.rb +0 -0
  31. data/lib/doc-gen19.rb +0 -0
  32. data/lib/doc-gen2.rb +0 -0
  33. data/lib/doc-gen20.rb +182 -0
  34. data/lib/doc-gen21.rb +0 -0
  35. data/lib/doc-gen3.rb +0 -0
  36. data/lib/doc-gen4.rb +0 -0
  37. data/lib/doc-gen5.rb +0 -0
  38. data/lib/doc-gen6.rb +0 -0
  39. data/lib/doc-gen7.rb +0 -0
  40. data/lib/doc-gen8.rb +0 -0
  41. data/lib/doc-gen9.rb +0 -0
  42. data/lib/mingle/bincodec.rb +512 -0
  43. data/lib/mingle/codec.rb +54 -0
  44. data/lib/mingle/http.rb +156 -0
  45. data/lib/mingle/io/stream.rb +142 -0
  46. data/lib/mingle/io.rb +160 -0
  47. data/lib/mingle/json.rb +257 -0
  48. data/lib/mingle/service.rb +110 -0
  49. data/lib/mingle-em.rb +92 -0
  50. data/lib/mingle.rb +2917 -0
  51. metadata +100 -0
@@ -0,0 +1,257 @@
1
+ require 'bitgirder/core'
2
+ require 'bitgirder/io'
3
+ require 'mingle'
4
+ require 'mingle/codec'
5
+
6
+ module Mingle
7
+ module Json
8
+
9
+ include BitGirder::Core
10
+ include Mingle
11
+
12
+ class OptionsError < StandardError; end
13
+
14
+ class JsonMingleCodec < BitGirderClass
15
+
16
+ KEY_TYPE = "$type"
17
+ KEY_CONSTANT = "$constant"
18
+
19
+ MSG_MISSING_TYPE_KEY = %Q{Missing type key ("#{KEY_TYPE}")}
20
+
21
+ bg_attr :expand_enums, :processor => :boolean, :default => false
22
+
23
+ bg_attr :id_format,
24
+ :processor => lambda { |v| MingleIdentifier.as_format_name( v ) },
25
+ :default => :lc_hyphenated
26
+
27
+ bg_attr :omit_type_fields, :processor => :boolean, :default => false
28
+
29
+ include Mingle::Codec::MingleCodecImpl
30
+ include BitGirder::Core
31
+
32
+ require 'json'
33
+ require 'base64'
34
+
35
+ private
36
+ def impl_initialize
37
+
38
+ if @omit_type_fields && @expand_enums
39
+ raise OptionsError.new(
40
+ "Illegal combination of :omit_type_fields and :expand_enums" )
41
+ end
42
+ end
43
+
44
+ private
45
+ def decode_raise( path, msg )
46
+
47
+ msg = "#{path.format}: #{msg}" if path
48
+ codec_raise( msg )
49
+ end
50
+
51
+ private
52
+ def from_mingle_symbol_map( map, res = {} )
53
+
54
+ map.each_pair do |k, v|
55
+ res[ k.format( @id_format ) ] = from_mingle_value( v )
56
+ end
57
+
58
+ res
59
+ end
60
+
61
+ private
62
+ def from_mingle_struct( val )
63
+
64
+ res = from_mingle_symbol_map( val.fields, {} )
65
+ res[ KEY_TYPE ] = val.type.external_form unless @omit_type_fields
66
+
67
+ res
68
+ end
69
+
70
+ private
71
+ def from_mingle_buffer( val )
72
+ Base64.strict_encode64( val.buf )
73
+ end
74
+
75
+ private
76
+ def from_mingle_enum( en )
77
+
78
+ val = en.value.external_form
79
+
80
+ if @expand_enums
81
+ { KEY_TYPE => en.type.external_form, KEY_CONSTANT => val }
82
+ else
83
+ val
84
+ end
85
+ end
86
+
87
+ private
88
+ def from_mingle_value( val )
89
+
90
+ case val
91
+ when MingleString then val.to_s
92
+ when MingleSymbolMap then from_mingle_symbol_map( val )
93
+ when MingleStruct then from_mingle_struct( val )
94
+ when MingleBoolean then val.as_boolean
95
+
96
+ when MingleInt64, MingleInt32, MingleFloat64, MingleFloat32
97
+ val.num
98
+
99
+ when MingleBuffer then from_mingle_buffer( val )
100
+ when MingleTimestamp then val.rfc3339
101
+ when MingleEnum then from_mingle_enum( val )
102
+ when MingleList then val.map { |elt| from_mingle_value( elt ) }
103
+ when MingleNull then nil
104
+ when nil then nil
105
+ else codec_raise "Can't convert to json an instance of #{val.class}"
106
+ end
107
+ end
108
+
109
+ public
110
+ def as_buffer( obj )
111
+ JSON.generate( from_mingle_value( obj ) )
112
+ # JSON.generate( from_mingle_value( obj ) ).encode( "binary" )
113
+ end
114
+
115
+ private
116
+ def descend( path, key )
117
+ path ? path.descend( key ) : ObjectPath.get_root( key )
118
+ end
119
+
120
+ private
121
+ def start_list( path )
122
+ path ? path.start_list : ObjectPath.get_root_list
123
+ end
124
+
125
+ private
126
+ def parse_identifier( s, path )
127
+
128
+ begin
129
+ MingleIdentifier.parse( s )
130
+ rescue MingleParseError => e
131
+ decode_raise( path, e.message )
132
+ end
133
+ end
134
+
135
+ private
136
+ def parse_type_reference( s, path )
137
+
138
+ begin
139
+ MingleTypeReference.parse( s )
140
+ rescue MingleParseError => e
141
+ decode_raise( path, e.message )
142
+ end
143
+ end
144
+
145
+ private
146
+ def enum_val_in( h, path )
147
+
148
+ if val = h[ KEY_CONSTANT ]
149
+ if val.is_a?( String )
150
+ parse_identifier( val, descend( path, KEY_CONSTANT ) )
151
+ else
152
+ decode_raise(
153
+ path.descend( KEY_CONSTANT ), "Invalid constant value" )
154
+ end
155
+ end
156
+ end
157
+
158
+ private
159
+ def type_ref_in( h, path )
160
+
161
+ if typ_str = h[ KEY_TYPE ]
162
+ parse_type_reference( typ_str, descend( path, KEY_TYPE ) )
163
+ end
164
+ end
165
+
166
+ private
167
+ def as_symbol_map( h, path )
168
+
169
+ res = {}
170
+
171
+ h.each_pair do |k, v|
172
+ unless k == KEY_TYPE
173
+ if /^\$/ =~ k
174
+ msg = "Unrecognized control key: #{k.inspect}"
175
+ decode_raise( path, msg )
176
+ else
177
+ key_path = descend( path, k )
178
+ id = parse_identifier( k, key_path )
179
+ val = as_mingle_value( v, key_path )
180
+ res[ id ] = val
181
+ end
182
+ end
183
+ end
184
+
185
+ MingleSymbolMap.create( res )
186
+ end
187
+
188
+ private
189
+ def from_json_hash( h, struct_cls, path )
190
+
191
+ en_const = enum_val_in( h, path )
192
+ type_ref = type_ref_in( h, path )
193
+
194
+ if en_const
195
+ if type_ref
196
+ if h.size > 2
197
+ decode_raise path, "Enum has one or more unrecognized keys"
198
+ else
199
+ MingleEnum.new( :type => type_ref, :value => en_const )
200
+ end
201
+ else
202
+ decode_raise path, MSG_MISSING_TYPE_KEY
203
+ end
204
+ else
205
+ flds = as_symbol_map( h, path )
206
+ type_ref ?
207
+ MingleStruct.new( :type => type_ref, :fields => flds ) : flds
208
+ end
209
+ end
210
+
211
+ private
212
+ def from_json_array( arr, path )
213
+
214
+ lp = start_list( path )
215
+
216
+ vals = arr.map do |v|
217
+ val = as_mingle_value( v, lp )
218
+ lp = lp.next
219
+ val
220
+ end
221
+
222
+ MingleList.new( vals )
223
+ end
224
+
225
+ private
226
+ def as_mingle_value( val, path )
227
+
228
+ case val
229
+ when Hash then from_json_hash( val, MingleStruct, path )
230
+ when Array then from_json_array( val, path )
231
+ else MingleModels.as_mingle_value( val )
232
+ end
233
+ end
234
+
235
+ public
236
+ def from_buffer( buf )
237
+
238
+ not_nil( buf, :buf )
239
+ json = BitGirder::Io.parse_json( buf )
240
+
241
+ if json.is_a?( Hash )
242
+
243
+ codec_raise( MSG_MISSING_TYPE_KEY ) if json.empty?
244
+
245
+ if ( res = as_mingle_value( json, nil ) ).is_a?( MingleStruct )
246
+ res
247
+ else
248
+ codec_raise( "Expected struct" )
249
+ end
250
+ else
251
+ codec_raise( "Unexpected top level JSON value" )
252
+ end
253
+ end
254
+ end
255
+
256
+ end
257
+ end
@@ -0,0 +1,110 @@
1
+ require 'bitgirder/core'
2
+ require 'mingle'
3
+
4
+ module Mingle
5
+ module Service
6
+
7
+ module MingleServices
8
+
9
+ extend BitGirder::Core::BitGirderMethods
10
+ include Mingle
11
+
12
+ TYPE_SERVICE_REQUEST =
13
+ MingleTypeReference.get( :"service@v1/ServiceRequest" )
14
+
15
+ TYPE_SERVICE_RESPONSE =
16
+ MingleTypeReference.get( :"service@v1/ServiceResponse" )
17
+
18
+ module_function
19
+
20
+ def check_type( ms, typ_expct, err_type )
21
+
22
+ ( typ = ms.type ) == typ_expct or
23
+ raise "Invalid #{err_type} type: #{typ}"
24
+ end
25
+
26
+ def from_svc_req( req )
27
+
28
+ flds = {
29
+ :namespace => req.namespace.external_form,
30
+ :service => req.service.external_form,
31
+ :operation => req.operation.external_form
32
+ }
33
+
34
+ if ( ( val = req.parameters ) && ( ! val.empty? ) )
35
+ flds[ :parameters ] = val
36
+ end
37
+
38
+ if ( val = req.authentication ) then flds[ :authentication ] = val end
39
+
40
+ MingleStruct.new( :type => TYPE_SERVICE_REQUEST, :fields => flds )
41
+ end
42
+
43
+ def from_svc_resp( resp )
44
+
45
+ flds =
46
+ if resp.ok?
47
+ resp.result ? { :result => resp.result } : {}
48
+ else
49
+ { :exception => resp.error }
50
+ end
51
+
52
+ MingleStruct.new( :type => TYPE_SERVICE_RESPONSE, :fields => flds )
53
+ end
54
+
55
+ def as_mingle_struct( obj )
56
+
57
+ not_nil( obj, :obj )
58
+
59
+ case obj
60
+ when MingleServiceRequest then from_svc_req( obj )
61
+ when MingleServiceResponse then from_svc_resp( obj )
62
+ else raise "Can't convert to mingle struct: #{obj}"
63
+ end
64
+ end
65
+
66
+ def as_service_request( ms )
67
+
68
+ not_nil( ms, :ms )
69
+ check_type( ms, TYPE_SERVICE_REQUEST, "service request" )
70
+
71
+ f = ms.fields
72
+
73
+ MingleServiceRequest.new(
74
+ :namespace => MingleNamespace.get( f.expect_string( :namespace ) ),
75
+ :service => MingleIdentifier.get( f.expect_string( :service ) ),
76
+ :operation => MingleIdentifier.get( f.expect_string( :operation ) ),
77
+ :parameters => f[ :parameters ],
78
+ :authentication => f[ :authentication ]
79
+ )
80
+ end
81
+
82
+ def get_non_nil( val )
83
+
84
+ case val
85
+ when MingleNull then nil
86
+ else val
87
+ end
88
+ end
89
+
90
+ def as_service_response( ms )
91
+
92
+ not_nil( ms, :ms )
93
+ check_type( ms, TYPE_SERVICE_RESPONSE, "service response" )
94
+
95
+ ex = get_non_nil( ms[ :exception ] )
96
+ res = get_non_nil( ms[ :result ] )
97
+
98
+ ( ex == nil || res == nil ) or
99
+ raise "Response has non-nil result and exception"
100
+
101
+ if ex
102
+ MingleServiceResponse.create_failure( ex )
103
+ else
104
+ MingleServiceResponse.create_success( res )
105
+ end
106
+ end
107
+ end
108
+
109
+ end
110
+ end
data/lib/mingle-em.rb ADDED
@@ -0,0 +1,92 @@
1
+ require 'bitgirder/core'
2
+ require 'mingle'
3
+
4
+ module Mingle
5
+
6
+ class EmHttpMingleRpcClient < BitGirder::Core::BitGirderClass
7
+
8
+ require 'eventmachine'
9
+ require 'em-http'
10
+
11
+ HTTP_PRE_POST = :http_pre_post
12
+ HTTP_RESP_RECEIVED = :http_resp_received
13
+ MG_RESP_CREATED = :mg_resp_created
14
+
15
+ bg_attr :endpoint
16
+
17
+ bg_attr :identifier => :logger
18
+
19
+ @@codec = JsonMingleCodec.new
20
+
21
+ private
22
+ def cli_log( ev, obj )
23
+
24
+ case @logger
25
+
26
+ when Proc then @logger.call( ev, obj )
27
+
28
+ when BitGirder::Core::BitGirderLogger
29
+ @logger.code( "#{self} logged event #{ev} with object #{obj}" )
30
+ end
31
+ end
32
+
33
+ private
34
+ def handle_response( resp, blk )
35
+
36
+ mg_resp, ex = [ nil, nil ]
37
+
38
+ begin
39
+ stat_str = resp.response_header.http_status
40
+
41
+ if stat_str.to_i == 200
42
+ mg_resp = @@codec.as_mingle_service_response( resp.response )
43
+ cli_log( MG_RESP_CREATED, mg_resp )
44
+ else
45
+ raise "http got non-success status: #{stat_str}"
46
+ end
47
+
48
+ rescue Exception => ex; end
49
+
50
+ begin
51
+ blk.call( mg_resp, ex )
52
+ rescue Exception => e
53
+ warn( "Response handler block failed: #{e}\n" +
54
+ e.backtrace.join( "\n" ) )
55
+ end
56
+ end
57
+
58
+ public
59
+ def begin( mg_req, &blk )
60
+
61
+ not_nil( mg_req, "mg_req" )
62
+ raise "Need a response handler block" unless blk
63
+
64
+ req = EM::HttpRequest.new( @endpoint )
65
+
66
+ post_args = { :body => @@codec.as_codec_object( mg_req ) }
67
+ cli_log( HTTP_PRE_POST, post_args )
68
+
69
+ http = req.post( post_args )
70
+
71
+ http.callback do |resp|
72
+
73
+ cli_log( HTTP_RESP_RECEIVED, resp )
74
+ handle_response( resp, blk )
75
+ end
76
+ end
77
+
78
+ def call( ns, svc, op, params = nil, auth = nil, &blk )
79
+
80
+ req =
81
+ MingleServiceRequest.new(
82
+ :namespace => ns,
83
+ :service => svc,
84
+ :operation => op,
85
+ :parameters => params,
86
+ :authentication => auth )
87
+
88
+ self.begin( req, &blk )
89
+ end
90
+ end
91
+
92
+ end