You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by GitBox <gi...@apache.org> on 2019/10/24 15:50:02 UTC

[GitHub] [incubator-daffodil] olabusayoT commented on a change in pull request #279: WIP: User Defined Functions feature

olabusayoT commented on a change in pull request #279: WIP: User Defined Functions feature
URL: https://github.com/apache/incubator-daffodil/pull/279#discussion_r338654131
 
 

 ##########
 File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/udf/UserDefinedFunctionService.scala
 ##########
 @@ -0,0 +1,271 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.daffodil.udf
+
+import collection.JavaConverters._
+import collection.mutable._;
+import java.util.ServiceLoader
+import java.util.ServiceConfigurationError
+import org.apache.daffodil.util.Misc
+import org.apache.daffodil.util.LogLevel
+import org.apache.daffodil.util.Logging
+import java.lang.reflect.Method
+import org.apache.daffodil.dpath.NodeInfo
+import java.io.ObjectOutputStream
+import java.io.Serializable
+import java.io.ObjectInputStream
+
+/**
+ * Loads, validates and caches (for use at schema compile time) all User Defined Functions
+ * and their Providers from the classpath.
+ *
+ */
+object UserDefinedFunctionService extends Logging {
+  lazy val evaluateMethodName = "evaluate"
+
+  /**
+   * Class to make the evaluate Method serializable and to optimize our reflection lookups
+   * to happen once (per call) during deserialization, rather than at evaluation time.
+   */
+  case class UserDefinedFunctionMethod(
+      val decClass: Class[_],
+      val methodName: String,
+      val paramTypes: Array[Class[_]])
+      extends Serializable {
+
+    @transient lazy val method: Method = {
+      lookupMethod
+    }
+
+    def lookupMethod() = {
+     val m = decClass.getMethod(methodName, paramTypes: _*)
+     m
+    }
+
+    @throws(classOf[java.io.IOException])
+    private def writeObject(out: ObjectOutputStream) : Unit = {
+      out.defaultWriteObject()
+    }
+
+    @throws(classOf[java.io.IOException])
+    @throws(classOf[java.lang.ClassNotFoundException])
+    private def readObject(in: ObjectInputStream): Unit = {
+      in.defaultReadObject()
+      this.method
+    }
+  }
 
 Review comment:
   Hmm, if we force the lookup at compile time (i.e in the constructor), then we'll do a lookup for every UDF found on the classpath. Even if it's never used. As it stands right now, we only do a lookup at deserialization, and even then only for the UDFs in the schema.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services