[jboss-cvs] JBoss Messaging SVN: r4202 - in trunk: src/main/org/jboss/messaging/util and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed May 14 19:29:47 EDT 2008


Author: clebert.suconic at jboss.com
Date: 2008-05-14 19:29:46 -0400 (Wed, 14 May 2008)
New Revision: 4202

Modified:
   trunk/src/main/org/jboss/messaging/core/message/impl/MessageImpl.java
   trunk/src/main/org/jboss/messaging/util/TypedProperties.java
   trunk/tests/src/org/jboss/messaging/tests/unit/core/message/impl/MessageTest.java
Log:
JBMESSAGING-1283 - Improving size calculation logic

Modified: trunk/src/main/org/jboss/messaging/core/message/impl/MessageImpl.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/message/impl/MessageImpl.java	2008-05-14 21:59:18 UTC (rev 4201)
+++ trunk/src/main/org/jboss/messaging/core/message/impl/MessageImpl.java	2008-05-14 23:29:46 UTC (rev 4202)
@@ -146,11 +146,11 @@
       
       properties.decode(buffer);
       int len = buffer.getInt();
-      
+
       //TODO - this can be optimised
       byte[] bytes = new byte[len];
       buffer.getBytes(bytes);
-      body = new BufferWrapper(1024);
+      body = new BufferWrapper(len);
       body.putBytes(bytes);      
    }
    

Modified: trunk/src/main/org/jboss/messaging/util/TypedProperties.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/TypedProperties.java	2008-05-14 21:59:18 UTC (rev 4201)
+++ trunk/src/main/org/jboss/messaging/util/TypedProperties.java	2008-05-14 23:29:46 UTC (rev 4202)
@@ -45,6 +45,7 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.jboss.messaging.core.journal.EncodingSupport;
 import org.jboss.messaging.core.logging.Logger;
@@ -63,6 +64,7 @@
 	private static final Logger log = Logger.getLogger(TypedProperties.class);
 	
 	private Map<SimpleString, PropertyValue> properties;
+	AtomicInteger size = new AtomicInteger(0);
 	
 	public TypedProperties()
 	{		
@@ -76,61 +78,61 @@
 	public void putBooleanProperty(final SimpleString key, final boolean value)
 	{
    	checkCreateProperties();   	
-		properties.put(key, new BooleanValue(value));
+   	doPutValue(key, new BooleanValue(value));
 	}
          	
 	public void putByteProperty(final SimpleString key, final byte value)
 	{
    	checkCreateProperties();   	
-   	properties.put(key, new ByteValue(value));
+   	doPutValue(key, new ByteValue(value));
 	}
 	
 	public void putBytesProperty(final SimpleString key, final byte[] value)
 	{
    	checkCreateProperties();   	
-   	properties.put(key, value == null ? new NullValue() : new BytesValue(value));
+   	doPutValue(key, value == null ? new NullValue() : new BytesValue(value));
 	}
 	
 	public void putShortProperty(final SimpleString key, final short value)
 	{
    	checkCreateProperties();
-   	properties.put(key, new ShortValue(value));
+   	doPutValue(key, new ShortValue(value));
 	}
 	
 	public void putIntProperty(final SimpleString key, final int value)
 	{
    	checkCreateProperties();
-   	properties.put(key, new IntValue(value));
+   	doPutValue(key, new IntValue(value));
 	}
 	
 	public void putLongProperty(final SimpleString key, final long value)
 	{
    	checkCreateProperties();
-   	properties.put(key, new LongValue(value));
+   	doPutValue(key, new LongValue(value));
 	}
 	
 	public void putFloatProperty(final SimpleString key, final float value)
 	{
    	checkCreateProperties();
-   	properties.put(key, new FloatValue(value));
+   	doPutValue(key, new FloatValue(value));
 	}
 	
 	public void putDoubleProperty(final SimpleString key, final double value)
 	{
    	checkCreateProperties();
-   	properties.put(key, new DoubleValue(value));
+   	doPutValue(key, new DoubleValue(value));
 	}
 	
 	public void putStringProperty(final SimpleString key, final SimpleString value)
 	{
    	checkCreateProperties();
-   	properties.put(key, value == null ? new NullValue() : new StringValue(value));
+   	doPutValue(key, value == null ? new NullValue() : new StringValue(value));
 	}
 	
 	public void putCharProperty(final SimpleString key, final char value)
 	{
    	checkCreateProperties();
-   	properties.put(key, new CharValue(value));
+   	doPutValue(key, new CharValue(value));
 	}
 	
 	public Object getProperty(final SimpleString key)
@@ -166,6 +168,7 @@
    	   int numHeaders = buffer.getInt();
    	 		
       	properties = new HashMap<SimpleString, PropertyValue>(numHeaders);
+      	size.set(0);
    		
    		for (int i = 0; i < numHeaders; i++)
    		{
@@ -183,67 +186,67 @@
    			   case NULL:
    			   {
    			      val = new NullValue();
-   			      properties.put(key, val);
+   			      doPutValue(key, val);
    			      break;
    			   }
    			   case CHAR:
    			   {
    			      val = new CharValue(buffer);
-   			      properties.put(key, val);
+   			      doPutValue(key, val);
    			      break;
    			   }
    				case BOOLEAN:
    				{
    					val = new BooleanValue(buffer);
-   					properties.put(key, val);	
+   					doPutValue(key, val);	
    					break;
    				}
    				case BYTE:
    				{
    					val = new ByteValue(buffer);
-   					properties.put(key, val);
+   					doPutValue(key, val);
    					break;
    				}
    				case BYTES:
    				{
    				   val = new BytesValue(buffer);
-   				   properties.put(key, val);
+   				   doPutValue(key, val);
    				   break;
    				}
    				case SHORT:
    				{
    					val = new ShortValue(buffer);
-   					properties.put(key, val);
+   					doPutValue(key, val);
    					break;
    				}
    				case INT:
    				{
    					val = new IntValue(buffer);
-   					properties.put(key, val);
+   					doPutValue(key, val);
    					break;
    				}
    				case LONG:
    				{
    					val = new LongValue(buffer);
-   					properties.put(key, val);
+   					doPutValue(key, val);
    					break;
    				}
    				case FLOAT:
    				{
    					val = new FloatValue(buffer);
-   					properties.put(key, val);   				
+   					doPutValue(key, val);   				
    					break;
    				}
    				case DOUBLE:
    				{
    					val = new DoubleValue(buffer);
-   					properties.put(key, val);
+   					doPutValue(key, val);
    					break;
    				}
    				case STRING:
    				{
    					val = new StringValue(buffer);
-   					properties.put(key, val);
+   					doPutValue(key, val);
    					break;
    				}
    				default:
@@ -287,12 +290,8 @@
 	   }
 	   else
 	   {
-	      int size = SIZE_BYTE + SIZE_INT;
-         for (Map.Entry<SimpleString, PropertyValue> entry: properties.entrySet())
-         {
-            size += SimpleString.sizeofString(entry.getKey()) + entry.getValue().encodeSize(); 
-         }
-         return size;
+         return SIZE_BYTE + SIZE_INT + size.intValue();
+         
 	   }
 	}
 	
@@ -314,7 +313,20 @@
    	}
    }
    
-	private Object doRemoveProperty(final Object key)
+   private void doPutValue(final SimpleString key, PropertyValue value)
+   {
+      PropertyValue oldValue = properties.put(key, value);
+      if (oldValue != null)
+      {
+         size.addAndGet(value.encodeSize() - oldValue.encodeSize());
+      }
+      else
+      {
+         size.addAndGet(SimpleString.sizeofString(key) + value.encodeSize());
+      }
+   }
+   
+	private Object doRemoveProperty(final SimpleString key)
 	{
 		if (properties == null)
 		{
@@ -323,6 +335,8 @@
 		
 		PropertyValue val = properties.remove(key);
 		
+		size.addAndGet((SimpleString.sizeofString(key) + val.encodeSize()) * -1);
+		
 		if (val == null)
 		{
          return null;

Modified: trunk/tests/src/org/jboss/messaging/tests/unit/core/message/impl/MessageTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/core/message/impl/MessageTest.java	2008-05-14 21:59:18 UTC (rev 4201)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/core/message/impl/MessageTest.java	2008-05-14 23:29:46 UTC (rev 4202)
@@ -269,8 +269,11 @@
       properties.putDoubleProperty(new SimpleString("str10"), (double) 1);
       properties.putCharProperty(new SimpleString("str11"), 'a');
       
-      checkSizes(properties);
+      checkSizes(properties, new TypedProperties());
       
+      properties.removeProperty(new SimpleString("str"));
+      checkSizes(properties, new TypedProperties());
+      
    }
 
    public void testEncodingMessage() throws Exception
@@ -288,21 +291,42 @@
       implMsg.setDestination(address);
       implMsg.setBody(bufferBody);
       implMsg.putStringProperty(new SimpleString("Key"), new SimpleString("This String is worthless!"));
+      implMsg.putStringProperty(new SimpleString("Key"), new SimpleString("This String is worthless and bigger!"));
+      implMsg.putStringProperty(new SimpleString("Key2"), new SimpleString("This String is worthless and bigger and bigger!"));
+      implMsg.removeProperty(new SimpleString("Key2"));
 
-      checkSizes(implMsg);
+      checkSizes(implMsg, new ServerMessageImpl());
 
       implMsg.removeProperty(new SimpleString("Key"));
       
-      checkSizes(implMsg);
+      checkSizes(implMsg, new ServerMessageImpl());
 
    }
    
-   private void checkSizes(EncodingSupport obj)
+   private void checkSizes(EncodingSupport obj, EncodingSupport newObject)
    {
       ByteBuffer bf = ByteBuffer.allocateDirect(1024);
       ByteBufferWrapper buffer = new ByteBufferWrapper(bf);
       obj.encode(buffer);
       assertEquals (buffer.position(), obj.encodeSize());
+      int originalSize = buffer.position();
+      
+      bf.rewind();
+      newObject.decode(buffer);
+      
+      log.info("Obj.size = " + obj.encodeSize() + " newObject.size = " + newObject.encodeSize());
+      
+      bf = ByteBuffer.allocateDirect(1024 * 10);
+      buffer = new ByteBufferWrapper(bf);
+      
+      newObject.encode(buffer);
+      
+      assertEquals(newObject.encodeSize(), bf.position());
+      assertEquals(originalSize, bf.position());
+      
+      
+      
+      
    }
    
 




More information about the jboss-cvs-commits mailing list