Class: Ccp::Storage
- Inherits:
-
Object
- Object
- Ccp::Storage
- Defined in:
- lib/ccp/storage.rb
Constant Summary collapse
- NotFound =
Class.new(RuntimeError)
Instance Attribute Summary collapse
-
#codec ⇒ Object
readonly
Returns the value of attribute codec.
-
#kvs ⇒ Object
readonly
Returns the value of attribute kvs.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(source, kvs, codec) ⇒ Storage
constructor
A new instance of Storage.
-
#read ⇒ Object
kvs.
- #table(name, file = nil) ⇒ Object
-
#table_names ⇒ Object
meta kvs.
- #tables ⇒ Object
Constructor Details
#initialize(source, kvs, codec) ⇒ Storage
Returns a new instance of Storage.
15 16 17 18 19 20 21 |
# File 'lib/ccp/storage.rb', line 15 def initialize(source, kvs, codec) @source = source @codec = codec @path = Pathname(source) @kvs = kvs.new(source).codec!(codec) @tables = {} end |
Instance Attribute Details
#codec ⇒ Object (readonly)
Returns the value of attribute codec.
12 13 14 |
# File 'lib/ccp/storage.rb', line 12 def codec @codec end |
#kvs ⇒ Object (readonly)
Returns the value of attribute kvs.
12 13 14 |
# File 'lib/ccp/storage.rb', line 12 def kvs @kvs end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/ccp/storage.rb', line 12 def path @path end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
12 13 14 |
# File 'lib/ccp/storage.rb', line 12 def source @source end |
Class Method Details
.load(path) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/ccp/storage.rb', line 5 def self.load(path) array = path.to_s.split(".") kvs = Ccp::Kvs[array.pop] codec = Ccp::Serializers[array.pop] return new(path, kvs, codec) end |
Instance Method Details
#close ⇒ Object
46 47 48 49 50 51 |
# File 'lib/ccp/storage.rb', line 46 def close @tables.each_pair do |_,kvs| kvs.close end @tables = {} end |
#read ⇒ Object
kvs
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ccp/storage.rb', line 56 def read if @path.directory? tables hash = {} @tables.each_pair do |k, kvs| hash[k] = kvs.read end return hash else return @kvs.read end end |
#table(name, file = nil) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/ccp/storage.rb', line 39 def table(name, file = nil) @tables[name.to_s] ||= ( file ||= "%s.%s.%s" % [name, @codec.ext, @kvs.ext] @kvs.class.new((@path + file).to_s).codec!(@codec) ) end |
#table_names ⇒ Object
meta kvs
26 27 28 29 |
# File 'lib/ccp/storage.rb', line 26 def table_names tables # force to update @tables @tables.keys end |
#tables ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/ccp/storage.rb', line 31 def tables files = Dir.chdir(@path) { Dir["**/*.#{@kvs.ext}"] } files.map{|file| name = file.sub(/(\.#{@codec.ext})?(\.#{@kvs.ext})?$/, '') table(name, file) } end |