Class: Polipus::Storage::S3Store

Inherits:
Base
  • Object
show all
Defined in:
lib/polipus/storage/s3_store.rb

Instance Attribute Summary

Attributes inherited from Base

#include_query_string_in_uuid

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ S3Store

Returns a new instance of S3Store.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/polipus/storage/s3_store.rb', line 8

def initialize(options = {})
  @options = options
  @except = @options[:except] ||= []
  @semaphore = Mutex.new

  AWS::S3::Base.establish_connection!(
    :access_key_id     => @options[:access_key_id],
    :secret_access_key => @options[:secret_access_key]
  )
  @options[:bucket] = "com.polipus.pages.#{@options[:bucket]}"
  begin
    @bucket = AWS::S3::Bucket.find(@options[:bucket])
  rescue AWS::S3::NoSuchBucket
    create_bucket
  end
end

Instance Method Details

#add(page) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/polipus/storage/s3_store.rb', line 25

def add page
  @semaphore.synchronize {
    obj = page.to_hash
    @except.each {|e| obj.delete e.to_s}
    puuid = uuid(page)
    obj['uuid'] = puuid
    data = Zlib::Deflate.deflate(obj.to_json)
    AWS::S3::S3Object.store(puuid, data, @bucket.name)
    puuid
  }
end

#clearObject



64
65
66
67
# File 'lib/polipus/storage/s3_store.rb', line 64

def clear
  AWS::S3::Bucket.delete(@bucket.name, :force => true)
  create_bucket
end

#countObject



60
61
62
# File 'lib/polipus/storage/s3_store.rb', line 60

def count
  @bucket.size
end

#eachObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/polipus/storage/s3_store.rb', line 69

def each
  objects = []
  last_key = nil
  begin
    objects = AWS::S3::Bucket.objects(@bucket.name, :marker => last_key)
    break if objects.size == 0
    objects.each do |o|
      page = load_page(o.value)
      yield o.key, page 
    end
    last_key   = objects.last.key
  end while true
end

#exists?(page) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/polipus/storage/s3_store.rb', line 37

def exists?(page)
  AWS::S3::S3Object.exists? uuid(page), @bucket.name
end

#get(page) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/polipus/storage/s3_store.rb', line 41

def get page
  @semaphore.synchronize {
    if exists?(page)
      data = AWS::S3::S3Object.find(uuid(page), @bucket.name).value
      return load_page(data)
    end
    nil
  }
end

#remove(page) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/polipus/storage/s3_store.rb', line 51

def remove page
  @semaphore.synchronize {
    if exists?(page)
      AWS::S3::S3Object.delete(uuid(page), @bucket.name)
    end
    true
  }
end