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
# File 'lib/hammerspace/backend/sparkey.rb', line 48

def [](key)
  close_logwriter
  open_hash

  return @hash[key] if @hash && @hash.include?(key)
  frontend.default(key)
end

#[]=(key, value) ⇒ Object



56
57
58
59
60
61
# File 'lib/hammerspace/backend/sparkey.rb', line 56

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



63
64
65
66
67
68
# File 'lib/hammerspace/backend/sparkey.rb', line 63

def clear
  close_hash
  close_logwriter_clear

  frontend
end

#closeObject



70
71
72
73
# File 'lib/hammerspace/backend/sparkey.rb', line 70

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.



77
78
79
80
81
82
# File 'lib/hammerspace/backend/sparkey.rb', line 77

def delete(key)
  close_hash
  open_logwriter

  @logwriter.del(key)
end

#dir_cleanup_works?Boolean

Returns:

  • (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



84
85
86
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
# File 'lib/hammerspace/backend/sparkey.rb', line 84

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

Returns:

  • (Boolean)


116
117
118
119
120
121
# File 'lib/hammerspace/backend/sparkey.rb', line 116

def has_key?(key)
  close_logwriter
  open_hash

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

#keysObject



123
124
125
126
127
128
# File 'lib/hammerspace/backend/sparkey.rb', line 123

def keys
  close_logwriter
  open_hash

  @hash ? @hash.keys : []
end

#replace(hash) ⇒ Object



130
131
132
133
134
135
# File 'lib/hammerspace/backend/sparkey.rb', line 130

def replace(hash)
  close_hash
  open_logwriter_replace

  merge!(hash)
end

#sizeObject



137
138
139
140
141
142
# File 'lib/hammerspace/backend/sparkey.rb', line 137

def size
  close_logwriter
  open_hash

  @hash ? @hash.size : 0
end

#uidObject



144
145
146
147
148
149
# File 'lib/hammerspace/backend/sparkey.rb', line 144

def uid
  close_logwriter
  open_hash

  @uid
end

#valuesObject



151
152
153
154
155
156
# File 'lib/hammerspace/backend/sparkey.rb', line 151

def values
  close_logwriter
  open_hash

  @hash ? @hash.values : []
end