Update QueryResult design 14/93314/4
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 23 Oct 2020 19:19:20 +0000 (21:19 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 24 Oct 2020 14:23:43 +0000 (16:23 +0200)
A query needs to result in a set of path/object tuples, as we really
need to discern matches. Having a concrete path of matches helps
getting identifiers of parent objects for subquent queries.

DOMQueryResult already contains this capability, we just need to
propagate it.

JIRA: MDSAL-604
Change-Id: Iec9ad34aac105eeb45c26a31a24611c9e0f2bb82
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-api/src/main/java/org/opendaylight/mdsal/binding/api/query/QueryExecutor.java
binding/mdsal-binding-api/src/main/java/org/opendaylight/mdsal/binding/api/query/QueryResult.java

index 490aed31f590d27c652e342812529945dbd92f06..77fbf348a26deb8c46e8a16f795ec7df1284ca2a 100644 (file)
@@ -9,9 +9,11 @@ package org.opendaylight.mdsal.binding.api.query;
 
 import com.google.common.annotations.Beta;
 import com.google.common.util.concurrent.ListenableFuture;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 
 @Beta
+@NonNullByDefault
 public interface QueryExecutor {
 
     <T extends DataObject> ListenableFuture<? extends QueryResult<T>> executeQuery(QueryExpression<T> query);
index 55b3703d5f85d62b29ee801fcfd578be4ac7c9e3..9d6e68e9c9136fd16208ced0ab88d54921c851f7 100644 (file)
@@ -7,14 +7,15 @@
  */
 package org.opendaylight.mdsal.binding.api.query;
 
-import com.google.common.annotations.Beta;
 import java.util.List;
 import java.util.Spliterator;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
 /**
  * Result of executing a {@link QueryExpression}. It is composed of one or more result values, which can be accessed via
@@ -22,8 +23,29 @@ import org.opendaylight.yangtools.yang.binding.DataObject;
  *
  * @param <T> Result object type
  */
-@Beta
+@NonNullByDefault
 public interface QueryResult<T extends DataObject> {
+    /**
+     * A single item in the result set. It is identified by its path and the corresponding object..
+     *
+     * @param <T> Result object type
+     */
+    interface Item<T extends DataObject> extends Immutable {
+        /**
+         * Return the result object.
+         *
+         * @return Result object
+         */
+        T object();
+
+        /**
+         * Return the {@link InstanceIdentifier} of the result object. This is guaranteed to be non-wildcard.
+         *
+         * @return InstanceIdentifier of the result object
+         */
+        InstanceIdentifier<T> path();
+    }
+
     /**
      * Returns a spliterator over values of the result.
      *
@@ -31,14 +53,14 @@ public interface QueryResult<T extends DataObject> {
      */
     // TODO: @throws IllegalStateException if values have been already been consumed?
     // FIXME: we really may want to wrap each entry in a CheckedValue, so that we can communicate fetch problems
-    @NonNull Spliterator<? extends T> spliterator();
+    Spliterator<? extends Item<T>> spliterator();
 
     /**
      * Returns a sequential {@link Stream} of values from the result.
      *
      * @return A stream of non-null values.
      */
-    default @NonNull Stream<? extends T> stream() {
+    default Stream<? extends Item<T>> stream() {
         return StreamSupport.stream(spliterator(), false);
     }
 
@@ -47,11 +69,15 @@ public interface QueryResult<T extends DataObject> {
      *
      * @return A stream of non-null values.
      */
-    default @NonNull Stream<? extends T> parallelStream() {
+    default Stream<? extends Item<T>> parallelStream() {
         return StreamSupport.stream(spliterator(), true);
     }
 
-    default @NonNull List<? extends T> getValues() {
+    default List<? extends T> getValues() {
+        return stream().map(Item::object).collect(Collectors.toUnmodifiableList());
+    }
+
+    default List<? extends Item<T>> getItems() {
         return stream().collect(Collectors.toUnmodifiableList());
     }
 }