Class: Ccp::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/ccp/storage.rb

Constant Summary collapse

NotFound =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#codecObject (readonly)

Returns the value of attribute codec.



12
13
14
# File 'lib/ccp/storage.rb', line 12

def codec
  @codec
end

#kvsObject (readonly)

Returns the value of attribute kvs.



12
13
14
# File 'lib/ccp/storage.rb', line 12

def kvs
  @kvs
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/ccp/storage.rb', line 12

def path
  @path
end

#sourceObject (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

#closeObject



56
57
58
59
60
61
# File 'lib/ccp/storage.rb', line 56

def close
  @tables.each_pair do |_,kvs|
    kvs.close
  end
  @tables = {}
end

#close_table(name) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/ccp/storage.rb', line 46

def close_table(name)
  t = @tables[name.to_s]
  if t && t.close
    @tables.delete(name.to_s)
    true
  else
    false
  end
end

#readObject

kvs



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ccp/storage.rb', line 66

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_namesObject

meta kvs



26
27
28
29
# File 'lib/ccp/storage.rb', line 26

def table_names
  tables                    # force to update @tables
  @tables.keys
end

#tablesObject



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