Class: Hammerspace::Backend::Sparkey

Inherits:
Base
  • Object
show all
Defined in:
lib/hammerspace/backend/sparkey.rb

Instance Attribute Summary

Attributes inherited from Base

#frontend, #options, #path

Instance Method Summary collapse

Methods inherited from Base

#flock_works?, #initialize

Methods included from HashMethods

#==, #assoc, #delete_if, #each_key, #each_value, #empty?, #eql?, #fetch, #flatten, #has_value?, #include?, #keep_if, #key, #member?, #merge!, #rassoc, #reject!, #select!, #shift, #to_hash, #values_at

Constructor Details

This class inherits a constructor from Hammerspace::Backend::Base

Instance Method Details

#[](key) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/hammerspace/backend/sparkey.rb', line 48

def [](key)
  close_logwriter
  open_hash

  if @hash
    value = @hash[key]
    return value if value
  end
  frontend.default(key)
end

#[]=(key, value) ⇒ Object



59
60
61
62
63
64
# File 'lib/hammerspace/backend/sparkey.rb', line 59

def []=(key, value)
  close_hash
  open_logwriter

  @logwriter[key] = value
end

#check_fsObject



10
11
12
13
# File 'lib/hammerspace/backend/sparkey.rb', line 10

def check_fs
  super
  warn_dir_cleanup unless dir_cleanup_works?
end

#clearObject



66
67
68
69
70
71
# File 'lib/hammerspace/backend/sparkey.rb', line 66

def clear
  close_hash
  close_logwriter_clear

  frontend
end

#closeObject



73
74
75
76
# File 'lib/hammerspace/backend/sparkey.rb', line 73

def close
  close_logwriter
  close_hash
end

#delete(key) ⇒ Object

TODO: This currently always returns nil. If the key is not found, return the default value. Also, support block usage.



80
81
82
83
84
85
# File 'lib/hammerspace/backend/sparkey.rb', line 80

def delete(key)
  close_hash
  open_logwriter

  @logwriter.del(key)
end

#dir_cleanup_works?Boolean



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hammerspace/backend/sparkey.rb', line 15

def dir_cleanup_works?
  dir_cleanup_works = false
  ensure_path_exists(path)
  begin
    Dir.mktmpdir('dir_cleanup_works.', path) do |tmpdir|
      test       = File.join(tmpdir, 'test')
      test_tmp   = File.join(tmpdir, 'test.tmp')
      test_file  = File.join(test, 'file')
      test1      = File.join(tmpdir, 'test.1')
      test1_file = File.join(test1, 'file')
      test2      = File.join(tmpdir, 'test.2')
      test2_file = File.join(test2, 'file')

      Dir.mkdir(test1)
      FileUtils.touch(test1_file)
      File.symlink(File.basename(test1), test)

      File.open(test_file) do
        Dir.mkdir(test2)
        FileUtils.touch(test2_file)
        File.symlink(File.basename(test2), test_tmp)
        File.rename(test_tmp, test)

        FileUtils.rm_rf(test1, :secure => true)

        dir_cleanup_works = File.directory?(test1) == false
      end
    end
  rescue
  end
  dir_cleanup_works
end

#each(&block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hammerspace/backend/sparkey.rb', line 87

def each(&block)
  close_logwriter

  # Open a private copy of the hash to ensure isolation during iteration.
  # Further, Gnista segfaults if the hash is closed during iteration (e.g.,
  # from interleaved reads and writes), so a private copy ensures that
  # the hash is only closed once iteration is complete.
  hash = open_hash_private

  unless hash
    return block_given? ? nil : Enumerator.new {}
  end

  if block_given?
    begin
      hash.each(&block)
    ensure
      hash.close
    end
    frontend
  else
    # Gnista does not support each w/o a block; emulate the behavior here.
    Enumerator.new do |y|
      begin
        hash.each { |*args| y << args }
      ensure
        hash.close
      end
    end
  end
end

#has_key?(key) ⇒ Boolean



119
120
121
122
123
124
# File 'lib/hammerspace/backend/sparkey.rb', line 119

def has_key?(key)
  close_logwriter
  open_hash

  @hash ? @hash.include?(key) : false
end

#keysObject



126
127
128
129
130
131
# File 'lib/hammerspace/backend/sparkey.rb', line 126

def keys
  close_logwriter
  open_hash

  @hash ? @hash.keys : []
end

#replace(hash) ⇒ Object



133
134
135
136
137
138
# File 'lib/hammerspace/backend/sparkey.rb', line 133

def replace(hash)
  close_hash
  open_logwriter_replace

  merge!(hash)
end

#sizeObject



140
141
142
143
144
145
# File 'lib/hammerspace/backend/sparkey.rb', line 140

def size
  close_logwriter
  open_hash

  @hash ? @hash.size : 0
end

#uidObject



147
148
149
150
151
152
# File 'lib/hammerspace/backend/sparkey.rb', line 147

def uid
  close_logwriter
  open_hash

  @uid
end

#valuesObject



154
155
156
157
158
159
# File 'lib/hammerspace/backend/sparkey.rb', line 154

def values
  close_logwriter
  open_hash

  @hash ? @hash.values : []
end