KV().Read

Contents

KV().Read#

template<typename T>
static inline std::shared_ptr<T> YR::KVManager::Read(const std::string &key, int timeout = DEFAULT_GET_TIMEOUT_SEC)#

Retrieves the value of a key.

int count = 100;
Counter c1("Counter1-", count);
auto result = YR::KV().Write(c1.name, c1);
auto v1 = *(YR::KV().Read<Counter>(c1.name));  // get Counter
Counter c2("Counter2-", count);
result = YR::KV().Write(c2.name, c2);
std::vector<std::string> keys{c1.name, c2.name};
auto returnVal = YR::KV().Read<Counter>(keys);  // get std::vector<shared_ptr<Counter>>

Warning

When timeout is configured, the Read method will wait for the Write method to complete, up to the timeout. There is no constraint on the order of Read and Write method calls. If a Write operation is followed by an exception such as a ds worker restart, metadata residue must be handled to ensure the Write operation is completed; otherwise, calling Read for the same key will throw an error without waiting for a timeout.

Template Parameters:

T – The type of the object.

Parameters:
  • key[in] The single key used to query the data.

  • timeout[in] Timeout in seconds, default is 300. Range [0, INT_MAX/1000). -1 indicates permanent blocking wait.

Throws:

YR::Exception – Thrown in the following cases:

  • 1001: Invalid input parameters (e.g., empty keys, mismatched sizes, or invalid characters).

  • 4005: Get operation failed (e.g., key not found or timeout exceeded).

  • 4201: RocksDB error (e.g., disk issues).

  • 4202: Shared memory limit exceeded.

  • 4203: Disk operation failed (e.g., permission issues).

  • 4204: Disk space full.

  • 1000, 1001, 1002: Internal communication errors.

Returns:

Returns the retrieved data.

template<typename T>
static inline std::vector<std::shared_ptr<T>> YR::KVManager::Read(const std::vector<std::string> &keys, int timeout = DEFAULT_GET_TIMEOUT_SEC, bool allowPartial = false)#

Retrieves the values of the keys.

Warning

  • When allowPartial is set, the timeout parameter must be explicitly provided.

  • When allowPartial is false: all keys must be successfully retrieved to return the results; any failure will throw an exception.

  • When allowPartial is true: returns results for any successful key retrieval, with failed keys having empty results at their indices; if all keys fail, an exception is thrown.

    int count = 100;
    Counter c1("Counter1-", count);
    auto result = YR::KV().Write(c1.name, c1);
    auto v1 = *(YR::KV().Read<Counter>(c1.name));  // get Counter
    Counter c2("Counter2-", count);
    result = YR::KV().Write(c2.name, c2);
    std::vector<std::string> keys{c1.name, c2.name};
    auto returnVal = YR::KV().Read<Counter>(keys);  // get std::vector<shared_ptr<Counter>>
    

Parameters:
  • keys[in] A collection of keys used to query the data. Maximum specification: 10000.

  • timeout[in] Timeout in seconds, default is 300. Range [0, INT_MAX).

  • allowPartial[in] Determines if partial success results are allowed. Default is false.

Throws:

YR::Exception – Thrown in the following cases:

  • 1001: Invalid input parameters (e.g., empty keys, mismatched sizes, or invalid characters).

  • 4005: Get operation failed (e.g., key not found or timeout exceeded).

  • 4201: RocksDB error (e.g., disk issues).

  • 4202: Shared memory limit exceeded.

  • 4203: Disk operation failed (e.g., permission issues).

  • 4204: Disk space full.

  • 1000, 1001, 1002: Internal communication errors.

Returns:

Returns the retrieved data. If a key does not exist, an exception is thrown.