Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuel-keller committed Sep 27, 2024
1 parent 6be5301 commit de73608
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/surrealdb/ValueBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ private static <T> ValueMut convertObject(final T object) throws IllegalAccessEx
final Optional<?> optional = (Optional<?>) object;
return optional.map(ValueBuilder::convert).orElse(null);
}
if (object instanceof Id) {
return ValueMut.createId((Id) object);
}
if (object instanceof Thing) {
return ValueMut.createThing((Thing) object);
}
final Field[] fields = object.getClass().getFields();
if (fields.length > 0) {
final List<EntryMut> entries = new ArrayList<>(fields.length);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/surrealdb/ValueMut.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ private ValueMut(long ptr) {

private static native long newDatetime(long seconds, long nanos);

private static native long newId(long ptr);

private static native long newThing(long ptr);

private static native long newArray(long[] ptrs);

private static native long newObject(long[] ptrs);
Expand Down Expand Up @@ -57,6 +61,14 @@ static ValueMut createDatetime(ZonedDateTime d) {
return new ValueMut(newDatetime(d.toEpochSecond(), d.getNano()));
}

static ValueMut createId(Id id) {
return new ValueMut(newId(id.getPtr()));
}

static ValueMut createThing(Thing thing) {
return new ValueMut(newThing(thing.getPtr()));
}

static ValueMut createArray(List<ValueMut> values) {
final long[] ptrs = new long[values.size()];
int idx = 0;
Expand Down
1 change: 0 additions & 1 deletion src/main/rust/entry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::hash::{DefaultHasher, Hash, Hasher};
use std::ptr::null_mut;
use std::sync::Arc;

use jni::objects::JClass;
use jni::sys::{jboolean, jint, jlong, jstring};
Expand Down
1 change: 0 additions & 1 deletion src/main/rust/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::hash::{DefaultHasher, Hash, Hasher};
use std::ptr::null_mut;
use std::sync::Arc;

use jni::objects::JClass;
use jni::sys::{jboolean, jdoubleArray, jint, jlong, jstring};
Expand Down
27 changes: 24 additions & 3 deletions src/main/rust/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,34 @@ use std::hash::{DefaultHasher, Hash, Hasher};
use std::ptr::null_mut;
use std::sync::Arc;

use jni::objects::JClass;
use jni::objects::{JClass, JString};
use jni::sys::{jboolean, jint, jlong, jstring};
use jni::JNIEnv;
use surrealdb::sql::{Id, Value};
use surrealdb::sql::{Id, Thing, Value};

use crate::error::SurrealError;
use crate::{create_instance, get_value_instance, new_string};
use crate::{create_instance, get_rust_string, get_value_instance, new_string};

#[no_mangle]
pub extern "system" fn Java_com_surrealdb_Id_newLongId<'local>(
_env: JNIEnv<'local>,
_class: JClass<'local>,
id: jlong,
) -> jlong {
let value = Value::Thing(Thing::from(("", Id::Number(id))));
create_instance(Arc::new(value))
}

#[no_mangle]
pub extern "system" fn Java_com_surrealdb_Id_newStringId<'local>(
mut env: JNIEnv<'local>,
_class: JClass<'local>,
id: JString<'local>,
) -> jlong {
let id = get_rust_string!(&mut env, id, || 0);
let value = Value::Thing(Thing::from(("", Id::String(id))));
create_instance(Arc::new(value))
}

#[no_mangle]
pub extern "system" fn Java_com_surrealdb_Id_isLong<'local>(
Expand Down
4 changes: 2 additions & 2 deletions src/main/rust/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ macro_rules! get_response_instance {
#[macro_export]
macro_rules! get_value_instance {
($env:expr, $id:expr, $default_fn:expr) => {
match $crate::get_instance::<Arc<surrealdb::sql::Value>>($id, "Value") {
match $crate::get_instance::<std::sync::Arc<surrealdb::sql::Value>>($id, "Value") {
Ok(s) => s.clone(),
Err(e) => return e.exception($env, $default_fn),
}
Expand All @@ -41,7 +41,7 @@ macro_rules! get_value_instance {
#[macro_export]
macro_rules! get_entry_instance {
($env:expr, $id:expr, $default_fn:expr) => {
match $crate::get_instance::<(String, Arc<surrealdb::sql::Value>)>($id, "Entry") {
match $crate::get_instance::<(String, std::sync::Arc<surrealdb::sql::Value>)>($id, "Entry") {
Ok(s) => s,
Err(e) => return e.exception($env, $default_fn),
}
Expand Down
36 changes: 32 additions & 4 deletions src/main/rust/valuemut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ use rust_decimal::Decimal;
use surrealdb::sql::{Array, Datetime, Duration, Number, Object, Strand, Value};

use crate::error::SurrealError;
use crate::{
create_instance, get_long_array, get_rust_string, get_value_mut_instance, new_string,
take_entry_mut_instance, take_value_mut_instance,
};
use crate::{create_instance, get_long_array, get_rust_string, get_value_instance, get_value_mut_instance, new_string, take_entry_mut_instance, take_value_mut_instance};

#[no_mangle]
pub extern "system" fn Java_com_surrealdb_ValueMut_newString<'local>(
Expand Down Expand Up @@ -100,6 +97,37 @@ pub extern "system" fn Java_com_surrealdb_ValueMut_newDatetime<'local>(
}
}

#[no_mangle]
pub extern "system" fn Java_com_surrealdb_ValueMut_newId<'local>(
mut env: JNIEnv<'local>,
_class: JClass<'local>,
ptr: jlong,
) -> jlong {
println!("1");
let value = get_value_instance!(&mut env, ptr, || 0);
println!("2");
if matches!(value.as_ref(), Value::Thing(_)) {
println!("3");
return create_instance(value.as_ref().clone());
}
println!("4");
SurrealError::NullPointerException("Id").exception(&mut env, || 0)
}

#[no_mangle]
pub extern "system" fn Java_com_surrealdb_ValueMut_newThing<'local>(
mut env: JNIEnv<'local>,
_class: JClass<'local>,
ptr: jlong,
) -> jlong {
let value = get_value_instance!(&mut env, ptr, || 0);
if matches!(value.as_ref(), Value::Thing(_)) {
return create_instance(value.clone());
}
SurrealError::NullPointerException("Thing").exception(&mut env, || 0)
}


#[no_mangle]
pub extern "system" fn Java_com_surrealdb_ValueMut_newArray<'local>(
mut env: JNIEnv<'local>,
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/com/surrealdb/InsertRelationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void insertRelationValue() {
final Thing tobie = persons.get(0).get(Person.class).id;
final Thing jaime = persons.get(1).get(Person.class).id;
// Relate records
final InsertRelation insert = new InsertRelation(new Id(1), tobie, jaime);
final InsertRelation insert = new InsertRelation(Id.from(1), tobie, jaime);
final Value value = surreal.insertRelation("brother", insert);
// Get the relation:
final InsertRelation relation = value.get(InsertRelation.class);
Expand All @@ -43,7 +43,7 @@ void insertRelationObject() {
final Thing tobie = persons.get(0).get(Person.class).id;
final Thing jaime = persons.get(1).get(Person.class).id;
// Relate records
final InsertRelation insert = new InsertRelation(new Id(1), tobie, jaime);
final InsertRelation insert = new InsertRelation(Id.from(1), tobie, jaime);
final InsertRelation relation = surreal.insertRelation(InsertRelation.class, "brother", insert);
// Assertion
assertEquals(insert, relation);
Expand All @@ -61,8 +61,8 @@ void insertRelationValues() {
final Thing tobie = persons.get(0).get(Person.class).id;
final Thing jaime = persons.get(1).get(Person.class).id;
// Relate records
final InsertRelation insert1 = new InsertRelation(new Id(1), tobie, jaime);
final InsertRelation insert2 = new InsertRelation(new Id(2), jaime, tobie);
final InsertRelation insert1 = new InsertRelation(Id.from("A"), tobie, jaime);
final InsertRelation insert2 = new InsertRelation(Id.from("B"), jaime, tobie);
final List<Value> relations = surreal.insertRelations("brother", insert1, insert2);
// Get the relation:
final InsertRelation relation1 = relations.get(0).get(InsertRelation.class);
Expand All @@ -85,8 +85,8 @@ void insertRelationObjects() {
final Thing tobie = persons.get(0).get(Person.class).id;
final Thing jaime = persons.get(1).get(Person.class).id;
// Relate records
final InsertRelation insert1 = new InsertRelation(new Id(1), tobie, jaime);
final InsertRelation insert2 = new InsertRelation(new Id(2), jaime, tobie);
final InsertRelation insert1 = new InsertRelation(Id.from(1), tobie, jaime);
final InsertRelation insert2 = new InsertRelation(Id.from(2), jaime, tobie);
final List<InsertRelation> relations = surreal.insertRelations(InsertRelation.class, "brother", insert1, insert2);
// Assertion
assertEquals(insert1, relations.get(0));
Expand Down

0 comments on commit de73608

Please sign in to comment.