From 39f69cc4182b47b429d2ed0fe71434f66ecd9a85 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Wed, 14 Aug 2024 19:09:48 +0200 Subject: [PATCH] feat(java): xxh3 hash checking --- dist/java/src/mp/code/BufferController.java | 5 ++--- dist/java/src/mp/code/Client.java | 1 - dist/java/src/mp/code/CursorController.java | 1 - dist/java/src/mp/code/Workspace.java | 1 - dist/java/src/mp/code/data/TextChange.java | 10 +++++++++- src/ffi/java/buffer.rs | 21 +++++++++++++++++++-- 6 files changed, 30 insertions(+), 9 deletions(-) diff --git a/dist/java/src/mp/code/BufferController.java b/dist/java/src/mp/code/BufferController.java index a6fcfd1..c0a706b 100644 --- a/dist/java/src/mp/code/BufferController.java +++ b/dist/java/src/mp/code/BufferController.java @@ -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); } @@ -40,7 +40,6 @@ public class BufferController { private static native void free(long self); @Override - @SuppressWarnings("removal") protected void finalize() { free(this.ptr); } diff --git a/dist/java/src/mp/code/Client.java b/dist/java/src/mp/code/Client.java index a0fd532..2643bf8 100644 --- a/dist/java/src/mp/code/Client.java +++ b/dist/java/src/mp/code/Client.java @@ -36,7 +36,6 @@ public class Client { private static native void free(long self); @Override - @SuppressWarnings("removal") // muh java 8 protected void finalize() { free(this.ptr); } diff --git a/dist/java/src/mp/code/CursorController.java b/dist/java/src/mp/code/CursorController.java index f4d903a..93d67db 100644 --- a/dist/java/src/mp/code/CursorController.java +++ b/dist/java/src/mp/code/CursorController.java @@ -29,7 +29,6 @@ public class CursorController { private static native void free(long self); @Override - @SuppressWarnings("removal") protected void finalize() { free(this.ptr); } diff --git a/dist/java/src/mp/code/Workspace.java b/dist/java/src/mp/code/Workspace.java index 89b2144..dcc94f0 100644 --- a/dist/java/src/mp/code/Workspace.java +++ b/dist/java/src/mp/code/Workspace.java @@ -79,7 +79,6 @@ public class Workspace { private static native void free(long self); @Override - @SuppressWarnings("removal") protected void finalize() { free(this.ptr); } diff --git a/dist/java/src/mp/code/data/TextChange.java b/dist/java/src/mp/code/data/TextChange.java index edba093..e50b6f0 100644 --- a/dist/java/src/mp/code/data/TextChange.java +++ b/dist/java/src/mp/code/data/TextChange.java @@ -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); } } diff --git a/src/ffi/java/buffer.rs b/src/ffi/java/buffer.rs index 0f7b59b..8517841 100644 --- a/src/ffi/java/buffer.rs +++ b/src/ffi/java/buffer.rs @@ -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; @@ -67,11 +68,12 @@ fn recv_jni(env: &mut JNIEnv, change: Option) -> 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) @@ -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()) +}