Class: FakeS3::SortedObjectList

Inherits:
Object
  • Object
show all
Defined in:
lib/fakes3/sorted_object_list.rb

Overview

This class has some of the semantics necessary for how buckets can return their items

It is currently implemented naively as a sorted set + hash If you are going to try to put massive lists inside buckets and ls them, you will be sorely disappointed about this performance.

Instance Method Summary collapse

Constructor Details

#initializeSortedObjectList

Returns a new instance of SortedObjectList.



19
20
21
22
23
# File 'lib/fakes3/sorted_object_list.rb', line 19

def initialize
  @sorted_set = SortedSet.new
  @object_map = {}
  @mutex = Mutex.new
end

Instance Method Details

#add(s3_object) ⇒ Object

Add an S3 object into the sorted list



34
35
36
37
38
39
# File 'lib/fakes3/sorted_object_list.rb', line 34

def add(s3_object)
  return if !s3_object

  @object_map[s3_object.name] = s3_object
  @sorted_set << s3_object
end

#countObject



25
26
27
# File 'lib/fakes3/sorted_object_list.rb', line 25

def count
  @sorted_set.count
end

#find(object_name) ⇒ Object



29
30
31
# File 'lib/fakes3/sorted_object_list.rb', line 29

def find(object_name)
  @object_map[object_name]
end

#list(options) ⇒ Object

Return back a set of matches based on the passed in options

options:

:marker : a string to start the lexographical search (it is not included

in the result)

:max_keys : a maximum number of results :prefix : a string to filter the results by :delimiter : not supported yet



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
# File 'lib/fakes3/sorted_object_list.rb', line 57

def list(options)
  marker = options[:marker]
  prefix = options[:prefix]
  max_keys = options[:max_keys] || 1000
  delimiter = options[:delimiter]

  ms = S3MatchSet.new

  marker_found = true
  pseudo = nil
  if marker
    marker_found = false
    if !@object_map[marker]
      pseudo = S3Object.new
      pseudo.name = marker
      @sorted_set << pseudo
    end
  end

  count = 0
  @sorted_set.each do |s3_object|
    if marker_found && (!prefix or s3_object.name.index(prefix) == 0)
      count += 1
      if count <= max_keys
        ms.matches << s3_object
      else
        is_truncated = true
        break
      end
    end

    if marker and marker == s3_object.name
      marker_found = true
    end
  end

  if pseudo
    @sorted_set.delete(pseudo)
  end

  return ms
end

#remove(s3_object) ⇒ Object



41
42
43
44
45
46
# File 'lib/fakes3/sorted_object_list.rb', line 41

def remove(s3_object)
  return if !s3_object

  @object_map.delete(s3_object.name)
  @sorted_set.delete(s3_object)
end