Class: Readorder::Datum

Inherits:
Object
  • Object
show all
Defined in:
lib/readorder/datum.rb

Overview

All the block, inode and stat information about one file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Datum

call-seq:

Datum.new( filename ) -> Datum

Create a new Datum instance for the given filename



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/readorder/datum.rb', line 49

def initialize( filename )
  @filename = ::File.expand_path( filename.strip )
  @inode_number = nil
  @first_physical_block_number = nil
  @physical_block_count = 0
  @error_reason = nil
  @original_order = 0
  @size = 0
  
  @stat = nil
  @valid = false
  @collected = false
end

Instance Attribute Details

#error_reasonObject (readonly)

if there is a reason this file is not eligible for analysis this explains why



21
22
23
# File 'lib/readorder/datum.rb', line 21

def error_reason
  @error_reason
end

#filenameObject (readonly)

The fully qualified path of the file



10
11
12
# File 'lib/readorder/datum.rb', line 10

def filename
  @filename
end

#first_physical_block_numberObject (readonly)

The physical block number of the first disc block of the file. This piece of data may not be gathered. This will be nil if that is the case



17
18
19
# File 'lib/readorder/datum.rb', line 17

def first_physical_block_number
  @first_physical_block_number
end

#inode_numberObject (readonly)

The inode number of the file



13
14
15
# File 'lib/readorder/datum.rb', line 13

def inode_number
  @inode_number
end

#original_orderObject

the original order in which the Datum was collected



31
32
33
# File 'lib/readorder/datum.rb', line 31

def original_order
  @original_order
end

#physical_block_countObject (readonly)

count of the number of physical disc blocks this file consumes. This is only gathered if the first_physical_block_number is also gathered.



28
29
30
# File 'lib/readorder/datum.rb', line 28

def physical_block_count
  @physical_block_count
end

#statObject (readonly)

File::Stat of the file



24
25
26
# File 'lib/readorder/datum.rb', line 24

def stat
  @stat
end

Class Method Details

.hash_keysObject



39
40
41
# File 'lib/readorder/datum.rb', line 39

def self.hash_keys
  %w[ filename inode_number first_physical_block_number original_order size ]
end

.is_linux?Boolean

Check if we are running on linux. We use this to enable us to check the physical block id.

Returns:

  • (Boolean)


35
36
37
# File 'lib/readorder/datum.rb', line 35

def self.is_linux?
  @is_linux ||= ::Config::CONFIG['host_os'] =~ /linux/i
end

Instance Method Details

#collect(get_physical = true) ⇒ Object

:call-seq:

datum.collect( get_physical = true ) -> true

Collect all the information about the file we need. This includes:

  • making sure we have a valid file, this means the file exists and is non-zero in size

  • getting the inode number of the file

  • getting the physical block number of the first block of the file

  • getting the device of the file

If false is passed in, then the physical block number is not collected.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/readorder/datum.rb', line 125

def collect( get_physical = true )
  unless @collected then
    begin
      @stat = ::File.stat( @filename )
      if not @stat.file? then
        @valid = false
        @error_reason = "Not a file"
      elsif @stat.zero? then
        @valid = false
        @error_reason = "0 byte file"
      else
        @inode_number = @stat.ino
        if get_physical then
          @first_physical_block_number = self.find_first_physical_block_number
        end
        @valid = true
      end
    rescue => e
      @error_reason = e.to_s
      logger.warn e.to_s
      @valid = false
    ensure
      @collected = true
    end
  end
  return @collected
end

#loggerObject

call-seq:

datum.logger -> Logger

The Logger for the instance



105
106
107
# File 'lib/readorder/datum.rb', line 105

def logger
  ::Logging::Logger[self]
end

#sizeObject

call-seq:

datum.size -> Integer

The number of bytes the file consumes



95
96
97
# File 'lib/readorder/datum.rb', line 95

def size
  @size ||= @stat.size
end

#to_csvObject

call-seq:

datum.to_csv

return the datum as a CSV in the format:

physical_id,inode_id,filename


71
72
73
# File 'lib/readorder/datum.rb', line 71

def to_csv
  "#{first_physical_block_number},#{inode_number},#{filename}"
end

#to_hashObject

:call-seq;

datum.to_hash -> Hash

return all the tiems in the datum as a hash



81
82
83
84
85
86
87
# File 'lib/readorder/datum.rb', line 81

def to_hash
  h = {}
  Datum.hash_keys.each do |k|
    h[k] = self.send( k )
  end
  return h
end

#valid?Boolean

call-seq:

datum.valid?

Does this Datum represent a collection of valid data

Returns:

  • (Boolean)


159
160
161
# File 'lib/readorder/datum.rb', line 159

def valid?
  @valid
end