Class: TrinoSqlParser::SupportProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/trino_sql_parser/support_process.rb

Instance Method Summary collapse

Constructor Details

#initialize(idle_wait: 2, with_tokens:) ⇒ SupportProcess

Returns a new instance of SupportProcess.



5
6
7
8
9
10
11
12
# File 'lib/trino_sql_parser/support_process.rb', line 5

def initialize(idle_wait: 2, with_tokens:)
  @idle_wait = idle_wait
  @with_tokens = with_tokens
  @mutex = Mutex.new
  @last_used_pid = nil
  @pipe = nil
  @pid = nil
end

Instance Method Details

#kill!Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/trino_sql_parser/support_process.rb', line 32

def kill!
  @mutex.synchronize do
    if @pid
      Process.kill("KILL", @pid)
      @pipe.close rescue nil
      @pipe = nil
      @pid = nil
    end
  end
end

#receive_lineObject



52
53
54
# File 'lib/trino_sql_parser/support_process.rb', line 52

def receive_line
  @pipe.gets
end

#send_line(line) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/trino_sql_parser/support_process.rb', line 43

def send_line(line)
  @mutex.synchronize do  # block kill! during execution
    start! unless @pipe
    @pipe.puts line
    @last_used_pid = @pipe.pid
  end
  nil
end

#start!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/trino_sql_parser/support_process.rb', line 14

def start!
  return if @pipe

  cmd = (
    [TrinoSqlParser.java_cmd] +
    TrinoSqlParser.java_args.map {|arg| Shellwords.escape(arg) } +
    ["-jar", Shellwords.escape(TrinoSqlParser.jar_path)]
  ).join(' ')

  if @with_tokens
    cmd << " --with-tokens"
  end

  @pipe = IO.popen(TrinoSqlParser.java_env, cmd, "r+", external_encoding: 'UTF-8')
  @pid = @pipe.pid
  Thread.new(@pid, &method(:monitor_thread))
end