Author: jason.greene(a)jboss.com
Date: 2007-09-20 23:15:01 -0400 (Thu, 20 Sep 2007)
New Revision: 4496
Modified:
pojo/trunk/src/main/java/org/jboss/cache/pojo/PojoCache.java
pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/PojoCacheDelegate.java
pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/PojoCacheImpl.java
pojo/trunk/src/test/java/org/jboss/cache/pojo/LocalTest.java
Log:
JBCACHE-1 Add low cost exists() to the API
Modified: pojo/trunk/src/main/java/org/jboss/cache/pojo/PojoCache.java
===================================================================
--- pojo/trunk/src/main/java/org/jboss/cache/pojo/PojoCache.java 2007-09-21 00:20:00 UTC
(rev 4495)
+++ pojo/trunk/src/main/java/org/jboss/cache/pojo/PojoCache.java 2007-09-21 03:15:01 UTC
(rev 4496)
@@ -113,6 +113,15 @@
String getPojoID(Object pojo);
/**
+ * Determines if an object is attached at a particular location. This is somewhat less
expensive
+ * than find() because an object is not created, and internal reference links are not
traversed.
+ *
+ * @param id the location in the cache to examine
+ * @return true if an attached object exists, false if not
+ */
+ boolean exists(Fqn<?> id);
+
+ /**
* Retrieve POJO from the cache system. Return null if object does not exist in the
cache.
* Note that this operation is fast if there is already a POJO instance attached to
the cache.
*
Modified: pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/PojoCacheDelegate.java
===================================================================
--- pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/PojoCacheDelegate.java 2007-09-21
00:20:00 UTC (rev 4495)
+++ pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/PojoCacheDelegate.java 2007-09-21
03:15:01 UTC (rev 4496)
@@ -476,4 +476,9 @@
}
}
}
+
+ public boolean exists(Fqn<?> id)
+ {
+ return internal_.getPojoReference(id, null) != null ||
internal_.getPojoInstance(id) != null;
+ }
}
Modified: pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/PojoCacheImpl.java
===================================================================
--- pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/PojoCacheImpl.java 2007-09-21
00:20:00 UTC (rev 4495)
+++ pojo/trunk/src/main/java/org/jboss/cache/pojo/impl/PojoCacheImpl.java 2007-09-21
03:15:01 UTC (rev 4496)
@@ -174,6 +174,11 @@
throw new PojoCacheException("getPojoID not yet implemented");
}
+ public boolean exists(Fqn<?> id)
+ {
+ return delegate_.exists(id);
+ }
+
public Object find(String id) throws PojoCacheException
{
return find(Fqn.fromString(id));
Modified: pojo/trunk/src/test/java/org/jboss/cache/pojo/LocalTest.java
===================================================================
--- pojo/trunk/src/test/java/org/jboss/cache/pojo/LocalTest.java 2007-09-21 00:20:00 UTC
(rev 4495)
+++ pojo/trunk/src/test/java/org/jboss/cache/pojo/LocalTest.java 2007-09-21 03:15:01 UTC
(rev 4496)
@@ -1,6 +1,7 @@
package org.jboss.cache.pojo;
import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertNull;
import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.fail;
@@ -16,6 +17,7 @@
import org.apache.commons.logging.LogFactory;
import org.jboss.aop.proxy.ClassProxy;
import org.jboss.cache.Fqn;
+import org.jboss.cache.pojo.impl.PojoReference;
import org.jboss.cache.pojo.test.Address;
import org.jboss.cache.pojo.test.Person;
import org.jboss.cache.pojo.test.Student;
@@ -366,9 +368,14 @@
cache_.detach("/person/joe");
}
+ public void testExists() throws Exception
+ {
+ Fqn<String> fqn = Fqn.fromString("/person/test1");
+ createPerson(fqn.toString(), "Joe Black", 32);
+ assertTrue(cache_.exists(fqn));
+ assertFalse(cache_.exists(Fqn.fromString("/blah")));
-
-
-
-}
-
+ PojoReference ref = (PojoReference) cache_.getCache().get(fqn,
PojoReference.KEY);
+ assertTrue(cache_.exists(ref.getFqn()));
+ }
+}
\ No newline at end of file