Class: Conveyor::BaseChannel

Inherits:
Object
  • Object
show all
Defined in:
lib/conveyor/base_channel.rb

Overview

BaseChannel

Base implementation for channels. Not useful to instantiate directly.

Direct Known Subclasses

Channel

Constant Summary collapse

NAME_PATTERN =
%r{\A[a-zA-Z\-0-9\_]+\Z}
BUCKET_SIZE =
100_000

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ BaseChannel

Returns a new instance of BaseChannel.



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
# File 'lib/conveyor/base_channel.rb', line 15

def initialize directory
  @directory               = directory
  @data_files              = []
  @index = []
  @iterator = 1

  if File.exists?(@directory)
    if !File.directory?(@directory)
      raise "#{@directory} is not a directory"
    end
  else
    Dir.mkdir(@directory)
  end

  index_path = File.join(@directory, 'index')

  if File.exists?(index_path) && File.size(index_path) > 0
    @index_file = File.open(index_path, 'r+')

    @index_file.each_line do |line|
      @index << parse_headers(line.strip, true)
      @last_id = @index.last[:id]
    end
    @index_file.seek(0, IO::SEEK_END)
  else
    @index_file = File.open(index_path, 'a')
    @last_id = 0
  end
  @index_file.sync = true

end

Class Method Details

.valid_channel_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/conveyor/base_channel.rb', line 114

def self.valid_channel_name? name
  !!name.match(NAME_PATTERN)
end

Instance Method Details

#bucket_file(i) {|| ... } ⇒ Object

Yields:

  • ()


55
56
57
58
59
60
61
# File 'lib/conveyor/base_channel.rb', line 55

def bucket_file i
  unless @data_files[i]
    @data_files[i] = File.open(File.join(@directory, i.to_s), 'a+')
    @data_files[i].sync = true
  end
  yield @data_files[i]
end

#commit(data, time = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/conveyor/base_channel.rb', line 63

def commit data, time = nil
  Thread.exclusive do
    i = @last_id + 1
    t = time || Time.now
    l = data.length
    h = Digest::MD5.hexdigest(data)
    b = pick_bucket(i)
    header, o = nil
    bucket_file(b) do |f|
      f.seek(0, IO::SEEK_END)
      o = f.pos
      header = "#{i} #{t.xmlschema} #{o} #{l} #{h}"
      f.write("#{header}\n" + data + "\n")
    end

    @last_id = i
    @index_file.write "#{header} #{b}\n"
    @index << {:id => i, :time => t, :offset => o, :length => l, :hash => h, :file => b}
    i
  end
end

#get(id) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/conveyor/base_channel.rb', line 85

def get id
  return nil unless id <= @last_id
  i = @index.find{|e| e[:id] == id}
  header, content = nil
  Thread.exclusive do
    bucket_file(i[:file]) do |f|
      f.seek i[:offset]
      header  = f.readline.strip
      content = f.read(i[:length])
    end
  end
  [parse_headers(header), content]
end

#inspectObject



47
48
49
# File 'lib/conveyor/base_channel.rb', line 47

def inspect
  "<#{self.class} dir:'#{@directory.to_s}' last_id:#{@last_id} iterator:#{@iterator}>"
end

#parse_headers(str, index_file = false) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/conveyor/base_channel.rb', line 99

def parse_headers str, index_file=false
  pattern =  '\A(\d+) (\d{4}\-\d{2}\-\d{2}T\d{2}\:\d{2}\:\d{2}[+\-]\d{2}\:\d{2}) (\d+) (\d+) ([a-f0-9]+)'
  pattern += ' (\d+)' if index_file
  pattern += '\Z'
  m = str.match(Regexp.new(pattern))
  {
    :id => m.captures[0].to_i,
    :time => m.captures[1],
    :offset => m.captures[2].to_i,
    :length => m.captures[3].to_i,
    :hash => m.captures[4], 
    :file => (index_file ? m.captures[5].to_i : nil)
  }.reject {|k,v| v == nil}
end

#pick_bucket(i) ⇒ Object



51
52
53
# File 'lib/conveyor/base_channel.rb', line 51

def pick_bucket i
  (i / BUCKET_SIZE).to_i
end