You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by al...@apache.org on 2008/02/25 23:59:31 UTC

svn commit: r631028 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ openjpa-lib-5/src/main/java/org/apache/openjpa/lib/util/ openjpa-lib/src/main/java/org/apa...

Author: allee8285
Date: Mon Feb 25 14:59:29 2008
New Revision: 631028

URL: http://svn.apache.org/viewvc?rev=631028&view=rev
Log:
OPENJPA-359 Improve Timestamp resolution for use by versioning.

Added:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NanoPrecisionTimestampVersionStrategy.java   (with props)
    openjpa/trunk/openjpa-lib-5/src/main/java/org/apache/openjpa/lib/util/Timestamp5Helper.java   (with props)
    openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/TimestampHelper.java   (with props)
Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java?rev=631028&r1=631027&r2=631028&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java Mon Feb 25 14:59:29 2008
@@ -49,6 +49,7 @@
 import org.apache.openjpa.jdbc.meta.strats.MaxEmbeddedByteArrayFieldStrategy;
 import org.apache.openjpa.jdbc.meta.strats.MaxEmbeddedCharArrayFieldStrategy;
 import org.apache.openjpa.jdbc.meta.strats.MaxEmbeddedClobFieldStrategy;
+import org.apache.openjpa.jdbc.meta.strats.NanoPrecisionTimestampVersionStrategy;
 import org.apache.openjpa.jdbc.meta.strats.NoneClassStrategy;
 import org.apache.openjpa.jdbc.meta.strats.NoneDiscriminatorStrategy;
 import org.apache.openjpa.jdbc.meta.strats.NoneFieldStrategy;
@@ -80,6 +81,7 @@
 import org.apache.openjpa.lib.conf.Configurable;
 import org.apache.openjpa.lib.conf.Configurations;
 import org.apache.openjpa.lib.util.J2DoPrivHelper;
+import org.apache.openjpa.lib.util.JavaVersions;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.meta.ClassMetaData;
 import org.apache.openjpa.meta.FieldMetaData;
@@ -618,6 +620,8 @@
             strat = NumberVersionStrategy.class;
         else if (TimestampVersionStrategy.ALIAS.equals(name))
             strat = TimestampVersionStrategy.class;
+        else if (NanoPrecisionTimestampVersionStrategy.ALIAS.equals(name))
+            strat = NanoPrecisionTimestampVersionStrategy.class;
         else if (StateComparisonVersionStrategy.ALIAS.equals(name))
             strat = StateComparisonVersionStrategy.class;
 
@@ -1221,7 +1225,9 @@
         switch (vfield.getTypeCode()) {
             case JavaTypes.DATE:
             case JavaTypes.CALENDAR:
-                return new TimestampVersionStrategy();
+                return (JavaVersions.VERSION >= 5) 
+                    ? new NanoPrecisionTimestampVersionStrategy()
+                    : new TimestampVersionStrategy();
             case JavaTypes.BYTE:
             case JavaTypes.INT:
             case JavaTypes.LONG:

Added: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NanoPrecisionTimestampVersionStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NanoPrecisionTimestampVersionStrategy.java?rev=631028&view=auto
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NanoPrecisionTimestampVersionStrategy.java (added)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NanoPrecisionTimestampVersionStrategy.java Mon Feb 25 14:59:29 2008
@@ -0,0 +1,41 @@
+/*
+ * 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.openjpa.jdbc.meta.strats;
+
+import org.apache.openjpa.lib.util.TimestampHelper;
+
+/**
+ * Uses a timestamp for optimistic versioning with nanosecond
+ * precision.
+ *
+ * @author Albert Lee
+ */
+public class NanoPrecisionTimestampVersionStrategy
+    extends TimestampVersionStrategy {
+
+    public static final String ALIAS = "nano-timestamp";
+
+    public String getAlias() {
+        return ALIAS;
+    }
+
+    protected Object nextVersion(Object version) {
+        return TimestampHelper.getNanoPrecisionTimestamp();
+    }
+}

Propchange: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NanoPrecisionTimestampVersionStrategy.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: openjpa/trunk/openjpa-lib-5/src/main/java/org/apache/openjpa/lib/util/Timestamp5Helper.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib-5/src/main/java/org/apache/openjpa/lib/util/Timestamp5Helper.java?rev=631028&view=auto
==============================================================================
--- openjpa/trunk/openjpa-lib-5/src/main/java/org/apache/openjpa/lib/util/Timestamp5Helper.java (added)
+++ openjpa/trunk/openjpa-lib-5/src/main/java/org/apache/openjpa/lib/util/Timestamp5Helper.java Mon Feb 25 14:59:29 2008
@@ -0,0 +1,56 @@
+/*
+ * 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.openjpa.lib.util;
+
+import java.sql.Timestamp;
+
+/**
+ * Helper class to create java.sql.Timestamp object with nanosecond precision.
+ * This is only available since JDB 5.
+ * 
+ * @author Albert Lee
+ */
+public final class Timestamp5Helper extends TimestampHelper{
+
+    // number of seconds passed 1970/1/1 00:00:00 GMT.
+    private static long sec0;
+    // fraction of seconds passed 1970/1/1 00:00:00 GMT, offset by
+    // the base System.nanoTime (nano0), in nanosecond unit.
+    private static long nano0;
+
+    static {
+        // initialize base time in second and fraction of second (ns).
+        long curTime = System.currentTimeMillis();
+        sec0 = curTime / MilliMuliplier;
+        nano0 = (curTime % MilliMuliplier) * MicroMuliplier - System.nanoTime();
+    }
+
+    /*
+     * This class implements a nanosecond precision Timestamp.
+     */
+    protected Timestamp getTimestamp() {
+        long nano_delta = nano0 + System.nanoTime();
+        long sec1 = sec0 + (nano_delta / NanoMuliplier);
+        long nano1 = nano_delta % NanoMuliplier;
+
+        Timestamp rtnTs = new Timestamp(sec1 * MilliMuliplier);
+        rtnTs.setNanos((int) nano1);
+        return rtnTs;
+    }
+}

Propchange: openjpa/trunk/openjpa-lib-5/src/main/java/org/apache/openjpa/lib/util/Timestamp5Helper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/TimestampHelper.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/TimestampHelper.java?rev=631028&view=auto
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/TimestampHelper.java (added)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/TimestampHelper.java Mon Feb 25 14:59:29 2008
@@ -0,0 +1,67 @@
+/*
+ * 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.openjpa.lib.util;
+
+import java.sql.Timestamp;
+
+/**
+ * Helper base class attempts to return java.sql.Timestamp object with
+ * nanosecond precision. This base class is created to allow JDK 1.4 maven build
+ * and only implements millisecond precision.
+ * 
+ * @author Albert Lee
+ */
+public class TimestampHelper {
+
+    // number of millisecond, mircoseconds and nanoseconds in one second.
+    protected static final long MilliMuliplier = 1000L;
+    protected static final long MicroMuliplier = MilliMuliplier * 1000L;
+    protected static final long NanoMuliplier = MicroMuliplier * 1000L;
+
+    private static TimestampHelper instance = null;
+    
+    static {
+        if (JavaVersions.VERSION >= 5) {
+            try {
+                Class timestamp5HelperClass = Class
+                    .forName("org.apache.openjpa.lib.util.Timestamp5Helper");
+                instance = (TimestampHelper) timestamp5HelperClass
+                    .newInstance();
+            } catch (Throwable e) {
+                instance = new TimestampHelper();
+            }
+        } else {
+            instance = new TimestampHelper();
+        }
+    }
+
+    /*
+     * Return a java.sql.Timestamp object of current time.
+     */
+    public static Timestamp getNanoPrecisionTimestamp() {
+        return instance.getTimestamp();
+    }
+    
+    /*
+     * This class implements a millisecond precision Timestamp.
+     */
+    protected Timestamp getTimestamp() { 
+        return new Timestamp(System.currentTimeMillis());
+    }
+}

Propchange: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/TimestampHelper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain