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



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

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

#create_database(name) ⇒ Object



226
227
228
229
230
# File 'lib/immudb/client.rb', line 226

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



302
303
304
305
306
# File 'lib/immudb/client.rb', line 302

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

#get(key) ⇒ Object



142
143
144
145
# File 'lib/immudb/client.rb', line 142

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

#get_all(keys) ⇒ Object



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

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)


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

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

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

history



246
247
248
249
250
251
252
253
254
# File 'lib/immudb/client.rb', line 246

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



309
310
311
# File 'lib/immudb/client.rb', line 309

def inspect
  to_s
end

#list_databasesObject



232
233
234
# File 'lib/immudb/client.rb', line 232

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

#list_tablesObject



298
299
300
# File 'lib/immudb/client.rb', line 298

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



256
257
258
259
260
261
262
263
264
265
# File 'lib/immudb/client.rb', line 256

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



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

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



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

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



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

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



236
237
238
239
240
241
242
# File 'lib/immudb/client.rb', line 236

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



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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/immudb/client.rb', line 147

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)

  entry_spec_digest = Store.entry_spec_digest_for(ventry.verifiableTx.tx.header.version.to_i)
  inclusion_proof = Schema.inclusion_proof_from_proto(ventry.inclusionProof)
  dual_proof = Schema.dual_proof_from_proto(ventry.verifiableTx.dualProof)

  if ventry.entry.referencedBy.nil? || ventry.entry.referencedBy.key == ""
    vTx = ventry.entry.tx
    e = Database.encode_entry_spec(key, Schema.(ventry.entry.), ventry.entry.value)
  else
    ref = ventry.entry.referencedBy
    vTx = ref.tx
    e = Database.encode_reference(ref.key, Schema.(ref.), ventry.entry.key, ref.atTx)
  end

  if state.txId <= vTx
    eh = Schema.digest_from_proto(ventry.verifiableTx.dualProof.targetTxHeader.eH)
    source_id = state.txId
    source_alh = Schema.digest_from_proto(state.txHash)
    target_id = vTx
    target_alh = dual_proof.targetTxHeader.alh
  else
    eh = Schema.digest_from_proto(ventry.verifiableTx.dualProof.sourceTxHeader.eH)
    source_id = vTx
    source_alh = dual_proof.sourceTxHeader.alh
    target_id = state.txId
    target_alh = Schema.digest_from_proto(state.txHash)
  end

  verifies = Store.verify_inclusion(inclusion_proof, entry_spec_digest.call(e), eh)
  if !verifies
    raise VerificationError
  end

  if state.txId > 0
    verifies =
      Store.verify_dual_proof(
        dual_proof,
        source_id,
        target_id,
        source_alh,
        target_alh
      )
    if !verifies
      raise VerificationError
    end
  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, metadata: nil) ⇒ 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
131
132
133
134
135
136
137
138
139
140
# File 'lib/immudb/client.rb', line 79

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

  raw_request = Schema::VerifiableSetRequest.new(
    setRequest: Schema::SetRequest.new(KVs: [kv]),
    proveSinceTx: state.txId
  )
  verifiable_tx = grpc.verifiable_set(raw_request)
  if verifiable_tx.tx.header.nentries != 1 || verifiable_tx.tx.entries.length != 1
    raise VerificationError
  end
  tx = Schema.tx_from_proto(verifiable_tx.tx)
  entry_spec_digest = Store.entry_spec_digest_for(tx.header.version)
  inclusion_proof = tx.proof(Database.encode_key(key))
  md = tx.entries[0].

  if !md.nil? && md.deleted?
    raise VerificationError
  end

  e = Database.encode_entry_spec(key, md, value)

  verifies = Store.verify_inclusion(inclusion_proof, entry_spec_digest.call(e), tx.header.eh)
  unless verifies
    raise VerificationError
  end
  if tx.header.eh != Schema.digest_from_proto(verifiable_tx.dualProof.targetTxHeader.eH)
    raise VerificationError
  end
  source_id = state.txId
  source_alh = Schema.digest_from_proto(state.txHash)
  target_id = tx.header.iD
  target_alh = tx.header.alh

  if state.txId > 0
    verifies = Store.verify_dual_proof(
      Schema.dual_proof_from_proto(verifiable_tx.dualProof),
      source_id,
      target_id,
      source_alh,
      target_alh
    )
    unless verifies
      raise VerificationError
    end
  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



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

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