|
| 1 | +package com.adaworldapi.interop.tinkerpop; |
| 2 | + |
| 3 | +import com.adaworldapi.lancegraph.RowStore; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.concurrent.atomic.AtomicLong; |
| 9 | + |
| 10 | +import org.apache.tinkerpop.gremlin.process.traversal.Step; |
| 11 | +import org.apache.tinkerpop.gremlin.process.traversal.Traversal; |
| 12 | +import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy.ProviderOptimizationStrategy; |
| 13 | +import org.apache.tinkerpop.gremlin.process.traversal.step.HasContainerHolder; |
| 14 | +import org.apache.tinkerpop.gremlin.process.traversal.step.filter.DedupGlobalStep; |
| 15 | +import org.apache.tinkerpop.gremlin.process.traversal.step.map.CountGlobalStep; |
| 16 | +import org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep; |
| 17 | +import org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep; |
| 18 | +import org.apache.tinkerpop.gremlin.process.traversal.step.util.EmptyStep; |
| 19 | +import org.apache.tinkerpop.gremlin.process.traversal.step.map.NoOpBarrierStep; |
| 20 | +import org.apache.tinkerpop.gremlin.process.traversal.strategy.AbstractTraversalStrategy; |
| 21 | +import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper; |
| 22 | +import org.apache.tinkerpop.gremlin.structure.Direction; |
| 23 | +import org.apache.tinkerpop.gremlin.structure.Vertex; |
| 24 | + |
| 25 | +/** |
| 26 | + * Quick win A2: a TinkerPop provider strategy that lowers one traversal shape onto the |
| 27 | + * lance-graph-java mask hop chain, and leaves every other traversal to Gremlin. |
| 28 | + * |
| 29 | + * <p>The lowered shape is exactly |
| 30 | + * |
| 31 | + * <pre>{@code g.V(ids).out(label)^k.dedup().count()}</pre> |
| 32 | + * |
| 33 | + * with k >= 1, one mapped edge label per hop, and no {@code has()} folded into the start |
| 34 | + * step. {@code repeat(out(label)).times(k)} reaches the same shape, because TinkerPop's |
| 35 | + * {@code RepeatUnrollStrategy} unrolls it before provider strategies run. A1 proved the |
| 36 | + * {@code dedup()} form equal to the mask chain; the form without {@code dedup()} counts walks, |
| 37 | + * not vertices, and is never lowered. |
| 38 | + * |
| 39 | + * <p><strong>Every decision is counted.</strong> {@link #lowered()} counts traversals replaced |
| 40 | + * by a {@link LanceHopCountStep}; {@link #fellBack()} counts root traversals left to Gremlin, |
| 41 | + * with the first reason kept in {@link #lastFallbackReason()}. A fallback is never an error: |
| 42 | + * Gremlin still answers, so a caller only loses speed, never correctness. |
| 43 | + * |
| 44 | + * <p>The strategy reads the row store it was given; the graph the traversal source wraps is not |
| 45 | + * consulted for a lowered traversal. Keeping the two consistent is the caller's job (in the |
| 46 | + * harness both are built from the same fixture). Vertex ids are row numbers. |
| 47 | + */ |
| 48 | +public final class LanceHopStrategy |
| 49 | + extends AbstractTraversalStrategy<ProviderOptimizationStrategy> |
| 50 | + implements ProviderOptimizationStrategy { |
| 51 | + |
| 52 | + private final RowStore store; |
| 53 | + private final Map<String, Integer> edgeClassids; |
| 54 | + private final AtomicLong lowered = new AtomicLong(); |
| 55 | + private final AtomicLong fellBack = new AtomicLong(); |
| 56 | + private volatile String lastFallbackReason = ""; |
| 57 | + |
| 58 | + /** |
| 59 | + * @param store the row store lowered traversals are answered from |
| 60 | + * @param edgeClassids Gremlin edge label to lance-graph edge classid; a label not in the map |
| 61 | + * is not lowered |
| 62 | + */ |
| 63 | + public LanceHopStrategy(RowStore store, Map<String, Integer> edgeClassids) { |
| 64 | + this.store = store; |
| 65 | + this.edgeClassids = Map.copyOf(edgeClassids); |
| 66 | + } |
| 67 | + |
| 68 | + /** Number of traversals answered by the mask hop chain. */ |
| 69 | + public long lowered() { |
| 70 | + return lowered.get(); |
| 71 | + } |
| 72 | + |
| 73 | + /** Number of root traversals left to Gremlin. */ |
| 74 | + public long fellBack() { |
| 75 | + return fellBack.get(); |
| 76 | + } |
| 77 | + |
| 78 | + /** Why the most recent fallback happened; empty if none has. */ |
| 79 | + public String lastFallbackReason() { |
| 80 | + return lastFallbackReason; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void apply(Traversal.Admin<?, ?> traversal) { |
| 85 | + // Child traversals (inside by(), repeat() bodies, ...) are not candidates; only the |
| 86 | + // root is, and only the root is counted, so one query counts once. |
| 87 | + if (!(traversal.getParent() instanceof EmptyStep)) { |
| 88 | + return; |
| 89 | + } |
| 90 | + String reason = plan(traversal); |
| 91 | + if (reason != null) { |
| 92 | + fellBack.incrementAndGet(); |
| 93 | + lastFallbackReason = reason; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** Lowers the traversal and returns null, or leaves it untouched and returns the reason. */ |
| 98 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 99 | + private String plan(Traversal.Admin<?, ?> traversal) { |
| 100 | + List<Step> steps = new ArrayList<>(); |
| 101 | + for (Step s : traversal.getSteps()) { |
| 102 | + if (!(s instanceof NoOpBarrierStep)) { |
| 103 | + steps.add(s); |
| 104 | + } |
| 105 | + } |
| 106 | + if (steps.size() < 4) { |
| 107 | + return "too few steps for V().out()..dedup().count()"; |
| 108 | + } |
| 109 | + if (!(steps.get(0) instanceof GraphStep<?, ?> start) || !start.returnsVertex()) { |
| 110 | + return "does not start with V()"; |
| 111 | + } |
| 112 | + if (start instanceof HasContainerHolder holder && !holder.getHasContainers().isEmpty()) { |
| 113 | + return "has() on the start step"; |
| 114 | + } |
| 115 | + long[] seeds = seedRows(start.getIds()); |
| 116 | + if (seeds == null) { |
| 117 | + return "V() needs explicit numeric row ids within the row store"; |
| 118 | + } |
| 119 | + int last = steps.size() - 1; |
| 120 | + if (!(steps.get(last) instanceof CountGlobalStep)) { |
| 121 | + return "does not end with count()"; |
| 122 | + } |
| 123 | + if (!(steps.get(last - 1) instanceof DedupGlobalStep<?> dedup) |
| 124 | + || !dedup.getLocalChildren().isEmpty() |
| 125 | + || !dedup.getScopeKeys().isEmpty()) { |
| 126 | + return "count() without a plain dedup() counts walks, not vertices"; |
| 127 | + } |
| 128 | + int[] hops = new int[last - 2]; |
| 129 | + for (int i = 1; i < last - 1; i++) { |
| 130 | + // Provider strategies run in no fixed order, so has() can still be its own step |
| 131 | + // here rather than folded into the start step. |
| 132 | + if (steps.get(i) instanceof HasContainerHolder) { |
| 133 | + return "has() at step " + i; |
| 134 | + } |
| 135 | + if (!(steps.get(i) instanceof VertexStep<?> v)) { |
| 136 | + return "step " + i + " is not out(label): " + steps.get(i); |
| 137 | + } |
| 138 | + if (v.getDirection() != Direction.OUT || !v.returnsVertex()) { |
| 139 | + return "step " + i + " is not an out() to vertices"; |
| 140 | + } |
| 141 | + if (v.getEdgeLabels().length != 1) { |
| 142 | + return "step " + i + " needs exactly one edge label"; |
| 143 | + } |
| 144 | + Integer classid = edgeClassids.get(v.getEdgeLabels()[0]); |
| 145 | + if (classid == null) { |
| 146 | + return "edge label '" + v.getEdgeLabels()[0] + "' has no lance-graph classid"; |
| 147 | + } |
| 148 | + hops[i - 1] = classid; |
| 149 | + } |
| 150 | + if (!steps.get(0).getLabels().isEmpty() || anyLabelled(steps)) { |
| 151 | + return "as() labels would be lost"; |
| 152 | + } |
| 153 | + TraversalHelper.removeAllSteps(traversal); |
| 154 | + traversal.addStep(new LanceHopCountStep(traversal, store, seeds, hops)); |
| 155 | + lowered.incrementAndGet(); |
| 156 | + return null; |
| 157 | + } |
| 158 | + |
| 159 | + @SuppressWarnings("rawtypes") |
| 160 | + private static boolean anyLabelled(List<Step> steps) { |
| 161 | + for (Step s : steps) { |
| 162 | + if (!s.getLabels().isEmpty()) { |
| 163 | + return true; |
| 164 | + } |
| 165 | + } |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | + /** Row numbers for V(ids), or null when any id is not a row of this store. */ |
| 170 | + private long[] seedRows(Object[] ids) { |
| 171 | + if (ids == null || ids.length == 0) { |
| 172 | + return null; |
| 173 | + } |
| 174 | + long rows = store.rowCount(); |
| 175 | + long[] out = new long[ids.length]; |
| 176 | + for (int i = 0; i < ids.length; i++) { |
| 177 | + Object id = ids[i] instanceof Vertex v ? v.id() : ids[i]; |
| 178 | + if (!(id instanceof Long || id instanceof Integer || id instanceof Short)) { |
| 179 | + return null; |
| 180 | + } |
| 181 | + long row = ((Number) id).longValue(); |
| 182 | + if (row < 0 || row >= rows) { |
| 183 | + return null; |
| 184 | + } |
| 185 | + out[i] = row; |
| 186 | + } |
| 187 | + return out; |
| 188 | + } |
| 189 | +} |
0 commit comments