killbill-uncached

Improve catalog perf for Mutable catalog apis by remove Iterables

9/29/2016 12:03:08 AM

Details

diff --git a/catalog/src/main/java/org/killbill/billing/catalog/CatalogUpdater.java b/catalog/src/main/java/org/killbill/billing/catalog/CatalogUpdater.java
index dd0dcfd..fcfe43c 100644
--- a/catalog/src/main/java/org/killbill/billing/catalog/CatalogUpdater.java
+++ b/catalog/src/main/java/org/killbill/billing/catalog/CatalogUpdater.java
@@ -44,10 +44,6 @@ import org.killbill.billing.catalog.rules.DefaultCasePriceList;
 import org.killbill.billing.catalog.rules.DefaultPlanRules;
 import org.killbill.xmlloader.XMLWriter;
 
-import com.google.common.base.Predicate;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Iterables;
-
 public class CatalogUpdater {
 
     private static URI DUMMY_URI;
@@ -253,15 +249,13 @@ public class CatalogUpdater {
 
     private boolean isCurrencySupported(final Currency targetCurrency) {
         if (catalog.getCurrentSupportedCurrencies() != null) {
-            return Iterables.any(ImmutableList.copyOf(catalog.getCurrentSupportedCurrencies()), new Predicate<Currency>() {
-                @Override
-                public boolean apply(final Currency input) {
-                    return input.equals(targetCurrency);
+            for (final Currency input : catalog.getCurrentSupportedCurrencies()) {
+                if (input.equals(targetCurrency)) {
+                    return true;
                 }
-            });
-        } else {
-            return false;
+            }
         }
+        return false;
     }
 
     private void validateNewPlanDescriptor(final SimplePlanDescriptor desc) throws CatalogApiException {
@@ -285,21 +279,21 @@ public class CatalogUpdater {
     }
 
     private DefaultProduct getExistingProduct(final String productName) {
-        return Iterables.tryFind(ImmutableList.copyOf(catalog.getCurrentProducts()), new Predicate<DefaultProduct>() {
-            @Override
-            public boolean apply(final DefaultProduct input) {
-                return input.getName().equals(productName);
+        for (final DefaultProduct input : catalog.getCurrentProducts()) {
+            if (input.getName().equals(productName)) {
+                return input;
             }
-        }).orNull();
+        }
+        return null;
     }
 
     private DefaultPlan getExistingPlan(final String planName) {
-        return Iterables.tryFind(ImmutableList.copyOf(catalog.getCurrentPlans()), new Predicate<DefaultPlan>() {
-            @Override
-            public boolean apply(final DefaultPlan input) {
-                return input.getName().equals(planName);
+        for (final DefaultPlan input : catalog.getCurrentPlans()) {
+            if (input.getName().equals(planName)) {
+                return input;
             }
-        }).orNull();
+        }
+        return null;
     }
 
     private DefaultPlanRules getSaneDefaultPlanRules(final DefaultPriceList defaultPriceList) {
diff --git a/catalog/src/main/java/org/killbill/billing/catalog/DefaultMutableStaticCatalog.java b/catalog/src/main/java/org/killbill/billing/catalog/DefaultMutableStaticCatalog.java
index 5aabf76..8e31268 100644
--- a/catalog/src/main/java/org/killbill/billing/catalog/DefaultMutableStaticCatalog.java
+++ b/catalog/src/main/java/org/killbill/billing/catalog/DefaultMutableStaticCatalog.java
@@ -18,27 +18,19 @@
 package org.killbill.billing.catalog;
 
 import java.lang.reflect.Array;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
-import javax.annotation.Nullable;
-
 import org.killbill.billing.catalog.api.CatalogApiException;
 import org.killbill.billing.catalog.api.CatalogEntity;
 import org.killbill.billing.catalog.api.Currency;
-import org.killbill.billing.catalog.api.InternationalPrice;
 import org.killbill.billing.catalog.api.MutableStaticCatalog;
 import org.killbill.billing.catalog.api.Plan;
-import org.killbill.billing.catalog.api.PlanPhasePriceOverridesWithCallContext;
-import org.killbill.billing.catalog.api.PlanSpecifier;
 import org.killbill.billing.catalog.api.Price;
 import org.killbill.billing.catalog.api.PriceList;
 import org.killbill.billing.catalog.api.Product;
 
-import com.google.common.base.Predicate;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Iterables;
-
 public class DefaultMutableStaticCatalog extends StandaloneCatalog implements MutableStaticCatalog {
 
     public DefaultMutableStaticCatalog() {
@@ -61,10 +53,9 @@ public class DefaultMutableStaticCatalog extends StandaloneCatalog implements Mu
         initialize(this, null);
     }
 
-
     @Override
     public void addCurrency(final Currency currency) throws CatalogApiException {
-        final Currency [] newEntries = allocateNewEntries(getCurrentSupportedCurrencies(), currency);
+        final Currency[] newEntries = allocateNewEntries(getCurrentSupportedCurrencies(), currency);
         setSupportedCurrencies(newEntries);
     }
 
@@ -79,28 +70,25 @@ public class DefaultMutableStaticCatalog extends StandaloneCatalog implements Mu
         final Plan[] newEntries = allocateNewEntries(getCurrentPlans(), plan);
         setPlans((DefaultPlan[]) newEntries);
 
-
         final DefaultPriceList priceList = getPriceLists().findPriceListFrom(plan.getPriceListName());
-
-        final Iterable<DefaultPlan> newPriceListPlan = Iterables.filter(ImmutableList.copyOf((DefaultPlan[]) newEntries), new Predicate<DefaultPlan>() {
-            @Override
-            public boolean apply(final DefaultPlan input) {
-                if (plan.getName().equals(input.getName())) {
-                    return true;
-                }
-                if (priceList.getPlans() != null) {
-                    for (final Plan priceListPlan : priceList.getPlans()) {
-                        if (priceListPlan.getName().equals(input.getName())) {
-                            return true;
-                        }
+        final List<DefaultPlan> newPriceListPlan = new ArrayList<DefaultPlan>();
+        for (Plan input : newEntries) {
+            if (plan.getName().equals(input.getName())) {
+                newPriceListPlan.add((DefaultPlan) plan);
+                continue;
+            }
+            if (priceList.getPlans() != null) {
+                for (final Plan priceListPlan : priceList.getPlans()) {
+                    if (priceListPlan.getName().equals(input.getName())) {
+                        newPriceListPlan.add((DefaultPlan) priceListPlan);
+                        break;
                     }
                 }
-                return false;
             }
-        });
-        final List<DefaultPlan> foo = ImmutableList.<DefaultPlan >copyOf(newPriceListPlan);
-        final Plan[] newPriceListEntries = new DefaultPlan[foo.size()];
-        final Plan[] bar = foo.toArray(newPriceListEntries);
+        }
+
+        final Plan[] newPriceListEntries = new DefaultPlan[newPriceListPlan.size()];
+        final Plan[] bar = newPriceListPlan.toArray(newPriceListEntries);
         priceList.setPlans((DefaultPlan[]) bar);
     }
 
@@ -111,10 +99,9 @@ public class DefaultMutableStaticCatalog extends StandaloneCatalog implements Mu
         setPriceLists(priceListSet);
     }
 
-
     public void addRecurringPriceToPlan(final DefaultInternationalPrice currentPrices, final Price newPrice) throws CatalogApiException {
-        final Price [] newEntries = allocateNewEntries(currentPrices.getPrices(), newPrice);
-        currentPrices.setPrices((DefaultPrice []) newEntries);
+        final Price[] newEntries = allocateNewEntries(currentPrices.getPrices(), newPrice);
+        currentPrices.setPrices((DefaultPrice[]) newEntries);
     }
 
     public void addProductAvailableAO(final DefaultProduct targetBasePlan, final DefaultProduct aoProduct) throws CatalogApiException {
@@ -122,30 +109,31 @@ public class DefaultMutableStaticCatalog extends StandaloneCatalog implements Mu
         targetBasePlan.setAvailable((DefaultProduct[]) newEntries);
     }
 
-    private <T> T [] allocateNewEntries(final T [] existingEntries, final T newEntry) throws CatalogApiException  {
+    private <T> T[] allocateNewEntries(final T[] existingEntries, final T newEntry) throws CatalogApiException {
 
-        // Verify entry does not already exists
-        if (existingEntries != null && Iterables.any(ImmutableList.<T>copyOf(existingEntries), new Predicate<T>() {
-            @Override
-            public boolean apply(final T input) {
+        if (existingEntries != null) {
+            for (T input : existingEntries) {
+                boolean found;
                 if (input instanceof CatalogEntity) {
-                    return ((CatalogEntity) input).getName().equals(((CatalogEntity) newEntry).getName());
+                    found = ((CatalogEntity) input).getName().equals(((CatalogEntity) newEntry).getName());
                 } else if (input instanceof Enum) {
-                    return ((Enum) input).name().equals(((Enum) newEntry).name());
+                    found = ((Enum) input).name().equals(((Enum) newEntry).name());
                 } else if (input instanceof Price) {
-                    return ((Price) input).getCurrency().equals(((Price) newEntry).getCurrency());
+                    found = ((Price) input).getCurrency().equals(((Price) newEntry).getCurrency());
+                } else {
+                    throw new IllegalStateException("Unexpected type " + newEntry.getClass());
+                }
+                if (found) {
+                    //throw new CatalogApiException();
+                    throw new IllegalStateException("Already existing " + newEntry);
                 }
-                throw new IllegalStateException("Unexpected type " + newEntry.getClass());
             }
-        })) {
-            //throw new CatalogApiException();
-            throw new IllegalStateException("Already existing " + newEntry);
         }
 
         // Realloc and assign new entry
         final int length = existingEntries != null ? existingEntries.length : 0;
-        final T [] newEntries = (T[]) Array.newInstance(newEntry.getClass(), length + 1);
-        for (int i = 0 ; i < newEntries.length + 1; i++) {
+        final T[] newEntries = (T[]) Array.newInstance(newEntry.getClass(), length + 1);
+        for (int i = 0; i < newEntries.length + 1; i++) {
             if (i < newEntries.length - 1) {
                 newEntries[i] = existingEntries[i];
             } else {