Class: Immudb::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, host: nil, port: nil, username: nil, password: nil, database: nil, timeout: nil, rs: nil) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/immudb/client.rb', line 15

def initialize(url: nil, host: nil, port: nil, username: nil, password: nil, database: nil, timeout: nil, rs: nil)
  url ||= ENV["IMMUDB_URL"]
  if url
    uri = URI.parse(url)

    if uri.scheme != "immudb"
      raise ArgumentError, "Expected url to start with immudb://"
    end

    host ||= uri.host
    port ||= uri.port
    username ||= uri.username
    password ||= uri.password
    database ||= uri.path.sub(/\A\//, "")
  end

  host ||= "localhost"
  port ||= 3322
  username ||= "immudb"
  password ||= "immudb"
  database ||= "defaultdb"

  raise ArgumentError, "Invalid host" if host.include?(":")

  @rs = rs || RootService.new

  @interceptor = Interceptor.new
  @grpc = Schema::ImmuService::Stub.new("#{host}:#{port.to_i}", :this_channel_is_insecure, timeout: timeout, interceptors: [interceptor])

  (username, password)
  use_database(database)
end

Instance Method Details

#change_password(user, old_password:, new_password:) ⇒ Object



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

def change_password(user, old_password:, new_password:)
  req = Schema::ChangePasswordRequest.new(user: user, oldPassword: old_password, newPassword: new_password)
  grpc.change_password(req)
  nil
end

#clean_indexObject

management



252
253
254
255
# File 'lib/immudb/client.rb', line 252

def clean_index
  grpc.compact_index(Google::Protobuf::Empty.new)
  nil
end

#create_database(name) ⇒ Object



209
210
211
212
213
# File 'lib/immudb/client.rb', line 209

def create_database(name)
  req = Schema::Database.new(databaseName: name)
  grpc.create_database(req)
  nil
end

#create_user(user, password:, permission:, database:) ⇒ Object



61
62
63
64
65
# File 'lib/immudb/client.rb', line 61

def create_user(user, password:, permission:, database:)
  req = Schema::CreateUserRequest.new(user: user, password: password, permission: PERMISSION.fetch(permission), database: database)
  grpc.create_user(req)
  nil
end

#describe_table(name) ⇒ Object



285
286
287
288
289
# File 'lib/immudb/client.rb', line 285

def describe_table(name)
  req = Schema::Table.new(tableName: name)
  res = grpc.describe_table(req)
  sql_result(res).to_a
end

#get(key) ⇒ Object



132
133
134
135
# File 'lib/immudb/client.rb', line 132

def get(key)
  req = Schema::KeyRequest.new(key: key)
  grpc.get(req).value
end

#get_all(keys) ⇒ Object



204
205
206
207
# File 'lib/immudb/client.rb', line 204

def get_all(keys)
  req = Schema::KeyListRequest.new(keys: keys)
  grpc.get_all(req).entries.to_h { |v| [v.key, v.value] }
end

#healthy?Boolean

Returns:

  • (Boolean)


257
258
259
260
261
# File 'lib/immudb/client.rb', line 257

def healthy?
  grpc.health(Google::Protobuf::Empty.new).status
rescue Error
  false
end

#history(key, offset: nil, limit: nil, desc: false) ⇒ Object

history



229
230
231
232
233
234
235
236
237
# File 'lib/immudb/client.rb', line 229

def history(key, offset: nil, limit: nil, desc: false)
  req = Schema::HistoryRequest.new(key: key, offset: offset, limit: limit, desc: desc)
  grpc.history(req).entries.map do |entry|
    {
      tx: entry.tx,
      value: entry.value
    }
  end
end

#inspectObject

hide token



292
293
294
# File 'lib/immudb/client.rb', line 292

def inspect
  to_s
end

#list_databasesObject



215
216
217
# File 'lib/immudb/client.rb', line 215

def list_databases
  grpc.database_list(Google::Protobuf::Empty.new).databases.map(&:databaseName)
end

#list_tablesObject



281
282
283
# File 'lib/immudb/client.rb', line 281

def list_tables
  grpc.list_tables(Google::Protobuf::Empty.new).rows.map { |r| r.values.first.s }
end

#list_usersObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/immudb/client.rb', line 48

def list_users
  permission_map = PERMISSION.invert
  grpc.list_users(Google::Protobuf::Empty.new).users.map do |user|
    {
      user: user.user,
      permissions: user.permissions.map { |v| {database: v.database, permission: permission_map.fetch(v.permission)} },
      created_by: user.createdby,
      created_at: Time.parse(user.createdat),
      active: user.active
    }
  end
end

#scan(seek_key: nil, prefix: nil, desc: false, limit: nil, since_tx: nil, no_wait: false) ⇒ Object



239
240
241
242
243
244
245
246
247
248
# File 'lib/immudb/client.rb', line 239

def scan(seek_key: nil, prefix: nil, desc: false, limit: nil, since_tx: nil, no_wait: false)
  req = Schema::ScanRequest.new(seekKey: seek_key, prefix: prefix, desc: desc, limit: limit, sinceTx: since_tx, noWait: no_wait)
  grpc.scan(req).entries.map do |entry|
    {
      tx: entry.tx,
      key: entry.key,
      value: entry.value
    }
  end
end

#set(key, value) ⇒ Object



73
74
75
76
77
# File 'lib/immudb/client.rb', line 73

def set(key, value)
  req = Schema::SetRequest.new(KVs: [Schema::KeyValue.new(key: key, value: value)])
  grpc.set(req)
  nil
end

#set_all(values) ⇒ Object



198
199
200
201
202
# File 'lib/immudb/client.rb', line 198

def set_all(values)
  req = Schema::SetRequest.new(KVs: values.map { |k, v| Schema::KeyValue.new(key: k, value: v) })
  grpc.set(req)
  nil
end

#sql_exec(sql, params = {}) ⇒ Object

sql



269
270
271
272
273
# File 'lib/immudb/client.rb', line 269

def sql_exec(sql, params = {})
  req = Schema::SQLExecRequest.new(sql: sql, params: sql_params(params))
  grpc.sql_exec(req)
  nil
end

#sql_query(sql, params = {}) ⇒ Object



275
276
277
278
279
# File 'lib/immudb/client.rb', line 275

def sql_query(sql, params = {})
  req = Schema::SQLQueryRequest.new(sql: sql, params: sql_params(params))
  res = grpc.sql_query(req)
  sql_result(res)
end

#use_database(name) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/immudb/client.rb', line 219

def use_database(name)
  req = Schema::Database.new(databaseName: name)
  res = grpc.use_database(req)
  interceptor.token = res.token
  @rs.init(name, grpc)
  nil
end

#verified_get(key) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/immudb/client.rb', line 137

def verified_get(key)
  state = @rs.get
  req = Schema::VerifiableGetRequest.new(
    keyRequest: Schema::KeyRequest.new(key: key),
    proveSinceTx: state.txId
  )
  ventry = grpc.verifiable_get(req)

  inclusion_proof = HTree.inclusion_proof_from(ventry.inclusionProof)
  dual_proof = HTree.dual_proof_from(ventry.verifiableTx.dualProof)

  if ventry.entry.referencedBy.nil? || ventry.entry.referencedBy.key == ""
    vTx = ventry.entry.tx
    kv = Store.encode_kv(key, ventry.entry.value)
  else
    vTx = ventry.entry.referencedBy.tx
    kv = store.encode_reference(ventry.entry.referencedBy.key, ventry.entry.key, ventry.entry.referencedBy.atTx)
  end

  if state.txId <= vTx
    eh = Store.digest_from(ventry.verifiableTx.dualProof..eH)
    source_id = state.txId
    source_alh = Store.digest_from(state.txHash)
    target_id = vTx
    target_alh = dual_proof..alh
  else
    eh = Store.digest_from(ventry.verifiableTx.dualProof..eH)
    source_id = vTx
    source_alh = dual_proof..alh
    target_id = state.txId
    target_alh = store.digest_from(state.txHash)
  end

  verifies = Store.verify_inclusion(inclusion_proof, kv.digest, eh)
  if !verifies
    raise VerificationError
  end
  verifies = Store.verify_dual_proof(
      dual_proof,
      source_id,
      target_id,
      source_alh,
      target_alh)
  if !verifies
    raise VerificationError
  end
  newstate = State.new(
    db: state.db,
    txId: target_id,
    txHash: target_alh,
    publicKey: ventry.verifiableTx.signature&.publicKey,
    signature: ventry.verifiableTx.signature&.signature,
  )
  if !verifying_key.nil?
    newstate.verify(verifying_key)
  end
  @rs.set(newstate)

  ventry.entry.value
end

#verified_set(key, value) ⇒ Object



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/immudb/client.rb', line 79

def verified_set(key, value)
  state = @rs.get
  kv = Schema::KeyValue.new(key: key, value: value)

  raw_request = Schema::VerifiableSetRequest.new(
    setRequest: Schema::SetRequest.new(KVs: [kv]),
    proveSinceTx: state.txId
  )
  verifiable_tx = grpc.verifiable_set(raw_request)
  tx = Store.tx_from(verifiable_tx.tx)
  inclusion_proof = tx.proof(SET_KEY_PREFIX + key)
  ekv = Store.encode_kv(key, value)
  verifies = Store.verify_inclusion(inclusion_proof, ekv.digest, tx.eh)
  unless verifies
    raise VerificationError
  end
  if tx.eh != Store.digest_from(verifiable_tx.dualProof..eH)
    raise VerificationError
  end
  if state.txId == 0
    source_id = tx.ID
    source_alh = tx.Alh
  else
    source_id = state.txId
    source_alh = Store.digest_from(state.txHash)
  end
  target_id = tx.ID
  target_alh = tx.Alh

  verifies = Store.verify_dual_proof(
    HTree.dual_proof_from(verifiable_tx.dualProof),
    source_id,
    target_id,
    source_alh,
    target_alh
  )
  if !verifies
    raise VerificationError
  end
  newstate = State.new(
    db: state.db,
    txId: target_id,
    txHash: target_alh,
    publicKey: verifiable_tx.signature&.publicKey,
    signature: verifiable_tx.signature&.signature,
  )
  if !verifying_key.nil?
    newstate.verify(verifying_key)
  end
  @rs.set(newstate)
  nil
end

#versionObject



263
264
265
# File 'lib/immudb/client.rb', line 263

def version
  grpc.health(Google::Protobuf::Empty.new).version
end