Class: Archipelago::Dump::Site

Inherits:
Object
  • Object
show all
Includes:
Archipelago::Disco::Publishable
Defined in:
lib/archipelago/dump.rb

Overview

The server class in the Archipipelago::Dump network.

Uses an Archipelago::Sanitation::Officer to keep track of what is needed to be done for redundancy, but Site does the actual work.

Constant Summary collapse

CHECK_INTERVAL =

The minimum pause between checking if keys belong with us.

30

Instance Attribute Summary collapse

Attributes included from Archipelago::Disco::Camel

#jockey

Instance Method Summary collapse

Methods included from Archipelago::Disco::Publishable

#_dump, append_features, #close!, #publish!, #service_id, #unpublish!, #valid?

Constructor Details

#initialize(options = {}) ⇒ Site

Returns a new instance of Site.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/archipelago/dump.rb', line 51

def initialize(options = {})
  initialize_publishable(options)
  #
  # The callable object that will get sent our debug messages if it exists.
  #
  @debug_callable = options[:debug_callable]

  #
  # The provider of checksumming magic and chunk distribution.
  #
  @officer = options[:officer] || (defined?(Archipelago::Sanitation::CLEANER) ? Archipelago::Sanitation::CLEANER : Archipelago::Sanitation::Officer.new)

  #
  # The database where the data lives.
  #
  @db = @persistence_provider.get_dup_tree("db")

  #
  # The minimum pause between checking if keys belong with us.
  #
  @check_interval = options[:check_interval] || CHECK_INTERVAL
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



49
50
51
# File 'lib/archipelago/dump.rb', line 49

def db
  @db
end

#debug_callableObject

Returns the value of attribute debug_callable.



49
50
51
# File 'lib/archipelago/dump.rb', line 49

def debug_callable
  @debug_callable
end

#officerObject

Returns the value of attribute officer.



49
50
51
# File 'lib/archipelago/dump.rb', line 49

def officer
  @officer
end

#persistence_providerObject

Returns the value of attribute persistence_provider.



49
50
51
# File 'lib/archipelago/dump.rb', line 49

def persistence_provider
  @persistence_provider
end

Instance Method Details

#delete!(key) ⇒ Object

Deletes key from the db.



125
126
127
# File 'lib/archipelago/dump.rb', line 125

def delete!(key)
  return @db.delete(key)
end

#fetch(key) ⇒ Object

Fetches all duplicates of key.

Returns [[TIMESTAMP0, VALUE0],…,[TIMESTAMPn, VALUEn]]



115
116
117
118
119
120
# File 'lib/archipelago/dump.rb', line 115

def fetch(key)
  values = @db.duplicates(key).collect do |value|
    [value[0...4], value[4..-1]]
  end
  return values
end

#insert!(key, values, timestamp = "\000\000\000\000") ⇒ Object

Will insert the array values under key in the db.

Will insert them with the timestamp or “000000000000”.

If values with the same timestamp already exists, it will overwrite them if they are greater in number than the given values. If they are fewer enough of the given values will be inserted to get the same number of values in the database as in +values.

Any values with a different timestamp will be deleted.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/archipelago/dump.rb', line 87

def insert!(key, values, timestamp = "\000\000\000\000")
  @debug_callable.call("#{self.service_id}.insert!(#{key}, #{values.inspect}, #{timestamp.inspect}) called") if @debug_callable
  if (duplicates = @db.duplicates(key)).empty?
    values.each do |value|
      @db[key] = timestamp + value
    end
  else
    my_timestamp = duplicates.first[0...4]
    if timestamp != my_timestamp || duplicates.size > values.size
      @db.env.begin(BDB::TXN_COMMIT, @db) do |txn, db|
        db.delete(key)
        values.each do |value|
          db[key] = timestamp + value
        end
      end
    else
      values[0...(values.size - duplicates.size)].each do |value|
        @db[key] = timestamp + value
      end
    end
  end
end