Skip to content

Commit

Permalink
Hello rust
Browse files Browse the repository at this point in the history
Signed-off-by: Harsha Vamsi Kalluri <[email protected]>
  • Loading branch information
harshavamsi committed Jan 24, 2024
1 parent 17f20c6 commit 739d0f3
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 1 deletion.
1 change: 1 addition & 0 deletions rusty-search/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
243 changes: 243 additions & 0 deletions rusty-search/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions rusty-search/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "rust_library"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
jni = "0.21.1"

[lib]
crate_type = ["cdylib"]
9 changes: 9 additions & 0 deletions rusty-search/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins{
id 'io.github.arc-blroth.cargo-wrapper' version '1.0.0' apply true
}


cargo {
outputs = ['': System.mapLibraryName('rust_library')]
cargoCommand="/opt/homebrew/bin/cargo"
}
6 changes: 6 additions & 0 deletions rusty-search/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use jni::JNIEnv;

#[no_mangle]
pub extern "system" fn Java_org_opensearch_search_query_QueryPhase_doStuff(_env: JNIEnv) {
println!("Hello Rust!");
}
16 changes: 16 additions & 0 deletions server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ sourceSets {
}
}

configurations {
// Declare a custom configuration to
// resolve the library from :native
backend {
canBeConsumed false
canBeResolved true
}
}

dependencies {

api project(':libs:opensearch-common')
Expand Down Expand Up @@ -131,6 +140,13 @@ dependencies {
internalClusterTestImplementation(project(":test:framework")) {
exclude group: 'org.opensearch', module: 'server'
}

backend(project(':rusty-search'))
}

processResources {
// Copy the native library into the final jar
from(configurations.backend)
}

tasks.withType(JavaCompile).configureEach {
Expand Down
26 changes: 26 additions & 0 deletions server/src/main/java/org/opensearch/search/query/QueryPhase.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@
import org.opensearch.search.suggest.SuggestProcessor;
import org.opensearch.threadpool.ThreadPool;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -101,6 +106,26 @@ public class QueryPhase {
private final SuggestProcessor suggestProcessor;
private final RescoreProcessor rescoreProcessor;

private native static void doStuff();

static {
try {
// Extract and load our native library
String nativeLibraryName = System.mapLibraryName("rust_library");
File tempFile = File.createTempFile("extracted_", nativeLibraryName);
try (
ReadableByteChannel src = Channels.newChannel(QueryPhase.class.getClassLoader().getResourceAsStream(nativeLibraryName));
FileChannel dst = new FileOutputStream(tempFile).getChannel()
) {
dst.transferFrom(src, 0, Long.MAX_VALUE);
}
System.load(tempFile.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}

public QueryPhase() {
this(DEFAULT_QUERY_PHASE_SEARCHER);
}
Expand Down Expand Up @@ -353,6 +378,7 @@ private static boolean searchWithCollector(
}
QuerySearchResult queryResult = searchContext.queryResult();
try {
doStuff();
searcher.search(query, queryCollector);
} catch (EarlyTerminatingCollector.EarlyTerminationException e) {
queryResult.terminatedEarly(true);
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ List projects = [
'test:fixtures:old-elasticsearch',
'test:fixtures:s3-fixture',
'test:logger-usage',
'test:telemetry'
'test:telemetry',
'rusty-search'
]

/**
Expand Down

0 comments on commit 739d0f3

Please sign in to comment.