Permalink
Browse files

added lazy bucket key load

when the bucket is opened for the first time (LS_BUCKET), all keys will be
identified and loaded. This fix is needed so that RightAWS::Key object exists?
calls return valid data.
  • Loading branch information...
1 parent e9fc740 commit d85b5166de6903372ea444a401fb6c40260049e3 @kierse committed Apr 24, 2012
Showing with 36 additions and 1 deletion.
  1. +2 −1 lib/fakes3/bucket.rb
  2. +30 −0 lib/fakes3/file_store.rb
  3. +4 −0 lib/fakes3/server.rb
@@ -5,7 +5,7 @@
module FakeS3
class Bucket
- attr_accessor :name,:creation_date,:objects
+ attr_accessor :name,:creation_date,:objects,:opened
def initialize(name,creation_date,objects)
@name = name
@@ -14,6 +14,7 @@ def initialize(name,creation_date,objects)
objects.each do |obj|
@objects.add(obj)
end
+ @opened = false
@mutex = Mutex.new
end
@@ -53,6 +53,36 @@ def get_bucket(bucket)
@bucket_hash[bucket]
end
+ def get_sorted_object_list(bucket)
+ list = SortedObjectList.new
+ for object in get_objects_under_path(bucket, "")
+ list.add(object)
+ end
+ return list
+ end
+
+ def get_objects_under_path(bucket, path)
@jubos
jubos Jun 26, 2012

This is a dangerous call. On the test cases on OSX ruby 1.8.7, I get a "Too many open files limit" hit because the test cases puts a lot of files in one of the buckets. The objects maintain a pointer to the file to allow webrick to do streaming, so this would need to be implemented at a higher level to not actually open the files.

+ objects = []
+ current = File.join(@root, bucket.name, path)
+ dir = Dir.new(current)
+ dir.each do |file|
+ next if file =~ /^\./
+ if path.empty?
+ new_path = file
+ else
+ new_path = File.join(path, file)
+ end
+ if File.directory?(File.join(current, file, SHUCK_METADATA_DIR))
+ objects.push(get_object(bucket.name, new_path, ""))
+ else
+ objects |= get_objects_under_path(bucket, new_path)
+ end
+ end
+
+ return objects
+ end
+ private :get_objects_under_path
+
def create_bucket(bucket)
FileUtils.mkdir_p(File.join(@root,bucket))
bucket_obj = Bucket.new(bucket,Time.now,[])
@@ -58,6 +58,10 @@ def do_GET(request, response)
when 'LS_BUCKET'
bucket_obj = @store.get_bucket(s_req.bucket)
if bucket_obj
+ unless bucket_obj.opened
+ bucket_obj.objects = @store.get_sorted_object_list(bucket_obj)
+ bucket_obj.opened = true
+ end
response.status = 200
response['Content-Type'] = "application/xml"
query = {

0 comments on commit d85b516

Please sign in to comment.