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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
use crate::{
	errors::SdkJniError,
	helper::{handle_result, validate_handle},
	mappings::{
		convert_jboolean, map_to_actions, map_to_dsnp_keys, map_to_environment, map_to_imports,
		serialize_config, serialize_dsnp_users, serialize_graph_edges, serialize_graph_keypair,
		serialize_graph_updates, serialize_public_keys,
	},
};
use dsnp_graph_config::{DsnpUserId, GraphKeyType, SchemaId};
use dsnp_graph_core::{
	api::api::{GraphAPI, GraphState},
	util::transactional_hashmap::Transactional,
};
use jni::{
	objects::{JByteArray, JClass, JObject, JString},
	sys::{jboolean, jint, jlong},
	JNIEnv,
};
use std::{
	ops::{Deref, DerefMut},
	panic,
	sync::RwLock,
};

pub type SdkJniResult<V> = Result<V, SdkJniError>;

// Collection of GraphStates memory locations
static GRAPH_STATES_MEMORY_LOCATIONS: RwLock<Vec<jlong>> = RwLock::new(Vec::new());

#[no_mangle]
pub extern "C" fn Java_io_projectliberty_graphsdk_Native_hello<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	input: JString<'local>,
) -> JString<'local> {
	let input: String = env.get_string(&input).expect("Couldn't get java string!").into();

	let output = env
		.new_string(format!("Hello, {}!", input))
		.expect("Couldn't create java string!");
	output
}

/// An optimization barrier / guard against garbage collection.
///
/// cbindgen:ignore
#[no_mangle]
pub extern "C" fn Java_io_projectliberty_graphsdk_Native_keepAlive<'local>(
	mut _env: JNIEnv<'local>,
	_class: JClass<'local>,
	_input: JObject<'local>,
) {
}

/// Initializes the graph state and returns a handle to it.
/// The handle is a pointer to the memory location of the state.
/// The memory will be freed when `freeGraphState` is called.
/// # Arguments
/// * `environment` - the environment to initialize the graph state with
/// # Returns
/// * `jlong` - the handle to the graph state
/// # Errors
/// * `SdkJniError` - if initializing graph state fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_initializeGraphState<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	environment: JByteArray,
) -> jlong {
	let result = panic::catch_unwind(|| {
		let rust_environment = map_to_environment(&env, &environment)?;
		let graph_state = GraphState::new(rust_environment);
		let boxed = Box::new(graph_state);
		let mut graph_states =
			GRAPH_STATES_MEMORY_LOCATIONS.write().map_err(|_| SdkJniError::LockError)?;

		// graph state memory will be handled manually after following line execution
		let handle = Box::into_raw(boxed) as jlong;
		graph_states.push(handle);
		Ok(handle)
	});
	handle_result(&mut env, result)
}

/// Frees the graph state memory.
/// # Arguments
/// * `handle` - the handle to the graph state
/// # Errors
/// * `SdkJniError` - if freeing graph state fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_freeGraphState<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
) {
	let result = panic::catch_unwind(|| {
		if handle == 0 {
			return Err(SdkJniError::InvalidHandle("is null"));
		}
		let mut graph_states =
			GRAPH_STATES_MEMORY_LOCATIONS.write().map_err(|_| SdkJniError::LockError)?;
		let index = graph_states
			.iter()
			.position(|x| *x == handle)
			.ok_or(SdkJniError::InvalidHandle("does not exist"))?;
		graph_states.remove(index);

		// following line frees the allocated memory for state
		let _ = unsafe { Box::from_raw(handle as *mut GraphState) };
		Ok(())
	});
	handle_result(&mut env, result);
}

/// Get config for an environment.
/// # Arguments
/// * `environment` - the environment to get config for
/// # Returns
/// * `jbyteArray` - the serialized config
/// # Errors
/// * `SdkJniError` - if getting config fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_getConfig<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	environment: JByteArray,
) -> JByteArray<'local> {
	let result = panic::catch_unwind(|| {
		let rust_environment = map_to_environment(&env, &environment)?;
		let config = rust_environment.get_config();
		let result = serialize_config(&env, &config)?;
		Ok(result)
	});
	handle_result(&mut env, result)
}

/// Check if user graph exists.
/// # Arguments
/// * `handle` - the handle to the graph state
/// * `dsnp_user_id` - the user id to check
/// # Returns
/// * `jboolean` - true if user graph exists
/// # Errors
/// * `SdkJniError` - if checking user graph fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_containsUserGraph<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
	dsnp_user_id: jlong,
) -> jboolean {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;
		// TODO: test edge case that dsnp_user_id is bigger than i64
		let user_id = u64::try_from(dsnp_user_id)
			.map_err(|_| SdkJniError::BadJniParameter("invalid dsnp_user_id"))?;

		// locking to read from state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.read().map_err(|_| SdkJniError::LockError)?;
		let graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		let result = graph.deref().contains_user_graph(&user_id).into();

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		Ok(result)
	});
	handle_result(&mut env, result)
}

/// Get user graph length.
/// # Arguments
/// * `handle` - the handle to the graph state
/// # Returns
/// * `jint` - the length of the user graph
/// # Errors
/// * `SdkJniError` - if getting user graph length fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_getGraphUsersLength<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
) -> jint {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;

		// locking to read from state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.read().map_err(|_| SdkJniError::LockError)?;
		let graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		let result = graph.deref().len() as jint;

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		Ok(result)
	});
	handle_result(&mut env, result)
}

/// Remove user from graph state.
/// # Arguments
/// * `handle` - the handle to the graph state
/// * `dsnp_user_id` - the user id to remove
/// # Errors
/// * `SdkJniError` - if removing user fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_removeUserGraph<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
	dsnp_user_id: jlong,
) {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;
		let user_id = u64::try_from(dsnp_user_id)
			.map_err(|_| SdkJniError::BadJniParameter("invalid dsnp_user_id"))?;

		// locking to write in state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.write().map_err(|_| SdkJniError::LockError)?;
		let mut graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		graph.deref_mut().remove_user_graph(&user_id);

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		Ok(())
	});
	handle_result(&mut env, result)
}

/// Import user data into graph state.
/// # Arguments
/// * `handle` - the handle to the graph state
/// * `imports` - the serialized imports
/// # Errors
/// * `SdkJniError` - if imports are invalid
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_importUserData<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
	imports: JByteArray,
) {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;
		let rust_imports = map_to_imports(&env, &imports)?;

		// locking to write in state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.write().map_err(|_| SdkJniError::LockError)?;
		let mut graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		// do not use `?` here to handle the error since it would drop the memory
		let result = graph
			.deref_mut()
			.import_users_data(&rust_imports)
			.map_err(|e| SdkJniError::from(e));

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		result
	});
	handle_result(&mut env, result)
}

/// Export updates to graph state.
/// # Arguments
/// * `handle` - the handle to the graph state
/// # Returns
/// * `jbyteArray` - the serialized updates
/// # Errors
/// * `SdkJniError` - if exporting updates fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_exportUpdates<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
) -> JByteArray<'local> {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;

		// locking to read from state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.read().map_err(|_| SdkJniError::LockError)?;
		let graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		// do not use `?` here to handle the error since it would drop the memory
		let result = graph
			.deref()
			.export_updates()
			.map_err(|e| SdkJniError::from(e))
			.and_then(|updates| serialize_graph_updates(&env, &updates));

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		result
	});
	handle_result(&mut env, result)
}

/// Export updates to graph state for a single user graph.
/// # Arguments
/// * `handle` - the handle to the graph state
/// * `dsnp_user_id` - the user id whose graph to export
/// # Returns
/// * `jbyteArray` - the serialized updates
/// # Errors
/// * `SdkJniError` - if exporting updates fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_exportUserGraphUpdates<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
	dsnp_user_id: jlong,
) -> JByteArray<'local> {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;
		let dsnp_user_id = DsnpUserId::try_from(dsnp_user_id)
			.map_err(|_| SdkJniError::BadJniParameter("invalid dsnp_user_id"))?;

		// locking to read from state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.read().map_err(|_| SdkJniError::LockError)?;
		let graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		// do not use `?` here to handle the error since it would drop the memory
		let result = graph
			.deref()
			.export_user_graph_updates(&dsnp_user_id)
			.map_err(|e| SdkJniError::from(e))
			.and_then(|updates| serialize_graph_updates(&env, &updates));

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		result
	});
	handle_result(&mut env, result)
}

/// Apply actions to graph state.
/// # Arguments
/// * `handle` - the handle to the graph state
/// * `actions` - the serialized actions
/// # Errors
/// * `SdkJniError` - if applying actions fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_applyActions<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
	actions: JByteArray,
) {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;
		let (actions, options) = map_to_actions(&env, &actions)?;

		// locking to write in state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.write().map_err(|_| SdkJniError::LockError)?;
		let mut graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		// do not use `?` here to handle the error since it would drop the memory
		let result = graph
			.deref_mut()
			.apply_actions(&actions, &options)
			.map_err(|e| SdkJniError::from(e));

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		result
	});
	handle_result(&mut env, result)
}

/// Commit pending actions to a graph state.
/// # Arguments
/// * `handle` - the handle to the graph state
/// * `actions` - the serialized actions
/// # Errors
/// * `SdkJniError` - if applying actions fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_commit<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
) {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;

		// locking to write in state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.write().map_err(|_| SdkJniError::LockError)?;
		let mut graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		// do not use `?` here to handle the error since it would drop the memory
		graph.deref_mut().commit();

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		Ok(())
	});
	handle_result(&mut env, result)
}

/// Rollback pending actions in a graph state.
/// # Arguments
/// * `handle` - the handle to the graph state
/// * `actions` - the serialized actions
/// # Errors
/// * `SdkJniError` - if applying actions fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_rollback<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
) {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;

		// locking to write in state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.write().map_err(|_| SdkJniError::LockError)?;
		let mut graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		// do not use `?` here to handle the error since it would drop the memory
		graph.deref_mut().rollback();

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		Ok(())
	});
	handle_result(&mut env, result)
}

/// Force calculate graph updates.
/// # Arguments
/// * `handle` - the handle to the graph state
/// * `dsnp_user_id` - the user id to calculate updates for
/// # Returns
/// * `jbyteArray` - the serialized updates
/// # Errors
/// * `SdkJniError` - if calculating updates fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_forceCalculateGraphs<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
	dsnp_user_id: jlong,
) -> JByteArray<'local> {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;
		let dsnp_user_id = DsnpUserId::try_from(dsnp_user_id)
			.map_err(|_| SdkJniError::BadJniParameter("invalid dsnp_user_id"))?;

		// locking to read from state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.read().map_err(|_| SdkJniError::LockError)?;
		let graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		// do not use `?` here to handle the error since it would drop the memory
		let result = graph
			.deref()
			.force_recalculate_graphs(&dsnp_user_id)
			.map_err(|e| SdkJniError::from(e))
			.and_then(|updates| serialize_graph_updates(&env, &updates));

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		result
	});
	handle_result(&mut env, result)
}

/// Get connections for a user graph.
/// # Arguments
/// * `handle` - the handle to the graph state
/// * `dsnp_user_id` - the user id to get connections for
/// * `schema_id` - the schema id to get connections for
/// * `include_pending` - whether to include pending connections
/// # Returns
/// * `jbyteArray` - the serialized connections
/// # Errors
/// * `SdkJniError` - if getting connections fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_getConnectionsForUserGraph<
	'local,
>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
	dsnp_user_id: jlong,
	schema_id: jint,
	include_pending: jboolean,
) -> JByteArray<'local> {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;
		let dsnp_user_id = DsnpUserId::try_from(dsnp_user_id)
			.map_err(|_| SdkJniError::BadJniParameter("invalid dsnp_user_id"))?;
		let schema_id = SchemaId::try_from(schema_id)
			.map_err(|_| SdkJniError::BadJniParameter("invalid schema_id"))?;
		let include_pending = convert_jboolean(include_pending)
			.map_err(|_| SdkJniError::BadJniParameter("invalid include_pending"))?;

		// locking to read from state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.read().map_err(|_| SdkJniError::LockError)?;
		let graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		// do not use `?` here to handle the error since it would drop the memory
		let result = graph
			.deref()
			.get_connections_for_user_graph(&dsnp_user_id, &schema_id, include_pending)
			.map_err(|e| SdkJniError::from(e))
			.and_then(|graph_edges| serialize_graph_edges(&env, &graph_edges));

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		result
	});
	handle_result(&mut env, result)
}

/// Get users connections without keys.
/// # Arguments
/// * `handle` - the handle to the graph state
/// # Returns
/// * `jbyteArray` - the serialized users
/// # Errors
/// * `SdkJniError` - if getting users fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_getUsersWithoutKeys<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
) -> JByteArray<'local> {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;

		// locking to read from state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.read().map_err(|_| SdkJniError::LockError)?;
		let graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		// do not use `?` here to handle the error since it would drop the memory
		let result = graph
			.deref()
			.get_connections_without_keys()
			.map_err(|e| SdkJniError::from(e))
			.and_then(|dsnp_users| serialize_dsnp_users(&env, &dsnp_users));

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		result
	});
	handle_result(&mut env, result)
}

/// Get one sided private friendship connections.
/// # Arguments
/// * `handle` - the handle to the graph state
/// * `dsnp_user_id` - the user id to get connections for
/// # Returns
/// * `jbyteArray` - the serialized connections
/// # Errors
/// * `SdkJniError` - if getting connections fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_getOneSidedPrivateFriendshipConnections<
	'local,
>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
	dsnp_user_id: jlong,
) -> JByteArray<'local> {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;
		let user_id = u64::try_from(dsnp_user_id)
			.map_err(|_| SdkJniError::BadJniParameter("invalid dsnp_user_id"))?;

		// locking to read from state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.read().map_err(|_| SdkJniError::LockError)?;
		let graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		// do not use `?` here to handle the error since it would drop the memory
		let result = graph
			.deref()
			.get_one_sided_private_friendship_connections(&user_id)
			.map_err(|e| SdkJniError::from(e))
			.and_then(|graph_edges| serialize_graph_edges(&env, &graph_edges));

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		result
	});
	handle_result(&mut env, result)
}

/// Get public keys for a user.
/// # Arguments
/// * `handle` - the handle to the graph state
/// * `dsnp_user_id` - the user id to get public keys for
/// # Returns
/// * `jbyteArray` - the serialized public keys
/// # Errors
/// * `SdkJniError` - if getting public keys fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_getPublicKeys<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	handle: jlong,
	dsnp_user_id: jlong,
) -> JByteArray<'local> {
	let result = panic::catch_unwind(|| {
		validate_handle(&GRAPH_STATES_MEMORY_LOCATIONS, handle)?;
		let user_id = u64::try_from(dsnp_user_id)
			.map_err(|_| SdkJniError::BadJniParameter("invalid dsnp_user_id"))?;

		// locking to read from state
		let _lock = GRAPH_STATES_MEMORY_LOCATIONS.read().map_err(|_| SdkJniError::LockError)?;
		let graph = unsafe { Box::from_raw(handle as *mut GraphState) };
		// do not use `?` here to handle the error since it would drop the memory
		let result = graph
			.deref()
			.get_public_keys(&user_id)
			.map_err(|e| SdkJniError::from(e))
			.and_then(|public_keys| serialize_public_keys(&env, &public_keys));

		// pulling out of the box as raw so that memory stays allocated
		let _ = Box::into_raw(graph) as jlong;
		result
	});
	handle_result(&mut env, result)
}

/// Deserialize DSNP keys.
/// # Arguments
/// * `dsnp_keys` - the serialized DSNP keys
/// # Returns
/// * `jbyteArray` - the deserialized public keys
/// # Errors
/// * `SdkJniError` - if deserializing DSNP keys fails
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_deserializeDsnpKeys<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	dsnp_keys: JByteArray,
) -> JByteArray<'local> {
	let result = panic::catch_unwind(|| {
		let rust_dsnp_keys = map_to_dsnp_keys(&env, &dsnp_keys)?;
		GraphState::deserialize_dsnp_keys(&rust_dsnp_keys)
			.map_err(|e| SdkJniError::from(e))
			.and_then(|public_keys| serialize_public_keys(&env, &public_keys))
	});
	handle_result(&mut env, result)
}

/// Generate GraphKeyPair for a given GraphKeyType.
/// # Arguments
/// * `graph_key_type` - the type of the key to generate
/// # Returns
/// * `jbyteArray` - the serialized GraphKeyPair
/// # Errors
/// * `SdkJniError` - if generating GraphKeyPair fails
/// * `SdkJniError` - if GraphKeyType is InvalidHandle
#[no_mangle]
pub unsafe extern "C" fn Java_io_projectliberty_graphsdk_Native_generateKeyPair<'local>(
	mut env: JNIEnv<'local>,
	_class: JClass<'local>,
	graph_key_type: jint,
) -> JByteArray<'local> {
	let result = panic::catch_unwind(|| {
		let key_type = u8::try_from(graph_key_type)
			.map_err(|_| SdkJniError::BadJniParameter("invalid graph_key_type"))?;

		match key_type {
			0 => GraphState::generate_keypair(GraphKeyType::X25519)
				.map_err(|e| SdkJniError::from(e))
				.and_then(|key_pair| serialize_graph_keypair(&env, &key_pair)),
			_ => Err(SdkJniError::BadJniParameter("invalid graph_key_type")),
		}
	});
	handle_result(&mut env, result)
}