Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bindings/java/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ default = [
"services-s3",
"services-webdav",
"services-webhdfs",
"layers-logging",
"layers-mime-guess",
"layers-throttle",
"layers-timeout",
"layers-hotpath",
]

services-all = [
Expand Down Expand Up @@ -154,6 +159,12 @@ services-webdav = ["opendal/services-webdav"]
services-webhdfs = ["opendal/services-webhdfs"]
services-yandex-disk = ["opendal/services-yandex-disk"]

layers-hotpath = ["opendal/layers-hotpath"]
layers-logging = ["opendal/layers-logging"]
layers-mime-guess = ["opendal/layers-mime-guess"]
layers-throttle = ["opendal/layers-throttle"]
layers-timeout = ["opendal/layers-timeout"]

[dependencies]
anyhow = { version = "1.0.100" }
jni = { version = "0.21.1" }
Expand Down
66 changes: 66 additions & 0 deletions bindings/java/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ use jni::sys::jfloat;
use jni::sys::jlong;
use opendal::Operator;
use opendal::layers::ConcurrentLimitLayer;
use opendal::layers::HotpathLayer;
use opendal::layers::LoggingLayer;
use opendal::layers::MimeGuessLayer;
use opendal::layers::RetryLayer;
use opendal::layers::ThrottleLayer;
use opendal::layers::TimeoutLayer;

#[unsafe(no_mangle)]
pub extern "system" fn Java_org_apache_opendal_layer_RetryLayer_doLayer(
Expand Down Expand Up @@ -62,3 +67,64 @@ pub extern "system" fn Java_org_apache_opendal_layer_ConcurrentLimitLayer_doLaye
let concurrent_limit = ConcurrentLimitLayer::new(permits as usize);
Box::into_raw(Box::new(op.clone().layer(concurrent_limit))) as jlong
}

#[unsafe(no_mangle)]
pub extern "system" fn Java_org_apache_opendal_layer_MimeGuessLayer_doLayer(
_: JNIEnv,
_: JClass,
op: *mut Operator,
) -> jlong {
let op = unsafe { &*op };
let mime_guess = MimeGuessLayer::new();
Box::into_raw(Box::new(op.clone().layer(mime_guess))) as jlong
}

#[unsafe(no_mangle)]
pub extern "system" fn Java_org_apache_opendal_layer_TimeoutLayer_doLayer(
_: JNIEnv,
_: JClass,
op: *mut Operator,
timeout_nanos: jlong,
io_timeout_nanos: jlong,
) -> jlong {
let op = unsafe { &*op };
let timeout = TimeoutLayer::new()
.with_timeout(Duration::from_nanos(timeout_nanos as u64))
.with_io_timeout(Duration::from_nanos(io_timeout_nanos as u64));
Box::into_raw(Box::new(op.clone().layer(timeout))) as jlong
}

#[unsafe(no_mangle)]
pub extern "system" fn Java_org_apache_opendal_layer_LoggingLayer_doLayer(
_: JNIEnv,
_: JClass,
op: *mut Operator,
) -> jlong {
let op = unsafe { &*op };
let logging = LoggingLayer::default();
Box::into_raw(Box::new(op.clone().layer(logging))) as jlong
}

#[unsafe(no_mangle)]
pub extern "system" fn Java_org_apache_opendal_layer_ThrottleLayer_doLayer(
_: JNIEnv,
_: JClass,
op: *mut Operator,
bandwidth: jlong,
burst: jlong,
) -> jlong {
let op = unsafe { &*op };
let throttle = ThrottleLayer::new(bandwidth as u32, burst as u32);
Box::into_raw(Box::new(op.clone().layer(throttle))) as jlong
}

#[unsafe(no_mangle)]
pub extern "system" fn Java_org_apache_opendal_layer_HotpathLayer_doLayer(
_: JNIEnv,
_: JClass,
op: *mut Operator,
) -> jlong {
let op = unsafe { &*op };
let hotpath = HotpathLayer::new();
Box::into_raw(Box::new(op.clone().layer(hotpath))) as jlong
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.opendal.layer;

import org.apache.opendal.Layer;

/**
* This layer adds hotpath profiling for every operation.
*
* @see <a href="https://docs.rs/opendal/latest/opendal/layers/struct.HotpathLayer.html">HotpathLayer's rustdoc</a>
*/
public class HotpathLayer extends Layer {

/**
* Create a new HotpathLayer.
*/
public HotpathLayer() {}

@Override
protected long layer(long nativeOp) {
return doLayer(nativeOp);
}

private static native long doLayer(long nativeHandle);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.opendal.layer;

import org.apache.opendal.Layer;

/**
* This layer adds structured logging for every operation.
*
* @see <a href="https://docs.rs/opendal/latest/opendal/layers/struct.LoggingLayer.html">LoggingLayer's rustdoc</a>
*/
public class LoggingLayer extends Layer {

/**
* Create a new LoggingLayer.
*/
public LoggingLayer() {}

@Override
protected long layer(long nativeOp) {
return doLayer(nativeOp);
}

private static native long doLayer(long nativeHandle);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.opendal.layer;

import org.apache.opendal.Layer;

/**
* This layer will automatically set Content-Type based on the file extension in the path.
*
* @see <a href="https://docs.rs/opendal/latest/opendal/layers/struct.MimeGuessLayer.html">MimeGuessLayer's rustdoc</a>
*/
public class MimeGuessLayer extends Layer {

/**
* Create a new MimeGuessLayer.
*/
public MimeGuessLayer() {}

@Override
protected long layer(long nativeOp) {
return doLayer(nativeOp);
}

private static native long doLayer(long nativeHandle);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.opendal.layer;

import org.apache.opendal.Layer;

/**
* This layer adds a bandwidth rate limiter to the underlying services.
*
* @see <a href="https://docs.rs/opendal/latest/opendal/layers/struct.ThrottleLayer.html">ThrottleLayer's rustdoc</a>
*/
public class ThrottleLayer extends Layer {

private final long bandwidth;

private final long burst;

/**
* Create a new ThrottleLayer with given bandwidth and burst.
*
* @param bandwidth the maximum number of bytes allowed to pass through per second
* @param burst the maximum number of bytes allowed to pass through at once
*/
public ThrottleLayer(long bandwidth, long burst) {
if (bandwidth <= 0) {
throw new IllegalArgumentException("bandwidth must be positive");
}
if (burst <= 0) {
throw new IllegalArgumentException("burst must be positive");
}
this.bandwidth = bandwidth;
this.burst = burst;
}

@Override
protected long layer(long nativeOp) {
return doLayer(nativeOp, bandwidth, burst);
}

private static native long doLayer(long nativeHandle, long bandwidth, long burst);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.opendal.layer;

import java.time.Duration;
import org.apache.opendal.Layer;

/**
* This layer adds timeout for every operation to avoid slow or unexpected hang operations.
*
* @see <a href="https://docs.rs/opendal/latest/opendal/layers/struct.TimeoutLayer.html">TimeoutLayer's rustdoc</a>
*/
public class TimeoutLayer extends Layer {

private final Duration timeout;

private final Duration ioTimeout;

/**
* Create a new TimeoutLayer with default settings.
* Default timeout: 60 seconds for non-IO operations, 10 seconds for IO operations.
*/
public TimeoutLayer() {
this.timeout = Duration.ofSeconds(60);
this.ioTimeout = Duration.ofSeconds(10);
}

/**
* Create a new TimeoutLayer with custom timeout settings.
*
* @param timeout timeout for non-IO operations (stat, delete, etc.)
* @param ioTimeout timeout for IO operations (read, write, etc.)
*/
public TimeoutLayer(Duration timeout, Duration ioTimeout) {
this.timeout = timeout;
this.ioTimeout = ioTimeout;
}

/**
* Set timeout for non-IO operations.
*
* @param timeout the timeout duration
* @return this TimeoutLayer for chaining
*/
public TimeoutLayer withTimeout(Duration timeout) {
return new TimeoutLayer(timeout, this.ioTimeout);
}

/**
* Set timeout for IO operations.
*
* @param ioTimeout the IO timeout duration
* @return this TimeoutLayer for chaining
*/
public TimeoutLayer withIoTimeout(Duration ioTimeout) {
return new TimeoutLayer(this.timeout, ioTimeout);
}

@Override
protected long layer(long nativeOp) {
return doLayer(nativeOp, timeout.toNanos(), ioTimeout.toNanos());
}

private static native long doLayer(long nativeHandle, long timeout, long ioTimeout);
}
Loading