You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ib...@apache.org on 2021/01/28 12:28:35 UTC

[ignite-3] branch main updated: IGNITE-14062 Basic classes and interfaces for traversable configuration tree (#37)

This is an automated email from the ASF dual-hosted git repository.

ibessonov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git


The following commit(s) were added to refs/heads/main by this push:
     new b82fce8  IGNITE-14062 Basic classes and interfaces for traversable configuration tree (#37)
b82fce8 is described below

commit b82fce8dee36f1884eff87f847701c64f9b0ea88
Author: ibessonov <be...@gmail.com>
AuthorDate: Thu Jan 28 15:28:09 2021 +0300

    IGNITE-14062 Basic classes and interfaces for traversable configuration tree (#37)
    
    Signed-off-by: ibessonov <be...@gmail.com>
---
 .../configuration/tree/ConfigurationVisitor.java   |  49 ++++++++++
 .../ignite/configuration/tree/InnerNode.java       |  91 ++++++++++++++++++
 .../ignite/configuration/tree/NamedListChange.java |  43 +++++++++
 .../ignite/configuration/tree/NamedListNode.java   | 107 +++++++++++++++++++++
 .../ignite/configuration/tree/NamedListView.java   |  36 +++++++
 .../configuration/tree/TraversableTreeNode.java    |  27 ++++++
 6 files changed, 353 insertions(+)

diff --git a/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/ConfigurationVisitor.java b/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/ConfigurationVisitor.java
new file mode 100644
index 0000000..98db039
--- /dev/null
+++ b/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/ConfigurationVisitor.java
@@ -0,0 +1,49 @@
+/*
+ * 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.ignite.configuration.tree;
+
+import java.io.Serializable;
+
+/**
+ * Configuration visitor - callback interface to traverse configuration tree.
+ */
+public interface ConfigurationVisitor {
+    /**
+     * Invoked on visiting leaf node.
+     *
+     * @param key Name of the serializable value retrieved from its holder object.
+     * @param val Configuration value.
+     */
+    void visitLeafNode(String key, Serializable val);
+
+    /**
+     * Invoked on visiting regular inner node.
+     *
+     * @param key Name of the node retrieved from its holder object.
+     * @param node Inner configuration node.
+     */
+    void visitInnerNode(String key, InnerNode node);
+
+    /**
+     * Invoked on visiting named list nodes.
+     *
+     * @param key Name of the node retrieved from its holder object.
+     * @param node Named list inner configuration node.
+     */
+    <N extends TraversableTreeNode> void visitNamedListNode(String key, NamedListNode<N> node);
+}
diff --git a/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/InnerNode.java b/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/InnerNode.java
new file mode 100644
index 0000000..1da138b
--- /dev/null
+++ b/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/InnerNode.java
@@ -0,0 +1,91 @@
+/*
+ * 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.ignite.configuration.tree;
+
+import java.util.NoSuchElementException;
+
+/** */
+public abstract class InnerNode implements TraversableTreeNode, Cloneable {
+    /** {@inheritDoc} */
+    @Override public final void accept(String key, ConfigurationVisitor visitor) {
+        visitor.visitInnerNode(key, this);
+    }
+
+    /**
+     * Method with auto-generated implementation. Must look like this:
+     * <pre>{@code
+     * @Override public void traverseChildren(ConfigurationVisitor visitor) {
+     *     this.pojoField1.accept("pojoField1", visitor);
+     *
+     *     this.pojoField2.accept("pojoField2", visitor);
+     *
+     *     visitor.visitLeafNode("primitiveField1", this.primitiveField1);
+     *
+     *     visitor.visitLeafNode("primitiveField2", this.primitiveField2);
+     * }
+     * }</pre>
+     *
+     * Order of fields must be the same as they are described in configuration schema.
+     *
+     * @param visitor Configuration visitor.
+     */
+    public abstract void traverseChildren(ConfigurationVisitor visitor);
+
+    /**
+     * Method with auto-generated implementation. Must look like this:
+     * <pre>{@code
+     * @Override public void traverseChild(String key, ConfigurationVisitor visitor) throws NoSuchElementException {
+     *     switch (key) {
+     *         case "pojoField1":
+     *             this.pojoField1.accept("pojoField1", visitor);
+     *             break;
+     *
+     *         case "pojoField2":
+     *             this.pojoField2.accept("pojoField2", visitor);
+     *             break;
+     *
+     *         case "primitiveField1":
+     *             visitor.visitLeafNode("primitiveField1", this.primitiveField1);
+     *             break;
+     *
+     *         case "primitiveField2":
+     *             visitor.visitLeafNode("primitiveField2", this.primitiveField2);
+     *             break;
+     *
+     *         default:
+     *             throw new NoSuchElementException(key);
+     *     }
+     * }
+     * }</pre>
+     *
+     * @param key Name of the child.
+     * @param visitor Configuration visitor.
+     * @throws NoSuchElementException If field {@code key} is not found.
+     */
+    public abstract void traverseChild(String key, ConfigurationVisitor visitor) throws NoSuchElementException;
+
+    /** {@inheritDoc} */
+    @Override protected Object clone() {
+        try {
+            return super.clone();
+        }
+        catch (CloneNotSupportedException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+}
diff --git a/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/NamedListChange.java b/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/NamedListChange.java
new file mode 100644
index 0000000..b386718
--- /dev/null
+++ b/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/NamedListChange.java
@@ -0,0 +1,43 @@
+/*
+ * 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.ignite.configuration.tree;
+
+import java.util.function.Consumer;
+
+/** */
+public interface NamedListChange<T> {
+    /**
+     * Update the value in named list configuration.
+     *
+     * @param key Key for the value to be updated.
+     * @param valConsumer Closure to modify value associated with the key. Object of type {@code T},
+     *      passed to the closure, must not be reused anywhere else.
+     *
+     * @throws IllegalStateException If {@link #remove(String)} has been invoked with the same key previously.
+     */
+    NamedListChange<T> put(String key, Consumer<T> valConsumer) throws IllegalStateException;
+
+    /**
+     * Remove the value from named list configuration.
+     *
+     * @param key Key for the value to be removed.
+     *
+     * @throws IllegalStateException If {@link #put(String, Consumer)} has been invoked with the same key previously.
+     */
+    NamedListChange<T> remove(String key) throws IllegalStateException;
+}
diff --git a/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/NamedListNode.java b/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/NamedListNode.java
new file mode 100644
index 0000000..c4d6ab4
--- /dev/null
+++ b/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/NamedListNode.java
@@ -0,0 +1,107 @@
+/*
+ * 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.ignite.configuration.tree;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+
+/** */
+public final class NamedListNode<N extends TraversableTreeNode> implements NamedListView<N>, NamedListChange<N>, TraversableTreeNode, Cloneable {
+    /** */
+    private final Supplier<N> valSupplier;
+
+    /** */
+    private final Map<String, N> map;
+
+    /**
+     * Default constructor.
+     *
+     * @param valSupplier Closure to instantiate values.
+     */
+    public NamedListNode(Supplier<N> valSupplier) {
+        this.valSupplier = valSupplier;
+        map = new HashMap<>();
+    }
+
+    /**
+     * Copy constructor.
+     *
+     * @param node Other node.
+     */
+    private NamedListNode(NamedListNode<N> node) {
+        valSupplier = node.valSupplier;
+        map = new HashMap<>(node.map);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void accept(String key, ConfigurationVisitor visitor) {
+        visitor.visitNamedListNode(key, this);
+    }
+
+    /** {@inheritDoc} */
+    @Override public final Set<String> namedListKeys() {
+        return Collections.unmodifiableSet(map.keySet());
+    }
+
+    /** {@inheritDoc} */
+    @Override public final N get(String key) {
+        return map.get(key);
+    }
+
+    /** {@inheritDoc} */
+    @Override public final NamedListChange<N> put(String key, Consumer<N> valConsumer) {
+        Objects.requireNonNull(valConsumer, "valConsumer");
+
+        if (map.containsKey(key) && map.get(key) == null)
+            throw new IllegalStateException("You can't add entity that has just been deleted [key=" + key + ']');
+
+        N val = map.get(key);
+
+        if (val == null)
+            map.put(key, val = valSupplier.get());
+
+        valConsumer.accept(val);
+
+        return this;
+    }
+
+    /** {@inheritDoc} */
+    @Override public NamedListChange<N> remove(String key) {
+        if (map.containsKey(key) && map.get(key) != null)
+            throw new IllegalStateException("You can't add entity that has just been modified [key=" + key + ']');
+
+        map.put(key, null);
+
+        return this;
+    }
+
+    /** */
+    public void delete(String key) {
+        map.remove(key);
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object clone() {
+        return new NamedListNode<>(this);
+    }
+}
diff --git a/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/NamedListView.java b/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/NamedListView.java
new file mode 100644
index 0000000..6914df8
--- /dev/null
+++ b/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/NamedListView.java
@@ -0,0 +1,36 @@
+/*
+ * 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.ignite.configuration.tree;
+
+import java.util.Set;
+
+/** */
+public interface NamedListView<T> {
+    /**
+     * @return Immutable collection of keys contained within this list.
+     */
+    Set<String> namedListKeys();
+
+    /**
+     * Returns value associated with the passed key.
+     *
+     * @param key Key string.
+     * @return Requested value or {@code null} if it's not found.
+     */
+    T get(String key);
+}
diff --git a/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/TraversableTreeNode.java b/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/TraversableTreeNode.java
new file mode 100644
index 0000000..c234fa9
--- /dev/null
+++ b/modules/configuration/src/main/java/org/apache/ignite/configuration/tree/TraversableTreeNode.java
@@ -0,0 +1,27 @@
+/*
+ * 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.ignite.configuration.tree;
+
+/** */
+public interface TraversableTreeNode {
+    /**
+     * @param key Name of the node retrieved from its holder object.
+     * @param visitor Configuration visitor.
+     */
+    void accept(String key, ConfigurationVisitor visitor);
+}