Class: Noda::Table

Inherits:
Object
  • Object
show all
Includes:
DRb::DRbUndumped, Enumerable
Defined in:
lib/noda/table.rb

Overview

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Table

Returns a new instance of Table.



24
25
26
27
28
29
30
# File 'lib/noda/table.rb', line 24

def initialize(name=nil)
  @hash = {}
  @name = name
  self.extend(MonitorMixin)
  @m_lock = self.new_cond
  @saved_keys = []
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/noda/table.rb', line 23

def name
  @name
end

Instance Method Details

#eachObject

Enumerable 用。

drb 経由で呼び出す時は、参照渡しになるので注意



52
53
54
55
# File 'lib/noda/table.rb', line 52

def each 
  @hash.each_pair{|k,v| yield( k,v[:data] )  }
  nil
end

#each_unsaved_pairObject

テーブルの未保存の値をすべて取り出す.」

永続ストレージに書き出すときに このメソッドを使って処理が出来る



77
78
79
80
# File 'lib/noda/table.rb', line 77

def each_unsaved_pair
  @hash.each_pair{|k,v| next if v[:saved]; yield( k, v[:data] )  }
  nil
end

#get(key) ⇒ Object

キー対応した値を取り出す



32
33
34
# File 'lib/noda/table.rb', line 32

def get(key)
  @hash[key][:data]
end

#has_key?(key) ⇒ Boolean Also known as: exists?

キーがテーブルに存在するか

Returns:

  • (Boolean)


42
# File 'lib/noda/table.rb', line 42

def has_key?(key) @hash.has_key? key end

#has_unsaved_key?Boolean

未保存の値があるか

永続ストレージに書き出すときに このフラグを使って処理をする

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/noda/table.rb', line 85

def has_unsaved_key?
  return true if @hash.find{|k,v| v[:saved]==false}
  return false
end

#keysObject

キーを全部取得



46
# File 'lib/noda/table.rb', line 46

def keys() @hash.keys end

#put(key, obj) ⇒ Object

キーを名前に値を保存する.



36
37
38
39
40
# File 'lib/noda/table.rb', line 36

def put(key, obj)
  self.synchronize{
    @hash[key] = {:data=>obj,:saved=>false}
  }
end

#saved?(key) ⇒ Boolean

テーブルの値はDump済みかどうか

永続ストレージに書き出すときに このフラグを使って処理をする

Returns:

  • (Boolean)


61
62
63
# File 'lib/noda/table.rb', line 61

def saved?(key)
  @hash[key][:saved]
end

#sizeObject

テーブルの値の数



48
# File 'lib/noda/table.rb', line 48

def size() @hash.size end

#update_saved_at(key, status = true) ⇒ Object

テーブルの値の保存状態を更新する.

永続ストレージに書き出すときに このフラグを使って処理をする



68
69
70
71
72
# File 'lib/noda/table.rb', line 68

def update_saved_at(key,status=true)
  self.synchronize{
    @hash[key][:saved]=status
  }
end