oracle_nosql_rust_sdk/system_request.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
//
// Copyright (c) 2024, 2025 Oracle and/or its affiliates. All rights reserved.
//
// Licensed under the Universal Permissive License v 1.0 as shown at
// https://oss.oracle.com/licenses/upl/
//
use crate::error::NoSQLErrorCode::RequestTimeout;
use crate::error::{ia_err, NoSQLError};
use crate::handle::Handle;
use crate::handle::SendOptions;
use crate::nson::*;
use crate::reader::Reader;
use crate::types::{OpCode, OperationState};
use crate::writer::Writer;
use std::result::Result;
use std::thread::sleep;
use std::time::{Duration, Instant};
/// Struct used for on-premise-only requests.
///
/// This is used to perform any table-independent administrative operation such as
/// create/drop of namespaces and security-relevant operations (create/drop
/// users and roles). These operations are asynchronous and completion needs
/// to be checked.
///
/// Examples of statements used in this object include:
/// - CREATE NAMESPACE mynamespace
/// - CREATE USER some_user IDENTIFIED BY password
/// - CREATE ROLE some_role
/// - GRANT ROLE some_role TO USER some_user
///
/// Execution of operations specified by this request are implicitly asynchronous.
/// These are potentially long-running operations.
/// [`SystemRequest::execute()`] returns a [`SystemResult`] instance that
/// can be used to poll until the operation succeeds or fails.
#[derive(Default, Debug)]
pub struct SystemRequest {
pub(crate) statement: String,
pub(crate) timeout: Option<Duration>,
}
/// Struct used to query the status of an in-progress [`SystemRequest`].
#[derive(Default, Debug)]
pub(crate) struct SystemStatusRequest {
pub operation_id: String,
pub timeout: Option<Duration>,
}
/// Struct representing the result of a [`SystemRequest`].
#[derive(Default, Debug)]
pub struct SystemResult {
pub(crate) operation_id: String, // TODO: Option<>?
pub(crate) state: OperationState,
pub(crate) statement: String,
pub(crate) result_string: String,
}
impl SystemRequest {
/// Create a new SystemRequest. `statement` must be non-empty.
pub fn new(statement: &str) -> SystemRequest {
SystemRequest {
statement: statement.to_string(),
..Default::default()
}
}
/// Specify the timeout value for the request.
///
/// This is optional.
/// If set, it must be greater than or equal to 1 millisecond, otherwise an
/// IllegalArgument error will be returned.
/// If not set, the default timeout value configured for the [`Handle`](crate::HandleBuilder::timeout()) is used.
pub fn timeout(mut self, t: &Duration) -> Self {
self.timeout = Some(t.clone());
self
}
/// Execute the system request.
///
/// This starts the asynchronous execution of the request in the system. The returned result should be
/// used to wait for completion by calling [`SystemResult::wait_for_completion()`].
pub async fn execute(&self, h: &Handle) -> Result<SystemResult, NoSQLError> {
// TODO: validate
let mut w: Writer = Writer::new();
w.write_i16(h.inner.serial_version);
let timeout = h.get_timeout(&self.timeout);
self.nson_serialize(&mut w, &timeout);
let mut opts = SendOptions {
timeout: timeout,
retryable: false,
..Default::default()
};
let mut r = h.send_and_receive(w, &mut opts).await?;
let resp = SystemRequest::nson_deserialize(&mut r)?;
Ok(resp)
}
pub(crate) fn nson_serialize(&self, w: &mut Writer, timeout: &Duration) {
let mut ns = NsonSerializer::start_request(w);
ns.start_header();
ns.write_header(OpCode::SystemRequest, timeout, "");
ns.end_header();
// payload
ns.start_payload();
ns.write_string_field(STATEMENT, &self.statement);
ns.end_payload();
ns.end_request();
}
pub(crate) fn nson_deserialize(r: &mut Reader) -> Result<SystemResult, NoSQLError> {
let mut walker = MapWalker::new(r)?;
let mut res: SystemResult = Default::default();
while walker.has_next() {
walker.next()?;
let name = walker.current_name();
match name.as_str() {
ERROR_CODE => {
walker.handle_error_code()?;
}
OPERATION_ID => {
res.operation_id = walker.read_nson_string()?;
//println!(" operation_id={:?}", res.operation_id);
}
STATEMENT => {
res.statement = walker.read_nson_string()?;
//println!(" statement={:?}", res.statement);
}
SYSOP_RESULT => {
res.result_string = walker.read_nson_string()?;
//println!(" result_string={:?}", res.result_string);
}
SYSOP_STATE => {
let s = walker.read_nson_i32()?;
res.state = OperationState::from_int(s)?;
//println!(" state={:?}", res.state);
}
_ => {
//println!(" system_result: skipping field '{}'", name);
walker.skip_nson_field()?;
}
}
}
Ok(res)
}
}
impl NsonRequest for SystemRequest {
fn serialize(&self, w: &mut Writer, timeout: &Duration) {
self.nson_serialize(w, timeout);
}
}
impl SystemStatusRequest {
pub fn new(operation_id: &str) -> SystemStatusRequest {
SystemStatusRequest {
operation_id: operation_id.to_string(),
..Default::default()
}
}
/// Specify the timeout value for the request.
///
/// This is optional.
/// If set, it must be greater than or equal to 1 millisecond, otherwise an
/// IllegalArgument error will be returned.
/// If not set, the default timeout value configured for the [`Handle`](crate::HandleBuilder::timeout()) is used.
#[allow(dead_code)]
pub fn timeout(mut self, t: &Duration) -> Self {
self.timeout = Some(t.clone());
self
}
pub async fn execute(&self, h: &Handle) -> Result<SystemResult, NoSQLError> {
// TODO: validate
let mut w: Writer = Writer::new();
w.write_i16(h.inner.serial_version);
let timeout = h.get_timeout(&self.timeout);
self.nson_serialize(&mut w, &timeout);
let mut opts = SendOptions {
timeout: timeout,
retryable: true,
..Default::default()
};
let mut r = h.send_and_receive(w, &mut opts).await?;
let resp = SystemRequest::nson_deserialize(&mut r)?;
Ok(resp)
}
pub(crate) fn nson_serialize(&self, w: &mut Writer, timeout: &Duration) {
let mut ns = NsonSerializer::start_request(w);
ns.start_header();
ns.write_header(OpCode::SystemStatusRequest, timeout, "");
ns.end_header();
// payload
ns.start_payload();
ns.write_string_field(OPERATION_ID, &self.operation_id);
ns.end_payload();
ns.end_request();
}
}
impl NsonRequest for SystemStatusRequest {
fn serialize(&self, w: &mut Writer, timeout: &Duration) {
self.nson_serialize(w, timeout);
}
}
impl SystemResult {
/// Wait for a SystemRequest to complete.
///
/// This method will loop, polling the system for the status of the SystemRequest
/// until it either succeeds, gets an error, or times out.
pub async fn wait_for_completion(
&mut self,
h: &Handle,
wait: Duration,
delay: Duration,
) -> Result<(), NoSQLError> {
if self.state == OperationState::Complete {
return Ok(());
}
if wait < delay {
return ia_err!("wait duration must be greater than delay duration");
}
let start_time = Instant::now();
let mut first_loop = true;
while self.state != OperationState::Complete {
if start_time.elapsed() > wait {
return Err(NoSQLError::new(
RequestTimeout,
"Operation not completed in expected time",
));
}
if !first_loop {
sleep(delay);
}
let res = SystemStatusRequest::new(self.operation_id.as_str())
.execute(h)
.await?;
// operation_id and statement do not change
self.state = res.state;
self.result_string = res.result_string;
first_loop = false;
}
Ok(())
}
/// Wait for a SystemRequest to complete.
///
/// This method will loop, polling the system for the status of the SystemRequest
/// until it either succeeds, gets an error, or times out.
///
/// This is a convenience method to allow direct millisecond values instead of creating
/// `Duration` structs.
pub async fn wait_for_completion_ms(
&mut self,
h: &Handle,
wait_ms: u64,
delay_ms: u64,
) -> Result<(), NoSQLError> {
self.wait_for_completion(
h,
Duration::from_millis(wait_ms),
Duration::from_millis(delay_ms),
)
.await
}
pub fn operation_id(&self) -> String {
self.operation_id.clone()
}
pub fn state(&self) -> OperationState {
self.state.clone()
}
pub fn statement(&self) -> String {
self.statement.clone()
}
pub fn result_string(&self) -> String {
self.result_string.clone()
}
}