edits
[iotcloud.git] / version2 / src / C / KeyValue.cc
index 0be46c580691076b81e5b3856dbcce54fed91b84..8aea6f2900550a2b33e0712ea60b06842ed82ab4 100644 (file)
@@ -1,74 +1,51 @@
-
+#include "KeyValue.h"
+#include "ByteBuffer.h"
+#include "IoTString.h"
 /**
  * KeyValue entry for Slot.
  * @author Brian Demsky <bdemsky@uci.edu>
  * @version 1.0
  */
 
-class KeyValue { /*extends Entry */
-       IoTString key;
-       IoTString value;
-
-       KeyValue(IoTString _key, IoTString _value) {
-               key = _key;
-               value = _value;
-       }
-
-       IoTString getKey() {
-               return key;
-       }
-
-       IoTString getValue() {
-               return value;
-       }
-
-       static KeyValue decode(ByteBuffer bb) {
-               int keylength = bb->getInt();
-               int valuelength = bb->getInt();
-               char[] key = new char[keylength];
-               bb->get(key);
+KeyValue::~KeyValue() {
+       delete key;
+       delete value;
+}
 
-               if (valuelength != 0) {
-                       char[] value = new char[valuelength];
-                       bb->get(value);
-                       return new KeyValue(IoTString.shallow(key), IoTString.shallow(value));
-               }
+KeyValue *KeyValue_decode(ByteBuffer *bb) {
+       int keylength = bb->getInt();
+       int valuelength = bb->getInt();
+       Array<char> *key = new Array<char>(keylength);
+       bb->get(key);
 
-               return new KeyValue(IoTString.shallow(key), NULL);
+       if (valuelength != 0) {
+               Array<char> *value = new Array<char>(valuelength);
+               bb->get(value);
+               return new KeyValue(IoTString_shallow(key), IoTString_shallow(value));
        }
 
-       void encode(ByteBuffer bb) {
-               bb->putInt(key.length());
-
-               if (value != NULL) {
-                       bb->putInt(value.length());
-               } else {
-                       bb->putInt(0);
-               }
-
-               bb->put(key.internalBytes());
+       return new KeyValue(IoTString_shallow(key), NULL);
+}
 
-               if (value != NULL) {
-                       bb->put(value.internalBytes());
-               }
+void KeyValue::encode(ByteBuffer *bb) {
+       bb->putInt(key->length());
+       if (value != NULL) {
+               bb->putInt(value->length());
+       } else {
+               bb->putInt(0);
        }
-
-       int getSize() {
-               if (value != NULL) {
-                       return 2 * sizeof(int32_t) + key.length() + value.length();
-               }
-
-               return 2 * sizeof(int32_t) + key.length();
+       bb->put(key->internalBytes());
+       if (value != NULL) {
+               bb->put(value->internalBytes());
        }
+}
 
-       String toString() {
-               if (value == NULL) {
-                       return "NULL";
-               }
-               return value.toString();
-       }
+int KeyValue::getSize() {
+       if (value != NULL)
+               return 2 * sizeof(int32_t) + key->length() + value->length();
+       return 2 * sizeof(int32_t) + key->length();
+}
 
-       KeyValue getCopy() {
-               return new KeyValue(key, value);
-       }
+KeyValue *KeyValue::getCopy() {
+       return new KeyValue(new IoTString(key), new IoTString(value));
 }