You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sqoop.apache.org by ab...@apache.org on 2014/10/10 04:52:01 UTC

[32/52] [abbrv] git commit: SQOOP-1479: Sqoop2: Validations: Cache Validator objects in ValidationRunner

SQOOP-1479: Sqoop2: Validations: Cache Validator objects in ValidationRunner

(Jarek Jarcec Cecho via Abraham Elmahrek)


Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/e540668b
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/e540668b
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/e540668b

Branch: refs/heads/SQOOP-1367
Commit: e540668b5569753165d812522e1c5bc24cc1fe5b
Parents: c1e53e5
Author: Abraham Elmahrek <ab...@elmahrek.com>
Authored: Thu Oct 2 21:26:41 2014 -0700
Committer: Abraham Elmahrek <ab...@elmahrek.com>
Committed: Thu Oct 9 17:59:25 2014 -0700

----------------------------------------------------------------------
 .../sqoop/validation/ValidationError.java       |  3 ++
 .../sqoop/validation/ValidationRunner.java      | 35 ++++++++++++++++++--
 2 files changed, 35 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/e540668b/common/src/main/java/org/apache/sqoop/validation/ValidationError.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/validation/ValidationError.java b/common/src/main/java/org/apache/sqoop/validation/ValidationError.java
index 264d6e3..ec64f10 100644
--- a/common/src/main/java/org/apache/sqoop/validation/ValidationError.java
+++ b/common/src/main/java/org/apache/sqoop/validation/ValidationError.java
@@ -31,6 +31,9 @@ public enum ValidationError implements ErrorCode {
   VALIDATION_0002("Usage of missing field"),
 
   VALIDATION_0003("Invalid representation of form and input field"),
+
+  VALIDATION_0004("Can't find validator class"),
+
   ;
 
   private final String message;

http://git-wip-us.apache.org/repos/asf/sqoop/blob/e540668b/common/src/main/java/org/apache/sqoop/validation/ValidationRunner.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/validation/ValidationRunner.java b/common/src/main/java/org/apache/sqoop/validation/ValidationRunner.java
index f36faf2..8ffc0d4 100644
--- a/common/src/main/java/org/apache/sqoop/validation/ValidationRunner.java
+++ b/common/src/main/java/org/apache/sqoop/validation/ValidationRunner.java
@@ -17,6 +17,7 @@
  */
 package org.apache.sqoop.validation;
 
+import org.apache.sqoop.common.SqoopException;
 import org.apache.sqoop.model.ConfigurationClass;
 import org.apache.sqoop.model.Form;
 import org.apache.sqoop.model.FormClass;
@@ -27,6 +28,8 @@ import org.apache.sqoop.utils.ClassUtils;
 import org.apache.sqoop.validation.validators.AbstractValidator;
 
 import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * Validation runner that will run validators associated with given configuration
@@ -39,12 +42,23 @@ import java.lang.reflect.Field;
  * Which means that form validator don't have to repeat it's input validators as it will
  * be never called if the input's are not valid. Similarly Class validators won't be called
  * unless all forms will pass validators.
- *
- * TODO: Cache the validators instances, so that we don't have create new instance every time
  */
 public class ValidationRunner {
 
   /**
+   * Private cache of instantiated validators.
+   *
+   * We're expecting that this cache will be very small as the number of possible validators
+   * is driven to high extent by the number of connectors and hence we don't have a cache
+   * eviction at the moment.
+   */
+  private Map<Class<? extends AbstractValidator>, AbstractValidator> cache;
+
+  public ValidationRunner() {
+    cache = new HashMap<Class<? extends AbstractValidator>, AbstractValidator>();
+  }
+
+  /**
    * Validate given configuration instance.
    *
    * @param config Configuration instance
@@ -137,7 +151,22 @@ public class ValidationRunner {
    * @return
    */
   private AbstractValidator executeValidator(Object object, Validator validator) {
-    AbstractValidator instance = (AbstractValidator) ClassUtils.instantiate(validator.value());
+    // Try to get validator instance from the cache
+    AbstractValidator instance = cache.get(validator.value());
+
+    if(instance == null) {
+      instance = (AbstractValidator) ClassUtils.instantiate(validator.value());
+
+      // This could happen if we would be missing some connector's jars on our classpath
+      if(instance == null) {
+        throw new SqoopException(ValidationError.VALIDATION_0004, validator.value().getName());
+      }
+
+      cache.put(validator.value(), instance);
+    } else {
+      instance.reset();
+    }
+
     instance.setStringArgument(validator.strArg());
     instance.validate(object);
     return instance;