From dc9c228e4a3c2c233bd971cfd2a9c02e911d489f Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Sun, 22 Oct 2017 22:04:20 -0400 Subject: [PATCH] WIP: flattenFacets --- .../net/helenus/core/AbstractUnitOfWork.java | 23 +++++++++++++++++-- .../net/helenus/core/cache/CacheUtil.java | 4 +++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/helenus/core/AbstractUnitOfWork.java b/src/main/java/net/helenus/core/AbstractUnitOfWork.java index 89b9f26..8779a75 100644 --- a/src/main/java/net/helenus/core/AbstractUnitOfWork.java +++ b/src/main/java/net/helenus/core/AbstractUnitOfWork.java @@ -16,6 +16,7 @@ package net.helenus.core; import java.util.*; +import java.util.stream.Collectors; import com.diffplug.common.base.Errors; import com.google.common.base.Stopwatch; @@ -24,6 +25,7 @@ import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; import com.google.common.collect.TreeTraverser; +import net.helenus.core.cache.CacheUtil; import net.helenus.core.cache.Facet; /** Encapsulates the concept of a "transaction" as a unit-of-work. */ @@ -98,7 +100,7 @@ public abstract class AbstractUnitOfWork implements UnitOfW // Be sure to check all enclosing UnitOfWork caches as well, we may be nested. if (parent != null) { return parent.cacheLookup(facets); - }/* else { + } else { Cache cache = session.getSessionCache(); String[] keys = flattenFacets(facets); for (String key : keys) { @@ -108,7 +110,7 @@ public abstract class AbstractUnitOfWork implements UnitOfW break; } } - }*/ + } } return result; } @@ -123,6 +125,23 @@ public abstract class AbstractUnitOfWork implements UnitOfW } } + private String[] flattenFacets(List facets) { + Facet table = facets.remove(0); + String tableName = table.value().toString(); + + List combinations = CacheUtil.combinations(facets.stream() + .map(facet -> { + return facet.name() + "==" + facet.value(); + }).collect(Collectors.toList()).toArray(new String[facets.size()])); + + int i = 0; + String[] results = new String[facets.size()]; + for (String[] combination : combinations) { + results[i++] = tableName + "." + combination; + } + return results; + } + private Iterator> getChildNodes() { return nested.iterator(); } diff --git a/src/main/java/net/helenus/core/cache/CacheUtil.java b/src/main/java/net/helenus/core/cache/CacheUtil.java index ef26818..89f4769 100644 --- a/src/main/java/net/helenus/core/cache/CacheUtil.java +++ b/src/main/java/net/helenus/core/cache/CacheUtil.java @@ -5,7 +5,8 @@ import java.util.Arrays; import java.util.List; public class CacheUtil { - public static List comb(String... items) { + + public static List combinations(String... items) { int n = items.length; if (n > 20 || n < 0) throw new IllegalArgumentException(n + " is out of range"); long e = Math.round(Math.pow(2, n)); @@ -27,4 +28,5 @@ public class CacheUtil { } } } + }