class TkrzwRPC::RemoteDBM

Remote database manager. All operations except for “connect” and “disconnect” are thread-safe; Multiple threads can access the same database concurrently. The “set_dbm_index” affects all threads so it should be called before the object is shared.

Constants

ANY_DATA

The special bytes value for no-operation or any data.

Attributes

channel[R]
dbm_index[R]
encoding[R]
stub[R]
timeout[R]

Public Class Methods

new() click to toggle source

Does nothing especially.

# File tkrzw_rpc.rb, line 224
def initialize
  @channel = nil
  @stub = nil
  @timeout = nil
  @dbm_index = 0
  @encoding = nil
end

Public Instance Methods

[](key) click to toggle source

Gets the value of a record, to enable the [] operator.

  • @param key The key of the record.

  • @return The value of the matching record or nil on failure.

# File tkrzw_rpc.rb, line 1074
def [](key)
  if not @channel
    return nil
  end
  request = GetRequest.new
  request.dbm_index = @dbm_index
  request.key = make_string(key)
  begin
    response = @stub.get(request)
  rescue GRPC::BadStatus
    return nil
  end
  if response.status.code == Status::SUCCESS
    if @encoding
      return response.value.dup.force_encoding(@encoding)
    end
    return response.value
  end
  nil
end
[]=(key, value) click to toggle source

Sets a record of a key and a value, to enable the []= operator.

  • @param key The key of the record.

  • @param value The value of the record.

  • @return The new value of the record or nil on failure.

# File tkrzw_rpc.rb, line 1099
def []=(key, value)
  if not @channel
    return nil
  end
  request = SetRequest.new
  request.dbm_index = @dbm_index
  request.key = make_string(key)
  request.value = make_string(value)
  request.overwrite = true
  begin
    response = @stub.set(request)
  rescue GRPC::BadStatus
    return nil
  end
  if response.status.code == Status::SUCCESS
    if @encoding
      return request.value.dup.force_encoding(@encoding)
    end
    return request.value
  end
  nil
end
append(key, value, delim="") click to toggle source

Appends data at the end of a record of a key.

  • @param key The key of the record.

  • @param value The value to append.

  • @param delim The delimiter to put after the existing record.

  • @return The result status.

If there's no existing record, the value is set without the delimiter.

# File tkrzw_rpc.rb, line 593
def append(key, value, delim="")
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  request = AppendRequest.new
  request.dbm_index = @dbm_index
  request.key = make_string(key)
  request.value = make_string(value)
  request.delim = make_string(delim)
  begin
    response = @stub.append(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  make_status_from_proto(response.status)
end
append_multi(delim="", **records) click to toggle source

Appends data to multiple records of the keyword arguments.

  • @param delim The delimiter to put after the existing record.

  • @param records Records to append.

  • @return The result status.

# File tkrzw_rpc.rb, line 614
def append_multi(delim="", **records)
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  request = AppendMultiRequest.new
  request.dbm_index = @dbm_index
  records.each { |key, value|
    req_record = BytesPair.new
    req_record.first = make_string(key)
    req_record.second = make_string(value)
    request.records.push(req_record)
  }
  request.delim = make_string(delim)
  begin
    response = @stub.append_multi(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  make_status_from_proto(response.status)
end
clear() click to toggle source

Removes all records.

  • @return The result status.

# File tkrzw_rpc.rb, line 917
def clear
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  request = ClearRequest.new
  request.dbm_index = @dbm_index
  begin
    response = @stub.clear(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  make_status_from_proto(response.status)
end
compare_exchange(key, expected, desired) click to toggle source

Compares the value of a record and exchanges if the condition meets.

  • @param key The key of the record.

  • @param expected The expected value. If it is nil, no existing record is expected. If it is ANY_DATA, an existing record with any value is expacted.

  • @param desired The desired value. If it is nil, the record is to be removed. If it is ANY_DATA, no update is done.

  • @return The result status. If the condition doesn't meet, INFEASIBLE_ERROR is returned.

# File tkrzw_rpc.rb, line 640
def compare_exchange(key, expected, desired)
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  request = CompareExchangeRequest.new
  request.dbm_index = @dbm_index
  request.key = make_string(key)
  if expected != nil
    if expected.equal?(ANY_DATA)
      request.expected_existence = true
      request.expect_any_value = true
    else
      request.expected_existence = true
      request.expected_value = make_string(expected)
    end
  end
  if desired != nil
    if desired.equal?(ANY_DATA)
      request.desire_no_update = true
    else
      request.desired_existence = true
      request.desired_value = make_string(desired)
    end
  end
  begin
    response = @stub.compare_exchange(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  make_status_from_proto(response.status)
end
compare_exchange_advanced(key, expected, desired, retry_wait=nil, notify=false) click to toggle source

Does compare-and-exchange and/or gets the old value of the record.

  • @param key The key of the record.

  • @param expected The expected value. If it is nil, no existing record is expected. If it is ANY_DATA, an existing record with any value is expacted.

  • @param desired The desired value. If it is nil, the record is to be removed. If it is ANY_DATA, no update is done.

  • @param retry_wait The maximum wait time in seconds before retrying. If it is nil, no retry is done. If it is positive, retry is done after waiting for the notifications of the next update for the time at most.

  • @param notify If true, a notification signal is sent to wake up retrying threads.

  • @return A pair of the result status and the.old value of the record. If the condition doesn't meet, the state is INFEASIBLE_ERROR. If there's no existing record, the value is nil.

# File tkrzw_rpc.rb, line 679
def compare_exchange_advanced(key, expected, desired, retry_wait=nil, notify=false)
  if not @channel
    return [Status.new(Status::PRECONDITION_ERROR, "not opened connection"), nil]
  end
  request = CompareExchangeRequest.new
  request.dbm_index = @dbm_index
  request.key = make_string(key)
  if expected != nil
    if expected.equal?(ANY_DATA)
      request.expected_existence = true
      request.expect_any_value = true
    else
      request.expected_existence = true
      request.expected_value = make_string(expected)
    end
  end
  if desired != nil
    if desired.equal?(ANY_DATA)
      request.desire_no_update = true
    else
      request.desired_existence = true
      request.desired_value = make_string(desired)
    end
  end
  request.get_actual = true
  request.retry_wait = retry_wait ? retry_wait : 0
  request.notify = notify
  begin
    response = @stub.compare_exchange(request)
  rescue GRPC::BadStatus => error
    return [Status.new(Status::NETWORK_ERROR, str_grpc_error(error)), nil]
  end
  actual = nil
  if response.found
    actual = response.actual
    if @encoding
      actual = actual.dup.force_encoding(@encoding)
    end
  end
  [make_status_from_proto(response.status), actual]
end
compare_exchange_multi(expected, desired) click to toggle source

Compares the values of records and exchanges if the condition meets.

  • @param expected An array of pairs of the record keys and their expected values. If the value is nil, no existing record is expected. If the value is ANY_DATA, an existing record with any value is expacted.

  • @param desired An array of pairs of the record keys and their desired values. If the value is nil, the record is to be removed.

  • @return The result status. If the condition doesn't meet, INFEASIBLE_ERROR is returned.

# File tkrzw_rpc.rb, line 761
def compare_exchange_multi(expected, desired)
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  request = CompareExchangeMultiRequest.new
  request.dbm_index = @dbm_index
  expected.each { |elem|
    state = RecordState.new
    state.key = make_string(elem[0])
    value = elem[1]
    if value != nil
      if value.equal?(ANY_DATA)
        state.existence = true
        state.any_value = true
      else
        state.existence = true
        state.value = make_string(value)
      end
    end
    request.expected.push(state)
  }
  desired.each { |elem|
    state = RecordState.new
    state.key = make_string(elem[0])
    value = elem[1]
    if value != nil
      state.existence = true
      state.value = make_string(value)
    end
    request.desired.push(state)
  }
  begin
    response = @stub.compare_exchange_multi(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  make_status_from_proto(response.status)
end
connect(address, timeout=nil, auth_config=nil) click to toggle source

Connects to the server.

  • @param address The address or the host name of the server and its port number. For IPv4 address, it's like “127.0.0.1:1978”. For IPv6, it's like “[::1]:1978”. For UNIX domain sockets, it's like “unix:/path/to/file”.

  • @param timeout The timeout in seconds for connection and each operation. Negative means unlimited.

  • @param auth_config The authentication configuration. It it is empty or null, no authentication is done. If it begins with “ssl:”, the SSL authentication is done. Key-value parameters in “key=value,key=value,…” format comes next. For SSL, “key”, “cert”, and “root” parameters specify the paths of the client private key file, the client certificate file, and the root CA certificate file respectively.

  • @return The result status.

# File tkrzw_rpc.rb, line 246
def connect(address, timeout=nil, auth_config=nil)
  if @channel
    return Status.new(Status::PRECONDITION_ERROR, "opened connection")
  end
  timeout = timeout == nil ? 1 << 27 : timeout
  begin
    if auth_config and not auth_config.empty?
      if auth_config.start_with?("ssl:")
        key_path, cert_path, root_path = nil, nil, nil
        auth_config[4..-1].split(",").each { |field|
          columns = field.split("=")
          if columns.length == 2
            case columns[0]
            when "key"
              key_path = columns[1]
            when "cert"
              cert_path = columns[1]
            when "root"
              root_path = columns[1]
            end
          end
        }
        if not key_path
          return Status.new(Status::INVALID_ARGUMENT_ERROR, "client private key unspecified")
        end
        if not cert_path
          return Status.new(Status::INVALID_ARGUMENT_ERROR, "client certificate unspecified")
        end
        if not root_path
          return Status.new(Status::INVALID_ARGUMENT_ERROR, "root certificate unspecified")
        end
        key_data = File.read(key_path)
        cert_data = File.read(cert_path)
        root_data = File.read(root_path)
        credentials = GRPC::Core::ChannelCredentials.new(root_data, key_data, cert_data)
        channel = GRPC::ClientStub.setup_channel(nil, address, credentials)
      else
        return Status.new(Status::INVALID_ARGUMENT_ERROR, "unknown authentication mode")
      end
    else
      credentials = :this_channel_is_insecure
    end
    channel = GRPC::ClientStub.setup_channel(nil, address, credentials)
    deadline = Time.now + timeout
    max_failures = 3
    num_failures = 0
    while true do
      if Time.now > deadline
        channel.close
        return Status.new(Status::PRECONDITION_ERROR, "connection timeout")
      end
      state = channel.connectivity_state(true)
      if state == GRPC::Core::ConnectivityStates::READY
        break
      end
      if state == GRPC::Core::ConnectivityStates::TRANSIENT_FAILURE
        num_failures += 1
      elsif state == GRPC::Core::ConnectivityStates::FATAL_FAILURE
        num_failures = max_failures
      end
      if num_failures >= max_failures
        channel.close
        return Status.new(Status::PRECONDITION_ERROR, "connection failed")
      end
      channel.watch_connectivity_state(state, Time.now + 0.1) 
    end
    stub = DBMService::Stub.new(address, credentials,
                                channel_override: @channel, timeout: timeout)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  @channel = channel
  @stub = stub
  @timeout = timeout
  @dbm_index = 0
  @encoding = nil
  return Status.new(Status::SUCCESS)
end
count() click to toggle source

Gets the number of records.

  • @return The number of records on success, or nil on failure.

# File tkrzw_rpc.rb, line 885
def count
  if not @channel
    return nil
  end
  request = CountRequest.new
  request.dbm_index = @dbm_index
  begin
    response = @stub.count(request)
  rescue GRPC::BadStatus
    return nil
  end
  response.count
end
delete(key) click to toggle source

Removes a record of a key.

  • @param key The key of the record.

  • @return an empty string on success or nil on failure.

# File tkrzw_rpc.rb, line 1125
def delete(key)
  if not @channel
    return false
  end
  request = RemoveRequest.new
  request.dbm_index = @dbm_index
  request.key = make_string(key)
  begin
    response = @stub.remove(request)
  rescue GRPC::BadStatus
    return false
  end
  response.status.code == Status::SUCCESS ? "" : nil
end
destruct() click to toggle source

Releases the resource explicitly.

# File tkrzw_rpc.rb, line 233
def destruct
  if @channel
    disconnect
  end
  @channel = nil
  @stub = nil
end
disconnect() click to toggle source

Disconnects the connection to the server.

  • @return The result status.

# File tkrzw_rpc.rb, line 327
def disconnect
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  status = Status.new(Status::SUCCESS)
  begin
    @channel.close
  rescue GRPC::BadStatus => error
    status = Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  @channel = nil
  @stub = nil
  @timeout = nil
  @dbm_index = 0
  @encoding = nil
  return status
end
each() { |record, record| ... } click to toggle source

Calls the given block with the key and the value of each record

# File tkrzw_rpc.rb, line 1141
def each(&block)
  iter = make_iterator
  begin
    iter.first
    while true
      record = iter.get
      if not record
        break
      end
      yield record[0], record[1]
      iter.next
    end
  ensure
    iter.destruct
  end
end
echo(message, status=nil) click to toggle source

Sends a message and gets back the echo message.

  • @param message The message to send.

  • @param status A status object to which the result status is assigned. It can be omitted.

  • @return The string value of the echoed message or nil on failure.

# File tkrzw_rpc.rb, line 393
def echo(message, status=nil)
  if not @channel
    if status
      status.set(Status::PRECONDITION_ERROR, "not opened connection")
    end
    return nil
  end
  request = EchoRequest.new
  request.message = make_string(message)
  begin
    response = @stub.echo(request)
  rescue GRPC::BadStatus => error
    if status
      status.set(Status::NETWORK_ERROR, str_grpc_error(error))
    end
    return nil
  end
  if status
    status.set(Status::SUCCESS)
  end
  if @encoding
    return response.echo.dup.force_encoding(@encoding)
  end
  response.echo
end
file_size() click to toggle source

Gets the current file size of the database.

  • @return The current file size of the database, or nil on failure.

# File tkrzw_rpc.rb, line 901
def file_size
  if not @channel
    return nil
  end
  request = GetFileSizeRequest.new
  request.dbm_index = @dbm_index
  begin
    response = @stub.get_file_size(request)
  rescue GRPC::BadStatus
    return nil
  end
  response.file_size
end
get(key, status=nil) click to toggle source

Gets the value of a record of a key.

  • @param key The key of the record.

  • @param status A status object to which the result status is assigned. It can be omitted.

  • @return The value of the matching record or nil on failure.

# File tkrzw_rpc.rb, line 442
def get(key, status=nil)
  if not @channel
    if status
      status.set(Status::PRECONDITION_ERROR, "not opened connection")
    end
    return nil
  end
  request = GetRequest.new
  request.dbm_index = @dbm_index
  request.key = make_string(key)
  begin
    response = @stub.get(request)
  rescue GRPC::BadStatus => error
    if status
      status.set(Status::NETWORK_ERROR, str_grpc_error(error))
    end
    return nil
  end
  if status
    set_status_from_proto(status, response.status)
  end
  if response.status.code == Status::SUCCESS
    if @encoding
      return response.value.dup.force_encoding(@encoding)
    end
    return response.value
  end
  nil
end
get_multi(*keys) click to toggle source

Gets the values of multiple records of keys.

  • @param keys The keys of records to retrieve.

  • @return A map of retrieved records. Keys which don't match existing records are ignored.

# File tkrzw_rpc.rb, line 475
def get_multi(*keys)
  result = {}
  if not @channel
    return result
  end
  request = GetMultiRequest.new
  request.dbm_index = @dbm_index
  keys.each { |key|
    request.keys.push(make_string(key))
  }
  begin
    response = @stub.get_multi(request)
  rescue GRPC::BadStatus => error
    print(error)
    return {}
  end
  response.records.each { |record|
    if @encoding
      result[record.first.dup.force_encoding(@encoding)] =
        record.second.dup.force_encoding(@encoding)
    else
      result[record.first] = record.second
    end
  }
  result
end
include?(key) click to toggle source

Checks if a record exists or not.

  • @param key The key of the record.

  • @return True if the record exists, or false if not.

# File tkrzw_rpc.rb, line 422
def include?(key)
  if not @channel
    return false
  end
  request = GetRequest.new
  request.dbm_index = @dbm_index
  request.key = make_string(key)
  request.omit_value = true
  begin
    response = @stub.get(request)
  rescue GRPC::BadStatus => error
    return false
  end
  return response.status.code == Status::SUCCESS
end
increment(key, inc=1, init=0, status=nil) click to toggle source

Increments the numeric value of a record.

  • @param key The key of the record.

  • @param inc The incremental value. If it is Utility::INT64MIN, the current value is not changed and a new record is not created.

  • @param init The initial value.

  • @param status A status object to which the result status is assigned. It can be omitted.

  • @return The current value, or nil on failure.

The record value is stored as an 8-byte big-endian integer. Negative is also supported.

# File tkrzw_rpc.rb, line 728
def increment(key, inc=1, init=0, status=nil)
  if not @channel
    if status
      status.set(Status::PRECONDITION_ERROR, "not opened connection")
    end
    return nil
  end
  request = IncrementRequest.new
  request.dbm_index = @dbm_index
  request.key = make_string(key)
  request.increment = inc
  request.initial = init
  begin
    response = @stub.increment(request)
  rescue GRPC::BadStatus => error
    if status
      status.set(Status::NETWORK_ERROR, str_grpc_error(error))
    end
    return nil
  end
  if status
    set_status_from_proto(status, response.status)
  end
  if response.status.code == Status::SUCCESS
    return response.current
  end
  nil
end
inspect() click to toggle source

Returns a string representation of the object.

  • @return The string representation of the object.

# File tkrzw_rpc.rb, line 1066
def inspect
  expr = @channel ? "connected" : "not connected"
  "#<TkrzwRPC::RemoteDBM: 0x" + object_id.to_s(16) + ": " + expr + ">"
end
inspect_details() click to toggle source

Inspects the database.

  • @return A hash of property names and their values.

If the DBM index is negative, basic metadata of all DBMs are obtained.

# File tkrzw_rpc.rb, line 371
def inspect_details
  result = {}
  if not @channel
    return result
  end
  request = InspectRequest.new
  request.dbm_index = @dbm_index
  begin
    response = @stub.inspect(request)
  rescue GRPC::BadStatus
    return result
  end
  for record in response.records
    result[record.first] = record.second
  end
  result
end
make_iterator() click to toggle source

Makes an iterator for each record.

  • @return The iterator for each record.

Every iterator should be destructed explicitly by the “destruct” method.

# File tkrzw_rpc.rb, line 1037
def make_iterator
  return Iterator.new(self)
end
pop_first(retry_wait=nil, status=nil) click to toggle source

Gets the first record and removes it.

  • @param retry_wait The maximum wait time in seconds before retrying. If it is nil, no retry is done. If it is positive, retry is done after waiting for the notifications of the next update for the time at most.

  • @param status A status object to which the result status is assigned. It can be omitted.

  • @return A tuple of The key and the value of the first record. On failure, nil is returned.

# File tkrzw_rpc.rb, line 829
def pop_first(retry_wait=nil, status=nil)
  if not @channel
    if status
      status.set(Status::PRECONDITION_ERROR, "not opened connection")
    end
    return nil
  end
  request = PopFirstRequest.new
  request.dbm_index = @dbm_index
  request.retry_wait = retry_wait ? retry_wait : 0
  begin
    response = @stub.pop_first(request)
  rescue GRPC::BadStatus => error
    if status
      status.set(Status::NETWORK_ERROR, str_grpc_error(error))
    end
    return nil
  end
  if status
    set_status_from_proto(status, response.status)
  end
  if response.status.code == Status::SUCCESS
    if @encoding
      return [response.key.dup.force_encoding(@encoding),
              response.value.dup.force_encoding(@encoding)]
    end
    return [response.key, response.value]
  end
  nil
end
push_last(value, wtime=nil, notify=false) click to toggle source

Adds a record with a key of the current timestamp.

  • @param value The value of the record.

  • @param wtime The current wall time used to generate the key. If it is nil, the system clock is used.

  • @param notify If true, notification signal is sent.

  • @return The result status.

The key is generated as an 8-bite big-endian binary string of the timestamp. If there is an existing record matching the generated key, the key is regenerated and the attempt is repeated until it succeeds.

# File tkrzw_rpc.rb, line 866
def push_last(value, wtime=nil, notify=false)
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  request = PushLastRequest.new
  request.dbm_index = @dbm_index
  request.value = make_string(value)
  request.wtime = wtime ? wtime : -1
  request.notify = notify
  begin
    response = @stub.push_last(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  make_status_from_proto(response.status)
end
rebuild(**params) click to toggle source

Rebuilds the entire database.

  • @param params Optional parameters of a hash object.

  • @return The result status.

The optional parameters are the same as the “open” method of the local DBM class and the database configurations of the server command. Omitted tuning parameters are kept the same or implicitly optimized.
In addition, HashDBM, TreeDBM, and SkipDBM supports the following parameters.

  • skip_broken_records (bool): If true, the operation continues even if there are broken records which can be skipped.

  • sync_hard (bool): If true, physical synchronization with the hardware is done before finishing the rebuilt file.

# File tkrzw_rpc.rb, line 938
def rebuild(**params)
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  request = RebuildRequest.new
  request.dbm_index = @dbm_index
  params.each { |name, value|
    req_param = StringPair.new
    req_param.first = make_string(name)
    req_param.second = make_string(value)
    request.params.push(req_param)
  }
  begin
    response = @stub.rebuild(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  make_status_from_proto(response.status)
end
rekey(old_key, new_key, overwrite=true, copying=false) click to toggle source

Changes the key of a record.

  • @param old_key The old key of the record.

  • @param new_key The new key of the record.

  • @param overwrite Whether to overwrite the existing record of the new key.

  • @param copying Whether to retain the record of the old key.

  • @return The result status. If there's no matching record to the old key, NOT_FOUND_ERROR is returned. If the overwrite flag is false and there is an existing record of the new key, DUPLICATION ERROR is returned.

This method is done atomically. The other threads observe that the record has either the old key or the new key. No intermediate states are observed.

# File tkrzw_rpc.rb, line 807
def rekey(old_key, new_key, overwrite=true, copying=false)
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  request = RekeyRequest.new
  request.dbm_index = @dbm_index
  request.old_key = make_string(old_key)
  request.new_key = make_string(new_key)
  request.overwrite = overwrite
  request.copying = copying
  begin
    response = @stub.rekey(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  make_status_from_proto(response.status)
end
remove(key) click to toggle source

Removes a record of a key.

  • @param key The key of the record.

  • @return The result status. If there's no matching record, NOT_FOUND_ERROR is returned.

# File tkrzw_rpc.rb, line 552
def remove(key)
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  request = RemoveRequest.new
  request.dbm_index = @dbm_index
  request.key = make_string(key)
  begin
    response = @stub.remove(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  make_status_from_proto(response.status)
end
remove_multi(*keys) click to toggle source

Removes records of keys.

  • @param keys The keys of the records.

  • @return The result status. If there are missing records, NOT_FOUND_ERROR is returned.

# File tkrzw_rpc.rb, line 570
def remove_multi(*keys)
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  request = RemoveMultiRequest.new
  request.dbm_index = @dbm_index
  keys.each { |key|
    request.keys.push(make_string(key))
  }
  begin
    response = @stub.remove_multi(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  make_status_from_proto(response.status)
end
set(key, value, overwrite=true) click to toggle source

Sets a record of a key and a value.

  • @param key The key of the record.

  • @param value The value of the record.

  • @param overwrite Whether to overwrite the existing value. It can be omitted and then false is set.

  • @return The result status. If overwriting is abandoned, DUPLICATION_ERROR is returned.

# File tkrzw_rpc.rb, line 507
def set(key, value, overwrite=true)
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  request = SetRequest.new
  request.dbm_index = @dbm_index
  request.key = make_string(key)
  request.value = make_string(value)
  request.overwrite = overwrite
  begin
    response = @stub.set(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  make_status_from_proto(response.status)
end
set_dbm_index(dbm_index) click to toggle source

Sets the index of the DBM to access.

  • @param dbm_index The index of the DBM to access.

  • @return The result status.

# File tkrzw_rpc.rb, line 348
def set_dbm_index(dbm_index)
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  @dbm_index = dbm_index
  Status.new(Status::SUCCESS)
end
set_encoding(encoding) click to toggle source

Sets the encoding of string values returned by some methods like Get.

  • @param encoding The encoding name like “UTF-8”, “ISO-8859-1” and “ASCII-8BIT”.

  • @return The result status.

The default encoding is “ASCII-8BIT” which is suitable for binary data.

# File tkrzw_rpc.rb, line 360
def set_encoding(encoding)
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  @encoding = encoding == "ASCII-8BIT" ? nil : encoding
  Status.new(Status::SUCCESS)
end
set_multi(overwrite=true, **records) click to toggle source

Sets multiple records of the keyword arguments.

  • @param overwrite Whether to overwrite the existing value if there's a record with the same key. If true, the existing value is overwritten by the new value. If false, the operation is given up and an error status is returned.

  • @param records Records to store.

  • @return The result status. If there are records avoiding overwriting, DUPLICATION_ERROR is returned.

# File tkrzw_rpc.rb, line 528
def set_multi(overwrite=true, **records)
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  request = SetMultiRequest.new
  request.dbm_index = @dbm_index
  records.each { |key, value|
    req_record = BytesPair.new
    req_record.first = make_string(key)
    req_record.second = make_string(value)
    request.records.push(req_record)
  }
  request.overwrite = overwrite
  begin
    response = @stub.set_multi(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  make_status_from_proto(response.status)
end
should_be_rebuilt?() click to toggle source

Checks whether the database should be rebuilt.

  • @return True to be optimized or False with no necessity.

# File tkrzw_rpc.rb, line 960
def should_be_rebuilt?
  if not @channel
    return false
  end
  request = ShouldBeRebuiltRequest.new
  request.dbm_index = @dbm_index
  begin
    response = @stub.should_be_rebuilt(request)
  rescue GRPC::BadStatus
    return false
  end
  response.tobe
end
synchronize(hard, **params) click to toggle source

Synchronizes the content of the database to the file system.

  • @param hard True to do physical synchronization with the hardware or false to do only logical synchronization with the file system.

  • @param params Optional parameters of a hash object.

  • @return The result status.

The “reducer” parameter specifies the reducer for SkipDBM. “ReduceToFirst”, “ReduceToSecond”, “ReduceToLast”, etc are supported. If the parameter “make_backup” exists, a backup file is created in the same directory as the database file. The backup file name has a date suffix in GMT, like “.backup.20210831213749”. If the value of “make_backup” not empty, it is the value is used as the suffix.

# File tkrzw_rpc.rb, line 979
def synchronize(hard, **params)
  if not @channel
    return Status.new(Status::PRECONDITION_ERROR, "not opened connection")
  end
  request = SynchronizeRequest.new
  request.dbm_index = @dbm_index
  request.hard = hard
  params.each { |name, value|
    req_param = StringPair.new
    req_param.first = make_string(name)
    req_param.second = make_string(value)
    request.params.push(req_param)
  }
  begin
    response = @stub.synchronize(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  make_status_from_proto(response.status)
end
to_i() click to toggle source

Gets the number of records.

  • @return The number of records on success, or -1 on failure.

# File tkrzw_rpc.rb, line 1050
def to_i
  if not @channel
    return -1
  end
  request = CountRequest.new
  request.dbm_index = @dbm_index
  begin
    response = @stub.count(request)
  rescue GRPC::BadStatus => error
    return Status.new(Status::NETWORK_ERROR, str_grpc_error(error))
  end
  response.count
end
to_s() click to toggle source

Returns a string representation of the content.

  • @return The string representation of the content.

# File tkrzw_rpc.rb, line 1043
def to_s
  expr = @channel ? "connected" : "not connected"
  "RemoteDBM: 0x" + object_id.to_s(16) + ": " + expr
end