Skip to content

Commit ebf5758

Browse files
authored
Refactor ExclusiveOperatorEngine (#36602)
1 parent 8a16717 commit ebf5758

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

mode/core/src/main/java/org/apache/shardingsphere/mode/exclusive/ExclusiveOperationCallback.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@
1919

2020
import java.sql.SQLException;
2121

22+
/**
23+
* Exclusive operation callback.
24+
*
25+
* @param <T> type of return value
26+
*/
2227
@FunctionalInterface
23-
public interface ExclusiveOperationCallback {
28+
public interface ExclusiveOperationCallback<T> {
2429

2530
/**
2631
* Execute.
2732
*
33+
* @return execution result
2834
* @throws SQLException SQL exception
2935
*/
30-
void execute() throws SQLException;
36+
T execute() throws SQLException;
3137
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.mode.exclusive;
19+
20+
import java.sql.SQLException;
21+
22+
/**
23+
* Exclusive operation void callback.
24+
*/
25+
@FunctionalInterface
26+
public interface ExclusiveOperationVoidCallback {
27+
28+
/**
29+
* Execute.
30+
*
31+
* @throws SQLException SQL exception
32+
*/
33+
void execute() throws SQLException;
34+
}

mode/core/src/main/java/org/apache/shardingsphere/mode/exclusive/ExclusiveOperatorEngine.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,42 @@ public final class ExclusiveOperatorEngine {
3030
private final ExclusiveOperatorContext exclusiveOperatorContext;
3131

3232
/**
33-
* Operate with exclusive lock.
33+
* Operate with exclusive operation and void callback.
34+
*
35+
* @param operation exclusive operation
36+
* @param timeoutMillis timeout millis
37+
* @param voidCallback void callback
38+
* @throws SQLException SQL exception
39+
*/
40+
public void operate(final ExclusiveOperation operation, final long timeoutMillis, final ExclusiveOperationVoidCallback voidCallback) throws SQLException {
41+
operateWithResult(operation, timeoutMillis, asVoidCallback(voidCallback));
42+
}
43+
44+
private ExclusiveOperationCallback<Void> asVoidCallback(final ExclusiveOperationVoidCallback voidCallback) {
45+
return () -> {
46+
voidCallback.execute();
47+
return null;
48+
};
49+
}
50+
51+
/**
52+
* Operate with exclusive operation and return result.
3453
*
3554
* @param operation exclusive operation
3655
* @param timeoutMillis timeout millis
3756
* @param callback callback
57+
* @param <T> type of return value
58+
* @return execution result
3859
* @throws SQLException SQL exception
3960
*/
40-
public void operate(final ExclusiveOperation operation, final long timeoutMillis, final ExclusiveOperationCallback callback) throws SQLException {
61+
public <T> T operateWithResult(final ExclusiveOperation operation, final long timeoutMillis, final ExclusiveOperationCallback<T> callback) throws SQLException {
4162
if (exclusiveOperatorContext.start(operation, timeoutMillis)) {
4263
try {
43-
callback.execute();
64+
return callback.execute();
4465
} finally {
4566
exclusiveOperatorContext.stop(operation);
4667
}
4768
}
69+
return null;
4870
}
4971
}

0 commit comments

Comments
 (0)