svn commit: r922758 - in /ofbiz/trunk/framework/base: build.xml src/org/ofbiz/base/concurrent/GeneratedResult.java src/org/ofbiz/base/concurrent/TTLCachedObject.java src/org/ofbiz/base/concurrent/test/TTLCachedObjectTest.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r922758 - in /ofbiz/trunk/framework/base: build.xml src/org/ofbiz/base/concurrent/GeneratedResult.java src/org/ofbiz/base/concurrent/TTLCachedObject.java src/org/ofbiz/base/concurrent/test/TTLCachedObjectTest.java

doogie-3
Author: doogie
Date: Sun Mar 14 03:57:12 2010
New Revision: 922758

URL: http://svn.apache.org/viewvc?rev=922758&view=rev
Log:
Helper wrapper to protect against objects that have some kind of timestamp
checking.

Added:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/GeneratedResult.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/TTLCachedObject.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/TTLCachedObjectTest.java
Modified:
    ofbiz/trunk/framework/base/build.xml

Modified: ofbiz/trunk/framework/base/build.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/build.xml?rev=922758&r1=922757&r2=922758&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/build.xml (original)
+++ ofbiz/trunk/framework/base/build.xml Sun Mar 14 03:57:12 2010
@@ -56,6 +56,7 @@ under the License.
         <file name="org/ofbiz/base/util/collections/test/GenericMapTest.java"/>
         <file name="org/ofbiz/base/concurrent/test/SyncTTLObjectTest.java"/>
         <file name="org/ofbiz/base/concurrent/test/AsyncTTLObjectTest.java"/>
+        <file name="org/ofbiz/base/concurrent/test/TTLCachedObjectTest.java"/>
     </filelist>
 
     <target name="init">

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/GeneratedResult.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/GeneratedResult.java?rev=922758&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/GeneratedResult.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/GeneratedResult.java Sun Mar 14 03:57:12 2010
@@ -0,0 +1,31 @@
+/*******************************************************************************
+ * 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.ofbiz.base.concurrent;
+
+import org.ofbiz.base.util.ObjectWrapper;
+
+public final class GeneratedResult<T> {
+    public final long lastModifiedTime;
+    public final T object;
+
+    public GeneratedResult(long lastModifiedTime, T object) {
+        this.lastModifiedTime = lastModifiedTime;
+        this.object = object;
+    }
+}

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/TTLCachedObject.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/TTLCachedObject.java?rev=922758&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/TTLCachedObject.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/TTLCachedObject.java Sun Mar 14 03:57:12 2010
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * 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.ofbiz.base.concurrent;
+
+public abstract class TTLCachedObject<T> extends TTLObject<T> {
+    public static final long NOT_EXISTANT_TIMESTAMP = Long.MIN_VALUE;
+    public static final long FORCE_REGEN = Long.MIN_VALUE + 1;
+
+    protected long lastModifiedTime = NOT_EXISTANT_TIMESTAMP;
+
+    public long getTimestamp() throws ObjectException {
+        getObject();
+        return lastModifiedTime;
+    }
+
+    protected T load(T old, int serial) throws Exception {
+        long timestamp = getTimestamp(old);
+        if (lastModifiedTime != timestamp) {
+            if (timestamp != NOT_EXISTANT_TIMESTAMP) {
+                GeneratedResult<T> result = generate(old, serial);
+                lastModifiedTime = result.lastModifiedTime;
+                return result.object;
+            } else {
+                lastModifiedTime = NOT_EXISTANT_TIMESTAMP;
+                return getInitial();
+            }
+        }
+        return old;
+    }
+
+    protected abstract GeneratedResult<T> generate(T old, int serial) throws Exception;
+    protected abstract long getTimestamp(T old) throws Exception;
+}

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/TTLCachedObjectTest.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/TTLCachedObjectTest.java?rev=922758&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/TTLCachedObjectTest.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/TTLCachedObjectTest.java Sun Mar 14 03:57:12 2010
@@ -0,0 +1,72 @@
+/*******************************************************************************
+ * 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.ofbiz.base.concurrent.test;
+
+import org.ofbiz.base.concurrent.GeneratedResult;
+import org.ofbiz.base.concurrent.TTLCachedObject;
+import org.ofbiz.base.concurrent.TTLObject;
+
+public class TTLCachedObjectTest extends TTLObjectTest {
+    static {
+        TTLObject.setDefaultTTLForClass(TTLCachedObjectTestTTLObject.class, 100);
+    }
+
+    public TTLCachedObjectTest(String name) {
+        super(name, true);
+    }
+
+    protected final class TTLCachedObjectTestTTLObject extends TTLCachedObject<String> {
+        protected long dataTimestamp = NOT_EXISTANT_TIMESTAMP;
+        protected String data = "first";
+
+        protected long getTimestamp(String old) throws Exception {
+            return dataTimestamp;
+        }
+
+        protected GeneratedResult<String> generate(String old, int serial) throws Exception {
+            return new GeneratedResult<String>(dataTimestamp, data);
+        }
+    }
+
+    public void testTTLCachedObject() throws Exception {
+        TTLCachedObjectTestTTLObject object = new TTLCachedObjectTestTTLObject();
+        assertNull("initial non-existant value", object.getObject());
+        assertEquals("initial non-existant timestamp", TTLCachedObject.NOT_EXISTANT_TIMESTAMP, object.getTimestamp());
+        object.dataTimestamp = 1;
+        assertNull("initial no-refresh value", object.getObject());
+        assertEquals("initial no-refresh timestamp", TTLCachedObject.NOT_EXISTANT_TIMESTAMP, object.getTimestamp());
+        object.refresh();
+        assertEquals("first value", "first", object.getObject());
+        assertEquals("first timestamp", 1, object.getTimestamp());
+        object.data = "second";
+        object.refresh();
+        assertEquals("not-modified value", "first", object.getObject());
+        assertEquals("not-modified timestamp", 1, object.getTimestamp());
+        object.dataTimestamp = 2;
+        assertEquals("cached modified value", "first", object.getObject());
+        assertEquals("cached modified timestamp", 1, object.getTimestamp());
+        object.refresh();
+        assertEquals("refresh second value", "second", object.getObject());
+        assertEquals("refresh second timestamp", 2, object.getTimestamp());
+        object.dataTimestamp = TTLCachedObject.NOT_EXISTANT_TIMESTAMP;
+        object.refresh();
+        assertNull("refresh non-existant value", object.getObject());
+        assertEquals("refresh non-existant timestamp", TTLCachedObject.NOT_EXISTANT_TIMESTAMP, object.getTimestamp());
+    }
+}