Class: CrateRuby::Client

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

Constant Summary collapse

DEFAULT_HOST =
"127.0.0.1"
DEFAULT_PORT =
"4200"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(servers = [], opts = {}) ⇒ CrateRuby::Client

Currently only a single server is supported. Fail over will be implemented in upcoming versions

  • logger: Custom Logger

  • http_options [Hash]: Net::HTTP options (open_timeout, read_timeout)

Parameters:

  • servers (Array) (defaults to: [])

    An Array of servers including ports [127.0.0.1:4200, 10.0.0.1:4201]

  • Optional (opts)

    paramters



37
38
39
40
41
42
# File 'lib/crate_ruby/client.rb', line 37

def initialize(servers = [], opts = {})
  @servers = servers
  @servers << "#{DEFAULT_HOST}:#{DEFAULT_PORT}" if servers.empty?
  @logger = opts[:logger] || CrateRuby.logger
  @http_options = opts[:http_options] || { read_timeout: 3600 }
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#blob_delete(table, digest) ⇒ Boolean

Delete blob

Parameters:

  • table (String)
  • digest (String)

    SHA1 hexdigest

Returns:

  • (Boolean)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/crate_ruby/client.rb', line 163

def blob_delete(table, digest)
  uri = blob_path(table, digest)
  @logger.debug("BLOB DELETE #{uri}")
  req = Net::HTTP::Delete.new(uri)
  response = request(req)
  success = case response.code
              when "200"
                true
              else
                @logger.info("Response #{response.code}: #{response.body}")
                false
            end
  success
end

#blob_get(table, digest) ⇒ Blob

Download blob

Parameters:

  • table (String)
  • digest (String)

    SHA1 hexdigest

Returns:

  • (Blob)

    File data to write to file or use directly



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/crate_ruby/client.rb', line 144

def blob_get(table, digest)
  uri = blob_path(table, digest)
  @logger.debug("BLOB GET #{uri}")
  req = Net::HTTP::Get.new(uri)
  response = request(req)
  case response.code
    when "200"
      response.body
    else
      @logger.info("Response #{response.code}: #{response.body}")
      false
  end
end

#blob_put(table, digest, data) ⇒ Object

Upload a File to a blob table

Parameters:

  • table (String)
  • digest (String)

    SHA1 hexdigest

  • data (Boolean)

    Can be any payload object that can be sent via HTTP, e.g. STRING, FILE



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/crate_ruby/client.rb', line 123

def blob_put(table, digest, data)
  uri = blob_path(table, digest)
  @logger.debug("BLOB PUT #{uri}")
  req = Net::HTTP::Put.new(blob_path(table, digest))
  req.body = data
  response = request(req)
  success = case response.code
              when "201"
                true
              else
                @logger.info("Response #{response.code}: " + response.body)
                false
            end
  success
end

#create_blob_table(name, shard_count = 5, replicas = 0) ⇒ ResultSet

Creates a table for storing blobs client.create_blob_table(“blob_table”)

Parameters:

  • name (String)

    Table name

  • shard (Integer)

    Shard count, defaults to 5

  • number (Integer)

    Number of replicas, defaults to 0

Returns:



68
69
70
71
# File 'lib/crate_ruby/client.rb', line 68

def create_blob_table(name, shard_count=5, replicas=0)
  stmt = "create blob table #{name} clustered into #{shard_count} shards with (number_of_replicas=#{replicas})"
  execute stmt
end

#create_table(table_name, column_definition = {}, blob = false) ⇒ ResultSet

Creates a table

client.create_table "posts", id: [:integer, "primary key"], my_column: :string, my_integer_col: :integer

Parameters:

  • table_name (String)
  • column_definition (Hash) (defaults to: {})

Options Hash (column_definition):

  • key (String)

    sets column name, value sets column type. an array passed as value can be used to set options like primary keys

Returns:



55
56
57
58
59
# File 'lib/crate_ruby/client.rb', line 55

def create_table(table_name, column_definition = {}, blob=false)
  cols = column_definition.to_a.map { |a| a.join(' ') }.join(', ')
  stmt = %Q{CREATE TABLE "#{table_name}" (#{cols})}
  execute(stmt)
end

#drop_table(table_name, blob = false) ⇒ ResultSet

Drop table

Parameters:

  • table_name, (String)

    Name of table to drop

  • blob (Boolean) (defaults to: false)

    Needs to be set to true if table is a blob table

Returns:



77
78
79
80
81
# File 'lib/crate_ruby/client.rb', line 77

def drop_table(table_name, blob=false)
  tbl = blob ? "BLOB TABLE" : "TABLE"
  stmt = %Q{DROP #{tbl} "#{table_name}"}
  execute(stmt)
end

#execute(sql, args = nil, bulk_args = nil, http_options = {}) ⇒ ResultSet

Executes a SQL statement against the Crate HTTP REST endpoint.

Parameters:

  • sql (String)

    statement to execute

  • args (Array) (defaults to: nil)

    Array of values used for parameter substitution

  • Net::HTTP (Hash)

    options (open_timeout, read_timeout)

Returns:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/crate_ruby/client.rb', line 100

def execute(sql, args = nil, bulk_args = nil, http_options = {})
  @logger.debug sql
  req = Net::HTTP::Post.new("/_sql", initheader = {'Content-Type' => 'application/json'})
  body = {"stmt" => sql}
  body.merge!({'args' => args}) if args
  body.merge!({'bulk_args' => bulk_args}) if bulk_args
  req.body = body.to_json
  response = request(req, http_options)
  @logger.debug response.body
  success = case response.code
              when /^2\d{2}/
                ResultSet.new response.body
              else
                @logger.info(response.body)
                raise CrateRuby::CrateError.new(response.body)
            end
  success
end

#insert(table_name, attributes) ⇒ Object



187
188
189
190
191
192
# File 'lib/crate_ruby/client.rb', line 187

def insert(table_name, attributes)
  vals = attributes.values
  binds = vals.count.times.map {|i| "$#{i+1}"}.join(',')
  stmt = %Q{INSERT INTO "#{table_name}" (#{attributes.keys.join(', ')}) VALUES(#{binds})}
  execute(stmt, vals)
end

#inspectObject



44
45
46
# File 'lib/crate_ruby/client.rb', line 44

def inspect
  %Q{#<CrateRuby::Client:#{object_id}>}
end

#refresh_table(table_name) ⇒ Object

Crate is eventually consistent, If you don’t query by primary key, it is not guaranteed that an insert record is found on the next query. Default refresh value is 1000ms. Using refresh_table you can force a refresh

Parameters:

  • table_name (String)

    Name of table to refresh



199
200
201
# File 'lib/crate_ruby/client.rb', line 199

def refresh_table(table_name)
  execute "refresh table #{table_name}"
end

#show_tablesResultSet

List all user tables

Returns:



85
86
87
# File 'lib/crate_ruby/client.rb', line 85

def show_tables
  execute("select table_name from information_schema.tables where table_schema = 'doc'")
end

#table_structure(table_name) ⇒ Object

Return the table structure

Parameters:

  • table_name (String)

    Table name to get structure

  • (ResultSet)


182
183
184
# File 'lib/crate_ruby/client.rb', line 182

def table_structure(table_name)
  execute("select * from information_schema.columns where table_schema = 'doc' AND table_name = '#{table_name}'")
end

#tablesArray

Returns all tables in schema ‘doc’

Returns:

  • (Array)

    Array of table names



91
92
93
# File 'lib/crate_ruby/client.rb', line 91

def tables
  execute("select table_name from information_schema.tables where table_schema = 'doc'").map(&:first)
end