strokedb 0.0.2.1 → 0.0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +18 -20
- data/bench.html +4001 -0
- data/bin/strokedb +14 -0
- data/examples/movies.rb +105 -0
- data/examples/movies2.rb +97 -0
- data/examples/strokewiki/README +28 -0
- data/examples/strokewiki/view/edit.xhtml +27 -0
- data/examples/strokewiki/view/new.xhtml +26 -0
- data/examples/strokewiki/view/pages.xhtml +27 -0
- data/examples/strokewiki/view/show.xhtml +40 -0
- data/examples/strokewiki/view/versions.xhtml +25 -0
- data/examples/strokewiki/wiki.rb +106 -0
- data/examples/todo.rb +92 -0
- data/lib/strokedb.rb +85 -0
- data/lib/{config → strokedb}/config.rb +14 -9
- data/lib/strokedb/console.rb +87 -0
- data/lib/strokedb/core_ext.rb +10 -0
- data/lib/{util/ext → strokedb/core_ext}/blank.rb +1 -1
- data/lib/{util/ext → strokedb/core_ext}/enumerable.rb +0 -0
- data/lib/{util/ext → strokedb/core_ext}/fixnum.rb +0 -0
- data/lib/strokedb/core_ext/float.rb +4 -0
- data/lib/{util/ext → strokedb/core_ext}/hash.rb +0 -0
- data/lib/strokedb/core_ext/infinity.rb +33 -0
- data/lib/strokedb/core_ext/kernel.rb +41 -0
- data/lib/strokedb/core_ext/object.rb +16 -0
- data/lib/{util/ext → strokedb/core_ext}/string.rb +28 -1
- data/lib/strokedb/core_ext/symbol.rb +13 -0
- data/lib/strokedb/data_structures.rb +5 -0
- data/lib/strokedb/data_structures/chunked_skiplist.rb +123 -0
- data/lib/{data_structures → strokedb/data_structures}/inverted_list.rb +0 -0
- data/lib/{data_structures → strokedb/data_structures}/point_query.rb +0 -0
- data/lib/strokedb/data_structures/simple_skiplist.rb +350 -0
- data/lib/{data_structures → strokedb/data_structures}/skiplist.rb +1 -1
- data/lib/{document → strokedb}/document.rb +180 -71
- data/lib/{document → strokedb/document}/callback.rb +0 -0
- data/lib/{document → strokedb/document}/delete.rb +2 -2
- data/lib/strokedb/document/dsl.rb +4 -0
- data/lib/{document → strokedb/document/dsl}/associations.rb +0 -0
- data/lib/{document → strokedb/document/dsl}/coercions.rb +0 -0
- data/lib/strokedb/document/dsl/meta_dsl.rb +7 -0
- data/lib/{document → strokedb/document/dsl}/validations.rb +26 -21
- data/lib/{document → strokedb/document/dsl}/virtualize.rb +0 -0
- data/lib/{document → strokedb/document}/meta.rb +92 -29
- data/lib/{document → strokedb/document}/slot.rb +17 -5
- data/lib/{document → strokedb/document}/util.rb +0 -0
- data/lib/{document → strokedb/document}/versions.rb +2 -2
- data/lib/strokedb/index.rb +2 -0
- data/lib/strokedb/nsurl.rb +24 -0
- data/lib/strokedb/store.rb +149 -0
- data/lib/strokedb/stores.rb +6 -0
- data/lib/{stores → strokedb/stores}/chainable_storage.rb +20 -14
- data/lib/strokedb/stores/file_storage.rb +118 -0
- data/lib/{stores/inverted_list_index → strokedb/stores}/inverted_list_file_storage.rb +50 -0
- data/lib/strokedb/stores/memory_storage.rb +80 -0
- data/lib/{stores → strokedb/stores}/remote_store.rb +10 -4
- data/lib/strokedb/sync.rb +4 -0
- data/lib/{sync → strokedb/sync}/chain_sync.rb +0 -0
- data/lib/{sync → strokedb/sync}/diff.rb +12 -1
- data/lib/{sync/stroke_diff → strokedb/sync/diff}/array.rb +1 -1
- data/lib/{sync/stroke_diff → strokedb/sync/diff}/default.rb +0 -0
- data/lib/{sync/stroke_diff → strokedb/sync/diff}/hash.rb +1 -1
- data/lib/{sync/stroke_diff → strokedb/sync/diff}/string.rb +1 -1
- data/lib/{sync → strokedb/sync}/lamport_timestamp.rb +0 -0
- data/lib/{sync → strokedb/sync}/store_sync.rb +15 -7
- data/lib/strokedb/transaction.rb +78 -0
- data/lib/{util → strokedb}/util.rb +14 -7
- data/lib/strokedb/util/attach_dsl.rb +29 -0
- data/lib/{util → strokedb/util}/blankslate.rb +0 -0
- data/lib/strokedb/util/class_optimization.rb +93 -0
- data/lib/{util → strokedb/util}/inflect.rb +0 -0
- data/lib/strokedb/util/java_util.rb +13 -0
- data/lib/{util → strokedb/util}/lazy_array.rb +0 -0
- data/lib/{util → strokedb/util}/lazy_mapping_array.rb +4 -0
- data/lib/{util → strokedb/util}/lazy_mapping_hash.rb +0 -0
- data/lib/{util → strokedb/util}/serialization.rb +21 -0
- data/lib/strokedb/util/uuid.rb +159 -0
- data/lib/{util → strokedb/util}/xml.rb +0 -0
- data/lib/{view → strokedb}/view.rb +2 -2
- data/lib/strokedb/volumes.rb +5 -0
- data/lib/strokedb/volumes/archive_volume.rb +165 -0
- data/lib/strokedb/volumes/block_volume.rb +169 -0
- data/lib/strokedb/volumes/distributed_pointer.rb +43 -0
- data/lib/strokedb/volumes/fixed_length_skiplist_volume.rb +109 -0
- data/lib/strokedb/volumes/map_volume.rb +268 -0
- data/meta/MANIFEST +175 -0
- data/script/console +2 -70
- data/spec/integration/remote_store_spec.rb +70 -0
- data/spec/integration/search_spec.rb +76 -0
- data/spec/integration/spec_helper.rb +1 -0
- data/spec/lib/spec_helper.rb +1 -0
- data/spec/lib/strokedb/config_spec.rb +250 -0
- data/spec/lib/strokedb/core_ext/blank_spec.rb +20 -0
- data/spec/lib/strokedb/core_ext/extract_spec.rb +42 -0
- data/spec/lib/strokedb/core_ext/float_spec.rb +62 -0
- data/spec/lib/strokedb/core_ext/infinity_spec.rb +40 -0
- data/spec/lib/strokedb/core_ext/spec_helper.rb +1 -0
- data/spec/lib/strokedb/core_ext/string_spec.rb +25 -0
- data/spec/lib/strokedb/core_ext/symbol_spec.rb +8 -0
- data/spec/lib/strokedb/data_structures/chunked_skiplist_spec.rb +144 -0
- data/spec/lib/strokedb/data_structures/inverted_list_spec.rb +172 -0
- data/spec/lib/strokedb/data_structures/simple_skiplist_spec.rb +200 -0
- data/spec/lib/strokedb/data_structures/skiplist_spec.rb +253 -0
- data/spec/lib/strokedb/data_structures/spec_helper.rb +1 -0
- data/spec/lib/strokedb/document/associations_spec.rb +319 -0
- data/spec/lib/strokedb/document/callbacks_spec.rb +134 -0
- data/spec/lib/strokedb/document/coercions_spec.rb +110 -0
- data/spec/lib/strokedb/document/document_spec.rb +1063 -0
- data/spec/lib/strokedb/document/meta_meta_spec.rb +30 -0
- data/spec/lib/strokedb/document/meta_spec.rb +435 -0
- data/spec/lib/strokedb/document/metaslot_spec.rb +43 -0
- data/spec/lib/strokedb/document/slot_spec.rb +130 -0
- data/spec/lib/strokedb/document/spec_helper.rb +1 -0
- data/spec/lib/strokedb/document/validations_spec.rb +1081 -0
- data/spec/lib/strokedb/document/virtualize_spec.rb +80 -0
- data/spec/lib/strokedb/nsurl_spec.rb +73 -0
- data/spec/lib/strokedb/spec_helper.rb +1 -0
- data/spec/lib/strokedb/stores/chained_storages_spec.rb +116 -0
- data/spec/lib/strokedb/stores/spec_helper.rb +1 -0
- data/spec/lib/strokedb/stores/store_spec.rb +201 -0
- data/spec/lib/strokedb/stores/transaction_spec.rb +107 -0
- data/spec/lib/strokedb/sync/chain_sync_spec.rb +43 -0
- data/spec/lib/strokedb/sync/diff_spec.rb +111 -0
- data/spec/lib/strokedb/sync/lamport_timestamp_spec.rb +174 -0
- data/spec/lib/strokedb/sync/slot_diff_spec.rb +164 -0
- data/spec/lib/strokedb/sync/spec_helper.rb +1 -0
- data/spec/lib/strokedb/sync/store_sync_spec.rb +181 -0
- data/spec/lib/strokedb/sync/stroke_diff/array_spec.rb +97 -0
- data/spec/lib/strokedb/sync/stroke_diff/complex_spec.rb +58 -0
- data/spec/lib/strokedb/sync/stroke_diff/hash_spec.rb +144 -0
- data/spec/lib/strokedb/sync/stroke_diff/scalar_spec.rb +23 -0
- data/spec/lib/strokedb/sync/stroke_diff/spec_helper.rb +25 -0
- data/spec/lib/strokedb/sync/stroke_diff/string_spec.rb +61 -0
- data/spec/lib/strokedb/util/attach_dsl_spec.rb +45 -0
- data/spec/lib/strokedb/util/inflect_spec.rb +14 -0
- data/spec/lib/strokedb/util/lazy_array_spec.rb +157 -0
- data/spec/lib/strokedb/util/lazy_mapping_array_spec.rb +174 -0
- data/spec/lib/strokedb/util/lazy_mapping_hash_spec.rb +92 -0
- data/spec/lib/strokedb/util/spec_helper.rb +1 -0
- data/spec/lib/strokedb/util/uuid_spec.rb +46 -0
- data/spec/lib/strokedb/view_spec.rb +228 -0
- data/spec/lib/strokedb/volumes/archive_volume_spec.rb +105 -0
- data/spec/lib/strokedb/volumes/block_volume_spec.rb +100 -0
- data/spec/lib/strokedb/volumes/distributed_pointer_spec.rb +14 -0
- data/spec/lib/strokedb/volumes/fixed_length_skiplist_volume_spec.rb +177 -0
- data/spec/lib/strokedb/volumes/map_volume_spec.rb +172 -0
- data/spec/lib/strokedb/volumes/spec_helper.rb +1 -0
- data/spec/regression/docref_spec.rb +94 -0
- data/spec/regression/meta_spec.rb +23 -0
- data/spec/regression/spec_helper.rb +1 -0
- data/spec/regression/sync_spec.rb +36 -0
- data/spec/spec.opts +7 -0
- data/spec/spec_helper.rb +37 -0
- data/spec/temp/storages/TIMESTAMP +1 -0
- data/spec/temp/storages/UUID +1 -0
- data/spec/temp/storages/database-sync/TIMESTAMP +1 -0
- data/spec/temp/storages/database-sync/UUID +1 -0
- data/spec/temp/storages/database-sync/config +1 -0
- data/spec/temp/storages/database-sync/file/LAST +1 -0
- data/spec/temp/storages/database-sync/file/bd/f6/bdf675e5-8a7b-494e-97f2-f74a14ccd95d.av +0 -0
- data/spec/temp/storages/database-sync/file/uindex.wal +0 -0
- data/spec/temp/storages/database-sync/inverted_list_file/INVERTED_INDEX +1 -0
- data/spec/temp/storages/inverted_list_storage/INVERTED_INDEX +0 -0
- data/strokedb.gemspec +120 -0
- data/task/benchmark.task +9 -0
- data/task/ditz.task +30 -0
- data/task/echoe.rb +17 -0
- data/task/rcov.task +50 -0
- data/task/rdoc.task +10 -0
- data/task/rspec.task +0 -0
- data/vendor/java_inline.rb +106 -0
- data/vendor/rbmodexcl/mrimodexcl.rb +82 -0
- data/vendor/rbmodexcl/rbmodexcl.rb +5 -0
- data/vendor/rbmodexcl/rbxmodexcl.rb +48 -0
- data/vendor/rbmodexcl/spec/unextend_spec.rb +50 -0
- data/vendor/rbmodexcl/spec/uninclude_spec.rb +26 -0
- metadata +271 -79
- data/CONTRIBUTORS +0 -7
- data/CREDITS +0 -13
- data/bin/sdbc +0 -2
- data/lib/init.rb +0 -57
- data/lib/stores/inverted_list_index/inverted_list_index.rb +0 -49
- data/lib/stores/skiplist_store/chunk.rb +0 -119
- data/lib/stores/skiplist_store/chunk_storage.rb +0 -21
- data/lib/stores/skiplist_store/file_chunk_storage.rb +0 -44
- data/lib/stores/skiplist_store/memory_chunk_storage.rb +0 -37
- data/lib/stores/skiplist_store/skiplist_store.rb +0 -217
- data/lib/stores/store.rb +0 -5
- data/lib/sync/stroke_diff/stroke_diff.rb +0 -9
- data/lib/util/ext/object.rb +0 -8
- data/lib/util/java_util.rb +0 -9
- data/lib/util/trigger_partition.rb +0 -136
- data/strokedb.rb +0 -75
@@ -1,3 +1,7 @@
|
|
1
|
+
require 'drb'
|
2
|
+
require 'drb/acl'
|
3
|
+
require 'drb/unix'
|
4
|
+
|
1
5
|
module StrokeDB
|
2
6
|
class Store
|
3
7
|
def remote_server(addr, protocol=:drb)
|
@@ -29,9 +33,10 @@ module StrokeDB
|
|
29
33
|
@server.search(*args).map{ |e| safe_document_from_undumped(e) }
|
30
34
|
end
|
31
35
|
|
32
|
-
def
|
33
|
-
@server.
|
36
|
+
def include?(uuid, version=nil)
|
37
|
+
@server.include?(uuid, version)
|
34
38
|
end
|
39
|
+
alias_method :contains?, :include?
|
35
40
|
|
36
41
|
def head_version(uuid)
|
37
42
|
raw_doc = find(uuid,nil, :no_instantiation => true)
|
@@ -111,9 +116,10 @@ module StrokeDB
|
|
111
116
|
@mutex.synchronize { @store.search(*args) }
|
112
117
|
end
|
113
118
|
|
114
|
-
def
|
115
|
-
!!@mutex.synchronize { @store.
|
119
|
+
def include?(*args)
|
120
|
+
!!@mutex.synchronize { @store.include?(*args) }
|
116
121
|
end
|
122
|
+
alias_method :contains?, :include?
|
117
123
|
|
118
124
|
def head_version(uuid)
|
119
125
|
raw_doc = @mutex.synchronize { find(uuid, nil, :no_instantiation => true) }
|
File without changes
|
@@ -1,5 +1,11 @@
|
|
1
1
|
require 'diff/lcs'
|
2
|
+
|
2
3
|
module StrokeDB
|
4
|
+
|
5
|
+
PATCH_REPLACE = 'R'.freeze
|
6
|
+
PATCH_PLUS = '+'.freeze
|
7
|
+
PATCH_MINUS = '-'.freeze
|
8
|
+
PATCH_DIFF = 'D'.freeze
|
3
9
|
|
4
10
|
class SlotDiffStrategy
|
5
11
|
def self.diff(from, to)
|
@@ -62,7 +68,7 @@ module StrokeDB
|
|
62
68
|
end
|
63
69
|
end
|
64
70
|
|
65
|
-
Diff = Meta.new
|
71
|
+
Diff = Meta.new do
|
66
72
|
|
67
73
|
on_initialization do |diff|
|
68
74
|
diff.added_slots = {} unless diff[:added_slots]
|
@@ -124,3 +130,8 @@ module StrokeDB
|
|
124
130
|
end
|
125
131
|
|
126
132
|
end
|
133
|
+
|
134
|
+
require 'sync/diff/default'
|
135
|
+
require 'sync/diff/string'
|
136
|
+
require 'sync/diff/array'
|
137
|
+
require 'sync/diff/hash'
|
File without changes
|
File without changes
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module StrokeDB
|
2
2
|
|
3
|
-
SynchronizationReport = Meta.new
|
3
|
+
SynchronizationReport = Meta.new do
|
4
4
|
on_new_document do |report|
|
5
5
|
report.conflicts = []
|
6
6
|
report.added_documents = []
|
@@ -9,7 +9,7 @@ module StrokeDB
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
SynchronizationConflict = Meta.new
|
12
|
+
SynchronizationConflict = Meta.new do
|
13
13
|
def resolve!
|
14
14
|
# by default, do nothing
|
15
15
|
end
|
@@ -22,7 +22,7 @@ module StrokeDB
|
|
22
22
|
existing_chain = {}
|
23
23
|
docs.group_by {|doc| doc.uuid}.each_pair do |uuid, versions|
|
24
24
|
doc = find(uuid)
|
25
|
-
existing_chain[uuid] = doc.versions.all_versions if doc
|
25
|
+
existing_chain[uuid] = doc.versions.all_versions.map {|v| [v, doc.versions[v].to_json ]} if doc
|
26
26
|
end
|
27
27
|
case _timestamp
|
28
28
|
when Numeric
|
@@ -31,9 +31,11 @@ module StrokeDB
|
|
31
31
|
@timestamp = LTS.new(_timestamp.counter, timestamp.uuid)
|
32
32
|
else
|
33
33
|
end
|
34
|
-
|
34
|
+
@txn = Transaction.new(:store => self)
|
35
|
+
@txn.execute do |txn|
|
36
|
+
docs.each {|doc| save!(doc) }
|
35
37
|
docs.group_by {|doc| doc.uuid}.each_pair do |uuid, versions|
|
36
|
-
incoming_chain = find(uuid, versions.last.version).versions.all_versions
|
38
|
+
incoming_chain = find(uuid, versions.last.version).versions.all_versions.map {|v| [v, find(uuid,v).to_json ]}
|
37
39
|
if existing_chain[uuid].nil? or existing_chain[uuid].empty? # It is a new document
|
38
40
|
added_doc = find(uuid, versions.last.version)
|
39
41
|
save_as_head!(added_doc)
|
@@ -43,6 +45,7 @@ module StrokeDB
|
|
43
45
|
sync = sync_chains(incoming_chain.reverse, existing_chain[uuid].reverse)
|
44
46
|
rescue NonMatchingChains
|
45
47
|
# raise NonMatchingDocumentCondition.new(uuid) # that will definitely leave garbage in the store (FIXME?)
|
48
|
+
txn.rollback!
|
46
49
|
non_matching_doc = find(uuid)
|
47
50
|
report.non_matching_documents << non_matching_doc
|
48
51
|
next
|
@@ -51,17 +54,22 @@ module StrokeDB
|
|
51
54
|
case resolution
|
52
55
|
when :up_to_date
|
53
56
|
# nothing to do
|
57
|
+
txn.commit!
|
54
58
|
when :merge
|
55
|
-
report.conflicts << SynchronizationConflict.create!(self, :document => find(uuid), :rev1 => sync[1], :rev2 => sync[2])
|
59
|
+
report.conflicts << SynchronizationConflict.create!(self, :document => find(uuid), :rev1 => sync[1].map{|e| e[0]}.reverse, :rev2 => sync[2].map{|e| e[0]}.reverse)
|
60
|
+
txn.commit!
|
56
61
|
when :fast_forward
|
57
|
-
fast_forwarded_doc = find(uuid, sync[1].last)
|
62
|
+
fast_forwarded_doc = find(uuid, sync[1].last.first)
|
58
63
|
save_as_head!(fast_forwarded_doc)
|
59
64
|
report.fast_forwarded_documents << fast_forwarded_doc
|
65
|
+
txn.commit!
|
60
66
|
else
|
67
|
+
txn.rollback!
|
61
68
|
raise "Invalid sync resolution #{resolution}"
|
62
69
|
end
|
63
70
|
end
|
64
71
|
end
|
72
|
+
end
|
65
73
|
report.conflicts.each do |conflict|
|
66
74
|
if resolution_strategy = conflict.document.meta[:resolution_strategy]
|
67
75
|
conflict.metas << resolution_strategy
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module StrokeDB
|
2
|
+
|
3
|
+
class Transaction
|
4
|
+
|
5
|
+
attr_reader :uuid
|
6
|
+
|
7
|
+
def initialize(options = {})
|
8
|
+
@options = options.stringify_keys
|
9
|
+
@uuid = Util.random_uuid
|
10
|
+
storage.add_chained_storage!(store.storage)
|
11
|
+
end
|
12
|
+
|
13
|
+
def storage
|
14
|
+
@options['storage'] ||= setup_storage
|
15
|
+
end
|
16
|
+
|
17
|
+
def store
|
18
|
+
@options['store']
|
19
|
+
end
|
20
|
+
|
21
|
+
def inspect
|
22
|
+
"#<Transaction #{@uuid}>"
|
23
|
+
end
|
24
|
+
|
25
|
+
def find(uuid, version=nil, opts = {})
|
26
|
+
storage.find(uuid,version,opts.merge(:store => self))
|
27
|
+
end
|
28
|
+
|
29
|
+
def head_version(uuid)
|
30
|
+
storage.head_version(uuid,{ :store => self })
|
31
|
+
end
|
32
|
+
|
33
|
+
def save!(doc)
|
34
|
+
@timestamp = @timestamp.next
|
35
|
+
storage.save!(doc,@timestamp)
|
36
|
+
end
|
37
|
+
|
38
|
+
def execute
|
39
|
+
raise ArgumentError, "no block provided" unless block_given?
|
40
|
+
|
41
|
+
Thread.current[:strokedb_transactions] ||= []
|
42
|
+
Thread.current[:strokedb_transactions].push self
|
43
|
+
|
44
|
+
@timestamp = LTS.new(store.timestamp.counter,uuid)
|
45
|
+
|
46
|
+
begin
|
47
|
+
result = yield(self)
|
48
|
+
rescue
|
49
|
+
throw $!
|
50
|
+
ensure
|
51
|
+
Thread.current[:strokedb_transactions].pop
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
result
|
56
|
+
end
|
57
|
+
|
58
|
+
def commit!
|
59
|
+
storage.sync_chained_storage!(store.storage)
|
60
|
+
true
|
61
|
+
end
|
62
|
+
|
63
|
+
def rollback!
|
64
|
+
@options['storage'] = setup_storage
|
65
|
+
true
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def setup_storage
|
71
|
+
storage = MemoryStorage.new
|
72
|
+
storage.authoritative_source = store.storage
|
73
|
+
storage
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -17,13 +17,6 @@ module StrokeDB
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
unless RUBY_PLATFORM =~ /java/
|
21
|
-
require 'uuidtools'
|
22
|
-
def self.random_uuid
|
23
|
-
::UUID.random_create.to_s
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
20
|
class CircularReferenceCondition < Exception ; end
|
28
21
|
class << self
|
29
22
|
def catch_circular_reference(value,name = 'StrokeDB.reference_stack')
|
@@ -35,4 +28,18 @@ module StrokeDB
|
|
35
28
|
end
|
36
29
|
end
|
37
30
|
end
|
31
|
+
|
38
32
|
end
|
33
|
+
|
34
|
+
require 'util/blankslate'
|
35
|
+
require 'util/class_optimization'
|
36
|
+
require 'util/inflect'
|
37
|
+
require 'util/lazy_array'
|
38
|
+
require 'util/lazy_mapping_array'
|
39
|
+
require 'util/lazy_mapping_hash'
|
40
|
+
require 'util/serialization'
|
41
|
+
require 'util/uuid'
|
42
|
+
require 'util/xml'
|
43
|
+
require 'util/attach_dsl'
|
44
|
+
require 'time'
|
45
|
+
require 'util/java_util' if RUBY_PLATFORM =~ /java/
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module StrokeDB
|
2
|
+
class ::Module
|
3
|
+
|
4
|
+
# Attach module as DSL. Module may use store_dsl_options
|
5
|
+
# method to store DSL.
|
6
|
+
#
|
7
|
+
def attach_dsl(*mods)
|
8
|
+
@dsl_options ||= {}
|
9
|
+
mods.each do |mod|
|
10
|
+
extend(mod)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
alias :attach_dsls :attach_dsl
|
14
|
+
|
15
|
+
# Store some data associated with a DSL name.
|
16
|
+
#
|
17
|
+
def store_dsl_options(name, opts)
|
18
|
+
@dsl_options[name] = opts
|
19
|
+
end
|
20
|
+
|
21
|
+
# Return a map of registered DSLs.
|
22
|
+
#
|
23
|
+
def dsl
|
24
|
+
@dsl_options
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
File without changes
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module StrokeDB
|
2
|
+
class ::Class
|
3
|
+
# Declare which methods are optimized for particular language.
|
4
|
+
# It is assumed, that optimized method name looks that way:
|
5
|
+
# <tt>method_name_(language name)</tt>
|
6
|
+
#
|
7
|
+
# If you supply a block of code, it will be executed in a context of a class
|
8
|
+
# each time <tt>optimize!</tt> is called.
|
9
|
+
#
|
10
|
+
# You may add some exception handling where you call <tt>optimize!</tt>.
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# # assume, there're methods find_C and insert_C
|
15
|
+
# declare_optimized_methods(:C, :find, :insert) { require 'bundle' }</tt>
|
16
|
+
#
|
17
|
+
def declare_optimized_methods(lang, *meths, &block)
|
18
|
+
meths.flatten!
|
19
|
+
@optimized_methods ||= {}
|
20
|
+
@optimized_methods_init ||= {}
|
21
|
+
@optimized_methods[lang.to_s] = meths
|
22
|
+
@optimized_methods_init[lang.to_s] = block
|
23
|
+
extend ClassOptimization::ClassMethods
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns a list of optimized methods for a given language.
|
27
|
+
# If no language given, a Hash is returned where key is a language name.
|
28
|
+
#
|
29
|
+
def optimized_methods(lang = nil)
|
30
|
+
@optimized_methods ||= {}
|
31
|
+
return @optimized_methods unless lang
|
32
|
+
@optimized_methods[lang.to_s] || []
|
33
|
+
end
|
34
|
+
end
|
35
|
+
module ClassOptimization
|
36
|
+
module ClassMethods
|
37
|
+
# Switches methods into optimized versions as declared in
|
38
|
+
# <tt>declare_optimized_methods</tt>.
|
39
|
+
# Pure ruby methods become accessible with suffix _PureRuby.
|
40
|
+
#
|
41
|
+
def optimize!(lang)
|
42
|
+
if block = @optimized_methods_init[lang.to_s]
|
43
|
+
self.instance_eval(&block)
|
44
|
+
end
|
45
|
+
optimized_methods(lang).each do |meth|
|
46
|
+
alias_method(:"#{meth}_PureRuby", :"#{meth}")
|
47
|
+
alias_method(:"#{meth}", :"#{meth}_#{lang}")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Reverts method optimization done with <tt>optimize!</tt>
|
52
|
+
# Note: you may call this method only after optimize! was called.
|
53
|
+
#
|
54
|
+
def deoptimize!(lang)
|
55
|
+
optimized_methods(lang).each do |meth|
|
56
|
+
alias_method(:"#{meth}", :"#{meth}_PureRuby")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Executes code in a block with optimizations turned on.
|
61
|
+
# This ensures that appropriate <tt>deoptimize!</tt> method is called.
|
62
|
+
#
|
63
|
+
def optimized_with(lang)
|
64
|
+
optimize!(lang)
|
65
|
+
yield
|
66
|
+
ensure
|
67
|
+
deoptimize!(lang)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Iterates through all the optimizations. Non-optimized
|
71
|
+
# mode ("pure Ruby") is yielded first.
|
72
|
+
# Useful for testing and benchmarks.
|
73
|
+
#
|
74
|
+
# Example:
|
75
|
+
#
|
76
|
+
# Klass.with_optimizations(:InlineC) do |lang|
|
77
|
+
# puts "Klass#some_method is written in #{lang}"
|
78
|
+
# puts Klass.new.some_method
|
79
|
+
# end
|
80
|
+
#
|
81
|
+
def with_optimizations(*langs)
|
82
|
+
langs.flatten!
|
83
|
+
yield("pure Ruby")
|
84
|
+
langs.each do |lang|
|
85
|
+
optimized_with(lang) do
|
86
|
+
yield(lang)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
self
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
File without changes
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'java'
|
2
|
+
# Some java overrides
|
3
|
+
module StrokeDB
|
4
|
+
module Util
|
5
|
+
def self.random_uuid
|
6
|
+
java.util.UUID.random_uuid.to_s
|
7
|
+
end
|
8
|
+
def self.random_uuid_raw
|
9
|
+
uuid = java.util.UUID.random_uuid
|
10
|
+
[uuid.most_significant_bits, uuid.least_significant_bits].pack("Q2")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
File without changes
|
File without changes
|