Class: SwiftStorage::Node

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/swift_storage/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#hmac, #sig_to_hex, #struct

Constructor Details

#initialize(parent, name = nil) ⇒ Node

Returns a new instance of Node.



8
9
10
11
# File 'lib/swift_storage/node.rb', line 8

def initialize(parent, name=nil)
  @parent = parent
  @name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/swift_storage/node.rb', line 5

def name
  @name
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/swift_storage/node.rb', line 5

def parent
  @parent
end

Instance Method Details

#clear_cacheObject



80
81
82
83
# File 'lib/swift_storage/node.rb', line 80

def clear_cache
  @headers = nil
  @metadata = nil
end

#deleteObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/swift_storage/node.rb', line 92

def delete
  # We try a few times, as the swift cluster might need time to get ready
  3.times do |i|
    begin
      return request(relative_path, :method => :delete)
    rescue SwiftStorage::Errors::ServerError
      sleep(i**2)
    end
  end
end

#delete_if_existsObject



103
104
105
106
107
# File 'lib/swift_storage/node.rb', line 103

def delete_if_exists
  delete
rescue SwiftStorage::Errors::NotFoundError
  false
end

#exists?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
# File 'lib/swift_storage/node.rb', line 85

def exists?
  request(relative_path, :method => :head)
  true
rescue SwiftStorage::Errors::NotFoundError
  false
end

#get_lines(path, prefix: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/swift_storage/node.rb', line 33

def get_lines(path, prefix: nil)
  headers = {'Accept' => 'text/plain'}
  response = request(path, :headers => headers, :params => {:prefix => prefix})
  body = response.body
  if body.nil? || body.empty?
    []
  else
    body.lines.map(&:strip)
  end
end

#headersObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/swift_storage/node.rb', line 52

def headers
  return @headers if @headers

  response ||= request(relative_path, :method => :head)

  h = {}
   = h[:metadata] = {}
  response.to_hash.each_pair do |k,v|
    if k =~ /^X-#{self.class.header_name}-Meta-?(.+)/i
      k = $1
      t = 
    elsif k =~ /^X-#{self.class.header_name}-?(.+)/i
      k = $1
      t = h
    else
      t = h
    end
    k = k.downcase.gsub(/\W/, '_')
    v = v.first if v.respond_to?(:to_ary)
    t[k] = v
  end
  @headers = struct(h)
end

#metadataObject



76
77
78
# File 'lib/swift_storage/node.rb', line 76

def 
  @metadata ||= struct(headers.)
end

#relative_pathObject

Raises:

  • (NotImplemented)


48
49
50
# File 'lib/swift_storage/node.rb', line 48

def relative_path
  raise NotImplemented
end

#request(*args) ⇒ Object



13
14
15
# File 'lib/swift_storage/node.rb', line 13

def request(*args)
  service.request(*args)
end

#serviceSwiftStorage::Service

Returns the service for this node

Returns:



22
23
24
25
26
27
28
29
30
31
# File 'lib/swift_storage/node.rb', line 22

def service
  unless defined?(@service)
    p = parent
    while p && !(SwiftStorage::Service === p)
       p = p.parent
    end
    @service = p
  end
  @service
end

#to_sObject



44
45
46
# File 'lib/swift_storage/node.rb', line 44

def to_s
  "#<#{self.class.name} #{name}>"
end