Class: Node

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/gitroom/models/node.rb

Constant Summary collapse

CHUNK_SIZE =
16384

Instance Method Summary collapse

Instance Method Details

#content_sizeObject

selected_chunks.each do |chunk|

      num = chunk.chunk_number - num_start
      chunk.content = buf[num*CHUNK_SIZE...(num+1)*CHUNK_SIZE]
    end
  end
  print "\n"
  selected_chunks.each do |chunk|
    rechunk = Chunk.find chunk.id
    rechunk.content = chunk.content.to_s
    print "set_content #{chunk.chunk_number} :: #{chunk.content}\n"
    rechunk.save!
  end
  print "\n"
end

end



175
176
177
178
179
180
181
# File 'lib/gitroom/models/node.rb', line 175

def content_size
  if chunks.count == 0
    0
  else
    (chunks.count - 1) * CHUNK_SIZE + chunks.order('chunk_number asc').last.content.size
  end
end

#get_chunk(number, offset = 0, right = CHUNK_SIZE - 1) ⇒ Object



43
44
45
46
# File 'lib/gitroom/models/node.rb', line 43

def get_chunk(number, offset = 0, right = CHUNK_SIZE - 1)
  chunk = chunks.where(:chunk_number => number).first
  chunk && chunk.content[offset..right] || ''
end

#get_chunk_cached(chunk, offset = 0, right = CHUNK_SIZE - 1) ⇒ Object



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

def get_chunk_cached(chunk, offset = 0, right = CHUNK_SIZE - 1)
  chunk && chunk.content[offset..right] || ''
end

#get_content(offset, size) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gitroom/models/node.rb', line 66

def get_content(offset, size)
  num_start = offset / CHUNK_SIZE
  offset_start = offset % CHUNK_SIZE
  num_end = (offset + size - 1) / CHUNK_SIZE
  offset_end = (offset + size - 1) % CHUNK_SIZE
  content = ''
  cached_chunks = chunks.where('chunk_number >= ? and chunk_number <= ?', num_start, num_end).order('chunk_number asc')
  # (num_start..num_end).to_a.each do |num|
  cached_chunks.each do |chunk|
    num = chunk.chunk_number
    offset = num == num_start ? offset_start : 0
    right = num == num_end ? offset_end : (CHUNK_SIZE - 1)
    content += get_chunk_cached chunk, offset, right
  end
  content
end

#get_full_contentObject



83
84
85
# File 'lib/gitroom/models/node.rb', line 83

def get_full_content
  chunks.order('chunk_number asc').pluck(:content).join
end

#getxattr(name) ⇒ Object



23
24
25
# File 'lib/gitroom/models/node.rb', line 23

def getxattr(name)
  JSON.parse(xattrs)[name]
end

#listxattrObject



39
40
41
# File 'lib/gitroom/models/node.rb', line 39

def listxattr
  JSON.parse(xattrs).keys
end

#removexattr(name) ⇒ Object



33
34
35
36
37
# File 'lib/gitroom/models/node.rb', line 33

def removexattr(name)
  json = JSON.parse(xattrs)
  json.delete name
  xattrs = json.to_s
end

#set_chunk(number, buf, offset = 0) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gitroom/models/node.rb', line 52

def set_chunk(number, buf, offset = 0)
  chunk = chunks.where(:chunk_number => number).first
  unless chunk
    chunk = chunks.create! :chunk_number => number, :node_id => id, :content => ""
  end
  if chunk.content.size < offset+buf.size
    chunk.content += "\0" * (offset + buf.size - chunk.content.size)
  end
  chunk.content[offset...offset+buf.size] = buf
  rechunk = Chunk.find chunk.id
  rechunk.content = chunk.content.to_s
  rechunk.save!
end

#set_content(offset, buf) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gitroom/models/node.rb', line 87

def set_content(offset, buf)
  size = buf.size
  num_start = offset / CHUNK_SIZE
  offset_start = offset % CHUNK_SIZE
  num_end = (offset + size - 1) / CHUNK_SIZE
  offset_end = (offset + size - 1) % CHUNK_SIZE
  (num_start..num_end).to_a.each do |num|
    offset = num == num_start ? offset_start : 0
    data_offset = num == num_start ? 0 : ((num - num_start) * CHUNK_SIZE - offset_start)
    data_right = num == num_end ? -1 : (data_offset + CHUNK_SIZE - 1)
    set_chunk num, buf[data_offset..data_right], offset
  end
  buf.size
end

#setxattr(name, value, flag) ⇒ Object



27
28
29
30
31
# File 'lib/gitroom/models/node.rb', line 27

def setxattr(name, value, flag)
  json = JSON.parse(xattrs)
  json[name] = value
  xattrs = json.to_s
end

#statObject



13
14
15
16
17
18
19
20
21
# File 'lib/gitroom/models/node.rb', line 13

def stat
  if directory
    RFuse::Stat.directory mode,:uid => uid, :gid => gid, :atime => updated_at,
                          :mtime => updated_at, :size => 48
  else
    RFuse::Stat.file mode,:uid => uid, :gid => gid, :atime => updated_at,
                     :mtime => updated_at, :size => content_size
  end
end

#truncate_content(offset) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/gitroom/models/node.rb', line 183

def truncate_content(offset)
  Chunk.transaction do
    num_end = (offset) / CHUNK_SIZE
    offset_end = (offset) % CHUNK_SIZE
    selected_chunks = chunks.where('chunk_number > ?', num_end)
    selected_chunks.each { |chunk| chunk.destroy }
    last_chunk = chunks.last
    last_chunk.content = last_chunk.content[0..offset_end]
    last_chunk.save
  end
end