Class: ClickhouseNative::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/clickhouse_native/pool.rb

Constant Summary collapse

STALE_IVAR =
:@clickhouse_native_settings_stale

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, database: "default", user: "default", password: "", compression: :none, logger: nil, settings: {}, pool_size: 5, pool_timeout: 5) ⇒ Pool

Returns a new instance of Pool.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/clickhouse_native/pool.rb', line 11

def initialize(host:, port:, database: "default", user: "default", password: "",
               compression: :none, logger: nil, settings: {},
               pool_size: 5, pool_timeout: 5)
  @host = host
  @port = port
  @database = database
  client_kwargs = { host:, port:, database:, user:, password:, compression:, logger: }
  @set_sql = settings_sql(settings)
  @pool = ConnectionPool.new(size: pool_size, timeout: pool_timeout) do
    client = Client.new(**client_kwargs)
    client.execute(@set_sql) if @set_sql
    client
  end
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



9
10
11
# File 'lib/clickhouse_native/pool.rb', line 9

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/clickhouse_native/pool.rb', line 9

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/clickhouse_native/pool.rb', line 9

def port
  @port
end

Instance Method Details

#describe_table(table, db_name: nil) ⇒ Object



70
71
72
# File 'lib/clickhouse_native/pool.rb', line 70

def describe_table(table, db_name: nil)
  with { |c| c.describe_table(table, db_name:) }
end

#execute(sql) ⇒ Object



42
43
44
# File 'lib/clickhouse_native/pool.rb', line 42

def execute(sql)
  with { |c| c.execute(sql) }
end

#insert(table, rows, **opts) ⇒ Object



58
59
60
# File 'lib/clickhouse_native/pool.rb', line 58

def insert(table, rows, **opts)
  with { |c| c.insert(table, rows, **opts) }
end

#pingObject



62
63
64
# File 'lib/clickhouse_native/pool.rb', line 62

def ping
  with(&:ping)
end

#query(sql) ⇒ Object



46
47
48
# File 'lib/clickhouse_native/pool.rb', line 46

def query(sql)
  with { |c| c.query(sql) }
end

#query_each(sql, &block) ⇒ Object



50
51
52
# File 'lib/clickhouse_native/pool.rb', line 50

def query_each(sql, &block)
  with { |c| c.query_each(sql, &block) }
end

#query_value(sql) ⇒ Object



54
55
56
# File 'lib/clickhouse_native/pool.rb', line 54

def query_value(sql)
  with { |c| c.query_value(sql) }
end

#server_versionObject



66
67
68
# File 'lib/clickhouse_native/pool.rb', line 66

def server_version
  with(&:server_version)
end

#withObject

Yields a client with the pool’s session settings applied. If the previous checkout raised (which, in this gem’s C++ bindings, always triggers a ResetConnection that wipes the session), re-apply the SET before yielding. Exceptions re-raise after marking the client stale.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/clickhouse_native/pool.rb', line 30

def with
  @pool.with do |client|
    reapply_settings_if_stale(client)
    begin
      yield client
    rescue
      client.instance_variable_set(STALE_IVAR, true)
      raise
    end
  end
end