Skip to content

Commit

Permalink
feat(java): xxh3 hash checking
Browse files Browse the repository at this point in the history
  • Loading branch information
zaaarf committed Aug 14, 2024
1 parent fcd2b9f commit 39f69cc
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
5 changes: 2 additions & 3 deletions dist/java/src/mp/code/BufferController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public class BufferController {
this.ptr = ptr;
}

public static native String get_name(long self);
private static native String get_name(long self);
public String getName() {
return get_name(this.ptr);
}

public static native String get_content(long self) throws CodeMPException;
private static native String get_content(long self) throws CodeMPException;
public String getContent() throws CodeMPException {
return get_content(this.ptr);
}
Expand All @@ -40,7 +40,6 @@ public void send(TextChange change) throws CodeMPException {

private static native void free(long self);
@Override
@SuppressWarnings("removal")
protected void finalize() {
free(this.ptr);
}
Expand Down
1 change: 0 additions & 1 deletion dist/java/src/mp/code/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public Optional<Workspace> getWorkspace() {

private static native void free(long self);
@Override
@SuppressWarnings("removal") // muh java 8
protected void finalize() {
free(this.ptr);
}
Expand Down
1 change: 0 additions & 1 deletion dist/java/src/mp/code/CursorController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public void send(Cursor cursor) throws CodeMPException {

private static native void free(long self);
@Override
@SuppressWarnings("removal")
protected void finalize() {
free(this.ptr);
}
Expand Down
1 change: 0 additions & 1 deletion dist/java/src/mp/code/Workspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public Optional<BufferController> selectBuffer(long timeout) throws CodeMPExcept

private static native void free(long self);
@Override
@SuppressWarnings("removal")
protected void finalize() {
free(this.ptr);
}
Expand Down
10 changes: 9 additions & 1 deletion dist/java/src/mp/code/data/TextChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ public class TextChange {
public final long start;
public final long end;
public final String content;
private final long hash; // xxh3 hash

public TextChange(long start, long end, String content) {
public TextChange(long start, long end, String content, long hash) {
this.start = start;
this.end = end;
this.content = content;
this.hash = hash;
}

private static native long hash(String content);
public boolean hashMatches(String content) {
// 0 is Rust default value and a very unlikely hash
return hash == 0L || this.hash == hash(content);
}
}
21 changes: 19 additions & 2 deletions src/ffi/java/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use jni::{objects::{JClass, JObject, JValueGen}, sys::{jlong, jobject, jstring}, JNIEnv};
use jni::{objects::{JClass, JObject, JString, JValueGen}, sys::{jlong, jobject, jstring}, JNIEnv};
use xxhash_rust::xxh3::xxh3_64;

use crate::api::Controller;

Expand Down Expand Up @@ -67,11 +68,12 @@ fn recv_jni(env: &mut JNIEnv, change: Option<crate::api::TextChange>) -> jobject
.and_then(|class| {
env.new_object(
class,
"(JJLjava/lang/String;)V",
"(JJLjava/lang/String;J)V",
&[
JValueGen::Long(jlong::from(event.start)),
JValueGen::Long(jlong::from(event.end)),
JValueGen::Object(&content),
JValueGen::Long(event.hash.unwrap_or_default())
]
)
}).jexcept(env)
Expand Down Expand Up @@ -116,3 +118,18 @@ pub extern "system" fn Java_mp_code_BufferController_free(
let _ = unsafe { Box::from_raw(self_ptr as *mut crate::cursor::Controller) };
}


/// Calculates the XXH3 hash for a given String.
#[no_mangle]
pub extern "system" fn Java_mp_code_data_TextChange_hash<'local>(
mut env: JNIEnv,
_class: JClass<'local>,
content: JString<'local>,
) -> jlong {
let content: String = env.get_string(&content)
.map(|s| s.into())
.jexcept(&mut env);

let hash = xxh3_64(content.as_bytes());
i64::from_ne_bytes(hash.to_ne_bytes())
}

0 comments on commit 39f69cc

Please sign in to comment.