Class: Ignite::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ignite/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host: "localhost", port: 10800, username: nil, password: nil, use_ssl: nil, ssl_params: {}, connect_timeout: nil) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ignite/client.rb', line 5

def initialize(host: "localhost", port: 10800, username: nil, password: nil, use_ssl: nil, ssl_params: {}, connect_timeout: nil)
  begin
    @socket = Socket.tcp(host, port, connect_timeout: connect_timeout, resolv_timeout: connect_timeout)
  rescue Errno::ETIMEDOUT
    raise TimeoutError, "Connection timed out"
  end

  use_ssl = use_ssl.nil? ? (username || password) : use_ssl
  if use_ssl
    ssl_context = OpenSSL::SSL::SSLContext.new

    # very important!!
    # call set_params so default params are applied
    # (like min_version and verify_mode)
    ssl_context.set_params(ssl_params)

    @socket = OpenSSL::SSL::SSLSocket.new(@socket, ssl_context)
    @socket.sync_close = true
    @socket.connect
  end

  send_handshake(username, password)
end

Instance Method Details

#cache(name) ⇒ Object



33
34
35
# File 'lib/ignite/client.rb', line 33

def cache(name)
  Cache.new(self, name)
end

#cachesObject



41
42
43
44
45
46
47
# File 'lib/ignite/client.rb', line 41

def caches
  req = Request.new(OP_CACHE_GET_NAMES)

  res = send_request(req)
  cache_count = res.read_int
  cache_count.times.map { cache(res.read_string_object) }
end

#closeObject



29
30
31
# File 'lib/ignite/client.rb', line 29

def close
  @socket.close
end

#close_resource(resource_id) ⇒ Object



118
119
120
121
122
123
# File 'lib/ignite/client.rb', line 118

def close_resource(resource_id)
  req = Request.new(OP_RESOURCE_CLOSE)
  req.long resource_id
  send_request(req)
  nil
end

#get_or_create_cache(name) ⇒ Object



37
38
39
# File 'lib/ignite/client.rb', line 37

def get_or_create_cache(name)
  cache(name).get_or_create
end

#query(statement, args = [], schema: "PUBLIC", page_size: 1000, max_rows: nil, statement_type: :any, timeout: nil) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
107
108
109
110
111
112
113
114
115
116
# File 'lib/ignite/client.rb', line 49

def query(statement, args = [], schema: "PUBLIC", page_size: 1000, max_rows: nil, statement_type: :any, timeout: nil)
  statement_type = [:any, :select, :update].index(statement_type)
  raise ArgumentError, "Invalid statement type" unless statement_type

  schema = get_or_create_cache(schema)

  req = Request.new(OP_QUERY_SQL_FIELDS)
  req.int schema.cache_id
  req.byte 0
  req.string schema.name
  req.int page_size
  req.int(max_rows || -1)
  req.string statement
  req.int args.size
  args.each do |arg|
    req.data_object arg
  end
  req.byte statement_type
  req.bool false
  req.bool false
  req.bool false
  req.bool false
  req.bool false
  req.bool false
  req.long(timeout || 0)
  req.bool true

  res = send_request(req)
  cursor_id = res.read_long
  field_count = res.read_int
  field_names = []
  field_count.times do
    field_names << res.read_string_object
  end

  rows = []
  row_count = res.read_int
  row_count.times do
    row = {}
    field_names.each do |field_name|
      row[field_name] = res.read_data_object
    end
    rows << row
  end
  more_results = res.read_bool

  while more_results && (!max_rows || rows.size < max_rows)
    req = Request.new(OP_QUERY_SQL_FIELDS_CURSOR_GET_PAGE)
    req.long cursor_id

    res = send_request(req)
    row_count = res.read_int
    row_count.times do
      row = {}
      field_names.each do |field_name|
        row[field_name] = res.read_data_object
      end
      rows << row
    end
    more_results = res.read_bool
  end

  if max_rows && rows.size > max_rows
    rows.pop(rows.size - max_rows)
  end

  rows
end

#read(len) ⇒ Object

internal



126
127
128
# File 'lib/ignite/client.rb', line 126

def read(len)
  @socket.read(len)
end

#send_request(req) ⇒ Object

internal



131
132
133
134
135
136
# File 'lib/ignite/client.rb', line 131

def send_request(req)
  @socket.write(req.to_bytes)
  res = Response.new(self)
  check_header res
  res
end