Module: Persist::TSVAdapter
- Included in:
- CDBAdapter, FWTAdapter, KCAdapter, LMDBAdapter, PKIAdapter, TCAdapter
- Defined in:
- lib/rbbt/persist/tsv/adapter.rb
Constant Summary collapse
- MAX_CHAR =
255.chr
Instance Attribute Summary collapse
-
#closed ⇒ Object
Returns the value of attribute closed.
-
#mutex ⇒ Object
Returns the value of attribute mutex.
-
#persistence_path ⇒ Object
Returns the value of attribute persistence_path.
-
#writable ⇒ Object
Returns the value of attribute writable.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #collect ⇒ Object
- #delete(key) ⇒ Object
- #get_prefix(key) ⇒ Object
- #merge!(hash) ⇒ Object
- #prefix(key) ⇒ Object
- #range(*args) ⇒ Object
- #read? ⇒ Boolean
- #read_and_close ⇒ Object
- #write? ⇒ Boolean
- #write_and_close ⇒ Object
- #write_and_read ⇒ Object
Instance Attribute Details
#closed ⇒ Object
Returns the value of attribute closed.
4 5 6 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 4 def closed @closed end |
#mutex ⇒ Object
Returns the value of attribute mutex.
4 5 6 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 4 def mutex @mutex end |
#persistence_path ⇒ Object
Returns the value of attribute persistence_path.
4 5 6 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 4 def persistence_path @persistence_path end |
#writable ⇒ Object
Returns the value of attribute writable.
4 5 6 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 4 def writable @writable end |
Instance Method Details
#close ⇒ Object
25 26 27 28 29 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 25 def close @closed = true super self end |
#closed? ⇒ Boolean
21 22 23 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 21 def closed? @closed end |
#collect ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 39 def collect res = [] each do |key, value| res << if block_given? yield key, value else [key, value] end end res end |
#delete(key) ⇒ Object
51 52 53 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 51 def delete(key) out(key) end |
#get_prefix(key) ⇒ Object
16 17 18 19 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 16 def get_prefix(key) keys = prefix(key) select(:key => keys) end |
#merge!(hash) ⇒ Object
101 102 103 104 105 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 101 def merge!(hash) hash.each do |key,values| self[key] = values end end |
#prefix(key) ⇒ Object
12 13 14 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 12 def prefix(key) range(key, 1, key + MAX_CHAR, 1) end |
#range(*args) ⇒ Object
108 109 110 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 108 def range(*args) super(*args) #- TSV::ENTRY_KEYS.to_a end |
#read? ⇒ Boolean
35 36 37 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 35 def read? ! write? end |
#read_and_close ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 88 def read_and_close #mutex.synchronize do read if closed? or not read? res = begin yield ensure close end res #end end |
#write? ⇒ Boolean
31 32 33 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 31 def write? @writable end |
#write_and_close ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 70 def write_and_close lock_filename = Persist.persistence_path(persistence_path + '.write', {:dir => TSV.lock_dir}) #mutex.synchronize do Misc.lock(lock_filename, true) do write if closed? or not write? res = begin yield rescue Exception Log.exception $! raise $! ensure close end res end #end end |
#write_and_read ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rbbt/persist/tsv/adapter.rb', line 55 def write_and_read lock_filename = Persist.persistence_path(persistence_path + '.write', {:dir => TSV.lock_dir}) #mutex.synchronize do Misc.lock(lock_filename) do write if closed? or not write? res = begin yield ensure read end res end #end end |