+#ifndef KEYVALUE_H
+#define KEYVALUE_H
+#include "common.h"
+#include "IoTString.h"
/**
* KeyValue entry for Slot.
* @version 1.0
*/
-class KeyValue { /*extends Entry */
- private IoTString key;
- private IoTString value;
+class KeyValue {/*extends Entry */
+private:
+ IoTString *key;
+ IoTString *value;
- public KeyValue(IoTString _key, IoTString _value) {
- key = _key;
- value = _value;
+public:
+ KeyValue(IoTString *_key, IoTString *_value) :
+ key(_key),
+ value(_value) {
}
+ ~KeyValue();
- public IoTString getKey() {
- return key;
- }
-
- public 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);
-
- if (valuelength != 0) {
- char[] value = new char[valuelength];
- bb.get(value);
- return new KeyValue(IoTString.shallow(key), IoTString.shallow(value));
- }
-
- return new KeyValue(IoTString.shallow(key), NULL);
- }
-
- public void encode(ByteBuffer bb) {
- bb.putInt(key.length());
-
- if (value != NULL) {
- bb.putInt(value.length());
- } else {
- bb.putInt(0);
- }
+ IoTString *getKey() { return key; }
+ IoTString *getValue() { return value; }
+ void encode(ByteBuffer *bb);
+ int32_t getSize();
+ KeyValue *getCopy();
+};
- bb.put(key.internalBytes());
+KeyValue *KeyValue_decode(ByteBuffer *bb);
- if (value != NULL) {
- bb.put(value.internalBytes());
- }
- }
-
- public int getSize() {
- if (value != NULL) {
- return 2 * sizeof(int32_t) + key.length() + value.length();
- }
-
- return 2 * sizeof(int32_t) + key.length();
- }
-
- public String toString() {
- if (value == NULL) {
- return "NULL";
- }
- return value.toString();
- }
+inline unsigned int hashKeyValue(KeyValue *a) {
+ return a->getKey()->hashValue();
+}
- public KeyValue getCopy() {
- return new KeyValue(key, value);
- }
+inline bool KeyValueEquals(KeyValue *a, KeyValue *b) {
+ return a->getKey()->equals(b->getKey());
}
+#endif