Class: Mingle::KeyvalueStore::PStoreBased

Inherits:
Monitor
  • Object
show all
Defined in:
lib/mingle_keyvalue_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, namespace, key_column, value_column) ⇒ PStoreBased

Returns a new instance of PStoreBased.



10
11
12
13
14
15
16
17
# File 'lib/mingle_keyvalue_store.rb', line 10

def initialize(path, namespace, key_column, value_column)
  @namespace = namespace
  @store_file = store_file(path)
  @key_column = key_column
  @value_column = value_column
  @pstore = PStore.new(@store_file)
  super()
end

Instance Method Details

#[](store_key) ⇒ Object



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

def [](store_key)
  synchronize do
    @pstore.transaction { @pstore[store_key] }
  end
end

#[]=(store_key, value) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
# File 'lib/mingle_keyvalue_store.rb', line 25

def []=(store_key, value)
  raise ArgumentError, 'Value must be String' unless value.is_a?(String)
  synchronize do
    @pstore.transaction do
      @pstore['all_names'] ||= []
      @pstore['all_names'] = (@pstore['all_names'] + [store_key]).uniq
      @pstore[store_key] = value
    end
  end
end

#all_itemsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mingle_keyvalue_store.rb', line 58

def all_items
  synchronize do
    @pstore.transaction do
      return [] unless @pstore['all_names']
      @pstore['all_names'].map do |name|
        {
          @key_column.to_s => name,
          @value_column.to_s => @pstore[name]
        }
      end
    end
  end

end

#clearObject



46
47
48
49
50
# File 'lib/mingle_keyvalue_store.rb', line 46

def clear
  FileUtils.rm_f @store_file
  @pstore = PStore.new(@store_file)
  nil
end

#delete(store_key) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/mingle_keyvalue_store.rb', line 36

def delete(store_key)
  synchronize do
    @pstore.transaction do
      @pstore['all_names'].delete_if {|name| name == store_key}
      @pstore.delete(store_key)
    end
  end
  nil
end

#namesObject



52
53
54
55
56
# File 'lib/mingle_keyvalue_store.rb', line 52

def names
  synchronize do
    @pstore.transaction { @pstore['all_names'] || [] }
  end
end