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

Defined Under Namespace

Modules: Flags

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ BaseChannel

Returns a new instance of BaseChannel.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/conveyor/base_channel.rb', line 22

def initialize directory
  @directory    = directory
  @data_files   = []
  @file_mutexes = []
  @index        = []
  @iterator     = 1
  @id_lock      = Mutex.new

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

  @index_file.sync    = true
end

Class Method Details

.parse_headers(str, index_file = false) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/conveyor/base_channel.rb', line 130

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

.valid_channel_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/conveyor/base_channel.rb', line 150

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

Instance Method Details

#bucket_file(i) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/conveyor/base_channel.rb', line 51

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
    @file_mutexes[i] = Mutex.new
  end
  @file_mutexes[i].synchronize do
    yield @data_files[i]
  end
end

#commit(data, time = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/conveyor/base_channel.rb', line 68

def commit data, time = nil
  l = nil
  gzip = data.length >= 256
  if gzip
    compressed_data = StringIO.new
    g = Zlib::GzipWriter.new(compressed_data)
    g << data
    g.finish
    compressed_data.rewind
    compressed_data = compressed_data.read
    l = compressed_data.length
  else
    l = data.length
  end

  h = Digest::MD5.hexdigest(data)

  id_lock do
    i = @last_id + 1
    t = time || Time.now
    b = pick_bucket(i)
    flags = 0
    flags = flags | Flags::GZIP if gzip
    header, o = nil
    bucket_file(b) do |f|
      f.seek(0, IO::SEEK_END)
      o = f.pos
      header = "#{i.to_s(36)} #{t.to_i.to_s(36)} #{o.to_s(36)} #{l.to_s(36)} #{h} #{flags.to_s(36)}"
      f.write("#{header}\n")
      f.write((gzip ? compressed_data : data))
      f.write("\n")
    end

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

#delete!Object



154
155
156
157
158
159
# File 'lib/conveyor/base_channel.rb', line 154

def delete!
  FileUtils.rm_r(@directory)
  @index = []
  @data_files =[]
  @last_id = 0
end

#get(id, stream = false) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/conveyor/base_channel.rb', line 108

def get id, stream = false
  return nil unless id <= @last_id && id > 0
  i = @index[id-1]
  headers, content, compressed_content, g = nil
  bucket_file(i[:file]) do |f|
    f.seek i[:offset]
    headers  = parse_headers(f.readline.strip)
    compressed_content = f.read(i[:length])
  end
  io = StringIO.new(compressed_content)
  if (headers[:flags] & Flags::GZIP) != 0
    g  = Zlib::GzipReader.new(io)
  else
    g = io
  end
  if stream
    [headers, g]
  else
    [headers, g.read]
  end
end

#id_lockObject



62
63
64
65
66
# File 'lib/conveyor/base_channel.rb', line 62

def id_lock
  @id_lock.synchronize do
    yield
  end
end

#inspectObject



43
44
45
# File 'lib/conveyor/base_channel.rb', line 43

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

#parse_headers(str, index_file = false) ⇒ Object



146
147
148
# File 'lib/conveyor/base_channel.rb', line 146

def parse_headers str, index_file=false
  self.class.parse_headers str, index_file
end

#pick_bucket(i) ⇒ Object



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

def pick_bucket i
  (i / BUCKET_SIZE).to_i
end