Class: SMPTool::VirtualVolume::VolumeData

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/smp_tool/virtual_volume/volume_data.rb

Overview

Provides set of methods to work with volume’s data.

Instance Method Summary collapse

Constructor Details

#initialize(obj, extra_word) ⇒ VolumeData

Returns a new instance of VolumeData.



9
10
11
12
13
14
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 9

def initialize(obj, extra_word)
  @obj = obj
  @extra_word = extra_word

  super(obj)
end

Instance Method Details

#calc_n_free_clustersInteger

Calculate total number of free clusters.

Returns:

  • (Integer)


90
91
92
93
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 90

def calc_n_free_clusters
  self.select(&:empty_entry?)
      .sum(&:n_clusters)
end

#f_delete(file_id) ⇒ String

Delete file from the virtual volume.

Parameters:

Returns:

  • (String)

    ASCII filename of a deleted file.

Raises:

  • (ArgumentError)
    • File with ‘file_id` not found.



57
58
59
60
61
62
63
64
65
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 57

def f_delete(file_id)
  idx = _find_idx(file_id.radix50)

  _raise_file_not_found(file_id.print_ascii) unless idx

  self[idx].clean

  file_id.print_ascii
end

#f_push(file) ⇒ String

Append a file.

Parameters:

Returns:

  • (String)

    file.print_ascii_filename ASCII filename of the pushed file.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 103

def f_push(file)
  _raise_already_exists(file.print_ascii_filename) if _already_exists?(file.filename)

  # We're starting from the end of the array, since free space tend to locate
  # at the end of the volume (esp. after the 'squeeze' command).
  idx = index(reverse_each.detect { |e| e.n_clusters >= file.n_clusters && e.empty_entry? })

  unless idx
    raise ArgumentError,
          "no free space found to fit the file (try to squeeze, delete files or allocate more clusters)"
  end

  _f_push(file, idx)

  file.print_ascii_filename
end

#f_rename(old_id, new_id) ⇒ Array<String>

Rename file on the virtual volume.

Parameters:

Returns:

  • (Array<String>)

    Old and new ASCII filenames of a renamed file.

Raises:

  • (ArgumentError)
    • Can’t assign name ‘new_id` to the file since another file with

    the same name already exists;

    • File ‘old_id` not found.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 34

def f_rename(old_id, new_id)
  _raise_already_exists(new_id.print_ascii) if _already_exists?(new_id.radix50)

  idx = _find_idx(old_id.radix50)

  _raise_file_not_found(old_id.print_ascii) unless idx

  self[idx].rename(new_id.radix50)

  [old_id.print_ascii, new_id.print_ascii]
end

#push_empty_entry(n_free_clusters) ⇒ Object



120
121
122
123
124
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 120

def push_empty_entry(n_free_clusters)
  push(_new_empty_entry(n_free_clusters))

  self
end

#resize(n_clusters) ⇒ Integer

<Description>

Parameters:

  • n_clusters (Integer)

    Number of clusters to add (pos. int.) or to trim (neg. int.).

Returns:

  • (Integer)

    n_clusters Number of clusters that were added/trimmed.



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 135

def resize(n_clusters)
  n_free_clusters = calc_n_free_clusters
  diff = n_free_clusters + n_clusters

  if reject(&:empty_entry?).empty? && diff < 1
    raise ArgumentError, "Can't trim: volume should keep at least one empty/file entry"
  end

  reject!(&:empty_entry?)
  push_empty_entry(diff) unless diff.zero?

  n_clusters
end

#snapshotObject



16
17
18
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 16

def snapshot
  map(&:snapshot)
end

#squeezeInteger

Consolidate all free space at the end of the volume.

Returns:

  • (Integer)

    n_free_clusters Number of free clusters that were joined.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 73

def squeeze
  n_free_clusters = calc_n_free_clusters

  return n_free_clusters if n_free_clusters.zero?

  reject!(&:empty_entry?)

  push_empty_entry(n_free_clusters)

  n_free_clusters
end