[infinispan-commits] Infinispan SVN: r233 - trunk/core/src/main/java/org/infinispan/container/entries.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Fri May 8 07:37:00 EDT 2009
Author: manik.surtani at jboss.com
Date: 2009-05-08 07:36:59 -0400 (Fri, 08 May 2009)
New Revision: 233
Added:
trunk/core/src/main/java/org/infinispan/container/entries/ExpiryHelper.java
trunk/core/src/main/java/org/infinispan/container/entries/ImmortalCacheValue.java
trunk/core/src/main/java/org/infinispan/container/entries/InternalCacheValue.java
trunk/core/src/main/java/org/infinispan/container/entries/MortalCacheValue.java
trunk/core/src/main/java/org/infinispan/container/entries/TransientCacheValue.java
trunk/core/src/main/java/org/infinispan/container/entries/TransientMortalCacheValue.java
Modified:
trunk/core/src/main/java/org/infinispan/container/entries/ImmortalCacheEntry.java
trunk/core/src/main/java/org/infinispan/container/entries/InternalCacheEntry.java
trunk/core/src/main/java/org/infinispan/container/entries/InternalEntryFactory.java
trunk/core/src/main/java/org/infinispan/container/entries/MortalCacheEntry.java
trunk/core/src/main/java/org/infinispan/container/entries/TransientCacheEntry.java
trunk/core/src/main/java/org/infinispan/container/entries/TransientMortalCacheEntry.java
Log:
Introduced InternalCacheValue as a more efficient form of InternalCacheEntry for marshalling when marshalling of the key is not needed.
Added: trunk/core/src/main/java/org/infinispan/container/entries/ExpiryHelper.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/container/entries/ExpiryHelper.java (rev 0)
+++ trunk/core/src/main/java/org/infinispan/container/entries/ExpiryHelper.java 2009-05-08 11:36:59 UTC (rev 233)
@@ -0,0 +1,21 @@
+package org.infinispan.container.entries;
+
+/**
+ * // TODO: Manik: Document this
+ *
+ * @author Manik Surtani
+ * @since 4.0
+ */
+class ExpiryHelper {
+ static final boolean isExpiredMortal(long lifespan, long created) {
+ return lifespan > -1 && System.currentTimeMillis() > created + lifespan;
+ }
+
+ static final boolean isExpiredTransient(long maxIdle, long lastUsed) {
+ return maxIdle > -1 && System.currentTimeMillis() > maxIdle + lastUsed;
+ }
+
+ static final boolean isExpiredTransientMortal(long maxIdle, long lastUsed, long lifespan, long created) {
+ return isExpiredTransient(maxIdle, lastUsed) || isExpiredMortal(lifespan, created);
+ }
+}
Property changes on: trunk/core/src/main/java/org/infinispan/container/entries/ExpiryHelper.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified: trunk/core/src/main/java/org/infinispan/container/entries/ImmortalCacheEntry.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/container/entries/ImmortalCacheEntry.java 2009-05-07 16:47:42 UTC (rev 232)
+++ trunk/core/src/main/java/org/infinispan/container/entries/ImmortalCacheEntry.java 2009-05-08 11:36:59 UTC (rev 233)
@@ -68,6 +68,10 @@
// no-op
}
+ public InternalCacheValue toInternalCacheValue() {
+ return new ImmortalCacheValue(value);
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) return true;
@@ -87,6 +91,4 @@
result = 31 * result + (value != null ? value.hashCode() : 0);
return result;
}
-
-
}
Added: trunk/core/src/main/java/org/infinispan/container/entries/ImmortalCacheValue.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/container/entries/ImmortalCacheValue.java (rev 0)
+++ trunk/core/src/main/java/org/infinispan/container/entries/ImmortalCacheValue.java 2009-05-08 11:36:59 UTC (rev 233)
@@ -0,0 +1,51 @@
+package org.infinispan.container.entries;
+
+/**
+ * An immortal cache value, to correspond with {@link org.infinispan.container.entries.ImmortalCacheEntry}
+ *
+ * @author Manik Surtani
+ * @since 4.0
+ */
+public class ImmortalCacheValue implements InternalCacheValue {
+ Object value;
+
+ ImmortalCacheValue(Object value) {
+ this.value = value;
+ }
+
+ public void setValue(Object value) {
+ this.value = value;
+ }
+
+ public Object getValue() {
+ return value;
+ }
+
+ public InternalCacheEntry toInternalCacheEntry(Object key) {
+ return new ImmortalCacheEntry(key, value);
+ }
+
+ public boolean isExpired() {
+ return false;
+ }
+
+ public boolean canExpire() {
+ return false;
+ }
+
+ public long getCreated() {
+ return -1;
+ }
+
+ public long getLastUsed() {
+ return -1;
+ }
+
+ public long getLifespan() {
+ return -1;
+ }
+
+ public long getMaxIdle() {
+ return -1;
+ }
+}
Property changes on: trunk/core/src/main/java/org/infinispan/container/entries/ImmortalCacheValue.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified: trunk/core/src/main/java/org/infinispan/container/entries/InternalCacheEntry.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/container/entries/InternalCacheEntry.java 2009-05-07 16:47:42 UTC (rev 232)
+++ trunk/core/src/main/java/org/infinispan/container/entries/InternalCacheEntry.java 2009-05-08 11:36:59 UTC (rev 233)
@@ -8,7 +8,6 @@
*/
public interface InternalCacheEntry extends CacheEntry {
/**
- *
* @return true if the entry has expired; false otherwise
*/
boolean isExpired();
@@ -18,12 +17,13 @@
*/
boolean canExpire();
- /**
+ /**
* Sets the maximum idle time of the entry.
- * <p />
- * Note that if this method is used, you should always use a reference to the
- * return value after invocation, since as an optimization, implementations may change type of CacheEntry used after
- * invoking this method, for example changing a MortalCacheEntry to an ImmortalCacheEntry.
+ * <p/>
+ * Note that if this method is used, you should always use a reference to the return value after invocation, since as
+ * an optimization, implementations may change type of CacheEntry used after invoking this method, for example
+ * changing a MortalCacheEntry to an ImmortalCacheEntry.
+ *
* @param maxIdle maxIdle to set
* @return the updated CacheEntry
*/
@@ -31,10 +31,11 @@
/**
* Sets the lifespan of the entry.
- * <p />
- * Note that if this method is used, you should always use a reference to the
- * return value after invocation, since as an optimization, implementations may change type of CacheEntry used after
- * invoking this method, for example changing a MortalCacheEntry to an ImmortalCacheEntry.
+ * <p/>
+ * Note that if this method is used, you should always use a reference to the return value after invocation, since as
+ * an optimization, implementations may change type of CacheEntry used after invoking this method, for example
+ * changing a MortalCacheEntry to an ImmortalCacheEntry.
+ *
* @param lifespan lifespan to set
* @return the updated CacheEntry
*/
@@ -52,6 +53,7 @@
/**
* Only used with entries that have a lifespan, this determines when an entry is due to expire.
+ *
* @return timestamp when the entry is due to expire, or -1 if it doesn't have a lifespan
*/
long getExpiryTime();
@@ -60,4 +62,20 @@
* Updates access timestamps on this instance
*/
void touch();
+
+ /**
+ * Creates a representation of this entry as an {@link org.infinispan.container.entries.InternalCacheValue}. The main
+ * purpose of this is to provide a representation that does <i>not</i> have a reference to the key. This is useful in
+ * situations where the key is already known or stored elsewhere, making serialization and deserialization more
+ * efficient.
+ * <p/>
+ * Note that this should not be used to optimize memory overhead, since the saving of an additional reference to a
+ * key (a single object reference) does not warrant the cost of constructing an InternalCacheValue. This <i>only</i>
+ * makes sense when marshalling is involved, since the cost of marshalling the key again can be sidestepped using an
+ * InternalCacheValue if the key is already known/marshalled.
+ * <p/>
+ *
+ * @return a new InternalCacheValue encapsulating this InternalCacheEntry's value and expiration information.
+ */
+ InternalCacheValue toInternalCacheValue();
}
Added: trunk/core/src/main/java/org/infinispan/container/entries/InternalCacheValue.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/container/entries/InternalCacheValue.java (rev 0)
+++ trunk/core/src/main/java/org/infinispan/container/entries/InternalCacheValue.java 2009-05-08 11:36:59 UTC (rev 233)
@@ -0,0 +1,58 @@
+package org.infinispan.container.entries;
+
+/**
+ * A representation of an InternalCacheEntry that does not have a reference to the key. This should be used if the key
+ * is either not needed or available elsewhere as it is more efficient to marshall and unmarshall. Probably most useful
+ * in cache stores.
+ * <p/>
+ * Note that this should not be used to optimize memory overhead, since the saving of an additional reference to a key
+ * (a single object reference) does not warrant the cost of constructing an InternalCacheValue, where an existing
+ * InternalCacheEntry is already referenced.
+ * <p/>
+ * Use of this interface <i>only</i> makes sense when marshalling is involved, since the cost of marshalling the key
+ * again can be sidestepped using an InternalCacheValue if the key is already known/marshalled.
+ * <p/>
+ *
+ * @author Manik Surtani
+ * @since 4.0
+ */
+public interface InternalCacheValue {
+
+ /**
+ * @return the value represented by this internal wrapper
+ */
+ Object getValue();
+
+ InternalCacheEntry toInternalCacheEntry(Object key);
+
+ /**
+ * @return true if the entry has expired; false otherwise
+ */
+ boolean isExpired();
+
+ /**
+ * @return true if the entry can expire, false otherwise
+ */
+ boolean canExpire();
+
+ /**
+ * @return timestamp when the entry was created
+ */
+ long getCreated();
+
+ /**
+ * @return timestamp when the entry was last used
+ */
+ long getLastUsed();
+
+ /**
+ * @return lifespan of the value
+ */
+ long getLifespan();
+
+ /**
+ * @return max idle time allowed
+ */
+ long getMaxIdle();
+
+}
Property changes on: trunk/core/src/main/java/org/infinispan/container/entries/InternalCacheValue.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified: trunk/core/src/main/java/org/infinispan/container/entries/InternalEntryFactory.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/container/entries/InternalEntryFactory.java 2009-05-07 16:47:42 UTC (rev 232)
+++ trunk/core/src/main/java/org/infinispan/container/entries/InternalEntryFactory.java 2009-05-08 11:36:59 UTC (rev 233)
@@ -28,4 +28,15 @@
if (lifespan < 0 && maxIdle > -1) return new TransientCacheEntry(key, value, maxIdle, lastUsed);
return new TransientMortalCacheEntry(key, value, maxIdle, lifespan, lastUsed, created);
}
+
+ public static final InternalCacheValue createValue(Object v) {
+ return new ImmortalCacheValue(v);
+ }
+
+ public static final InternalCacheValue createValue(Object v, long created, long lifespan, long lastUsed, long maxIdle) {
+ if (lifespan < 0 && maxIdle < 0) return new ImmortalCacheValue(v);
+ if (lifespan > -1 && maxIdle < 0) return new MortalCacheValue(v, lifespan, created);
+ if (lifespan < 0 && maxIdle > -1) return new TransientCacheValue(v, maxIdle, lastUsed);
+ return new TransientMortalCacheValue(v, maxIdle, lifespan, lastUsed, created);
+ }
}
Modified: trunk/core/src/main/java/org/infinispan/container/entries/MortalCacheEntry.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/container/entries/MortalCacheEntry.java 2009-05-07 16:47:42 UTC (rev 232)
+++ trunk/core/src/main/java/org/infinispan/container/entries/MortalCacheEntry.java 2009-05-08 11:36:59 UTC (rev 233)
@@ -27,7 +27,7 @@
}
public final boolean isExpired() {
- return lifespan > -1 && System.currentTimeMillis() > created + lifespan;
+ return ExpiryHelper.isExpiredMortal(lifespan, created);
}
public final boolean canExpire() {
@@ -77,6 +77,10 @@
// no-op
}
+ public InternalCacheValue toInternalCacheValue() {
+ return new MortalCacheValue(value, created, lifespan);
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) return true;
Added: trunk/core/src/main/java/org/infinispan/container/entries/MortalCacheValue.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/container/entries/MortalCacheValue.java (rev 0)
+++ trunk/core/src/main/java/org/infinispan/container/entries/MortalCacheValue.java 2009-05-08 11:36:59 UTC (rev 233)
@@ -0,0 +1,51 @@
+package org.infinispan.container.entries;
+
+/**
+ * A mortal cache value, to correspond with {@link org.infinispan.container.entries.MortalCacheEntry}
+ *
+ * @author Manik Surtani
+ * @since 4.0
+ */
+public class MortalCacheValue extends ImmortalCacheValue {
+ long created;
+ long lifespan = -1;
+
+ MortalCacheValue(Object value, long created, long lifespan) {
+ super(value);
+ this.created = created;
+ this.lifespan = lifespan;
+ }
+
+ @Override
+ public long getCreated() {
+ return created;
+ }
+
+ public void setCreated(long created) {
+ this.created = created;
+ }
+
+ @Override
+ public long getLifespan() {
+ return lifespan;
+ }
+
+ public void setLifespan(long lifespan) {
+ this.lifespan = lifespan;
+ }
+
+ @Override
+ public boolean isExpired() {
+ return ExpiryHelper.isExpiredMortal(lifespan, created);
+ }
+
+ @Override
+ public boolean canExpire() {
+ return true;
+ }
+
+ @Override
+ public InternalCacheEntry toInternalCacheEntry(Object key) {
+ return new MortalCacheEntry(key, value, lifespan, created);
+ }
+}
Property changes on: trunk/core/src/main/java/org/infinispan/container/entries/MortalCacheValue.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified: trunk/core/src/main/java/org/infinispan/container/entries/TransientCacheEntry.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/container/entries/TransientCacheEntry.java 2009-05-07 16:47:42 UTC (rev 232)
+++ trunk/core/src/main/java/org/infinispan/container/entries/TransientCacheEntry.java 2009-05-08 11:36:59 UTC (rev 233)
@@ -36,7 +36,7 @@
}
public boolean isExpired() {
- return maxIdle > -1 && System.currentTimeMillis() > maxIdle + lastUsed;
+ return ExpiryHelper.isExpiredTransient(maxIdle, lastUsed);
}
public InternalCacheEntry setMaxIdle(long maxIdle) {
@@ -78,6 +78,10 @@
return maxIdle;
}
+ public InternalCacheValue toInternalCacheValue() {
+ return new TransientCacheValue(value, maxIdle, lastUsed);
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) return true;
Added: trunk/core/src/main/java/org/infinispan/container/entries/TransientCacheValue.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/container/entries/TransientCacheValue.java (rev 0)
+++ trunk/core/src/main/java/org/infinispan/container/entries/TransientCacheValue.java 2009-05-08 11:36:59 UTC (rev 233)
@@ -0,0 +1,51 @@
+package org.infinispan.container.entries;
+
+/**
+ * A transient cache value, to correspond with {@link org.infinispan.container.entries.TransientCacheEntry}
+ *
+ * @author Manik Surtani
+ * @since 4.0
+ */
+public class TransientCacheValue extends ImmortalCacheValue {
+ long maxIdle = -1;
+ long lastUsed;
+
+ TransientCacheValue(Object value, long maxIdle, long lastUsed) {
+ super(value);
+ this.maxIdle = maxIdle;
+ this.lastUsed = lastUsed;
+ }
+
+ @Override
+ public long getMaxIdle() {
+ return maxIdle;
+ }
+
+ public void setMaxIdle(long maxIdle) {
+ this.maxIdle = maxIdle;
+ }
+
+ @Override
+ public long getLastUsed() {
+ return lastUsed;
+ }
+
+ public void setLastUsed(long lastUsed) {
+ this.lastUsed = lastUsed;
+ }
+
+ @Override
+ public boolean isExpired() {
+ return ExpiryHelper.isExpiredTransient(maxIdle, lastUsed);
+ }
+
+ @Override
+ public boolean canExpire() {
+ return true;
+ }
+
+ @Override
+ public InternalCacheEntry toInternalCacheEntry(Object key) {
+ return new TransientCacheEntry(key, value, maxIdle, lastUsed);
+ }
+}
Property changes on: trunk/core/src/main/java/org/infinispan/container/entries/TransientCacheValue.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified: trunk/core/src/main/java/org/infinispan/container/entries/TransientMortalCacheEntry.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/container/entries/TransientMortalCacheEntry.java 2009-05-07 16:47:42 UTC (rev 232)
+++ trunk/core/src/main/java/org/infinispan/container/entries/TransientMortalCacheEntry.java 2009-05-08 11:36:59 UTC (rev 233)
@@ -74,7 +74,7 @@
@Override
public boolean isExpired() {
- return super.isExpired() || (lifespan > -1 && System.currentTimeMillis() > created + lifespan);
+ return ExpiryHelper.isExpiredTransientMortal(maxIdle, lastUsed, lifespan, created);
}
@Override
@@ -83,6 +83,11 @@
}
@Override
+ public InternalCacheValue toInternalCacheValue() {
+ return new TransientMortalCacheValue(value, created, lifespan, maxIdle, lastUsed);
+ }
+
+ @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Added: trunk/core/src/main/java/org/infinispan/container/entries/TransientMortalCacheValue.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/container/entries/TransientMortalCacheValue.java (rev 0)
+++ trunk/core/src/main/java/org/infinispan/container/entries/TransientMortalCacheValue.java 2009-05-08 11:36:59 UTC (rev 233)
@@ -0,0 +1,46 @@
+package org.infinispan.container.entries;
+
+/**
+ * A transient, mortal cache value to correspond with {@link org.infinispan.container.entries.TransientMortalCacheEntry}
+ *
+ * @author Manik Surtani
+ * @since 4.0
+ */
+public class TransientMortalCacheValue extends MortalCacheValue {
+ private long maxIdle = -1;
+ private long lastUsed;
+
+ TransientMortalCacheValue(Object value, long created, long lifespan, long maxIdle, long lastUsed) {
+ super(value, created, lifespan);
+ this.maxIdle = maxIdle;
+ this.lastUsed = lastUsed;
+ }
+
+ @Override
+ public long getMaxIdle() {
+ return maxIdle;
+ }
+
+ public void setMaxIdle(long maxIdle) {
+ this.maxIdle = maxIdle;
+ }
+
+ @Override
+ public long getLastUsed() {
+ return lastUsed;
+ }
+
+ public void setLastUsed(long lastUsed) {
+ this.lastUsed = lastUsed;
+ }
+
+ @Override
+ public boolean isExpired() {
+ return ExpiryHelper.isExpiredTransientMortal(maxIdle, lastUsed, lifespan, created);
+ }
+
+ @Override
+ public InternalCacheEntry toInternalCacheEntry(Object key) {
+ return new TransientMortalCacheEntry(key, value, maxIdle, lifespan, lastUsed, created);
+ }
+}
Property changes on: trunk/core/src/main/java/org/infinispan/container/entries/TransientMortalCacheValue.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
More information about the infinispan-commits
mailing list