You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by xy...@apache.org on 2023/01/18 19:42:54 UTC
[helix] branch metaclient updated: MetaClient - Op and OpResult Logic for transactional support
This is an automated email from the ASF dual-hosted git repository.
xyuanlu pushed a commit to branch metaclient
in repository https://gitbox.apache.org/repos/asf/helix.git
The following commit(s) were added to refs/heads/metaclient by this push:
new 9b6b08eb5 MetaClient - Op and OpResult Logic for transactional support
9b6b08eb5 is described below
commit 9b6b08eb51762aebccfabfe052c55a291379d2cd
Author: Marcos Rico Peng <55...@users.noreply.github.com>
AuthorDate: Wed Jan 18 20:42:48 2023 +0100
MetaClient - Op and OpResult Logic for transactional support
Metaclient - Op and OpResult Logic for transactional suppor
---
.../java/org/apache/helix/metaclient/api/Op.java | 106 ++++++++++++++-
.../org/apache/helix/metaclient/api/OpResult.java | 143 ++++++++++++++++++++-
2 files changed, 244 insertions(+), 5 deletions(-)
diff --git a/meta-client/src/main/java/org/apache/helix/metaclient/api/Op.java b/meta-client/src/main/java/org/apache/helix/metaclient/api/Op.java
index 0c596d877..483be92b2 100644
--- a/meta-client/src/main/java/org/apache/helix/metaclient/api/Op.java
+++ b/meta-client/src/main/java/org/apache/helix/metaclient/api/Op.java
@@ -23,16 +23,114 @@ package org.apache.helix.metaclient.api;
* Represents a single operation in a multi-operation transaction. Each operation can be a create, set,
* version check or delete operation.
*/
-public class Op {
+public abstract class Op {
+ enum Type {
+ CREATE,
+ DELETE,
+ SET,
+ CHECK
+ }
+
+ private String _path;
+ private Type _type;
+
+ private Op(Type type, String path) {
+ this._type = type;
+ this._path = path;
+ }
+ public static Op create(String path, byte[] data) {
+ return new Create(path, data);
+ }
+
+ public static Op create(String path, byte[] data, MetaClientInterface.EntryMode createMode) {
+ return new Create(path, data, createMode);
+ }
+
+ public static Op delete(String path, int version) {
+ return new Op.Delete(path, version);
+ }
+
+ public static Op set(String path, byte[] data, int version) {
+ return new Set(path, data, version);
+ }
+
+ public static Op check(String path, int version) {
+ return new Check(path, version);
+ }
+
+ public Type getType() {
+ return this._type;
+ }
+
+ public String getPath() {
+ return this._path;
+ }
+
/**
* Check the version of an entry. True only when the version is the same as expected.
*/
public static class Check extends Op {
+ private final int version;
+ public int getVersion() { return version;}
+ private Check(String path, int version) {
+ super(Type.CHECK, path);
+ this.version = version;
+ }
}
- public static class Create extends Op{
+
+ /**
+ * Represents a Create operation. Creates a new node.
+ */
+ public static class Create extends Op {
+ protected final byte[] data;
+ private MetaClientInterface.EntryMode mode;
+
+ public byte[] getData() {
+ return data;
+ }
+ public MetaClientInterface.EntryMode getEntryMode() {return mode;}
+
+ private Create(String path, byte[] data) {
+ super(Type.CREATE, path);
+ this.data = data;
+ }
+
+ private Create(String path, byte[] data, MetaClientInterface.EntryMode mode) {
+ super(Type.CREATE, path);
+ this.data = data;
+ this.mode = mode;
+ }
}
+
+ /**
+ * Represents a Delete operations. Deletes an existing node.
+ */
public static class Delete extends Op{
+ private final int version;
+ public int getVersion() { return version;}
+
+ private Delete(String path, int version) {
+ super(Type.DELETE, path);
+ this.version = version;
+ }
}
- public static class Set extends Op{
+
+ /**
+ * Represents a Set operation. Sets or updates the data of a node.
+ */
+ public static class Set extends Op {
+ private final byte[] data;
+ private final int version;
+
+ public byte[] getData() {
+ return data;
+ }
+ public int getVersion() { return version;}
+
+ private Set(String path, byte[] data, int version) {
+ super(Type.SET, path);
+ this.data = data;
+ this.version = version;
+ }
}
-}
+}
\ No newline at end of file
diff --git a/meta-client/src/main/java/org/apache/helix/metaclient/api/OpResult.java b/meta-client/src/main/java/org/apache/helix/metaclient/api/OpResult.java
index aae379df7..effed8543 100644
--- a/meta-client/src/main/java/org/apache/helix/metaclient/api/OpResult.java
+++ b/meta-client/src/main/java/org/apache/helix/metaclient/api/OpResult.java
@@ -19,8 +19,149 @@ package org.apache.helix.metaclient.api;
* under the License.
*/
+import java.util.Arrays;
+import java.util.List;
/**
* Represent the result of a single operation of a multi operation transaction.
*/
public class OpResult {
-}
+
+ enum Type {
+ ERRORRESULT,
+ GETDATARESULT,
+ GETCHILDRENRESULT,
+ CHECKRESULT,
+ SETDATARESULT,
+ DELETERESULT,
+ CREATERESULT,
+ CREATERESULT_WITH_STAT
+ }
+
+ private Type type;
+
+ private OpResult(Type type) {
+ this.type = type;
+ }
+
+ public Type getType() {
+ return this.type;
+ }
+
+ /**
+ * Represents the result of an operation that was attempted to execute but failed.
+ */
+ public static class ErrorResult extends OpResult {
+ private int err;
+
+ public ErrorResult(int err) {
+ super(Type.ERRORRESULT);
+ this.err = err;
+ }
+
+ public int getErr() {
+ return this.err;
+ }
+ }
+
+ /**
+ * Represents the result of a getData() operation.
+ */
+ public static class GetDataResult extends OpResult {
+ private byte[] data;
+ private MetaClientInterface.Stat stat;
+
+ public GetDataResult(byte[] data, MetaClientInterface.Stat stat) {
+ super(Type.GETDATARESULT);
+ this.data = data == null ? null : Arrays.copyOf(data, data.length);
+ this.stat = stat;
+ }
+
+ public byte[] getData() {
+ return this.data == null ? null : Arrays.copyOf(this.data, this.data.length);
+ }
+
+ public MetaClientInterface.Stat getStat() {
+ return this.stat;
+ }
+ }
+
+ /**
+ * Represents the result of a getChildren() operation.
+ */
+ public static class GetChildrenResult extends OpResult {
+ private List<String> children;
+
+ public GetChildrenResult(List<String> children) {
+ super(Type.GETCHILDRENRESULT);
+ this.children = children;
+ }
+
+ public List<String> getChildren() {
+ return this.children;
+ }
+ }
+
+ /**
+ * Represents the result of a check() operation.
+ */
+ public static class CheckResult extends OpResult {
+ public CheckResult() {
+ super(Type.CHECKRESULT);
+ }
+ }
+
+ /**
+ * Represents the result of a set() operation.
+ */
+ public static class SetDataResult extends OpResult {
+ private MetaClientInterface.Stat stat;
+
+ public SetDataResult(MetaClientInterface.Stat stat) {
+ super(Type.SETDATARESULT);
+ this.stat = stat;
+ }
+
+ public MetaClientInterface.Stat getStat() {
+ return this.stat;
+ }
+ }
+
+ /**
+ * Represents the result of a delete() operation.
+ */
+ public static class DeleteResult extends OpResult {
+ public DeleteResult() {
+ super(Type.DELETERESULT);
+ }
+ }
+
+ /**
+ * Represents the result of a create() operation.
+ */
+ public static class CreateResult extends OpResult {
+ private String path;
+ private MetaClientInterface.Stat stat;
+
+ public CreateResult(String path) {
+ this(Type.CREATERESULT, path, null);
+ }
+
+ public CreateResult(String path, MetaClientInterface.Stat stat) {
+ this(Type.CREATERESULT_WITH_STAT, path, stat);
+ }
+
+ private CreateResult(Type type, String path, MetaClientInterface.Stat stat) {
+ super(type);
+ this.path = path;
+ this.stat = stat;
+ }
+
+ public String getPath() {
+ return this.path;
+ }
+
+ public MetaClientInterface.Stat getStat() {
+ return this.stat;
+ }
+ }
+}
\ No newline at end of file