Check key/value size to mimic RAMCloud limit.

Note: In RAMCloud spec, it is defined as Key <= 64KB, Value Blob <= 1MB.

Change-Id: I3924f50f27353238d7d5859bb2d644a7968955c9
diff --git a/pom.xml b/pom.xml
index 191a7c8..955c8ca 100644
--- a/pom.xml
+++ b/pom.xml
@@ -577,6 +577,11 @@
       <version>3.1</version>
     </dependency>
     <dependency>
+      <groupId>commons-collections</groupId>
+      <artifactId>commons-collections</artifactId>
+      <version>3.2.1</version>
+    </dependency>
+    <dependency>
       <groupId>org.apache.zookeeper</groupId>
       <artifactId>zookeeper</artifactId>
       <version>3.4.5</version>
diff --git a/src/main/java/net/onrc/onos/core/datastore/DataStoreClient.java b/src/main/java/net/onrc/onos/core/datastore/DataStoreClient.java
index b5c1ae7..1215770 100644
--- a/src/main/java/net/onrc/onos/core/datastore/DataStoreClient.java
+++ b/src/main/java/net/onrc/onos/core/datastore/DataStoreClient.java
@@ -5,6 +5,10 @@
 
 // This class probably need to be a service
 public final class DataStoreClient {
+
+    public static final int MAX_KEY_BYTES = 64 * 1024;
+    public static final int MAX_VALUE_BYTES = 1024 * 1024;
+
     private static final String BACKEND = System.getProperty("net.onrc.onos.core.datastore.backend", "hazelcast");
 
     // Suppresses default constructor, ensuring non-instantiability.
diff --git a/src/main/java/net/onrc/onos/core/datastore/hazelcast/HZTable.java b/src/main/java/net/onrc/onos/core/datastore/hazelcast/HZTable.java
index c83e45d..da2c102 100644
--- a/src/main/java/net/onrc/onos/core/datastore/hazelcast/HZTable.java
+++ b/src/main/java/net/onrc/onos/core/datastore/hazelcast/HZTable.java
@@ -7,12 +7,14 @@
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicLong;
 
+import net.onrc.onos.core.datastore.DataStoreClient;
 import net.onrc.onos.core.datastore.IKVTable;
 import net.onrc.onos.core.datastore.IKVTableID;
 import net.onrc.onos.core.datastore.ObjectDoesntExistException;
 import net.onrc.onos.core.datastore.ObjectExistsException;
 import net.onrc.onos.core.datastore.WrongVersionException;
 
+import org.apache.commons.collections.BufferOverflowException;
 import org.apache.commons.lang.ArrayUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -69,7 +71,7 @@
         }
 
         public VersionedValue(final byte[] value, final long version) {
-            this.value = ArrayUtils.clone(value);
+            setValue(value);
             this.version = version;
         }
 
@@ -82,6 +84,9 @@
         }
 
         public void setValue(final byte[] value) {
+            if (value != null && value.length > DataStoreClient.MAX_VALUE_BYTES) {
+                throw new BufferOverflowException("Value must be smaller than 1MB");
+            }
             this.value = ArrayUtils.clone(value);
         }
 
@@ -154,6 +159,9 @@
         long version;
 
         public Entry(final byte[] key, final byte[] value, final long version) {
+            if (key.length > DataStoreClient.MAX_KEY_BYTES) {
+                throw new BufferOverflowException("Key must be smaller than 64KB");
+            }
             this.key = key.clone();
             this.setValue(value);
             this.setVersion(version);