Skip to content

Commit

Permalink
Implement stream reads for the disk adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ivgiuliani committed Apr 12, 2023
1 parent 055d6b0 commit 4649dea
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/bucket_store/disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit 4649dea

Please sign in to comment.