byte_batcher 0.1.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 +7 -0
- data/CHANGELOG.md +8 -0
- data/LICENSE +21 -0
- data/README.md +50 -0
- data/lib/byte_batcher/json.rb +42 -0
- data/lib/byte_batcher.rb +72 -0
- metadata +42 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 5114168d61f9b8faa697ba972eaefbbaaf7a74f90698cd9588c658b8c914f6c6
|
|
4
|
+
data.tar.gz: e7cda449615b251091315aa8ae4d8bb8fc5c86b8f4ba73f92d06536c022ce5da
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8c2189c0f340751c5e1da7fd5fe0d208fedef202fe332dbb4537e531a5a6eb84326bb956f00af56085362466731218568a9e447c3cfea83046507d68d5535e8c
|
|
7
|
+
data.tar.gz: b7091ae641b1423ac22be79ff56407f6bc0e2a905435695f068ec3c373e95c76080d1e7d7ab2f79ae56c194489bba5e6ed028ff1e2fe50d7b0cae522dc1d13c9
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
- Add lazy batching with exact serialized byte limits, optional item limits, and custom framing.
|
|
6
|
+
- Add UTF-8 JSON arrays and NDJSON output with byte accounting for all framing.
|
|
7
|
+
- Report oversized items by index and byte counts.
|
|
8
|
+
- Support Ruby 3.4 and later with zero third-party runtime dependencies. This is the initial development release.
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Small Primitives
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Byte Batcher
|
|
2
|
+
|
|
3
|
+
Byte Batcher groups records into serialized payloads that fit a byte limit. It preserves input order and yields batches as you iterate.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Requires Ruby 3.4 or newer. The JSON formats use Ruby's standard `json` library. Byte Batcher adds no third-party runtime dependencies.
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
require "byte_batcher"
|
|
11
|
+
|
|
12
|
+
records = [{"name" => "Ada"}, {"name" => "Renée"}]
|
|
13
|
+
|
|
14
|
+
ByteBatcher.json(records, max_bytes: 64, max_items: 100).each do |payload|
|
|
15
|
+
# payload is a compact UTF-8 JSON array
|
|
16
|
+
puts payload
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
ByteBatcher.ndjson(records, max_bytes: 64).each do |payload|
|
|
20
|
+
# payload has one compact UTF-8 JSON value per line
|
|
21
|
+
print payload
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
ByteBatcher.pack(records.map { |record| record.fetch("name") },
|
|
25
|
+
max_bytes: 12, encode: ->(name) { name },
|
|
26
|
+
prefix: "<", separator: "|", suffix: ">").each do |payload|
|
|
27
|
+
puts payload
|
|
28
|
+
end
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
- `max_bytes` and the optional `max_items` must be positive integers. The byte limit includes brackets, separators, and suffixes. NDJSON ends each payload with a newline.
|
|
32
|
+
- `json` and `ndjson` accept JSON-compatible Ruby values such as strings, numbers, booleans, `nil`, arrays, and hashes with string keys. Convert custom objects to those values before batching.
|
|
33
|
+
- `pack` accepts an encoder that returns a string and emits binary strings. The caller defines and validates the custom format.
|
|
34
|
+
- Empty input yields zero batches. Use each payload unchanged to preserve the byte limit. Any transport overhead outside the payload belongs in the caller's budget.
|
|
35
|
+
- An item that exceeds the byte limit on its own raises `ByteBatcher::OversizedItemError`. The error exposes a zero-based `index`, `encoded_bytes`, `framed_bytes`, and `max_bytes`.
|
|
36
|
+
- A source, encoder, or oversized-item error stops iteration and discards the batch still being built. Previously yielded payloads stay with the caller.
|
|
37
|
+
- To find a byte boundary, the enumerator may encode one item ahead. It buffers the current payload and that item. An exceptionally large item can allocate its serialized form before the limit is checked. Each enumerator is for one consumer.
|
|
38
|
+
- Each record is encoded once. With linear-time encoders, batching takes time proportional to the record count plus the total encoded bytes.
|
|
39
|
+
|
|
40
|
+
## Tests
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
ruby -Ilib -Itest -e 'Dir["test/*_test.rb"].sort.each { |file| require_relative file }'
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The tests use Minitest, which ships with standard Ruby releases.
|
|
47
|
+
|
|
48
|
+
## Benchmark
|
|
49
|
+
|
|
50
|
+
Run `ruby benchmark/batching.rb` to compare the JSON packer against whole-batch re-encoding and count-only slicing. The script reports throughput, Ruby object allocations per record, peak emitted payload bytes, and payloads that exceed the byte limit. Count-only slicing can exceed the limit. Run results depend on the input and machine.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module ByteBatcher
|
|
6
|
+
def self.json(items, max_bytes:, max_items: nil)
|
|
7
|
+
json_batches(items, max_bytes: max_bytes, max_items: max_items, prefix: "[", separator: ",", suffix: "]")
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def self.ndjson(items, max_bytes:, max_items: nil)
|
|
11
|
+
json_batches(items, max_bytes: max_bytes, max_items: max_items, separator: "\n", suffix: "\n")
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.json_batches(items, **options)
|
|
15
|
+
batches = pack(items, encode: method(:encode_json), **options)
|
|
16
|
+
|
|
17
|
+
Enumerator.new do |output|
|
|
18
|
+
batches.each { |payload| output << payload.force_encoding(Encoding::UTF_8) }
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
private_class_method :json_batches
|
|
22
|
+
|
|
23
|
+
def self.encode_json(item)
|
|
24
|
+
encoded = JSON.generate(item, strict: true)
|
|
25
|
+
reject_raw_fragments(item) if defined?(JSON::Fragment)
|
|
26
|
+
encoded
|
|
27
|
+
end
|
|
28
|
+
private_class_method :encode_json
|
|
29
|
+
|
|
30
|
+
# JSON's strict generator still copies raw fragments verbatim on versions that support them.
|
|
31
|
+
def self.reject_raw_fragments(item)
|
|
32
|
+
case item
|
|
33
|
+
when JSON::Fragment
|
|
34
|
+
raise JSON::GeneratorError, "raw JSON fragments are not supported"
|
|
35
|
+
when Array
|
|
36
|
+
item.each { |value| reject_raw_fragments(value) }
|
|
37
|
+
when Hash
|
|
38
|
+
item.each_value { |value| reject_raw_fragments(value) }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
private_class_method :reject_raw_fragments
|
|
42
|
+
end
|
data/lib/byte_batcher.rb
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ByteBatcher
|
|
4
|
+
class OversizedItemError < StandardError
|
|
5
|
+
attr_reader :index, :encoded_bytes, :framed_bytes, :max_bytes
|
|
6
|
+
|
|
7
|
+
def initialize(index:, encoded_bytes:, framed_bytes:, max_bytes:)
|
|
8
|
+
@index = index
|
|
9
|
+
@encoded_bytes = encoded_bytes
|
|
10
|
+
@framed_bytes = framed_bytes
|
|
11
|
+
@max_bytes = max_bytes
|
|
12
|
+
|
|
13
|
+
super("item at index #{index} is #{encoded_bytes} encoded bytes " \
|
|
14
|
+
"(#{framed_bytes} with framing), exceeding max_bytes #{max_bytes}")
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.pack(items, max_bytes:, encode:, max_items: nil, prefix: "", separator: "", suffix: "")
|
|
19
|
+
unless max_bytes.is_a?(Integer) && max_bytes.positive?
|
|
20
|
+
raise ArgumentError, "max_bytes must be a positive Integer"
|
|
21
|
+
end
|
|
22
|
+
unless max_items.nil? || (max_items.is_a?(Integer) && max_items.positive?)
|
|
23
|
+
raise ArgumentError, "max_items must be a positive Integer or nil"
|
|
24
|
+
end
|
|
25
|
+
raise ArgumentError, "items must respond to each" unless items.respond_to?(:each)
|
|
26
|
+
raise ArgumentError, "encode must respond to call" unless encode.respond_to?(:call)
|
|
27
|
+
unless [prefix, separator, suffix].all? { |part| part.is_a?(String) }
|
|
28
|
+
raise ArgumentError, "prefix, separator, and suffix must be Strings"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
prefix, separator, suffix = [prefix.b, separator.b, suffix.b]
|
|
32
|
+
frame_bytes = prefix.bytesize + suffix.bytesize
|
|
33
|
+
raise ArgumentError, "prefix and suffix exceed max_bytes" if frame_bytes > max_bytes
|
|
34
|
+
|
|
35
|
+
Enumerator.new do |output|
|
|
36
|
+
payload = prefix.dup
|
|
37
|
+
count = 0
|
|
38
|
+
|
|
39
|
+
items.to_enum(:each).each_entry.with_index do |item, index|
|
|
40
|
+
encoded = encode.call(item)
|
|
41
|
+
raise TypeError, "encoder must return a String for item at index #{index}" unless encoded.is_a?(String)
|
|
42
|
+
|
|
43
|
+
framed_bytes = frame_bytes + encoded.bytesize
|
|
44
|
+
if framed_bytes > max_bytes
|
|
45
|
+
raise OversizedItemError.new(index: index, encoded_bytes: encoded.bytesize,
|
|
46
|
+
framed_bytes: framed_bytes, max_bytes: max_bytes)
|
|
47
|
+
end
|
|
48
|
+
encoded = encoded.b
|
|
49
|
+
|
|
50
|
+
if count.positive? && payload.bytesize + separator.bytesize + encoded.bytesize + suffix.bytesize > max_bytes
|
|
51
|
+
output << (payload << suffix)
|
|
52
|
+
payload = prefix.dup
|
|
53
|
+
count = 0
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
payload << separator if count.positive?
|
|
57
|
+
payload << encoded
|
|
58
|
+
count += 1
|
|
59
|
+
|
|
60
|
+
if count == max_items
|
|
61
|
+
output << (payload << suffix)
|
|
62
|
+
payload = prefix.dup
|
|
63
|
+
count = 0
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
output << (payload << suffix) if count.positive?
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
require_relative "byte_batcher/json"
|
metadata
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: byte_batcher
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Small Primitives
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2026-11-26 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
executables: []
|
|
13
|
+
extensions: []
|
|
14
|
+
extra_rdoc_files: []
|
|
15
|
+
files:
|
|
16
|
+
- CHANGELOG.md
|
|
17
|
+
- LICENSE
|
|
18
|
+
- README.md
|
|
19
|
+
- lib/byte_batcher.rb
|
|
20
|
+
- lib/byte_batcher/json.rb
|
|
21
|
+
homepage: https://github.com/smallprimitives/byte_batcher
|
|
22
|
+
licenses:
|
|
23
|
+
- MIT
|
|
24
|
+
metadata: {}
|
|
25
|
+
rdoc_options: []
|
|
26
|
+
require_paths:
|
|
27
|
+
- lib
|
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '3.4'
|
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
requirements: []
|
|
39
|
+
rubygems_version: 3.6.9
|
|
40
|
+
specification_version: 4
|
|
41
|
+
summary: Lazy batching with exact serialized byte limits
|
|
42
|
+
test_files: []
|