xitdb 0.1.0 → 0.2.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.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -0
  3. data/README.md +15 -3
  4. data/docs/benchmarks.md +4 -1
  5. data/docs/core.md +15 -2
  6. data/docs/learning.md +211 -0
  7. data/docs/native-profile-performance.md +254 -0
  8. data/docs/ruby-profile-v1.md +2 -0
  9. data/docs/ruby-profile-v2.md +279 -0
  10. data/docs/ruby-profile-v3.md +53 -0
  11. data/docs/ruby-profile-v4.md +67 -0
  12. data/docs/ruby.md +26 -3
  13. data/examples/indexed_users.rb +3 -0
  14. data/examples/portable.rb +3 -0
  15. data/examples/ruby.rb +2 -0
  16. data/lib/xitdb/collection_declaration.rb +39 -0
  17. data/lib/xitdb/context.rb +154 -8
  18. data/lib/xitdb/conversion/byte_source.rb +112 -0
  19. data/lib/xitdb/conversion.rb +35 -73
  20. data/lib/xitdb/core/array_builder.rb +74 -0
  21. data/lib/xitdb/core/array_list.rb +92 -8
  22. data/lib/xitdb/core/b_tree.rb +127 -19
  23. data/lib/xitdb/core/byte_value.rb +33 -0
  24. data/lib/xitdb/core/bytes/source.rb +59 -0
  25. data/lib/xitdb/core/bytes.rb +54 -58
  26. data/lib/xitdb/core/compaction.rb +49 -7
  27. data/lib/xitdb/core/crc64.rb +8 -1
  28. data/lib/xitdb/core/cursor.rb +108 -18
  29. data/lib/xitdb/core/database.rb +163 -29
  30. data/lib/xitdb/core/file_store.rb +43 -1
  31. data/lib/xitdb/core/format.rb +39 -98
  32. data/lib/xitdb/core/hash_map.rb +119 -17
  33. data/lib/xitdb/core/hasher.rb +33 -2
  34. data/lib/xitdb/core/header.rb +98 -0
  35. data/lib/xitdb/core/linked_array_list.rb +70 -1
  36. data/lib/xitdb/core/memory_store.rb +23 -224
  37. data/lib/xitdb/core/offset_map.rb +22 -1
  38. data/lib/xitdb/core/record.rb +64 -0
  39. data/lib/xitdb/core/replication.rb +108 -25
  40. data/lib/xitdb/core/slot.rb +123 -0
  41. data/lib/xitdb/core/sorted_map/tree.rb +317 -0
  42. data/lib/xitdb/core/sorted_map.rb +78 -256
  43. data/lib/xitdb/core/store.rb +286 -0
  44. data/lib/xitdb/core/transaction.rb +84 -6
  45. data/lib/xitdb/core.rb +12 -0
  46. data/lib/xitdb/database.rb +194 -15
  47. data/lib/xitdb/declarations.rb +33 -33
  48. data/lib/xitdb/errors.rb +15 -0
  49. data/lib/xitdb/history.rb +21 -0
  50. data/lib/xitdb/input_guard.rb +15 -1
  51. data/lib/xitdb/profiles/portable/adapter.rb +380 -0
  52. data/lib/xitdb/profiles/portable.rb +129 -243
  53. data/lib/xitdb/profiles/ruby.rb +206 -24
  54. data/lib/xitdb/revision.rb +17 -0
  55. data/lib/xitdb/ruby/codec.rb +61 -54
  56. data/lib/xitdb/ruby/codec_registry.rb +79 -0
  57. data/lib/xitdb/ruby/containers.rb +355 -82
  58. data/lib/xitdb/ruby/encoded_value.rb +38 -0
  59. data/lib/xitdb/ruby/indexed_page_decoder.rb +72 -0
  60. data/lib/xitdb/ruby/indexed_page_encoder.rb +58 -0
  61. data/lib/xitdb/ruby/input.rb +55 -0
  62. data/lib/xitdb/ruby/key_encoding.rb +43 -5
  63. data/lib/xitdb/ruby/ordered_keys.rb +46 -3
  64. data/lib/xitdb/ruby/packed_array.rb +179 -0
  65. data/lib/xitdb/ruby/packed_map.rb +272 -0
  66. data/lib/xitdb/ruby/packing_state.rb +84 -0
  67. data/lib/xitdb/ruby/page_array.rb +218 -0
  68. data/lib/xitdb/ruby/page_cell.rb +20 -0
  69. data/lib/xitdb/ruby/page_codec.rb +46 -0
  70. data/lib/xitdb/ruby/page_decoder.rb +172 -0
  71. data/lib/xitdb/ruby/page_draft.rb +24 -0
  72. data/lib/xitdb/ruby/page_encoder.rb +149 -0
  73. data/lib/xitdb/ruby/page_reference.rb +26 -0
  74. data/lib/xitdb/ruby/page_row.rb +108 -0
  75. data/lib/xitdb/ruby/pages.rb +94 -0
  76. data/lib/xitdb/ruby/scalars.rb +175 -33
  77. data/lib/xitdb/ruby/wire/reader.rb +138 -0
  78. data/lib/xitdb/ruby/wire.rb +36 -90
  79. data/lib/xitdb/ruby.rb +43 -3
  80. data/lib/xitdb/snapshot.rb +8 -0
  81. data/lib/xitdb/streams/handle.rb +46 -0
  82. data/lib/xitdb/streams/reader.rb +122 -0
  83. data/lib/xitdb/streams/writer.rb +43 -0
  84. data/lib/xitdb/streams.rb +8 -140
  85. data/lib/xitdb/uint.rb +17 -0
  86. data/lib/xitdb/value_walker.rb +86 -16
  87. data/lib/xitdb/version.rb +2 -1
  88. data/lib/xitdb/views/array.rb +94 -74
  89. data/lib/xitdb/views/base.rb +144 -12
  90. data/lib/xitdb/views/linked_array.rb +7 -1
  91. data/lib/xitdb/views/map.rb +71 -3
  92. data/lib/xitdb/views/set.rb +13 -1
  93. data/lib/xitdb/views/slice.rb +101 -0
  94. data/lib/xitdb/views/sorted.rb +25 -37
  95. data/lib/xitdb/views/sorted_map.rb +39 -0
  96. data/lib/xitdb/views/sorted_set.rb +10 -0
  97. data/lib/xitdb.rb +3 -0
  98. data/sig/core.rbs +65 -34
  99. data/sig/public.rbs +24 -19
  100. data/sig/ruby.rbs +260 -42
  101. metadata +42 -1
data/lib/xitdb/ruby.rb CHANGED
@@ -1,14 +1,45 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../xitdb"
4
+ require_relative "ruby/page_codec"
4
5
  require_relative "ruby/scalars"
5
6
  require_relative "ruby/key_encoding"
6
7
  require_relative "ruby/containers"
8
+ require_relative "ruby/packed_map"
9
+ require_relative "ruby/packed_array"
10
+ require_relative "ruby/packing_state"
11
+ require_relative "ruby/page_reference"
12
+ require_relative "ruby/page_cell"
13
+ require_relative "ruby/page_draft"
14
+ require_relative "ruby/pages"
15
+ require_relative "ruby/page_row"
16
+ require_relative "ruby/page_array"
7
17
  require_relative "profiles/ruby"
8
18
 
9
19
  module XitDB
10
- # Opens databases with the Ruby value profile.
20
+ # Opens file-backed and in-memory databases that preserve Ruby value types.
21
+ # The Ruby profile retains string encodings, insertion order, and registered application values.
22
+ # Its files use the Ruby profile hash identifier and require the Ruby profile to reopen.
23
+ #
24
+ # @example Create and close an in-memory database
25
+ # XitDB::Ruby.memory do |db|
26
+ # db.transaction { |root| root["enabled"] = true }
27
+ # db.snapshot["enabled"] # => true
28
+ # end
11
29
  module Ruby
30
+ # Opens a Ruby-profile file, or creates it when writable and absent.
31
+ # With a block, closes the database after the block, including when the block raises.
32
+ #
33
+ # @param path [String] database file path
34
+ # @param readonly [Boolean] whether to reject writes and require an existing file
35
+ # @param lock_timeout [Float] seconds to wait for a writer lock
36
+ # @param codecs [Array<Codec>] application serializers needed to read or write custom values
37
+ # @return [Database, Object] the open database, or the block result
38
+ # @yield [database] uses the open database
39
+ # @yieldparam database [Database] database closed when the block finishes
40
+ # @yieldreturn [Object] result returned by this method
41
+ # @raise [CodecConfigurationError] if codec registrations are invalid
42
+ # @raise [ProfileMismatchError] if the file header does not match this profile
12
43
  def self.open(path, readonly: false, lock_timeout: 5.0, codecs: [])
13
44
  profile = Profiles::Ruby.new(codecs: codecs)
14
45
  store, created = Database.send(:open_store, path, readonly: readonly)
@@ -16,7 +47,7 @@ module XitDB
16
47
  # @type var database: Database
17
48
  begin
18
49
  core = Core::Database.new(store: store, initialize_empty: created, lock_timeout: lock_timeout,
19
- hash_id: "rb01", hash_size: 32)
50
+ hash_id: "rb04", hash_size: 32)
20
51
  database = Database.new(core: core, profile: profile, readonly: readonly)
21
52
  opened = true
22
53
  ensure
@@ -31,9 +62,18 @@ module XitDB
31
62
  end
32
63
  end
33
64
 
65
+ # Creates a Ruby-profile database held in memory.
66
+ # With a block, closes the database after the block, including when the block raises.
67
+ #
68
+ # @param codecs [Array<Codec>] application serializers for custom values
69
+ # @return [Database, Object] the open database, or the block result
70
+ # @yield [database] uses the temporary database
71
+ # @yieldparam database [Database] database closed when the block finishes
72
+ # @yieldreturn [Object] result returned by this method
73
+ # @raise [CodecConfigurationError] if codec registrations are invalid
34
74
  def self.memory(codecs: [])
35
75
  profile = Profiles::Ruby.new(codecs: codecs)
36
- database = Database.new(core: Core::Database.memory(hash_id: "rb01", hash_size: 32), profile: profile)
76
+ database = Database.new(core: Core::Database.memory(hash_id: "rb04", hash_size: 32), profile: profile)
37
77
  return database unless block_given?
38
78
 
39
79
  begin
@@ -2,9 +2,17 @@
2
2
 
3
3
  module XitDB
4
4
  # A captured root map with an optional committed revision.
5
+ #
6
+ # Reading later commits leaves this map unchanged. It remains tied to its
7
+ # database handle; use {#to_ruby} before closing when you need independent data.
5
8
  class Snapshot < Views::Map
9
+ # @return [Revision, nil] saved identity, or nil for the initial empty root
6
10
  attr_reader :revision
7
11
 
12
+ # Create a root map bound to a committed read boundary.
13
+ # @api private
14
+ # @param context [Context] read-only owner
15
+ # @param revision [Revision, nil] nil for the initial empty state
8
16
  def initialize(context:, revision:)
9
17
  @revision = revision
10
18
  super(context: context, path: [], kind: :map)
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module XitDB
4
+ module Streams
5
+ # Restricts stream access to the lifetime of its owning block.
6
+ # @api private
7
+ class Handle
8
+ # Bind a stream handle to the snapshot or transaction that owns its bytes.
9
+ # @api private
10
+ # @param context [Context]
11
+ def initialize(context)
12
+ @context = context
13
+ @active = true
14
+ end
15
+
16
+ # Forbid further stream access after its block ends.
17
+ # @api private
18
+ # @return [void]
19
+ def expire!
20
+ @active = false
21
+ end
22
+
23
+ private
24
+
25
+ # Require both a live stream block and a usable database context.
26
+ # @return [void]
27
+ # @raise [ExpiredViewError] if the stream block has ended
28
+ def check!
29
+ raise ExpiredViewError, "byte stream block has ended" unless @active
30
+
31
+ @context.check!
32
+ end
33
+
34
+ # Convert an IO argument to a bounded native integer.
35
+ # @param value [Object]
36
+ # @return [Integer]
37
+ def integer!(value)
38
+ result = Integer.try_convert(value) || raise(TypeError, "expected an Integer")
39
+ limit = (1 << ((0.size * 8) - 1)) - 1
40
+ raise RangeError, "integer too big to convert into long" unless result.between?(-limit - 1, limit)
41
+
42
+ result
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ module XitDB
4
+ module Streams
5
+ # Reads a captured byte value with bounded storage reads.
6
+ class Reader < Handle
7
+ # Capture an untagged byte value and start reading at position zero.
8
+ # @api private
9
+ # @param context [Context]
10
+ # @param slot [Core::Slot]
11
+ # @raise [InvalidValueError] if the slot is not untagged bytes
12
+ def initialize(context, slot)
13
+ super(context)
14
+ unless %i[bytes short_bytes].include?(slot.tag) && !slot.full?
15
+ raise InvalidValueError, "stream requires untagged bytes"
16
+ end
17
+
18
+ @source = Core::Bytes.open(context.database.core, slot, bound: context.read_bound)
19
+ @size = @source.size
20
+ @pos = 0
21
+ end
22
+
23
+ # Read bytes and advance the position, optionally reusing a String buffer.
24
+ #
25
+ # Without length, read all remaining bytes and return an empty String at EOF.
26
+ # With positive length, return nil at EOF. A zero length returns an empty String.
27
+ # @param length [Integer, nil] maximum bytes; nil reads the remainder
28
+ # @param output [String, nil] buffer replaced with binary data
29
+ # @return [String, nil]
30
+ # @raise [ArgumentError] if length is negative
31
+ def read(length = nil, output = nil)
32
+ check!
33
+ @context.read do
34
+ check!
35
+ count = length.nil? ? [@size - @pos, 0].max : integer!(length)
36
+ raise ArgumentError, "negative length" if count.negative?
37
+
38
+ buffer = if output.nil?
39
+ +"".b
40
+ else
41
+ String.try_convert(output) || raise(TypeError, "expected a String buffer")
42
+ end
43
+ buffer.replace("".b)
44
+ return nil if !length.nil? && count.positive? && @pos >= @size
45
+
46
+ remaining = count.clamp(0, [@size - @pos, 0].max)
47
+ while remaining.positive?
48
+ size = [remaining, Core::Bytes::CHUNK_SIZE].min
49
+ buffer << @source.read(@pos, size)
50
+ @pos += size
51
+ remaining -= size
52
+ end
53
+ buffer
54
+ end
55
+ end
56
+
57
+ # Read up to length bytes, raising at the end of the stream.
58
+ # @param length [Integer] maximum bytes
59
+ # @param output [String, nil] optional reusable buffer
60
+ # @return [String]
61
+ # @raise [EOFError] if a positive-length read starts at EOF
62
+ def readpartial(length, output = nil)
63
+ check!
64
+ read(integer!(length), output) || raise(EOFError, "end of byte stream")
65
+ end
66
+
67
+ # Move the position relative to the start, current position, or end.
68
+ # @param offset [Integer] signed byte displacement
69
+ # @param whence [Integer, Symbol] IO seek constant or :SET, :CUR, :END
70
+ # @return [Integer] zero
71
+ # @raise [Errno::EINVAL] if the origin is invalid or the resulting position is negative
72
+ def seek(offset, whence = IO::SEEK_SET)
73
+ check!
74
+ offset = integer!(offset)
75
+ whence = case whence
76
+ when :SET then IO::SEEK_SET
77
+ when :CUR then IO::SEEK_CUR
78
+ when :END then IO::SEEK_END
79
+ else whence
80
+ end
81
+ base = case integer!(whence)
82
+ when IO::SEEK_SET then 0
83
+ when IO::SEEK_CUR then @pos
84
+ when IO::SEEK_END then @size
85
+ else raise Errno::EINVAL, "invalid seek origin"
86
+ end
87
+ position = base + offset
88
+ raise Errno::EINVAL, "negative seek position" if position.negative?
89
+
90
+ @pos = integer!(position)
91
+ 0
92
+ end
93
+
94
+ # Move back to the first byte.
95
+ # @return [Integer] zero
96
+ def rewind
97
+ seek(0)
98
+ end
99
+
100
+ # Read the current byte position within the stream.
101
+ # @return [Integer]
102
+ def pos
103
+ check!
104
+ @pos
105
+ end
106
+
107
+ # Read the captured value's total byte length.
108
+ # @return [Integer]
109
+ def size
110
+ check!
111
+ @size
112
+ end
113
+
114
+ # Report whether the position is at or beyond the captured end.
115
+ # @return [Boolean]
116
+ def eof?
117
+ check!
118
+ @pos >= @size
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module XitDB
4
+ module Streams
5
+ # Spools bytes before the enclosing assignment is published.
6
+ class Writer < Handle
7
+ # Attach an active context to the temporary file receiving streamed bytes.
8
+ # @api private
9
+ # @param context [Context]
10
+ # @param spool [File] temporary binary file
11
+ def initialize(context, spool)
12
+ super(context)
13
+ @spool = spool
14
+ end
15
+
16
+ # Append the value's String representation in bounded chunks.
17
+ #
18
+ # Successful writes still require the enclosing writer block and transaction to complete.
19
+ # @param value [#to_s] value to convert to bytes
20
+ # @return [Integer] number of bytes written
21
+ def write(value)
22
+ check!
23
+ bytes = value.to_s
24
+ raise TypeError, "to_s must return a String" unless bytes.is_a?(String)
25
+
26
+ position = 0
27
+ while position < bytes.bytesize
28
+ check!
29
+ chunk = bytes.byteslice(position, Core::Bytes::CHUNK_SIZE) || raise(EOFError, "byte source ended")
30
+ position += @spool.write(chunk)
31
+ end
32
+ position
33
+ end
34
+
35
+ # Read the current byte position within the stream.
36
+ # @return [Integer]
37
+ def pos
38
+ check!
39
+ @spool.pos
40
+ end
41
+ end
42
+ end
43
+ end
data/lib/xitdb/streams.rb CHANGED
@@ -3,146 +3,14 @@
3
3
  require "tempfile"
4
4
 
5
5
  module XitDB
6
+ # Block-scoped access to untagged byte values without full-value reads.
7
+ #
8
+ # Open streams through {Views::Base#open_bytes} or {Views::Base#write_bytes}.
9
+ # Stream methods reject access after the owning block ends.
6
10
  module Streams
7
- # Restricts stream access to the lifetime of its owning block.
8
- class Handle
9
- def initialize(context)
10
- @context = context
11
- @active = true
12
- end
13
-
14
- def expire!
15
- @active = false
16
- end
17
-
18
- private
19
-
20
- def check!
21
- raise ExpiredViewError, "byte stream block has ended" unless @active
22
-
23
- @context.check!
24
- end
25
-
26
- def integer(value)
27
- result = Integer.try_convert(value) || raise(TypeError, "expected an Integer")
28
- limit = (1 << ((0.size * 8) - 1)) - 1
29
- raise RangeError, "integer too big to convert into long" unless result.between?(-limit - 1, limit)
30
-
31
- result
32
- end
33
- end
34
-
35
- # Reads a captured byte value with bounded storage reads.
36
- class Reader < Handle
37
- def initialize(context, slot)
38
- super(context)
39
- unless %i[bytes short_bytes].include?(slot.tag) && !slot.full
40
- raise InvalidValueError, "stream requires untagged bytes"
41
- end
42
-
43
- @source = Core::Bytes.open(context.database.core, slot, bound: context.read_bound)
44
- @size = @source.size
45
- @pos = 0
46
- end
47
-
48
- def read(length = nil, output = nil)
49
- check!
50
- @context.read do
51
- check!
52
- count = length.nil? ? [@size - @pos, 0].max : integer(length)
53
- raise ArgumentError, "negative length" if count.negative?
54
-
55
- buffer = if output.nil?
56
- +"".b
57
- else
58
- String.try_convert(output) || raise(TypeError, "expected a String buffer")
59
- end
60
- buffer.replace("".b)
61
- return nil if !length.nil? && count.positive? && @pos >= @size
62
-
63
- remaining = count.clamp(0, [@size - @pos, 0].max)
64
- while remaining.positive?
65
- size = [remaining, Core::Bytes::CHUNK_SIZE].min
66
- buffer << @source.read(@pos, size)
67
- @pos += size
68
- remaining -= size
69
- end
70
- buffer
71
- end
72
- end
73
-
74
- def readpartial(length, output = nil)
75
- check!
76
- read(integer(length), output) || raise(EOFError, "end of byte stream")
77
- end
78
-
79
- def seek(offset, whence = IO::SEEK_SET)
80
- check!
81
- offset = integer(offset)
82
- whence = case whence
83
- when :SET then IO::SEEK_SET
84
- when :CUR then IO::SEEK_CUR
85
- when :END then IO::SEEK_END
86
- else whence
87
- end
88
- base = case integer(whence)
89
- when IO::SEEK_SET then 0
90
- when IO::SEEK_CUR then @pos
91
- when IO::SEEK_END then @size
92
- else raise Errno::EINVAL, "invalid seek origin"
93
- end
94
- position = base + offset
95
- raise Errno::EINVAL, "negative seek position" if position.negative?
96
-
97
- @pos = integer(position)
98
- 0
99
- end
100
-
101
- def rewind
102
- seek(0)
103
- end
104
-
105
- def pos
106
- check!
107
- @pos
108
- end
109
-
110
- def size
111
- check!
112
- @size
113
- end
114
-
115
- def eof?
116
- check!
117
- @pos >= @size
118
- end
119
- end
120
-
121
- # Spools bytes before the enclosing assignment is published.
122
- class Writer < Handle
123
- def initialize(context, spool)
124
- super(context)
125
- @spool = spool
126
- end
127
-
128
- def write(value)
129
- check!
130
- bytes = value.to_s
131
- raise TypeError, "to_s must return a String" unless bytes.is_a?(String)
132
-
133
- position = 0
134
- while position < bytes.bytesize
135
- check!
136
- chunk = bytes.byteslice(position, Core::Bytes::CHUNK_SIZE) || raise(EOFError, "byte source ended")
137
- position += @spool.write(chunk)
138
- end
139
- position
140
- end
141
-
142
- def pos
143
- check!
144
- @spool.pos
145
- end
146
- end
147
11
  end
148
12
  end
13
+
14
+ require_relative "streams/handle"
15
+ require_relative "streams/reader"
16
+ require_relative "streams/writer"
data/lib/xitdb/uint.rb ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module XitDB
4
+ # Explicit unsigned storage for an integer.
5
+ class UInt
6
+ # @return [Integer] unsigned value represented by this wrapper
7
+ attr_reader :value
8
+
9
+ # Validate and freeze an explicitly unsigned 64-bit integer.
10
+ # @param value [Integer] integer from zero through 2**64 - 1
11
+ # @raise [RangeError] if the integer is outside that range
12
+ def initialize(value)
13
+ @value = Core::Format.integer!(value, 0..Core::Format::UINT64_MAX)
14
+ freeze
15
+ end
16
+ end
17
+ end