Skip to content

Commit

Permalink
Merge pull request #5 from Daniel-Boll/feature/1-implement-optional-c…
Browse files Browse the repository at this point in the history
…ompression-methods-in-cluster-constructor

Implement optional compression methods in cluster constructor
  • Loading branch information
Daniel-Boll authored Oct 14, 2023
2 parents 56d77c3 + 47db793 commit 92eb0c1
Show file tree
Hide file tree
Showing 9 changed files with 1,128 additions and 2,386 deletions.
14 changes: 10 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

/* auto-generated by NAPI-RS */

export interface ClusterConfig {
nodes: Array<string>
defaultExecutionProfile?: ExecutionProfile
}
export const enum Consistency {
Any = 0,
One = 1,
Expand All @@ -24,6 +20,16 @@ export interface ExecutionProfile {
consistency?: Consistency
requestTimeout?: number
}
export interface ClusterConfig {
nodes: Array<string>
compression?: Compression
defaultExecutionProfile?: ExecutionProfile
}
export const enum Compression {
None = 0,
Lz4 = 1,
Snappy = 2
}
export type ScyllaCluster = Cluster
export class Cluster {
/**
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,10 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { Consistency, Cluster, ScyllaSession, Uuid } = nativeBinding
const { Consistency, Cluster, Compression, ScyllaSession, Uuid } = nativeBinding

module.exports.Consistency = Consistency
module.exports.Cluster = Cluster
module.exports.Compression = Compression
module.exports.ScyllaSession = ScyllaSession
module.exports.Uuid = Uuid
7 changes: 0 additions & 7 deletions src/cluster/cluster_config.rs

This file was deleted.

8 changes: 8 additions & 0 deletions src/cluster/config/cluster_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::cluster::{execution_profile::ExecutionProfile, config::compression::Compression};

#[napi(object)]
pub struct ClusterConfig {
pub nodes: Vec<String>,
pub compression: Option<Compression>,
pub default_execution_profile: Option<ExecutionProfile>,
}
18 changes: 18 additions & 0 deletions src/cluster/config/compression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use napi::bindgen_prelude::*;

#[napi]
pub enum Compression {
None,
Lz4,
Snappy
}

impl From<Compression> for Option<scylla::transport::Compression> {
fn from(value: Compression)-> Self {
match value {
Compression::None => None,
Compression::Lz4 => Some(scylla::transport::Compression::Lz4),
Compression::Snappy => Some(scylla::transport::Compression::Snappy)
}
}
}
2 changes: 2 additions & 0 deletions src/cluster/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod cluster_config;
pub mod compression;
2 changes: 1 addition & 1 deletion src/cluster/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod cluster_config;
pub mod execution_profile;
pub mod scylla_cluster;
pub mod config;
14 changes: 11 additions & 3 deletions src/cluster/scylla_cluster.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::{
cluster::{cluster_config::ClusterConfig, execution_profile::ExecutionProfile},
session::scylla_session::ScyllaSession,
use crate::session::scylla_session::ScyllaSession;
use crate::cluster::{
config::{cluster_config::ClusterConfig, compression::Compression},
execution_profile::ExecutionProfile
};

#[napi(js_name = "Cluster")]
struct ScyllaCluster {
uri: String,
compression: Option<Compression>,
default_execution_profile: Option<ExecutionProfile>,
}

Expand All @@ -19,13 +21,15 @@ impl ScyllaCluster {
pub fn new(cluster_config: ClusterConfig) -> Self {
let ClusterConfig {
nodes,
compression,
default_execution_profile,
} = cluster_config;

let uri = nodes.get(0).expect("at least one node is required");

Self {
uri: uri.to_string(),
compression,
default_execution_profile,
}
}
Expand All @@ -43,6 +47,10 @@ impl ScyllaCluster {
builder = builder.default_execution_profile_handle(default_execution_profile.into_handle());
}

if let Some(compression) = self.compression {
builder = builder.compression(compression.into());
}

ScyllaSession::new(builder.build().await.unwrap())
}
}
Loading

0 comments on commit 92eb0c1

Please sign in to comment.