Class: Bucketrb::ObjectStore

Inherits:
Object
  • Object
show all
Defined in:
lib/bucketrb/object_store.rb

Defined Under Namespace

Classes: ListResult, ObjectEntry

Constant Summary collapse

DEFAULT_MAX_KEYS =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ ObjectStore

Returns a new instance of ObjectStore.



46
47
48
49
# File 'lib/bucketrb/object_store.rb', line 46

def initialize(root)
  @root = Pathname.new(root).expand_path
  FileUtils.mkdir_p(@root)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



44
45
46
# File 'lib/bucketrb/object_store.rb', line 44

def root
  @root
end

Instance Method Details

#bucket?(bucket) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/bucketrb/object_store.rb', line 65

def bucket?(bucket)
  bucket_dir(bucket).join("objects").directory?
end

#bucketsObject



59
60
61
62
63
# File 'lib/bucketrb/object_store.rb', line 59

def buckets
  return [] unless root.directory?

  root.children.select { |path| path.directory? && path.join("objects").directory? }.map { |path| path.basename.to_s }.sort
end

#copy_object(source_bucket:, source_key:, target_bucket:, target_key:) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/bucketrb/object_store.rb', line 130

def copy_object(source_bucket:, source_key:, target_bucket:, target_key:)
  source = get_object(bucket: source_bucket, key: source_key)

  File.open(source.path, "rb") do |body|
    put_object(
      bucket: target_bucket,
      key: target_key,
      body: body,
      content_type: source.content_type,
      metadata: source.
    )
  end
end

#create_bucket(bucket) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/bucketrb/object_store.rb', line 51

def create_bucket(bucket)
  validate_bucket!(bucket)

  FileUtils.mkdir_p(objects_dir(bucket))
  FileUtils.mkdir_p((bucket))
  true
end

#delete_object(bucket:, key:) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/bucketrb/object_store.rb', line 115

def delete_object(bucket:, key:)
  ensure_bucket!(bucket)
  validate_key!(key)

  path = object_path(bucket, key)
  FileUtils.rm_f(path)
  FileUtils.rm_f((bucket, key))
  true
end

#delete_objects(bucket:, keys:) ⇒ Object



125
126
127
128
# File 'lib/bucketrb/object_store.rb', line 125

def delete_objects(bucket:, keys:)
  keys.each { |key| delete_object(bucket: bucket, key: key) }
  keys
end

#ensure_bucket!(bucket) ⇒ Object



69
70
71
72
# File 'lib/bucketrb/object_store.rb', line 69

def ensure_bucket!(bucket)
  validate_bucket!(bucket)
  raise BucketNotFoundError, "No such bucket: #{bucket}" unless bucket?(bucket)
end

#get_object(bucket:, key:) ⇒ Object



106
107
108
109
# File 'lib/bucketrb/object_store.rb', line 106

def get_object(bucket:, key:)
  ensure_bucket!(bucket)
  entry_for(bucket, key)
end

#head_object(bucket:, key:) ⇒ Object



111
112
113
# File 'lib/bucketrb/object_store.rb', line 111

def head_object(bucket:, key:)
  get_object(bucket: bucket, key: key)
end

#list_objects(bucket:, prefix: "", marker: nil, continuation_token: nil, max_keys: DEFAULT_MAX_KEYS) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/bucketrb/object_store.rb', line 144

def list_objects(bucket:, prefix: "", marker: nil, continuation_token: nil, max_keys: DEFAULT_MAX_KEYS)
  ensure_bucket!(bucket)

  prefix = prefix.to_s
  max_keys = max_keys.to_i
  max_keys = DEFAULT_MAX_KEYS if max_keys <= 0
  start_after = continuation_token || marker

  keys = object_keys(bucket).select { |key| key.start_with?(prefix) }.sort
  keys = keys.drop_while { |key| key <= start_after } if start_after && !start_after.empty?

  limited_keys = keys.first(max_keys)
  is_truncated = keys.length > limited_keys.length
  objects = limited_keys.map { |key| entry_for(bucket, key) }
  next_token = is_truncated && objects.any? ? objects.last.key : nil

  ListResult.new(
    bucket: bucket,
    prefix: prefix,
    marker: marker,
    max_keys: max_keys,
    objects: objects,
    is_truncated: is_truncated,
    next_marker: next_token,
    next_continuation_token: next_token
  )
end

#put_object(bucket:, key:, body:, content_type: nil, metadata: {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bucketrb/object_store.rb', line 74

def put_object(bucket:, key:, body:, content_type: nil, metadata: {})
  ensure_bucket!(bucket)
  validate_key!(key)

  path = object_path(bucket, key)
  FileUtils.mkdir_p(path.dirname)

  tmp_path = Pathname.new("#{path}.tmp-#{SecureRandom.hex(8)}")
  File.open(tmp_path, "wb") do |file|
    if body.respond_to?(:read)
      IO.copy_stream(body, file)
    else
      file.write(body.to_s)
    end
  end
  FileUtils.mv(tmp_path, path)

  entry = build_entry(
    bucket: bucket,
    key: key,
    path: path,
    content_type: content_type || "application/octet-stream",
    metadata: (),
    etag: Digest::MD5.file(path).hexdigest,
    last_modified: Time.now.utc
  )
  (entry)
  entry
ensure
  FileUtils.rm_f(tmp_path) if tmp_path && tmp_path.exist?
end