Class: Polipus::Storage::RethinkStore

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

Constant Summary collapse

BINARY_FIELDS =
%w(body headers data)

Instance Attribute Summary

Attributes inherited from Base

#include_query_string_in_uuid

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RethinkStore

Returns a new instance of RethinkStore.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/polipus/storage/rethink_store.rb', line 10

def initialize(options = {})
  @r       = RethinkDB::RQL.new
  @rethink = options[:conn]
  @table   = options[:table]

  unless @r.db_list.run(@rethink).include?(@rethink.default_db)
    @r.db_create(@rethink.default_db).run(@rethink)
  end
  unless @r.table_list.run(@rethink).include?(@table)
    @r.table_create(@table).run(@rethink)
    @r.table(@table).index_create('created_at')
  end

  @compress_body = options[:compress_body] ||= true
  @except = options[:except] ||= []
  @semaphore = Mutex.new
end

Instance Method Details

#add(page) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/polipus/storage/rethink_store.rb', line 28

def add(page)
  @semaphore.synchronize do
    obj = page.to_hash
    @except.each { |e| obj.delete e.to_s }
    obj[:id] = uuid(page)
    obj['body'] = Zlib::Deflate.deflate(obj['body']) if @compress_body && obj['body']
    obj['created_at'] ||= Time.now.to_i
    BINARY_FIELDS.each do |field|
      # Use some marshalling?
      obj[field] = @r.binary(obj[field]) unless obj[field].nil?
    end

    @r.table(@table).insert(obj).run(@rethink, durability: 'soft')
    obj[:id]
  end
end

#clearObject



76
77
78
# File 'lib/polipus/storage/rethink_store.rb', line 76

def clear
  @r.table(@table).delete.run(@rethink)
end

#countObject



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

def count
  @r.table(@table).count.run(@rethink)
end

#eachObject



69
70
71
72
73
74
# File 'lib/polipus/storage/rethink_store.rb', line 69

def each
  @r.table(@table).run(@rethink).each do |doc|
    page = load_page(doc)
    yield doc[:id], page
  end
end

#exists?(page) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/polipus/storage/rethink_store.rb', line 45

def exists?(page)
  @semaphore.synchronize do
    doc = @r.table(@table).get(uuid(page)).run(@rethink)
    !doc.nil?
  end
end

#get(page) ⇒ Object



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

def get(page)
  @semaphore.synchronize do
    data = @r.table(@table).get(uuid(page)).run(@rethink)
    return load_page(data) if data
  end
end

#remove(page) ⇒ Object



59
60
61
62
63
# File 'lib/polipus/storage/rethink_store.rb', line 59

def remove(page)
  @semaphore.synchronize do
    @r.table(@table).get(uuid(page)).delete.run(@rethink)
  end
end