Skip to content

Commit 149fe64

Browse files
committed
Added TorcHelper methods for supporting gremlin-less queries.
1 parent 637ed45 commit 149fe64

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/main/java/net/ellitron/torc/util/TorcHelper.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,64 @@ public static List<TorcVertex> neighborList(
339339

340340
return list;
341341
}
342+
343+
/**
344+
* Take two maps, map1: a to b, map2: b to c, and return map3: a to c.
345+
*
346+
* @param a First map
347+
* @param b Second map
348+
*
349+
* @return Joined map
350+
*/
351+
public static Map<TorcVertex, List<TorcVertex>> fuse(
352+
Map<TorcVertex, List<TorcVertex>> a,
353+
Map<TorcVertex, List<TorcVertex>> b) {
354+
Map<TorcVertex, List<TorcVertex>> fusedMap = new HashMap<>(a.size());
355+
356+
for (Map.Entry e : a.entrySet()) {
357+
TorcVertex aVertex = (TorcVertex)e.getKey();
358+
List<TorcVertex> aVertexList = (List<TorcVertex>)e.getValue();
359+
360+
List<TorcVertex> fusedList = new ArrayList<>();
361+
for (TorcVertex v : aVertexList) {
362+
if (b.containsKey(v)) {
363+
List<TorcVertex> bVertexList = b.get(v);
364+
fusedList.addAll(bVertexList);
365+
}
366+
}
367+
368+
if (fusedList.size() > 0) {
369+
fusedMap.put(aVertex, fusedList);
370+
}
371+
}
372+
373+
return fusedMap;
374+
}
375+
376+
/**
377+
* Intersects the values in the map with the values in the list.
378+
* If the resulting value is an empty list, then remove the key from the map.
379+
* The resulting map will never have emtpy list values.
380+
*
381+
* @param a Map to intersect values on.
382+
* @param b Values to intersect map values with.
383+
*/
384+
public void intersect(
385+
Map<TorcVertex, List<TorcVertex>> a,
386+
List<TorcVertex> b) {
387+
for (Map.Entry e : a.entrySet()) {
388+
List<TorcVertex> aVertexList = (List<TorcVertex>)e.getValue();
389+
aVertexList.retainAll(b);
390+
if (aVertexList.size() == 0) {
391+
a.remove(e.getKey());
392+
}
393+
}
394+
}
395+
396+
public List<TorcVertex> keylist(
397+
Map<TorcVertex, List<TorcVertex>> a) {
398+
List<TorcVertex> keylist = new ArrayList<TorcVertex>(a.size());
399+
keylist.addAll(a.keySet());
400+
return keylist;
401+
}
342402
}

0 commit comments

Comments
 (0)