use crate::errors::BigQueryError;
use crate::types::error::CodecError;
use crate::write::batch::MAX_REQUEST_BYTES;
use crate::BigQueryInstant;
use crate::{BigQueryResult, BigQueryTableRef};
use rsb_derive::Builder;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use std::time::Duration;
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQueryInsertParams {
pub table: BigQueryTableRef,
#[default = "BigQueryStreamingWriteOptions::new()"]
pub options: BigQueryStreamingWriteOptions,
}
#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct BigQueryStreamingWriteOptions {
#[default = "BigQueryWriteMode::Default"]
pub mode: BigQueryWriteMode,
#[default = "MAX_REQUEST_BYTES"]
pub max_request_bytes: usize,
pub max_batch_rows: Option<usize>,
#[default = "Duration::from_millis(100)"]
pub max_batch_delay: Duration,
#[default = "8"]
pub max_inflight_requests: usize,
#[default = "64 * 1024 * 1024"]
pub max_inflight_bytes: usize,
#[default = "Duration::from_secs(1)"]
pub schema_refresh_interval: Duration,
pub missing_value: Option<BigQueryMissingValue>,
pub trace_id: Option<BigQueryTraceId>,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct BigQueryTraceId(String);
impl BigQueryTraceId {
pub fn new(id: impl Into<String>) -> BigQueryResult<Self> {
let id = id.into();
if id.is_empty() {
return Err(BigQueryError::invalid_parameters(
"trace_id",
"must not be empty",
));
}
Ok(Self(id))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Display for BigQueryTraceId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl TryFrom<&str> for BigQueryTraceId {
type Error = BigQueryError;
fn try_from(id: &str) -> Result<Self, Self::Error> {
Self::new(id)
}
}
impl TryFrom<String> for BigQueryTraceId {
type Error = BigQueryError;
fn try_from(id: String) -> Result<Self, Self::Error> {
Self::new(id)
}
}
impl FromStr for BigQueryTraceId {
type Err = BigQueryError;
fn from_str(id: &str) -> Result<Self, Self::Err> {
Self::new(id)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct BigQueryWriteStreamName(String);
impl BigQueryWriteStreamName {
pub(crate) fn reported(name: String) -> Self {
Self(name)
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Display for BigQueryWriteStreamName {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum BigQueryWriteMode {
Default,
Committed,
Pending,
Buffered,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BigQueryMissingValue {
Null,
DefaultValue,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BigQueryWriteResponse {
pub batch_index: u64,
pub first_row: u64,
pub row_count: u64,
pub offset: Option<i64>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BigQueryWriteSummary {
pub rows_written: u64,
pub rows_failed: u64,
pub batches: u64,
pub bytes_sent: u64,
pub stream: Option<BigQueryWriteStreamName>,
pub commit_time: Option<BigQueryInstant>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BigQueryFinalizedStream {
pub table: BigQueryTableRef,
pub name: BigQueryWriteStreamName,
pub row_count: i64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BigQueryChangeType {
Upsert,
Delete,
}
impl BigQueryChangeType {
pub(crate) fn name(self) -> &'static str {
match self {
BigQueryChangeType::Upsert => "UPSERT",
BigQueryChangeType::Delete => "DELETE",
}
}
pub(crate) fn from_text(text: &str) -> Result<Self, CodecError> {
match text {
"UPSERT" => Ok(BigQueryChangeType::Upsert),
"DELETE" => Ok(BigQueryChangeType::Delete),
_ => Err(CodecError::invalid_text(format!(
"{text:?} is not UPSERT or DELETE"
))),
}
}
}
impl FromStr for BigQueryChangeType {
type Err = BigQueryError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_text(s).map_err(CodecError::into_serialize)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BigQueryChangeSequenceNumber(String);
impl From<u64> for BigQueryChangeSequenceNumber {
fn from(n: u64) -> Self {
Self(format!("{n:X}"))
}
}
impl BigQueryChangeSequenceNumber {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl FromStr for BigQueryChangeSequenceNumber {
type Err = BigQueryError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.is_empty() {
return Err(
CodecError::invalid_text("an empty _CHANGE_SEQUENCE_NUMBER").into_serialize()
);
}
Ok(Self(s.to_string()))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BigQueryChange<T> {
pub change_type: BigQueryChangeType,
pub sequence_number: Option<BigQueryChangeSequenceNumber>,
pub row: T,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::errors::BigQueryCodecErrorKind;
#[test]
fn a_sequence_number_is_sent_as_written_and_only_empty_is_refused() {
for text in ["1F", "A/B/C/D", "g", "1/2/3/4/5", "11111111111111111"] {
let parsed: BigQueryChangeSequenceNumber = text.parse().expect(text);
assert_eq!(parsed.as_str(), text);
}
match "".parse::<BigQueryChangeSequenceNumber>() {
Err(BigQueryError::SerializeError(err)) => {
assert_eq!(err.kind, BigQueryCodecErrorKind::InvalidText);
}
other => panic!("an empty sequence number must be InvalidText, got {other:?}"),
}
assert_eq!(BigQueryChangeSequenceNumber::from(255).as_str(), "FF");
}
#[test]
fn an_empty_trace_id_is_refused() {
match BigQueryTraceId::new("") {
Err(BigQueryError::InvalidParametersError(err)) => {
assert_eq!(err.public.field, "trace_id")
}
other => panic!("expected an invalid trace ID, got {other:?}"),
}
assert_eq!(
BigQueryTraceId::new("app:1.0")
.expect("a trace ID")
.as_str(),
"app:1.0"
);
}
}