Skip to content

Commit

Permalink
Java: Configure GEDS using string types.
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Spörri <[email protected]>
  • Loading branch information
pspoerri committed Mar 29, 2023
1 parent dfa9903 commit f088eac
Show file tree
Hide file tree
Showing 9 changed files with 403 additions and 34 deletions.
7 changes: 5 additions & 2 deletions src/java/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
include(UseJava)

SET(JAVA_SOURCES
com/ibm/geds/GEDSFile.java
com/ibm/geds/GEDS.java
com/ibm/geds/GEDSConfig.java
com/ibm/geds/GEDSFile.java
com/ibm/geds/GEDSFileStatus.java
)
SET(SOURCES
com_ibm_geds_GEDS.cpp
com_ibm_geds_GEDSConfig.cpp
com_ibm_geds_GEDSFile.cpp

JavaError.cpp
JavaError.h
Platform.cpp
)

set(GEDS_JAVA_VERSION 1.0)
set(GEDS_JAVA_VERSION 1.1)

set(GEDS_JAR_GROUPID "com.ibm.geds")
set(CMAKE_JAR_ARTIFACT_NAME "geds")
Expand Down
14 changes: 14 additions & 0 deletions src/java/GEDSConfigContainer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright 2023- IBM Inc. All rights reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <memory>

#include "GEDSConfig.h"

struct GEDSConfigContainer {
std::shared_ptr<GEDSConfig> element;
};
34 changes: 27 additions & 7 deletions src/java/com/ibm/geds/GEDS.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public class GEDS {
}

private long nativePtr = 0;
public final String pathPrefix;
public final String metadataServiceAddress;
private final GEDSConfig config;

public GEDS(GEDSConfig config) {
this.config = config;
this.nativePtr = initGEDS(this.config.getNativePtr());
}

/**
* Constructor for GEDS.
Expand All @@ -30,41 +34,57 @@ public class GEDS {
* @param blockSize Block Size used for prefetching data
* (Optional). `0` indicates default block size.
*/
@Deprecated
public GEDS(String metadataServiceAddress, String pathPrefix, String hostname, int port, long blockSize) {
if (port < 0 || port > 65535) {
throw new IllegalArgumentException("Invalid port.");
}
if (blockSize < 0) {
throw new IllegalArgumentException("Invalid block size");
}
this.metadataServiceAddress = metadataServiceAddress;
this.pathPrefix = pathPrefix;
this.nativePtr = initGEDS(metadataServiceAddress, pathPrefix, hostname, port, blockSize);
config = new GEDSConfig(metadataServiceAddress);
if (port != 0) {
config.set("port", port);
}
if (hostname != "") {
config.set("hostname", hostname);
}
if (pathPrefix != "") {
config.set("local_storage_path", pathPrefix);
}
if (blockSize != 0) {
config.set("cache_block_size", blockSize);
}
this.nativePtr = initGEDS(config.getNativePtr());
}

@Deprecated
public GEDS(String metadataServiceAddress, String pathPrefix, int port, long blockSize) {
this(metadataServiceAddress, pathPrefix, "", port, blockSize);
}

@Deprecated
public GEDS(String metadataServiceAddress, String pathPrefix, long blockSize) {
this(metadataServiceAddress, pathPrefix, "", 0, blockSize);
}

@Deprecated
public GEDS(String metadataServiceAddress, String pathPrefix) {
this(metadataServiceAddress, pathPrefix, "", 0, 0);
}

@Deprecated
public GEDS(String metadataServiceAddress) {
this(metadataServiceAddress, "", "", 0, 0);
}

private static native long initGEDS(String metadataServiceAddress, String pathPrefix, String hostname, int port,
long blockSize);
private static native long initGEDS(long nativePtrConfig);

public void stopGEDS() {
checkGEDS();
nativeStopGEDS(nativePtr);
}

private static native void nativeStopGEDS(long ptr);

public static native void printStatistics();
Expand Down
63 changes: 63 additions & 0 deletions src/java/com/ibm/geds/GEDSConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright 2023- IBM Inc. All rights reserved
* SPDX-License-Identifier: Apache-2.0
*/

package com.ibm.geds;

public class GEDSConfig {
static {
System.loadLibrary("geds_java");
}

private long nativePtr = 0;

public long getNativePtr() {
return nativePtr;
}

public final String serverAddress;

public GEDSConfig(String serverAddress) {
this.serverAddress = serverAddress;
this.nativePtr = initGEDSConfig(serverAddress);
}

public void set(String key, String value) {
nativeSetString(nativePtr, key, value);
}

public void set(String key, int value) {
nativeSetInt(nativePtr, key, value);
}

public void set(String key, long value) {
nativeSetLong(nativePtr, key, value);
}

public String getString(String key) {
return nativeGetString(nativePtr, key);
}

public int getInt(String key) {
return nativeGetInt(nativePtr, key);
}

public long getLong(String key) {
return nativeGetLong(nativePtr, key);
}

private static native long initGEDSConfig(String serverAddress);

private static native void nativeSetString(long nativePtr, String key, String value);

private static native void nativeSetInt(long nativePtr, String key, int value);

private static native void nativeSetLong(long nativePtr, String key, long value);

private static native String nativeGetString(long nativePtr, String key);

private static native int nativeGetInt(long nativePtr, String key);

private static native long nativeGetLong(long nativePtr, String key);
}
40 changes: 15 additions & 25 deletions src/java/com_ibm_geds_GEDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "GEDS.h"
#include "GEDSConfig.h"
#include "GEDSConfigContainer.h"
#include "JavaError.h"
#include "Logging.h"
#include "Ports.h"
Expand All @@ -22,32 +23,21 @@ struct GEDSContainer {
std::shared_ptr<GEDS> element;
};

// NOLINTNEXTLINE(modernize-use-trailing-return-type)
/*
* Class: com_ibm_geds_GEDS
* Method: initGEDS
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_ibm_geds_GEDS_initGEDS(JNIEnv *env, jclass,
jstring metadataServiceAddressJava,
jstring pathPrefixJava,
jstring hostnameJava, jint port,
jlong blockSize) {
auto hostname = env->GetStringUTFChars(hostnameJava, nullptr);
auto hostnameStr = std::string{hostname};
auto pathPrefix = env->GetStringUTFChars(pathPrefixJava, nullptr);
auto pathPrefixStr = std::string{pathPrefix};
auto metadataServiceAddress = env->GetStringUTFChars(metadataServiceAddressJava, nullptr);
auto config = GEDSConfig(metadataServiceAddress);
config.hostname = hostnameStr != "" ? std::make_optional(hostnameStr) : std::nullopt;
if (port != 0) {
config.port = port;
}
if (blockSize != 0) {
config.cacheBlockSize = blockSize;
}
if (pathPrefixStr != "") {
config.localStoragePath = pathPrefixStr;
}
auto geds = GEDS::factory(std::move(config));
env->ReleaseStringUTFChars(pathPrefixJava, pathPrefix);
env->ReleaseStringUTFChars(metadataServiceAddressJava, metadataServiceAddress);
env->ReleaseStringUTFChars(hostnameJava, hostname);
jlong nativePtrConfig) {
if (nativePtrConfig == 0) {
throwNullPointerException(env, "Invalid nativePtr.");
return 0;
}
auto container = reinterpret_cast<GEDSConfigContainer *>(nativePtrConfig); // NOLINT

auto geds = GEDS::factory(*(container->element));

auto status = geds->start();
if (!status.ok()) {
return throwRuntimeException(env, std::string{status.message()});
Expand Down
Loading

0 comments on commit f088eac

Please sign in to comment.