From 4649dea719f66551422f9aa82482727bc1d23bb6 Mon Sep 17 00:00:00 2001 From: Ivan Giuliani Date: Tue, 7 Feb 2023 14:37:39 +0000 Subject: [PATCH] Implement stream reads for the disk adapter --- lib/bucket_store/disk.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/bucket_store/disk.rb b/lib/bucket_store/disk.rb index 28b183a..f13312b 100644 --- a/lib/bucket_store/disk.rb +++ b/lib/bucket_store/disk.rb @@ -4,6 +4,8 @@ module BucketStore class Disk + DEFAULT_STREAM_CHUNK_SIZE_BYTES = 1024 * 1024 * 4 # 4Mb + def self.build(base_dir = ENV["DISK_ADAPTER_BASE_DIR"]) base_dir ||= Dir.tmpdir Disk.new(base_dir) @@ -33,6 +35,25 @@ def download(bucket:, key:) end end + def stream_download(bucket:, key:, chunk_size: nil) + chunk_size ||= DEFAULT_STREAM_CHUNK_SIZE_BYTES + + fd = File.open(key_path(bucket, key), "r") + metadata = { + bucket: bucket, + key: key, + }.freeze + + Enumerator.new do |yielder| + loop do + v = fd.gets(chunk_size) + break if v.nil? + + yielder.yield([metadata, v]) + end + end + end + def list(bucket:, key:, page_size:) root = Pathname.new(bucket_root(bucket))