qwe 0.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a2a6e78eee154ac187e57a6f648989edbeb98bb6d09ee98118a386148f04d24d
4
+ data.tar.gz: a65ced572c633210b481e3779dce21524858687c42fd3667e71727e4f165ceb9
5
+ SHA512:
6
+ metadata.gz: 428a88b7fd3e63e9236931cc65d0a123f272e71e503bd7cbb0a309e23440b57d4d8421ac442cf6be993f088978f39ef57563f246c4f44ecc49828f8475ce11b9
7
+ data.tar.gz: d5536e2f23c54ae7aaa8c09384ed72f3d94f7af4224a93bdfca35d74a7b11d67807d8f13602758a0bcfe2d230167e7c41ab2af839afb94bc7419ccae2e5570e9
data/DOCS.md ADDED
@@ -0,0 +1,469 @@
1
+ # Starting server
2
+
3
+ ## Command line
4
+
5
+ Gem provides `qwe` executable, which starts the server by default. See `qwe -h` for details.
6
+
7
+ `-r` parameter is required for entrypoint to load your classes.
8
+ Int rails it's reasonable to have `config/qwe.rb` for that purpose:
9
+ ```ruby
10
+ APP_PATH = File.realpath("../")
11
+
12
+ # Load the Rails application.
13
+ require_relative "application"
14
+
15
+ # Initialize the Rails application.
16
+ Rails.application.initialize!
17
+ ```
18
+ And server startup would be
19
+ ```
20
+ qwe -r config/qwe.rb
21
+ ```
22
+ or
23
+ ```
24
+ bundle exec qwe -r config/qwe.rb
25
+ ```
26
+ depending on your setup.
27
+
28
+ ## Development server example
29
+
30
+ Autoloaders will handle includes,
31
+ but the behavior of qwe on constant reloading is uncertain.
32
+ In development you can restart server on code changes with watch (`-w`) option.
33
+ Also it may be reasonable to limit worker threads count `-t` to not use all your cores,
34
+ and discard experiments by saving data to `/tmp`:
35
+
36
+ ```
37
+ qwe -r config/requirements.rb -w app -w config -t 2 -d /tmp/qwe_db
38
+ ```
39
+
40
+ ## Puma plugin
41
+
42
+ Qwe server can be started in the same process as rails or any other puma application.
43
+ With `plugin :qwe` in `config/puma.rb` server will start alongside rails on `rails server`.
44
+
45
+ Configuration of server started from puma is done with `config/qwe.yml`. Valid config keys
46
+ are last variants of cli argument names. For example, detach timeout can be set with
47
+ `-s`, `--detach` and `--detach_timeout` cli options, or `detach_timeout` config key.
48
+
49
+ ## Production example
50
+
51
+ config/puma.rb
52
+ ```ruby
53
+ # ...
54
+ plugin :qwe if ENV["Qwe_IN_PUMA"]
55
+ # ...
56
+ ```
57
+ config/qwe.yml
58
+ ```yml
59
+ dir: ./storage/qwe_db
60
+ require_file: ./config/qwe.rb
61
+ detach_timeout: 100
62
+ ```
63
+
64
+ # Connecting to server
65
+
66
+ Each time you invoke `Qwe::DB` methods, it tries to connect to localhost at default port.
67
+ If the server is running differently, you can connect to it once per process with
68
+ ```ruby
69
+ Qwe::DB.connect("druby://example.com:1234")
70
+ ```
71
+
72
+ ## Creating an object
73
+
74
+ ```ruby
75
+ Qwe::DB.create(MyModule::MyClass)
76
+ ```
77
+
78
+ You can also create an instance of class which is unknown locally,
79
+ but known to server by passing symbol instead of constant:
80
+ ```ruby
81
+ Qwe::DB.create(:MyClass)
82
+ ```
83
+
84
+ Also an object can be created from a string dump:
85
+ ```ruby
86
+ class A
87
+ ...
88
+ end
89
+
90
+ a = A.new
91
+ ...
92
+
93
+ Qwe::DB.create(Marshal.dump(a))
94
+ ```
95
+
96
+ Physically records are stored in `qwe_db/records/id/`. There would be a 4 files:
97
+ - `0` - Marshal dump of an object right after creation.
98
+ - `dump` - current marshal dump.
99
+ - `commits` or `commits.zst` - ruby code of all the changes.
100
+ - `meta` - JSON metadata.
101
+
102
+ Usually you don't need to mess up with these files,
103
+ but simple file-based storage makes it possible if needed.
104
+
105
+ ## Deleting objects
106
+
107
+ ```ruby
108
+ Qwe::DB.destroy(id)
109
+ ```
110
+
111
+ Or just rm -r record files, it's fine unless record is in use.
112
+
113
+ ## Accessing objects
114
+
115
+ `Qwe::DB.create` returns integer id of the record.
116
+
117
+ `Qwe::DB[id]` returns DRb reference to your object. Methods you invoke on that reference
118
+ will be delegated to the object living on the server, and return value is transmitted back.
119
+
120
+ To avoid confusions with DRb, prevent creation of unnecessary references, and reduce lag,
121
+ the best practice is not to descend on method chains whenever possible.
122
+
123
+ ```ruby
124
+ Qwe::DB[id].one.two.three(arg)
125
+ ```
126
+ will ping server 4 times. Instead, you should
127
+ ```ruby
128
+ class A
129
+ def :one_two_three(...)
130
+ one.two.three(...)
131
+ end
132
+ end
133
+ ```
134
+ ```ruby
135
+ Qwe::DB[id].one_two_three(arg)
136
+ ```
137
+
138
+ So it is preferable, but not required, to interact remotely only with the root object.
139
+
140
+ Return values are transmitted through network sockets, so ensuring you don't return large objects unless necessary also increases performance.
141
+
142
+ ## Managing object state
143
+
144
+ Your object can be either in RAM, or dumped to disk.
145
+
146
+ Whenever you access an object with `Qwe::DB[]`, it gets loaded in RAM unless it's already there.
147
+
148
+ It can be put back on disk (detached) with `Qwe::DB[id].record.detach`.
149
+
150
+ Unless the server is explicitly started with `-d 0`, object will detach on it's own after `detach_timeout`, default 5 min.
151
+ `record.keep` will prevent this by scheduling detachment at now + configured timeout.
152
+
153
+ # The root and things
154
+
155
+ `Qwe::Mixins::Root` mix-in provides a way to mark your object as **root**.
156
+
157
+ Everything that is stored in top-level `Qwe::DB` should include it.
158
+ In other words, when you `Qwe::DB.create(MyClass)`, `MyClass` should include `Root`.
159
+ To avoid loosing objects to garbage collector, everything else that should persist
160
+ is attached to root object. So `Root` is like a container for smaller things.
161
+
162
+ `Qwe::Mixins::Thing` makes object attachable to root and provides attributes API.
163
+ `Root` includes it too. Therefore most of your classes should include `Thing`, and one should include `Root`.
164
+
165
+ ```ruby
166
+ class Passenger
167
+ include Qwe::Mixins::Thing
168
+ attribute :name, convert: :to_s
169
+ end
170
+
171
+ class Bus
172
+ include Qwe::Mixins::Root
173
+ attribute :passengers, :array
174
+
175
+ def board(name)
176
+ passengers << create(:Passenger, name:)
177
+ end
178
+ end
179
+ ```
180
+ ```ruby
181
+ Qwe::DB.create(:Bus)
182
+ ```
183
+
184
+ ## `Root` instance methods
185
+
186
+
187
+ - `create(type, **attrs)` - create a new `Thing`, and attach it to self.
188
+ optional `**attrs` provide initial attribute key-values.
189
+ - `self[id]`, `things[id]` - whenever you create a thing, root stores it in `@things` array.
190
+ `[]` is shorthand for accessing them.
191
+ - `record` - Reference to `Qwe::DB::Record` containing object, see [later](#Record)
192
+ - `commit(thing, method, *args, **keywords)` - Write to commits command to call `method` on `thing` with respective arguments.
193
+
194
+ ## `Thing` instance methods
195
+
196
+ - `root` - reference to `Root` object.
197
+ - `id` - respective index in root collection, so `root[id]` is `self`
198
+
199
+ ## `Thing` class methods
200
+
201
+ - `attribute(...)` - `attr_accessor` with extras, see [attributes](#Attributes)
202
+ - `attributes` - hash of attributes associated with this class.
203
+ - `init` - init function, see [functions](#Functions)
204
+
205
+ # Record
206
+
207
+ `Qwe::DB::Record` represents all the logic around storing and serving your root, and can be accessed as `.record` from there.
208
+
209
+ ## Record Instance methods:
210
+
211
+ - `id` - the same id you use in `Qwe::DB[]`, represents folder name where the record is stored.
212
+ - `object` - reference to contained object.
213
+ - `keep` - delay detachment until `Time.now + detach_timeout`.
214
+ - `detach` - detach immediately
215
+ - `save` - save immediately. Object will be saved anyway on detach, but you can save twice just to be sure.
216
+ - `commits(line = nil)` - commits until `line` as a string. All commits if `line` is omitted.
217
+ - `commits_length` - Current amount of lines in commits file. Some commits may be
218
+ multi-line, so it isn't equal to commits count.
219
+ - `commit(str)` - write string to commits file. Attributes do that for you.
220
+ - `object_at(line)` - returns historic object state, or wayback, by evaluating commits until `line` in context of initial object state.
221
+ - `fork(line)` - same as object_at, but creates a new record. Returns newly created `Qwe::DB::Record`.
222
+ - `archive!` - compress commits file with `zst`. Requires native `zstd` package, see [ruby zstd docs](https://github.com/andrew-aladev/ruby-zstds?tab=readme-ov-file#installation). If commit is written into archived record, file will get unpacked with a warning.
223
+ - `dir` - path to the record directory in filesystem
224
+ - `dump` - `Marshal.dump(object)`
225
+
226
+ # Attributes
227
+
228
+ `Qwe::Mixins::Thing` provides `attribute` method, which behaves like `:attr_accessor`,
229
+ but writer commits every change.
230
+
231
+ ```ruby
232
+ attribute(name, *booleans, writer: nil, reader: nil, **params)
233
+ ```
234
+
235
+ Any positional argument after first (name) is shorthand for true in params.
236
+ For example, these are equal invocations:
237
+ ```ruby
238
+ attribute :one, array: true
239
+ attribute :one, :array
240
+ ```
241
+
242
+ Beyond committing, attributes have a few additional parameters:
243
+ - `writer` - `Proc` - define custom writer, but keep responsible for commits part of native writer. Should return value instead of setting instance variable.
244
+ ```ruby
245
+ attribute :qwe, writer: ->(value) { value**2 }
246
+ ```
247
+ - `reader` - `Proc` - replace reader entirely with given block.
248
+ ```ruby
249
+ attribute :qwe, reader: -> { @my_attr || self.class.my_attr_default || self.class.my_attr_fallback }
250
+ ```
251
+ - `convert` - `Symbol` - call this method on argument, used mostly for type conversions.
252
+ ```ruby
253
+ attribute :qwe, convert: :to_f
254
+ ```
255
+ - `init` - `Proc`, `Class` or any object - on initialization set attribute to
256
+ `Proc` return value, `Class.new` or any value.
257
+ Initialization happens in root.create, not constructor.
258
+ ```ruby
259
+ attribute :qwe, init: 123
260
+ attribute :asd, :array, init: [1, 2, 3]
261
+ attribute :zxc, init: -> { Time.now }
262
+ ```
263
+ - `default` - same as init, but value is set at reader if it is nil, like `@value || 123`. Since reader checks whether the value is falsy at every call, it is a bit slower.
264
+ - `min`, `max` - set value to min/max on write if it is lesser/greater.
265
+ ```ruby
266
+ attribute :qwe, min: 10
267
+ attribute :asd, max: 20
268
+ ```
269
+ - `array` - `bool` - initializes attribute to `Qwe::Attribute::ArrayProxy`, which is like array, but commits changes, see [limitations](#Limitations) on why it matters.
270
+ - `enum` - `Array` or any object responding to `include?` - raise on write if value is not included in provided enum.
271
+ ```ruby
272
+ attribute :weather, enum: [:hot, :cold, :normal]
273
+ ```
274
+
275
+ # Functions
276
+
277
+ To achieve desired performance, attributes are eval-compiled.
278
+ `Qwe::Function` provides a simple way to join a few lines to a method.
279
+ Performance is gained, for example, by checking whether an attribute has
280
+ min, max, convert, etc... at server startup instead of every method call.
281
+
282
+ ## Function instance methods
283
+ - `new(klass, name)` - creates an instance. Despite being instances, functions are created at module level.
284
+ - `arg(name, default)` - add positional argument
285
+ - `args` - hash of all arguments
286
+ - `keyword(name, default)` - add keyword argument
287
+ - `keywords` - hash of all keywords
288
+ - `stage(string)` - add block of code
289
+ - `code` - source code to be evaluated
290
+ - `compile` - eval code and define method
291
+
292
+ Creating function for a class defines `compile` method on it, which compiles all functions for that class.
293
+
294
+ On startup worker compiles all existing functions, but if you are lazy-loading constants
295
+ (it's on by default in non-production rails environments),
296
+ adding `compile` before closing class is necessary.
297
+ Or just enable enable eager loading, in rails it's in `confg/environments`.
298
+
299
+ ```ruby
300
+ class A
301
+ @func = Qwe::Function.new(self, :func)
302
+ @func.stage("do_something(1 + 1)")
303
+ @func.stage("do_something_else") if 1 == 1.0
304
+
305
+ # ...
306
+
307
+ compile
308
+ end
309
+
310
+ # Equivalent to:
311
+ class A
312
+ def func
313
+ do_something(1 + 1)
314
+ do_something_else
315
+ end
316
+ end
317
+ ```
318
+
319
+ Using compile multiple times is safe - function won't be re-evaluated unless it's changed.
320
+
321
+ ## Init function
322
+
323
+ Functions are used for attribute reader, writer, and `Thing` initialization.
324
+
325
+ `Thing` and `Root` don't use initializers in any way.
326
+ Instead, root calls `init` function when creating thing with `create` method.
327
+
328
+ Init function is accessible as `init` class method, so you can extend it like that:
329
+
330
+ ```ruby
331
+ class A
332
+ init.stage "do_something"
333
+
334
+ def do_something
335
+ # ...
336
+ end
337
+
338
+ compile
339
+ end
340
+ ```
341
+
342
+ This will `do_something` each time `A` instance is created.
343
+
344
+ Standard ruby initializers are left for possible future hacking.
345
+
346
+ # Extending attributes
347
+
348
+ Instead of directly manipulating attribute functions,
349
+ you can extend attributes API itself with `add(param = nil, &block)` method.
350
+ If param is passed, block will be executed only if attribute
351
+ was invoked with this parameter.
352
+
353
+ Block is evaluated in `Qwe::Attribute` instance context.
354
+ Attribute instance is created every time you invoke `attribute` class method, and represents specific attribute of specific class.
355
+
356
+ ## `Qwe::Attribute` instance methods
357
+
358
+ - `klass` - class for which attribute is declared.
359
+ - `name` - attribute name.
360
+ - `writer` - writer function. It has single argument `value`.
361
+ At the last stage sets instance variable corresponding to attribute name to this value,
362
+ so you should modify `value` instead of directly setting instance variable.
363
+ - `reader` - reader function with no arguments. Returns respective instance variable.
364
+ - `[keyword]` - returns keyword or boolean passed to `attribute` method. Also available with respond_to, i.e. `self.min`
365
+
366
+ ```ruby
367
+ Qwe::Attribute.add(:rand) do
368
+ init.stage "self.#{name} = rand"
369
+ end
370
+
371
+ Qwe::Attribute.add(:plus) do
372
+ klass.class_variable_set(:"@@#{name}_plus", plus)
373
+ writer.stage "value += @@#{name}_plus"
374
+ end
375
+
376
+ class A
377
+ include Qwe::Mixins::Thing
378
+
379
+ attribute :one, :rand
380
+ attribute :two, plus: 3
381
+ end
382
+
383
+ a = A.new
384
+ a.one # Random value between 0 and 1
385
+ a.two = 1
386
+ a.two # 4
387
+ ```
388
+
389
+ # Commits internals
390
+
391
+ Commits work by implementing `to_rb` method on all classes of interest - Integer, Float, Time, etc.
392
+ This method is implemented for most standard types, but if something is missing you'll get an exception on commit, complaining that `to_rb` method is undefined.
393
+
394
+ In that case just define `to_rb` to return object as a valid ruby code string in context of root.
395
+ Here are a few examples of bundled to_rb's:
396
+
397
+ ```ruby
398
+ class Integer
399
+ alias_method :to_rb, :to_s
400
+ end
401
+
402
+ class Rational
403
+ def to_rb
404
+ to_s + "r"
405
+ end
406
+ end
407
+
408
+ class Time
409
+ def to_rb
410
+ "Time.at(#{to_r}r)"
411
+ end
412
+ end
413
+ ```
414
+
415
+ # Limitations
416
+
417
+ ## In-place modifications of attributes
418
+
419
+ Let's consider this code block:
420
+
421
+ ```ruby
422
+ class A
423
+ attribute :str
424
+ end
425
+
426
+ a = A.new
427
+ a.str = "qwe"
428
+ a.str.sub! "w", "x"
429
+ ```
430
+
431
+ As we expect, resulting `a.str` value would be "qxe". However, `sub!` modifies string in place, therefore does not call attribute writer and does not commit the change. This problem currently is not solved, since you can just do
432
+ ```ruby
433
+ a.str = a.str.sub "w", "x"
434
+ ```
435
+
436
+ For strings this is somewhat fine, but for large arrays things get worse. For arrays there is `Qwe::Attribute::ArrayProxy` class, which extends `Array` class and commits changes.
437
+
438
+ So the rule here is **don't use in-place modifications on attributes**, and if you need these - extend desired class and write committing logic. Be sure to benchmark results - sometimes replacing object entirely would be faster than doing complex checks.
439
+
440
+ Commits integrity can be validated by comparing object with it's fork
441
+ ```ruby
442
+ class R
443
+ include Qwe::Mixins::Root
444
+
445
+ def validate
446
+ if record.dump == record.fork.dump
447
+ puts "Commits are fine"
448
+ else
449
+ puts "Commits are broken, you are doing something wrong"
450
+ end
451
+ end
452
+ end
453
+ ```
454
+
455
+ ## Thread safety
456
+
457
+ As in ruby itself, much of thread safety is left up to you. Since DRb is multi-threaded, unsafety usually comes from DRb interactions, and providing single-threaded interaction layer is somewhat on roadmap.
458
+
459
+ The two common pitfalls are `record.save` and `record.archive` - you should ensure that nothing is committed while record gets dumped or commits file gets archived.
460
+
461
+ ## Not everything can be dumped
462
+
463
+ `Proc`, `Thread`, and a few other types can't be dumped with `Marshal`, and therefore can't be saved to disk.
464
+
465
+ ## Fail safety
466
+
467
+ In case worker process is killed with INT or TERM, it saves all records before stopping, but record persistence is not resilient to power outage, drive corruption, etc.
468
+
469
+ It's possible to recreate objects from commits on startup after unhandled shutdown, but precise implementation and real value are uncertain - fail-safe database has little use without corresponding application-side code.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in om.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "minitest", "~> 5.0"
10
+ gem "standardrb"
11
+ gem "erb" # For standardrb
12
+
13
+ gem "listen", "~> 3.9"
data/Gemfile.lock ADDED
@@ -0,0 +1,84 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ qwe (0.0.0)
5
+ drb (~> 2.2.1)
6
+ ruby-zstds (~> 1.3)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ adsp (1.0.10)
12
+ ast (2.4.2)
13
+ cgi (0.4.1)
14
+ drb (2.2.1)
15
+ erb (4.0.4)
16
+ cgi (>= 0.3.3)
17
+ ffi (1.16.3)
18
+ json (2.7.2)
19
+ language_server-protocol (3.17.0.3)
20
+ lint_roller (1.1.0)
21
+ listen (3.9.0)
22
+ rb-fsevent (~> 0.10, >= 0.10.3)
23
+ rb-inotify (~> 0.9, >= 0.9.10)
24
+ minitest (5.22.3)
25
+ parallel (1.24.0)
26
+ parser (3.3.0.5)
27
+ ast (~> 2.4.1)
28
+ racc
29
+ racc (1.7.3)
30
+ rainbow (3.1.1)
31
+ rake (13.2.0)
32
+ rb-fsevent (0.11.2)
33
+ rb-inotify (0.10.1)
34
+ ffi (~> 1.0)
35
+ regexp_parser (2.9.0)
36
+ rexml (3.2.6)
37
+ rubocop (1.62.1)
38
+ json (~> 2.3)
39
+ language_server-protocol (>= 3.17.0)
40
+ parallel (~> 1.10)
41
+ parser (>= 3.3.0.2)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ regexp_parser (>= 1.8, < 3.0)
44
+ rexml (>= 3.2.5, < 4.0)
45
+ rubocop-ast (>= 1.31.1, < 2.0)
46
+ ruby-progressbar (~> 1.7)
47
+ unicode-display_width (>= 2.4.0, < 3.0)
48
+ rubocop-ast (1.31.2)
49
+ parser (>= 3.3.0.4)
50
+ rubocop-performance (1.20.2)
51
+ rubocop (>= 1.48.1, < 2.0)
52
+ rubocop-ast (>= 1.30.0, < 2.0)
53
+ ruby-progressbar (1.13.0)
54
+ ruby-zstds (1.3.1)
55
+ adsp (~> 1.0)
56
+ standard (1.35.1)
57
+ language_server-protocol (~> 3.17.0.2)
58
+ lint_roller (~> 1.0)
59
+ rubocop (~> 1.62.0)
60
+ standard-custom (~> 1.0.0)
61
+ standard-performance (~> 1.3)
62
+ standard-custom (1.0.2)
63
+ lint_roller (~> 1.0)
64
+ rubocop (~> 1.50)
65
+ standard-performance (1.3.1)
66
+ lint_roller (~> 1.1)
67
+ rubocop-performance (~> 1.20.2)
68
+ standardrb (1.0.1)
69
+ standard
70
+ unicode-display_width (2.5.0)
71
+
72
+ PLATFORMS
73
+ x86_64-linux
74
+
75
+ DEPENDENCIES
76
+ erb
77
+ listen (~> 3.9)
78
+ minitest (~> 5.0)
79
+ qwe!
80
+ rake (~> 13.0)
81
+ standardrb
82
+
83
+ BUNDLED WITH
84
+ 2.5.23
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 goose3228
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.