[infinispan-commits] Infinispan SVN: r1997 - in branches/4.1.x/client/hotrod-client/src: main/java/org/infinispan/client/hotrod/impl and 2 other directories.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Tue Jul 6 12:39:47 EDT 2010


Author: mircea.markus
Date: 2010-07-06 12:39:47 -0400 (Tue, 06 Jul 2010)
New Revision: 1997

Modified:
   branches/4.1.x/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/HotRodMarshaller.java
   branches/4.1.x/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/RemoteCacheImpl.java
   branches/4.1.x/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/SerializationMarshaller.java
   branches/4.1.x/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/HotRodIntegrationTest.java
   branches/4.1.x/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/retry/DistributionRetryTest.java
Log:
[ISPN-513] = hotrod client:optimize serialization of key/values

Modified: branches/4.1.x/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/HotRodMarshaller.java
===================================================================
--- branches/4.1.x/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/HotRodMarshaller.java	2010-07-06 16:24:38 UTC (rev 1996)
+++ branches/4.1.x/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/HotRodMarshaller.java	2010-07-06 16:39:47 UTC (rev 1997)
@@ -16,7 +16,11 @@
 
    void init(Properties config);
 
-   byte[] marshallObject(Object toMarshall);
+   /**
+    * @param isKeyHint if true the object passed to the marshaller is a key. This info can be used to optimize the
+    * size of the allocated byte[].
+    */
+   byte[] marshallObject(Object toMarshall, boolean isKeyHint);
 
    Object readObject(byte[] bytes);
 }

Modified: branches/4.1.x/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/RemoteCacheImpl.java
===================================================================
--- branches/4.1.x/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/RemoteCacheImpl.java	2010-07-06 16:24:38 UTC (rev 1996)
+++ branches/4.1.x/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/RemoteCacheImpl.java	2010-07-06 16:39:47 UTC (rev 1997)
@@ -60,7 +60,7 @@
    @Override
    public boolean removeWithVersion(K key, long version) {
       assertRemoteCacheManagerIsStarted();
-      VersionedOperationResponse response = operations.removeIfUnmodified(obj2bytes(key), version, flags());
+      VersionedOperationResponse response = operations.removeIfUnmodified(obj2bytes(key, true), version, flags());
       return response.getCode().isUpdated();
    }
 
@@ -83,7 +83,7 @@
    @Override
    public boolean replaceWithVersion(K key, V newValue, long version, int lifespanSeconds, int maxIdleTimeSeconds) {
       assertRemoteCacheManagerIsStarted();
-      VersionedOperationResponse response = operations.replaceIfUnmodified(obj2bytes(key), obj2bytes(newValue), lifespanSeconds, maxIdleTimeSeconds, version, flags());
+      VersionedOperationResponse response = operations.replaceIfUnmodified(obj2bytes(key, true), obj2bytes(newValue, false), lifespanSeconds, maxIdleTimeSeconds, version, flags());
       return response.getCode().isUpdated();
    }
 
@@ -106,7 +106,7 @@
    @Override
    public VersionedValue<V> getVersioned(K key) {
       assertRemoteCacheManagerIsStarted();
-      BinaryVersionedValue value = operations.getWithVersion(obj2bytes(key), flags());
+      BinaryVersionedValue value = operations.getWithVersion(obj2bytes(key, true), flags());
       return binary2VersionedValue(value);
    }
 
@@ -164,7 +164,7 @@
       if (log.isTraceEnabled()) {
          log.trace("About to add (K,V): (" + key + ", " + value + ") lifespanSecs:" + lifespanSecs + ", maxIdleSecs:" + maxIdleSecs);
       }
-      byte[] result = operations.put(obj2bytes(key), obj2bytes(value), lifespanSecs, maxIdleSecs, flags());
+      byte[] result = operations.put(obj2bytes(key, true), obj2bytes(value, false), lifespanSecs, maxIdleSecs, flags());
       return (V) bytes2obj(result);
    }
 
@@ -174,7 +174,7 @@
       assertRemoteCacheManagerIsStarted();
       int lifespanSecs = toSeconds(lifespan, lifespanUnit);
       int maxIdleSecs = toSeconds(maxIdleTime, maxIdleTimeUnit);
-      byte[] bytes = operations.putIfAbsent(obj2bytes(key), obj2bytes(value), lifespanSecs, maxIdleSecs, flags());
+      byte[] bytes = operations.putIfAbsent(obj2bytes(key, true), obj2bytes(value, false), lifespanSecs, maxIdleSecs, flags());
       return (V) bytes2obj(bytes);
    }
 
@@ -183,7 +183,7 @@
       assertRemoteCacheManagerIsStarted();
       int lifespanSecs = toSeconds(lifespan, lifespanUnit);
       int maxIdleSecs = toSeconds(maxIdleTime, maxIdleTimeUnit);
-      byte[] bytes = operations.replace(obj2bytes(key), obj2bytes(value), lifespanSecs, maxIdleSecs, flags());
+      byte[] bytes = operations.replace(obj2bytes(key, true), obj2bytes(value, false), lifespanSecs, maxIdleSecs, flags());
       return (V) bytes2obj(bytes);
    }
 
@@ -270,20 +270,20 @@
    @Override
    public boolean containsKey(Object key) {
       assertRemoteCacheManagerIsStarted();
-      return operations.containsKey(obj2bytes(key), flags());
+      return operations.containsKey(obj2bytes(key, true), flags());
    }
 
    @Override
    public V get(Object key) {
       assertRemoteCacheManagerIsStarted();
-      byte[] bytes = operations.get(obj2bytes(key), flags());
+      byte[] bytes = operations.get(obj2bytes(key, true), flags());
       return (V) bytes2obj(bytes);
    }
 
    @Override
    public V remove(Object key) {
       assertRemoteCacheManagerIsStarted();
-      byte[] existingValue = operations.remove(obj2bytes(key), flags());
+      byte[] existingValue = operations.remove(obj2bytes(key, true), flags());
       return (V) bytes2obj(existingValue);
    }
 
@@ -323,8 +323,8 @@
       return flags;
    }
 
-   private byte[] obj2bytes(Object obj) {
-      return this.marshaller.marshallObject(obj);
+   private byte[] obj2bytes(Object obj, boolean isKey) {
+      return this.marshaller.marshallObject(obj, isKey);
    }
 
    private Object bytes2obj(byte[] bytes) {

Modified: branches/4.1.x/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/SerializationMarshaller.java
===================================================================
--- branches/4.1.x/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/SerializationMarshaller.java	2010-07-06 16:24:38 UTC (rev 1996)
+++ branches/4.1.x/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/SerializationMarshaller.java	2010-07-06 16:39:47 UTC (rev 1997)
@@ -15,6 +15,14 @@
 
 /**
  * Default marshaller implementation based on object serialization.
+ * Supports two configuration elements:
+ * <ul>
+ *  <li>marshaller.default-array-size.key - the size of the {@link ExposedByteArrayOutputStream} that will be
+ *   created for marshalling keys</li>
+ *  <li> marshaller.default-array-size.value - the size of the {@link ExposedByteArrayOutputStream} that will be
+ *   created for marshalling values
+ *  </li>
+ * </ul>
  *
  * @author Mircea.Markus at jboss.com
  * @since 4.1
@@ -23,19 +31,22 @@
 
    private static Log log = LogFactory.getLog(SerializationMarshaller.class);
 
-   private volatile int defaultArraySize = 128;
+   private volatile int defaultArraySizeForKey = 128;
+   private volatile int defaultArraySizeForValue = 256;
 
    @Override
    public void init(Properties config) {
-      if (config.contains("marshaller.default-array-size")) {
-         defaultArraySize = Integer.parseInt(config.getProperty("marshaller.default-array-size"));
+      if (config.contains("marshaller.default-array-size.key")) {
+         defaultArraySizeForKey = Integer.parseInt(config.getProperty("marshaller.default-array-size.key"));
       }
-      defaultArraySize = 128;
+      if (config.contains("marshaller.default-array-size.value")) {
+         defaultArraySizeForValue = Integer.parseInt(config.getProperty("marshaller.default-array-size.value"));
+      }
    }
 
    @Override
-   public byte[] marshallObject(Object toMarshall) {
-      ExposedByteArrayOutputStream result = new ExposedByteArrayOutputStream(defaultArraySize);
+   public byte[] marshallObject(Object toMarshall, boolean isKeyHint) {
+      ExposedByteArrayOutputStream result = getByteArray(isKeyHint);
       try {
          ObjectOutputStream oos = new ObjectOutputStream(result);
          oos.writeObject(toMarshall);
@@ -45,6 +56,14 @@
       }
    }
 
+   private ExposedByteArrayOutputStream getByteArray(boolean keyHint) {
+      if (keyHint) {
+         return new ExposedByteArrayOutputStream(defaultArraySizeForKey);
+      } else {
+         return new ExposedByteArrayOutputStream(defaultArraySizeForValue);
+      }
+   }
+
    @Override
    public Object readObject(byte[] bytes) {
       try {

Modified: branches/4.1.x/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/HotRodIntegrationTest.java
===================================================================
--- branches/4.1.x/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/HotRodIntegrationTest.java	2010-07-06 16:24:38 UTC (rev 1996)
+++ branches/4.1.x/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/HotRodIntegrationTest.java	2010-07-06 16:39:47 UTC (rev 1997)
@@ -187,8 +187,8 @@
 
    private void assertCacheContains(Cache cache, String key, String value) {
       SerializationMarshaller marshaller = new SerializationMarshaller();
-      byte[] keyBytes = marshaller.marshallObject(key);
-      byte[] valueBytes = marshaller.marshallObject(value);
+      byte[] keyBytes = marshaller.marshallObject(key, true);
+      byte[] valueBytes = marshaller.marshallObject(value, false);
       ByteArrayKey cacheKey = new ByteArrayKey(keyBytes);
       CacheValue cacheValue = (CacheValue) cache.get(cacheKey);
       if (value == null) {

Modified: branches/4.1.x/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/retry/DistributionRetryTest.java
===================================================================
--- branches/4.1.x/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/retry/DistributionRetryTest.java	2010-07-06 16:24:38 UTC (rev 1996)
+++ branches/4.1.x/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/retry/DistributionRetryTest.java	2010-07-06 16:39:47 UTC (rev 1997)
@@ -115,7 +115,7 @@
       TcpTransportFactory tcpTp = (TcpTransportFactory) TestingUtil.extractField(remoteCacheManager, "transportFactory");
 
       SerializationMarshaller sm = new SerializationMarshaller();
-      TcpTransport transport = (TcpTransport) tcpTp.getTransport(sm.marshallObject(key));
+      TcpTransport transport = (TcpTransport) tcpTp.getTransport(sm.marshallObject(key, true));
       try {
       assertEquals(transport.getServerAddress(), new InetSocketAddress("localhost", hotRodServer2.getPort()));
       } finally {
@@ -136,7 +136,7 @@
       public Object getKey() {
          String result = String.valueOf(r.nextLong());
          SerializationMarshaller sm = new SerializationMarshaller();
-         return sm.marshallObject(result);
+         return sm.marshallObject(result, true);
       }
 
       static String getStringObject(byte[] bytes) {



More information about the infinispan-commits mailing list