extralite 2.5 → 2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +34 -13
  4. data/Gemfile +4 -0
  5. data/Gemfile-bundle +1 -1
  6. data/LICENSE +1 -1
  7. data/README.md +1059 -247
  8. data/Rakefile +18 -0
  9. data/TODO.md +0 -7
  10. data/examples/kv_store.rb +49 -0
  11. data/examples/multi_fiber.rb +16 -0
  12. data/examples/on_progress.rb +9 -0
  13. data/examples/pubsub_store_polyphony.rb +194 -0
  14. data/examples/pubsub_store_threads.rb +204 -0
  15. data/ext/extralite/changeset.c +463 -0
  16. data/ext/extralite/common.c +177 -91
  17. data/ext/extralite/database.c +745 -276
  18. data/ext/extralite/extconf-bundle.rb +10 -4
  19. data/ext/extralite/extconf.rb +34 -34
  20. data/ext/extralite/extralite.h +104 -47
  21. data/ext/extralite/extralite_ext.c +6 -0
  22. data/ext/extralite/iterator.c +14 -86
  23. data/ext/extralite/query.c +171 -264
  24. data/extralite-bundle.gemspec +1 -1
  25. data/extralite.gemspec +1 -1
  26. data/gemspec.rb +10 -11
  27. data/lib/extralite/version.rb +1 -1
  28. data/lib/extralite.rb +69 -10
  29. data/lib/sequel/adapters/extralite.rb +1 -1
  30. data/test/helper.rb +9 -1
  31. data/test/perf_argv_transform.rb +74 -0
  32. data/test/perf_ary.rb +14 -12
  33. data/test/perf_hash.rb +17 -15
  34. data/test/perf_hash_prepared.rb +58 -0
  35. data/test/perf_hash_transform.rb +66 -0
  36. data/test/perf_polyphony.rb +74 -0
  37. data/test/test_changeset.rb +161 -0
  38. data/test/test_database.rb +720 -104
  39. data/test/test_extralite.rb +2 -2
  40. data/test/test_iterator.rb +28 -13
  41. data/test/test_query.rb +352 -110
  42. data/test/test_sequel.rb +4 -4
  43. metadata +24 -16
  44. data/Gemfile.lock +0 -37
  45. data/test/perf_prepared.rb +0 -64
@@ -0,0 +1,161 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+
5
+ require 'date'
6
+ require 'tempfile'
7
+
8
+ class ChangesetTest < Minitest::Test
9
+ def setup
10
+ @db = Extralite::Database.new(':memory:')
11
+ skip if !@db.respond_to?(:track_changes)
12
+
13
+ @db.execute('create table if not exists t (x integer primary key, y, z)')
14
+ end
15
+
16
+ def test_each
17
+ changeset = Extralite::Changeset.new
18
+
19
+ changeset.track(@db, [nil]) do
20
+ @db.execute('insert into t values (1, 2, 3)')
21
+ end
22
+ changes = []
23
+ changeset.each { |*o| changes << o }
24
+ assert_equal [
25
+ [:insert, 't', nil, [1, 2, 3]]
26
+ ], changes
27
+
28
+
29
+ changeset.track(@db, [nil]) do
30
+ @db.execute('update t set y = 22 where x = 1')
31
+ end
32
+ changes = []
33
+ changeset.each { |*o| changes << o }
34
+ assert_equal [
35
+ [:update, 't', [1, 2, nil], [nil, 22, nil]]
36
+ ], changes
37
+
38
+
39
+ changeset.track(@db, [nil]) do
40
+ @db.execute('delete from t where x = 1')
41
+ end
42
+ changes = []
43
+ changeset.each { |*o| changes << o }
44
+ assert_equal [
45
+ [:delete, 't', [1, 22, 3], nil]
46
+ ], changes
47
+ end
48
+
49
+ def test_to_a
50
+ changeset = Extralite::Changeset.new
51
+
52
+ changeset.track(@db, [nil]) do
53
+ @db.execute('insert into t values (1, 2, 3)')
54
+ @db.execute('insert into t values (4, 5, 6)')
55
+ end
56
+ assert_equal [
57
+ [:insert, 't', nil, [1, 2, 3]],
58
+ [:insert, 't', nil, [4, 5, 6]]
59
+ ], changeset.to_a
60
+
61
+
62
+ changeset.track(@db, [nil]) do
63
+ @db.execute('update t set y = 22.22 where z < 10')
64
+ end
65
+ assert_equal [
66
+ [:update, 't', [1, 2, nil], [nil, 22.22, nil]],
67
+ [:update, 't', [4, 5, nil], [nil, 22.22, nil]]
68
+ ], changeset.to_a
69
+
70
+
71
+ changeset.track(@db, [nil]) do
72
+ @db.execute('delete from t where x = 1')
73
+ end
74
+ assert_equal [
75
+ [:delete, 't', [1, 22.22, 3], nil]
76
+ ], changeset.to_a
77
+ end
78
+
79
+ def test_apply
80
+ changeset = Extralite::Changeset.new
81
+
82
+ changeset.track(@db, [:t]) do
83
+ @db.execute('insert into t values (1, 2, 3)')
84
+ @db.execute('insert into t values (4, 5, 6)')
85
+ end
86
+
87
+ db2 = Extralite::Database.new(':memory:')
88
+ db2.execute('create table if not exists t (x integer primary key, y, z)')
89
+
90
+ changeset.apply(db2)
91
+
92
+ assert_equal [
93
+ { x: 1, y: 2, z: 3 },
94
+ { x: 4, y: 5, z: 6 }
95
+ ], db2.query('select * from t')
96
+ end
97
+
98
+ def test_invert
99
+ changeset = Extralite::Changeset.new
100
+
101
+ changeset.track(@db, [:t]) do
102
+ @db.execute('insert into t values (1, 2, 3)')
103
+ @db.execute('insert into t values (4, 5, 6)')
104
+ end
105
+
106
+ db2 = Extralite::Database.new(':memory:')
107
+ db2.execute('create table if not exists t (x integer primary key, y, z)')
108
+
109
+ changeset.apply(db2)
110
+
111
+ assert_equal [
112
+ { x: 1, y: 2, z: 3 },
113
+ { x: 4, y: 5, z: 6 }
114
+ ], db2.query('select * from t')
115
+
116
+ db2.execute('insert into t values (7, 8, 9)')
117
+ inverted = changeset.invert
118
+
119
+ assert_kind_of Extralite::Changeset, inverted
120
+ refute_equal inverted, changeset
121
+
122
+ assert_equal [
123
+ [:delete, 't', [1, 2, 3], nil],
124
+ [:delete, 't', [4, 5, 6], nil],
125
+ ], inverted.to_a
126
+
127
+ inverted.apply(@db)
128
+ assert_equal [], @db.query('select * from t')
129
+ end
130
+
131
+ def test_blob
132
+ changeset = Extralite::Changeset.new
133
+ assert_equal '', changeset.to_blob
134
+
135
+ changeset.track(@db, [:t]) do
136
+ @db.execute('insert into t values (1, 2, 3)')
137
+ @db.execute('insert into t values (4, 5, 6)')
138
+ end
139
+
140
+ blob = changeset.to_blob
141
+ assert_kind_of String, blob
142
+ assert_equal Encoding::ASCII_8BIT, blob.encoding
143
+ assert !blob.empty?
144
+
145
+ c2 = Extralite::Changeset.new
146
+ c2.load(blob)
147
+ assert_equal c2.to_blob, blob
148
+
149
+ assert_equal [
150
+ [:insert, 't', nil, [1, 2, 3]],
151
+ [:insert, 't', nil, [4, 5, 6]]
152
+ ], c2.to_a
153
+ end
154
+
155
+ def test_empty_blob
156
+ changeset = Extralite::Changeset.new
157
+ changeset.load('')
158
+
159
+ assert_raises(Extralite::Error) { changeset.to_a }
160
+ end
161
+ end