Class: LS4::DirectoryStorageService

Inherits:
StorageService show all
Defined in:
lib/ls4/service/storage_dir.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Service

init

Methods included from EventBus::SingletonMixin

#ebus_bind!, #ebus_connect, extended

Methods included from EventBus::BusMixin

#ebus_all_slots, #ebus_disconnect!

Methods included from EventBus::DeclarerBase::Methods

#connect, #ebus_all_slots, #ebus_call_log, #ebus_call_slots, #ebus_signal_error, #ebus_signal_log, #ebus_signal_slots

Methods included from EventBus::DeclarerBase

#call_slot, #signal_slot

Class Method Details

.encode_okey(okey) ⇒ Object

for DataServerURLService



168
169
170
171
# File 'lib/ls4/service/storage_dir.rb', line 168

def self.encode_okey(okey)
  subpath = key_to_subpath(okey.vtime, okey.key)
  File.join(*subpath)
end

.key_to_subpath(vtime, key) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/ls4/service/storage_dir.rb', line 178

def self.key_to_subpath(vtime, key)
  digest = Digest::MD5.digest(key)
  box = "%03d" % digest.unpack('C')[0]
  kdir = encode_path(key)
  fname = "v#{vtime}"
  return box, kdir, fname
end

Instance Method Details

#closeObject



30
31
# File 'lib/ls4/service/storage_dir.rb', line 30

def close
end

#delete(vtime, key) ⇒ Object

def resize(vtime, key, size) $log.trace { “DirectoryStorage: resize vtime=#vtime key=#LS4::DirectoryStorageService.keykey.dump size=#size” }

path = key_to_path(vtime, key) make_dir(path)

File.open(path, File::WRONLY|File::CREAT) {|f| f.truncate(size) }

true end



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ls4/service/storage_dir.rb', line 132

def delete(vtime, key)
  $log.trace { "DirectoryStorage: delete vtime=#{vtime} key=#{key.dump}" }

  path = key_to_path(vtime, key)
  begin
    File.unlink(path)
    Dir.rmdir(File.dirname(path)) rescue nil
    return true
  rescue
    return false
  end
end

#exist(vtime, key) ⇒ Object



145
146
147
148
149
150
# File 'lib/ls4/service/storage_dir.rb', line 145

def exist(vtime, key)
  $log.trace { "DirectoryStorage: exist vtime=#{vtime} key=#{key.dump}" }

  path = key_to_path(vtime, key)
  return File.exist?(path)
end

#get(vtime, key) ⇒ Object



33
34
35
# File 'lib/ls4/service/storage_dir.rb', line 33

def get(vtime, key)
  read(vtime, key, nil, nil)
end

#get_itemsObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ls4/service/storage_dir.rb', line 152

def get_items
  num = 0
  (0..0xff).each {|d|
    box = "%03d" % d
    dirpath = File.join(@dir, "data", box)
    begin
      e = Dir.entries(dirpath).size
      e -= 2  # skip "." and ".."
      num += e if e > 0
    rescue
    end
  }
  num
end

#key_to_path(vtime, key) ⇒ Object



173
174
175
176
# File 'lib/ls4/service/storage_dir.rb', line 173

def key_to_path(vtime, key)
  subpath = self.class.key_to_subpath(vtime, key)
  File.join(@dir, "data", *subpath)
end

#open(expr) ⇒ Object



26
27
28
# File 'lib/ls4/service/storage_dir.rb', line 26

def open(expr)
  @dir = File.expand_path(expr)
end

#read(vtime, key, offset, size) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/ls4/service/storage_dir.rb', line 37

def read(vtime, key, offset, size)
  $log.trace { "DirectoryStorage: read vtime=#{vtime} key=#{key.dump} offset=#{offset} size=#{size}" }

  path = key_to_path(vtime, key)
  begin
    return File.read(path, size, offset)
  rescue
    return nil
  end
end

#set(vtime, key, data) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ls4/service/storage_dir.rb', line 48

def set(vtime, key, data)
  $log.trace { "DirectoryStorage: set vtime=#{vtime} key=#{key.dump} data=#{data.size}byte)" }

  path = key_to_path(vtime, key)
  make_dir(path)

  tmp_path = path+".tmp"
  File.open(tmp_path, File::WRONLY|File::CREAT|File::TRUNC) {|f|
    #f.write(data)
    while true
      n = f.syswrite(data)
      if data.size <= n
        break
      else
        data = data[n..-1]
        #data.slice!(0,n-1)
      end
    end
  }
  File.rename(tmp_path, path)

  true
end