Class: DpStmMap::ClientLocalStore

Inherits:
Object
  • Object
show all
Defined in:
lib/dp_stm_map/ClientLocalStore.rb

Instance Method Summary collapse

Constructor Details

#initialize(store_dir) ⇒ ClientLocalStore

Returns a new instance of ClientLocalStore.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dp_stm_map/ClientLocalStore.rb', line 18

def initialize store_dir

  @store_dir=store_dir

  @mapping_dir="%s/mapping" % store_dir
  FileUtils.mkdir_p(@mapping_dir) unless File.exist? @mapping_dir

  @content_dir="%s/content" % store_dir
  FileUtils.mkdir_p(@content_dir) unless File.exist? @content_dir


  transaction_sequence_file_name="%s/transaction_sequence.txt" % @store_dir

  File.open(transaction_sequence_file_name,"w") {|f| f.write(0)} unless File.exist? transaction_sequence_file_name

  @content_cache=ThreadSafeLru::LruCache.new 9000
  @mapping_cache=ThreadSafeLru::LruCache.new 9000

end

Instance Method Details

#[](key) ⇒ Object



110
111
112
# File 'lib/dp_stm_map/ClientLocalStore.rb', line 110

def [] key
  get_content(get_mapping(key))
end

#current_transaction_sequenceObject



75
76
77
# File 'lib/dp_stm_map/ClientLocalStore.rb', line 75

def current_transaction_sequence
  File.open("%s/transaction_sequence.txt" % @store_dir,"r") {|f| f.read().to_i}
end

#get_content(h) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/dp_stm_map/ClientLocalStore.rb', line 84

def get_content h
  @content_cache.get(h) do |hash|
    if hash == nil
      nil
    else
      File.open("%s/%s" % [@content_dir,hash],"r") {|f| f.read }
    end
  end
end

#get_mapping(k) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dp_stm_map/ClientLocalStore.rb', line 94

def get_mapping k
  @mapping_cache.get(k) do |key|
    if key == nil
      nil
    else
      if File.exist? "%s/%s" % [@mapping_dir,key]
        File.open("%s/%s" % [@mapping_dir,key],"r") {|f| f.read }
      else
        nil
      end
    end
  end
end

#has_content?(hash) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/dp_stm_map/ClientLocalStore.rb', line 80

def has_content? hash
  File.exist?("%s/%s" % [@content_dir,hash])
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/dp_stm_map/ClientLocalStore.rb', line 114

def has_key? key
  File.exist?("%s/%s" % [@mapping_dir,key])
end

#update(transaction_sequence, new_content, updates, to_delete) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dp_stm_map/ClientLocalStore.rb', line 38

def update transaction_sequence, new_content, updates, to_delete



  File.open("%s/transaction_sequence.txt" % @store_dir,"w") {|f| f.write(transaction_sequence)}


  new_content.each_pair do |k,v|
    File.open("%s/%s" % [@content_dir,k],"w") {|f| f.write(v)}
  end


  changes=updates.inject({}) do |c, (k, new_hash)|
    c[k]=[self[k],get_content(new_hash)]
    c
  end

  to_delete.each do |hash|
    File.delete("%s/%s" % [@content_dir,hash])
  end

  updates.each_pair do |k,v|
    @mapping_cache.drop(k)
    if v == nil
      File.delete("%s/%s" % [@mapping_dir,k])
    else
      raise StandardError, "unknown content #{v} for key #{k}" unless has_content?(v)
      File.open("%s/%s" % [@mapping_dir,k],"w") {|f| f.write(v)}
    end
  end



  changes

end