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
//! Batch operations for efficient bulk data operations
use crate::client::NexusClient;
use crate::error::{NexusError, Result};
use crate::models::Value;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// Batch create nodes request
#[derive(Debug, Clone, Serialize)]
pub struct BatchCreateNodesRequest {
/// List of nodes to create
pub nodes: Vec<BatchNode>,
}
/// Batch node definition
#[derive(Debug, Clone, Serialize)]
pub struct BatchNode {
/// Node labels
pub labels: Vec<String>,
/// Node properties
#[serde(default)]
pub properties: HashMap<String, Value>,
}
/// Batch create nodes response
#[derive(Debug, Clone, Deserialize)]
pub struct BatchCreateNodesResponse {
/// List of created node IDs
pub node_ids: Vec<u64>,
/// Success message
pub message: String,
/// Error message if any
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
/// Batch create relationships request
#[derive(Debug, Clone, Serialize)]
pub struct BatchCreateRelationshipsRequest {
/// List of relationships to create
pub relationships: Vec<BatchRelationship>,
}
/// Batch relationship definition
#[derive(Debug, Clone, Serialize)]
pub struct BatchRelationship {
/// Source node ID
pub source_id: u64,
/// Target node ID
pub target_id: u64,
/// Relationship type
pub rel_type: String,
/// Relationship properties
#[serde(default)]
pub properties: HashMap<String, Value>,
}
/// Batch create relationships response
#[derive(Debug, Clone, Deserialize)]
pub struct BatchCreateRelationshipsResponse {
/// List of created relationship IDs
pub rel_ids: Vec<u64>,
/// Success message
pub message: String,
/// Error message if any
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
impl NexusClient {
/// Batch create multiple nodes
///
/// # Arguments
///
/// * `nodes` - Vector of batch node definitions
///
/// # Example
///
/// ```no_run
/// # use nexus_sdk::{NexusClient, Value};
/// # use std::collections::HashMap;
/// # #[tokio::main]
/// # async fn main() -> Result<(), nexus_sdk::NexusError> {
/// # let client = NexusClient::new("http://localhost:15474")?;
/// let mut nodes = Vec::new();
/// for i in 0..10 {
/// let mut properties = HashMap::new();
/// properties.insert("name".to_string(), Value::String(format!("Node{}", i)));
/// nodes.push(nexus_sdk::BatchNode {
/// labels: vec!["Person".to_string()],
/// properties,
/// });
/// }
/// let response = client.batch_create_nodes(nodes).await?;
/// tracing::info!("Created {} nodes", response.node_ids.len());
/// # Ok(())
/// # }
/// ```
pub async fn batch_create_nodes(
&self,
nodes: Vec<BatchNode>,
) -> Result<BatchCreateNodesResponse> {
// For now, create nodes sequentially
// TODO: Implement proper batch endpoint if available
let mut node_ids = Vec::new();
let mut errors = Vec::new();
for node in nodes {
match self.create_node(node.labels, node.properties).await {
Ok(response) => node_ids.push(response.node_id),
Err(e) => errors.push(format!("Failed to create node: {}", e)),
}
}
if !errors.is_empty() {
return Err(NexusError::Validation(format!(
"Some nodes failed to create: {}",
errors.join(", ")
)));
}
let node_count = node_ids.len();
Ok(BatchCreateNodesResponse {
node_ids,
message: format!("Successfully created {} nodes", node_count),
error: None,
})
}
/// Batch create multiple relationships
///
/// # Arguments
///
/// * `relationships` - Vector of batch relationship definitions
///
/// # Example
///
/// ```no_run
/// # use nexus_sdk::{NexusClient, Value};
/// # use std::collections::HashMap;
/// # #[tokio::main]
/// # async fn main() -> Result<(), nexus_sdk::NexusError> {
/// # let client = NexusClient::new("http://localhost:15474")?;
/// let mut relationships = Vec::new();
/// for i in 0..5 {
/// relationships.push(nexus_sdk::BatchRelationship {
/// source_id: i,
/// target_id: i + 1,
/// rel_type: "KNOWS".to_string(),
/// properties: HashMap::new(),
/// });
/// }
/// let response = client.batch_create_relationships(relationships).await?;
/// tracing::info!("Created {} relationships", response.rel_ids.len());
/// # Ok(())
/// # }
/// ```
pub async fn batch_create_relationships(
&self,
relationships: Vec<BatchRelationship>,
) -> Result<BatchCreateRelationshipsResponse> {
// For now, create relationships sequentially
// TODO: Implement proper batch endpoint if available
let mut rel_ids = Vec::new();
let mut errors = Vec::new();
for rel in relationships {
match self
.create_relationship(rel.source_id, rel.target_id, rel.rel_type, rel.properties)
.await
{
Ok(response) => rel_ids.push(response.rel_id),
Err(e) => errors.push(format!("Failed to create relationship: {}", e)),
}
}
if !errors.is_empty() {
return Err(NexusError::Validation(format!(
"Some relationships failed to create: {}",
errors.join(", ")
)));
}
let rel_count = rel_ids.len();
Ok(BatchCreateRelationshipsResponse {
rel_ids,
message: format!("Successfully created {} relationships", rel_count),
error: None,
})
}
}