hutch-xamplr 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/Rakefile +1 -0
  2. data/VERSION.yml +2 -2
  3. data/examples/random-people-shared-addresses/Makefile +2 -2
  4. data/examples/random-people-shared-addresses/batch-load-users.rb +2 -5
  5. data/examples/random-people-shared-addresses/find-mentions.rb +1 -4
  6. data/examples/random-people-shared-addresses/find-people-by-address.rb +1 -4
  7. data/examples/random-people-shared-addresses/optimise.rb +1 -4
  8. data/examples/random-people-shared-addresses/people.rb +0 -15
  9. data/examples/random-people-shared-addresses/query.rb +1 -4
  10. data/examples/random-people-shared-addresses/query2.rb +1 -4
  11. data/examples/random-people-shared-addresses/results.write.BASELINE +298 -0
  12. data/examples/random-people-shared-addresses/results.write.NEW_ATTR_ENCODING +294 -0
  13. data/examples/random-people-shared-addresses/settings.rb +3 -0
  14. data/examples/random-people/batch-load-users.rb +1 -4
  15. data/examples/random-people/optimise.rb +1 -4
  16. data/examples/random-people/people.rb +0 -3
  17. data/examples/random-people/query.rb +1 -5
  18. data/examples/random-people/query2.rb +1 -4
  19. data/examples/random-people/settings.rb +3 -0
  20. data/examples/random-people/what-to-query-on.rb +1 -3
  21. data/examples/read-testing/Makefile +10 -0
  22. data/examples/read-testing/load.rb +65 -0
  23. data/examples/read-testing/read.rb +51 -0
  24. data/examples/read-testing/results.read.BASELINE +6 -0
  25. data/examples/read-testing/results.read.FAST +5 -0
  26. data/examples/read-testing/rrr.rb +87 -0
  27. data/examples/read-testing/settings.rb +2 -0
  28. data/examples/read-testing/xampl-gen.rb +36 -0
  29. data/examples/read-testing/xml/text.xml +8 -0
  30. data/lib/xamplr.rb +10 -1
  31. data/lib/xamplr/exceptions.rb +97 -0
  32. data/lib/xamplr/from-xml-orig.rb +350 -0
  33. data/lib/xamplr/from-xml.rb +272 -183
  34. data/lib/xamplr/handwritten/example.rb +0 -58
  35. data/lib/xamplr/handwritten/hand-example.rb +0 -27
  36. data/lib/xamplr/handwritten/test-handwritten.rb +0 -37
  37. data/lib/xamplr/mixins.rb +10 -48
  38. data/lib/xamplr/persist-to-xml.rb +249 -0
  39. data/lib/xamplr/persistence.rb +44 -412
  40. data/lib/xamplr/persistence.rb.more_thread_safe +0 -13
  41. data/lib/xamplr/persistence.rb.partially_thread_safe +0 -13
  42. data/lib/xamplr/persister.rb +298 -0
  43. data/lib/xamplr/{persister → persisters}/caches.rb +0 -0
  44. data/lib/xamplr/{persister → persisters}/caching.rb +1 -1
  45. data/lib/xamplr/{persister → persisters}/filesystem.rb +4 -5
  46. data/lib/xamplr/{persister → persisters}/in-memory.rb +1 -1
  47. data/lib/xamplr/{persister → persisters}/simple.rb +0 -0
  48. data/lib/xamplr/{persister → persisters}/tokyo-cabinet.rb +53 -15
  49. data/lib/xamplr/simpleTemplate/{input-c.r4 → obsolete/input-c.r4} +0 -0
  50. data/lib/xamplr/simpleTemplate/{play.r6.txt → obsolete/play.r6.txt} +0 -0
  51. data/lib/xamplr/simpleTemplate/{play_more.r6.txt → obsolete/play_more.r6.txt} +0 -0
  52. data/lib/xamplr/simpleTemplate/{test001.r5 → obsolete/test001.r5} +0 -0
  53. data/lib/xamplr/simpleTemplate/{test002.r5 → obsolete/test002.r5} +0 -0
  54. data/lib/xamplr/simpleTemplate/{test003.r5 → obsolete/test003.r5} +0 -0
  55. data/lib/xamplr/templates/child_indexed.template +1 -1
  56. data/lib/xamplr/templates/element_classes.template +1 -1
  57. data/lib/xamplr/templates/element_data.template +0 -39
  58. data/lib/xamplr/templates/element_empty.template +0 -40
  59. data/lib/xamplr/templates/element_mixed.template +0 -41
  60. data/lib/xamplr/templates/element_simple.template +0 -40
  61. data/lib/xamplr/test-support/bench.rb +6 -26
  62. data/lib/xamplr/test-support/test.rb +1 -89
  63. data/lib/xamplr/visitor.rb +0 -778
  64. data/lib/xamplr/visitors.rb +573 -0
  65. data/lib/xamplr/xampl-generator.rb +1 -1
  66. data/lib/xamplr/xampl-hand-generated.rb +0 -85
  67. data/lib/xamplr/xampl-module.rb +36 -0
  68. data/lib/xamplr/xampl-object-internals.rb +6 -0
  69. data/lib/xamplr/xampl-object.rb +10 -341
  70. data/lib/xamplr/xampl-persisted-object.rb +122 -0
  71. data/lib/xamplr/xml-text.rb +117 -0
  72. metadata +53 -18
  73. data/lib/xamplr/persister/subversion.rb +0 -61
  74. data/lib/xamplr/rac.sh +0 -6
  75. data/lib/xamplr/rac_gen.sh +0 -1
  76. data/lib/xamplr/templates/child_indexed.template.000 +0 -87
@@ -228,16 +228,6 @@ module XamplExample
228
228
 
229
229
  include ThingAsChild
230
230
 
231
- @@to_yaml_properties = [
232
- "@children",
233
- "@content"
234
- ]
235
-
236
-
237
- def to_yaml_properties
238
- @@to_yaml_properties
239
- end
240
-
241
231
  def Things.tag
242
232
  @@tag
243
233
  end
@@ -313,18 +303,8 @@ module XamplExample
313
303
  include ThingAsChild
314
304
  include ThingsAsChild
315
305
 
316
- @@to_yaml_properties = [
317
- "@pid",
318
- "@children",
319
- "@content"
320
- ]
321
-
322
306
  attr_reader :pid
323
307
 
324
- def to_yaml_properties
325
- @@to_yaml_properties
326
- end
327
-
328
308
  def Thing.tag
329
309
  @@tag
330
310
  end
@@ -413,17 +393,8 @@ module XamplExample
413
393
 
414
394
  include EmphAsChild
415
395
 
416
- @@to_yaml_properties = [
417
- "@kind",
418
- "@children"
419
- ]
420
-
421
396
  attr_reader :kind
422
397
 
423
- def to_yaml_properties
424
- @@to_yaml_properties
425
- end
426
-
427
398
  def Description.tag
428
399
  @@tag
429
400
  end
@@ -498,13 +469,6 @@ module XamplExample
498
469
  ]
499
470
 
500
471
 
501
- @@to_yaml_properties = [
502
- "@content"
503
- ]
504
-
505
-
506
- @@to_yaml_properties = [ "@content" ]
507
-
508
472
  def Emph.tag
509
473
  @@tag
510
474
  end
@@ -534,10 +498,6 @@ module XamplExample
534
498
  changed
535
499
  end
536
500
 
537
- def to_yaml_properties
538
- @@to_yaml_properties
539
- end
540
-
541
501
  def append_to(other)
542
502
  other.add_emph(self)
543
503
  end
@@ -577,18 +537,9 @@ module XamplExample
577
537
  ]
578
538
 
579
539
 
580
- @@to_yaml_properties = [
581
- "@id",
582
- "@value",
583
- ]
584
-
585
540
  attr_reader :id
586
541
  attr_reader :value
587
542
 
588
- def to_yaml_properties
589
- @@to_yaml_properties
590
- end
591
-
592
543
  def KeyValue.tag
593
544
  @@tag
594
545
  end
@@ -676,18 +627,9 @@ module XamplExample
676
627
  ]
677
628
 
678
629
 
679
- @@to_yaml_properties = [
680
- "@kind",
681
- "@special",
682
- ]
683
-
684
630
  attr_reader :kind
685
631
  attr_reader :special
686
632
 
687
- def to_yaml_properties
688
- @@to_yaml_properties
689
- end
690
-
691
633
  def Stuff.tag
692
634
  @@tag
693
635
  end
@@ -150,7 +150,6 @@ module XamplExample
150
150
  @@ns_tag = "{http://xampl.com/example}emph"
151
151
  @@module_name = "XamplExample"
152
152
  @@attributes = [ ]
153
- @@to_yaml_properties = [ "@content" ]
154
153
 
155
154
  def Emph.tag
156
155
  @@tag
@@ -177,10 +176,6 @@ module XamplExample
177
176
  changed
178
177
  end
179
178
 
180
- def to_yaml_properties
181
- @@to_yaml_properties
182
- end
183
-
184
179
  def append_to(other)
185
180
  other.add_emph(self)
186
181
  end
@@ -218,14 +213,9 @@ module XamplExample
218
213
  [:@kind, "kind"],
219
214
  [:@special, "special", "http://xampl.com/example/special"]
220
215
  ]
221
- @@to_yaml_properties = [ "@kind", "@special" ]
222
216
 
223
217
  attr_reader :kind, :special
224
218
 
225
- def to_yaml_properties
226
- @@to_yaml_properties
227
- end
228
-
229
219
  def Stuff.tag
230
220
  @@tag
231
221
  end
@@ -303,14 +293,8 @@ module XamplExample
303
293
 
304
294
  include EmphAsChild
305
295
 
306
- @@to_yaml_properties = [ "@kind", "@children" ]
307
-
308
296
  attr_reader :kind
309
297
 
310
- def to_yaml_properties
311
- @@to_yaml_properties
312
- end
313
-
314
298
  def Description.tag
315
299
  @@tag
316
300
  end
@@ -386,14 +370,8 @@ module XamplExample
386
370
  include ThingAsChild
387
371
  include KeyValueAsChild
388
372
 
389
- @@to_yaml_properties = [ "@pid", "@content", "@children" ]
390
-
391
373
  attr_reader :pid
392
374
 
393
- def to_yaml_properties
394
- @@to_yaml_properties
395
- end
396
-
397
375
  def Thing.tag
398
376
  @@tag
399
377
  end
@@ -478,14 +456,9 @@ module XamplExample
478
456
  [:@id, "id"],
479
457
  [:@value, "value"]
480
458
  ]
481
- @@to_yaml_properties = [ "@id", "@value" ]
482
459
 
483
460
  attr_reader :id, :value
484
461
 
485
- def to_yaml_properties
486
- @@to_yaml_properties
487
- end
488
-
489
462
  def KeyValue.tag
490
463
  @@tag
491
464
  end
@@ -192,43 +192,6 @@ class TestXampl < Test::Unit::TestCase
192
192
  check_parents(big_thing)
193
193
  end
194
194
 
195
- def test_yaml_round_trip
196
- emph_content_1 = "there"
197
- emph1 = Emph.new
198
- emph1 << emph_content_1
199
-
200
- emph_content_2 = "are"
201
- emph2 = Emph.new
202
- emph2.content = emph_content_2
203
-
204
- desc1 = Description.new
205
- desc1.kind = "desc1"
206
-
207
- desc1.is_changed = nil
208
- desc1 << "hello " << emph1 << "! How " << emph2 << " you?"
209
-
210
- thing = Thing.new
211
- thing.pid = "thing"
212
- thing.new_stuff.kind = "stuff1"
213
- thing << desc1
214
-
215
- big_thing = Thing.new
216
- big_thing << "leading content" << thing << "trailing content"
217
-
218
- check_parents(big_thing)
219
-
220
- #puts YAML::dump(big_thing)
221
- #puts big_thing.to_xml
222
-
223
- something = XamplObject.from_yaml(YAML::dump(big_thing))
224
- assert_equal(something.to_xml, big_thing.to_xml)
225
- check_parents(something)
226
-
227
- something = XamplObject.from_yaml(big_thing.to_yaml)
228
- assert_equal(something.to_xml, big_thing.to_xml)
229
- check_parents(something)
230
- end
231
-
232
195
  def test_from_xml
233
196
  emph_content_1 = "there"
234
197
  emph1 = Emph.new
@@ -59,13 +59,6 @@ module Xampl
59
59
  end
60
60
  return self
61
61
  end
62
-
63
- def stitch_yaml(depth=0)
64
- initialize
65
- if 0 < depth && self.persist_required then
66
- self.force_load
67
- end
68
- end
69
62
  end
70
63
 
71
64
  module XamplWithSimpleContent
@@ -92,6 +85,11 @@ module Xampl
92
85
  visitor.after_visit_simple_content(self)
93
86
  end
94
87
 
88
+ def _content
89
+ accessed
90
+ @_content
91
+ end
92
+
95
93
  def _content=(v)
96
94
  accessed
97
95
  v = v.to_s if (v.kind_of? Symbol) and !Xampl.xampl_extends_symbols
@@ -168,13 +166,6 @@ module Xampl
168
166
  end
169
167
  return self
170
168
  end
171
-
172
- def stitch_yaml(depth=0)
173
- initialize
174
- if 0 < depth && self.persist_required then
175
- self.force_load
176
- end
177
- end
178
169
  end
179
170
 
180
171
  module XamplWithDataContent
@@ -211,6 +202,11 @@ module Xampl
211
202
  return @children
212
203
  end
213
204
 
205
+ def _content
206
+ accessed
207
+ @_content
208
+ end
209
+
214
210
  def _content=(v)
215
211
  accessed
216
212
  v = v.to_s if (v.kind_of? Symbol) and !Xampl.xampl_extends_symbols
@@ -288,24 +284,6 @@ module Xampl
288
284
  end
289
285
  return self
290
286
  end
291
-
292
- def stitch_yaml(depth=0)
293
- children_array = @children if defined? @children
294
- initialize
295
-
296
- if 0 < depth && self.persist_required then
297
- self.force_load
298
- return
299
- end
300
-
301
- @children = []
302
- if children_array then
303
- children_array.each{ | child |
304
- child.stitch_yaml(1 + depth) if (child.kind_of? XamplObject)
305
- self << child
306
- }
307
- end
308
- end
309
287
  end
310
288
 
311
289
  module XamplWithMixedContent
@@ -417,22 +395,6 @@ module Xampl
417
395
  end
418
396
  return self
419
397
  end
420
-
421
- def stitch_yaml(depth=0)
422
- children_array = @children
423
- initialize
424
-
425
- if 0 < depth && self.persist_required then
426
- self.force_load
427
- return
428
- end
429
-
430
- @children = []
431
- children_array.each{ | child |
432
- child.stitch_yaml(1 + depth) if (child.kind_of? XamplObject)
433
- self << child
434
- }
435
- end
436
398
  end
437
399
  end
438
400
 
@@ -0,0 +1,249 @@
1
+ require 'libxml'
2
+
3
+ module Xampl
4
+
5
+ class PersistXML < Visitor
6
+ attr_accessor :ns_to_prefix, :start_body, :body, :out, :mentions
7
+
8
+ def initialize(out="", mentions=nil)
9
+ super()
10
+
11
+ @out = out
12
+ @was_attr = false
13
+
14
+ @mentions = mentions
15
+
16
+ @ns_to_prefix = {}
17
+ @start_body = nil
18
+ @body = ""
19
+ @attr_list = nil
20
+ end
21
+
22
+ def cycle(xampl)
23
+ raise XamplException.new(:cycle_detected_in_xampl_cluster) unless xampl.kind_of?(XamplPersistedObject)
24
+ return true
25
+ end
26
+
27
+ def revisit(xampl)
28
+ return true
29
+ end
30
+
31
+ def register_ns(ns)
32
+ if (0 == ns.length) then
33
+ return ""
34
+ end
35
+
36
+ prefix = ns_to_prefix[ns]
37
+ if (nil == prefix) then
38
+ preferred = XamplObject.lookup_preferred_ns_prefix(ns)
39
+ prefix = "" << preferred << ":" if preferred
40
+ prefix = "ns" << ns_to_prefix.size.to_s << ":" unless prefix
41
+ ns_to_prefix[ns] = prefix
42
+ end
43
+ return prefix
44
+ end
45
+
46
+ def attr_esc(s)
47
+ unless defined?(@@doc) then
48
+ @@doc = LibXML::XML::Document.new()
49
+ @@doc.root = LibXML::XML::Node.new('r')
50
+ @@attr = LibXML::XML::Attr.new(@@doc.root, 'v', 'v')
51
+ end
52
+
53
+ @@attr.value = s.to_s
54
+ (@@doc.root.to_s)[6..-4]
55
+ end
56
+
57
+
58
+ =begin
59
+
60
+ def attr_esc_old(s)
61
+ if (s.kind_of? XamplObject)
62
+ return attr_esc(s.to_xml)
63
+ end
64
+
65
+ # stupid_test()
66
+
67
+ result = s.to_s.dup
68
+
69
+ result.gsub!("&", "&amp;")
70
+ result.gsub!("<", "&lt;")
71
+ result.gsub!(">", "&gt;")
72
+ result.gsub!("'", "&apos;")
73
+ result.gsub!("\"", "&quot;")
74
+
75
+ return result
76
+ end
77
+
78
+ =end
79
+
80
+
81
+ # def content_esc(s)
82
+ # #NO! the attribute has the right to compact white space
83
+ # unless defined?(@@doc) then
84
+ # @@doc = LibXML::XML::Document.new()
85
+ # @@doc.root = LibXML::XML::Node.new('r')
86
+ # @@attr = LibXML::XML::Attr.new(@@doc.root, 'v', 'v')
87
+ # end
88
+ #
89
+ # @@attr.value = s
90
+ # (@@doc.root.to_s)[6..-4]
91
+ # end
92
+
93
+ #TODO -- use libxml for this too
94
+ def content_esc(s)
95
+ result = s.to_s.dup
96
+
97
+ return result if (s.kind_of? XamplObject)
98
+
99
+ result.gsub!("&", "&amp;")
100
+ result.gsub!("<", "&lt;")
101
+
102
+ return result
103
+ end
104
+
105
+ def attribute(xampl)
106
+ @attr_list = []
107
+ if (nil != xampl.attributes) then
108
+ xampl.attributes.each do |attr_spec|
109
+ prefix = (2 < attr_spec.length) ? register_ns(attr_spec[2]) : ""
110
+ value = xampl.instance_variable_get(attr_spec[0])
111
+ # @attr_list << (" " << prefix << attr_spec[1] << "='" << attr_esc(value) << "'") unless nil == value
112
+ @attr_list << (" " << prefix << attr_spec[1] << '="' << attr_esc(value) << '"') unless nil == value
113
+ end
114
+ end
115
+ end
116
+
117
+ def persist_attribute(xampl)
118
+ @attr_list = []
119
+ pattr = xampl.indexed_by.to_s
120
+ if (nil != xampl.attributes) then
121
+ xampl.attributes.each do |attr_spec|
122
+ if pattr == attr_spec[1] then
123
+ prefix = (2 < attr_spec.length) ? register_ns(attr_spec[2]) : ""
124
+ value = xampl.instance_variable_get(attr_spec[0])
125
+ # @attr_list << (" " << prefix << attr_spec[1] << "='" << attr_esc(value) << "'") unless nil == value
126
+ @attr_list << (" " << prefix << attr_spec[1] << '="' << attr_esc(value) << '"') unless nil == value
127
+ break
128
+ end
129
+ end
130
+ end
131
+ end
132
+
133
+ def show_attributes
134
+ result = @attr_list.join(" ")
135
+ if (0 == result.length) then
136
+ return ""
137
+ else
138
+ return result
139
+ end
140
+ end
141
+
142
+ def start_element(xampl)
143
+ tag = xampl.tag
144
+ ns = xampl.ns
145
+ tag_info = "" << "<" << register_ns(ns) << tag
146
+ unless @start_body then
147
+ attribute(xampl)
148
+ attr_defn = show_attributes
149
+ @start_body = "" << tag_info << attr_defn
150
+ @was_attr = true if 0 < attr_defn.size
151
+ else
152
+ if xampl.persist_required then
153
+ @mentions << xampl if @mentions
154
+ @no_children = true
155
+ persist_attribute(xampl)
156
+ else
157
+ attribute(xampl)
158
+ end
159
+ @body << tag_info << show_attributes
160
+ end
161
+ end
162
+
163
+ def end_element(xampl)
164
+ tag = xampl.tag
165
+ ns = xampl.ns
166
+ @body << "</" << register_ns(ns) << tag << ">"
167
+ end
168
+
169
+ def define_ns
170
+ result = ""
171
+ ns_to_prefix.each do |ns, prefix|
172
+ # result = sprintf("%s xmlns:%s='%s'", result, prefix[0..-2], ns)
173
+ result = sprintf("%s xmlns:%s=\"%s\"", result, prefix[0..-2], ns)
174
+ end
175
+ return result
176
+ end
177
+
178
+ def done
179
+ out << @start_body << define_ns << @body
180
+ end
181
+
182
+ def before_visit_without_content(xampl)
183
+ start_element(xampl)
184
+ @body << "/>"
185
+ end
186
+
187
+ def before_visit_simple_content(xampl)
188
+ start_element(xampl)
189
+ if @no_children then
190
+ @body << "/>"
191
+ else
192
+ @body << ">"
193
+ @body << content_esc(xampl._content) if xampl._content
194
+ end_element(xampl)
195
+ end
196
+ end
197
+
198
+ def before_visit_data_content(xampl)
199
+ start_element(xampl)
200
+ if @no_children then
201
+ @body << "/>"
202
+ else
203
+ @body << ">"
204
+ @body << content_esc(xampl._content) if xampl._content
205
+ end
206
+ end
207
+
208
+ def after_visit_data_content(xampl)
209
+ end_element(xampl) unless @no_children
210
+ end
211
+
212
+ def before_visit_mixed_content(xampl)
213
+ if @no_children then
214
+ @body << "/>"
215
+ else
216
+ start_element(xampl)
217
+ @body << ">"
218
+ end
219
+ end
220
+
221
+ def after_visit_mixed_content(xampl)
222
+ end_element(xampl) unless @no_children
223
+ end
224
+
225
+ def before_visit(xampl)
226
+ if xampl.respond_to? "before_visit_by_element_kind" then
227
+ xampl.before_visit_by_element_kind(self)
228
+ else
229
+ @body << xampl.to_s
230
+ end
231
+ end
232
+
233
+ def after_visit(xampl)
234
+ xampl.after_visit_by_element_kind(self) if xampl.respond_to? "after_visit_by_element_kind"
235
+ end
236
+
237
+ def visit_string(string)
238
+ @body << string
239
+ end
240
+ end
241
+
242
+ module XamplObject
243
+ def to_xml(out="", skip=[])
244
+ PersistXML.new(out).start(self).done
245
+ end
246
+ end
247
+
248
+ end
249
+