|
| 1 | +# OpenSearch SQL Native Engine POC |
| 2 | + |
| 3 | +This is a proof-of-concept (POC) for a Java+C++ hybrid project, demonstrating how to integrate C++ native code into the OpenSearch SQL project. |
| 4 | + |
| 5 | +## Project Structure |
| 6 | + |
| 7 | +``` |
| 8 | +cpp/ |
| 9 | +├── CMakeLists.txt # Main CMake configuration file |
| 10 | +├── src/ |
| 11 | +│ ├── CMakeLists.txt # Source CMake configuration |
| 12 | +│ ├── core/ |
| 13 | +│ │ ├── Calculator.h # C++ core calculation class header |
| 14 | +│ │ └── Calculator.cpp # C++ core calculation class implementation |
| 15 | +│ └── jni/ |
| 16 | +│ └── NativeEngine.cpp # JNI wrapper implementation |
| 17 | +└── build/ # CMake build directory (auto-generated) |
| 18 | +
|
| 19 | +native/ |
| 20 | +└── src/main/java/org/opensearch/sql/native/ |
| 21 | + ├── NativeEngine.java # Java JNI wrapper |
| 22 | + └── NativeEngineDemo.java # Demo program |
| 23 | +``` |
| 24 | + |
| 25 | +## Features |
| 26 | + |
| 27 | +### 1. Java Calling C++ |
| 28 | +- `nativeAdd(int a, int b)` - Addition operation implemented in C++ |
| 29 | +- `nativeMultiply(int a, int b)` - Multiplication operation implemented in C++ |
| 30 | + |
| 31 | +### 2. C++ Calling Java |
| 32 | +- `nativeCallJava()` - C++ calls Java callback method |
| 33 | +- `onNativeCallback(String message)` - Java callback method |
| 34 | + |
| 35 | +## Build Requirements |
| 36 | + |
| 37 | +- Java 11+ |
| 38 | +- CMake 3.16+ |
| 39 | +- C++17 compatible compiler (GCC/Clang/MSVC) |
| 40 | +- Make (Linux/macOS) or Visual Studio (Windows) |
| 41 | + |
| 42 | +## Quick Start |
| 43 | + |
| 44 | +### 1. Build Project |
| 45 | +```bash |
| 46 | +# Execute in project root directory |
| 47 | +./gradlew :native:build |
| 48 | +``` |
| 49 | + |
| 50 | +### 2. Run Demo |
| 51 | +```bash |
| 52 | +./gradlew :native:run |
| 53 | +``` |
| 54 | + |
| 55 | +### 3. Run Tests |
| 56 | +```bash |
| 57 | +./gradlew :native:test |
| 58 | +``` |
| 59 | + |
| 60 | +### 4. One-click Build and Run |
| 61 | +```bash |
| 62 | +./build-and-run-demo.sh |
| 63 | +``` |
| 64 | + |
| 65 | +## Build Process |
| 66 | + |
| 67 | +1. **C++ Compilation**: Gradle calls CMake to compile C++ code and generate dynamic library |
| 68 | +2. **Library Copy**: Copy generated dynamic library to Java project's libs directory |
| 69 | +3. **Java Compilation**: Compile Java code including JNI interfaces |
| 70 | +4. **Runtime Loading**: Java runtime loads C++ library via `System.loadLibrary()` |
| 71 | + |
| 72 | +## Extension Guide |
| 73 | + |
| 74 | +### Adding New C++ Features |
| 75 | +1. Add new C++ classes in `cpp/src/core/` |
| 76 | +2. Add JNI wrapper functions in `cpp/src/jni/NativeEngine.cpp` |
| 77 | +3. Declare corresponding native methods in `native/src/main/java/.../NativeEngine.java` |
| 78 | + |
| 79 | +### JNI Method Naming Convention |
| 80 | +```cpp |
| 81 | +JNIEXPORT <return_type> JNICALL |
| 82 | +Java_<package_name_with_underscores>_<class_name>_<method_name>(JNIEnv *env, ...) |
| 83 | +``` |
| 84 | + |
| 85 | +## Notes |
| 86 | + |
| 87 | +- Ensure JNI method signatures match Java declarations exactly |
| 88 | +- Pay attention to memory management to avoid memory leaks |
| 89 | +- Exception handling needs to be considered on both C++ and Java sides |
| 90 | +- Cross-platform compatibility needs to be configured in CMakeLists.txt |
| 91 | + |
| 92 | +## Troubleshooting |
| 93 | + |
| 94 | +### Common Issues |
| 95 | + |
| 96 | +1. **UnsatisfiedLinkError**: |
| 97 | + - Check if dynamic library is correctly generated and copied |
| 98 | + - Confirm `java.library.path` is set correctly |
| 99 | + |
| 100 | +2. **CMake cannot find JNI**: |
| 101 | + - Ensure JAVA_HOME environment variable is set correctly |
| 102 | + - Check if JDK installation is complete |
| 103 | + |
| 104 | +3. **Compilation errors**: |
| 105 | + - Confirm C++ compiler supports C++17 |
| 106 | + - Check if CMake version meets requirements |
| 107 | + |
| 108 | +## Performance Considerations |
| 109 | + |
| 110 | +- JNI calls have overhead, suitable for compute-intensive tasks |
| 111 | +- Avoid frequent small data JNI calls |
| 112 | +- Consider batch processing to reduce JNI boundary crossings |
0 commit comments