edits
[iotcloud.git] / version2 / src / C / KeyValue.cc
1 #include "KeyValue.h"
2 #include "ByteBuffer.h"
3 #include "IoTString.h"
4 /**
5  * KeyValue entry for Slot.
6  * @author Brian Demsky <bdemsky@uci.edu>
7  * @version 1.0
8  */
9
10 KeyValue *KeyValue_decode(ByteBuffer *bb) {
11         int keylength = bb->getInt();
12         int valuelength = bb->getInt();
13         Array<char> *key = new Array<char>(keylength);
14         bb->get(key);
15
16         if (valuelength != 0) {
17                 Array<char> *value = new Array<char>(valuelength);
18                 bb->get(value);
19                 return new KeyValue(IoTString_shallow(key), IoTString_shallow(value));
20         }
21
22         return new KeyValue(IoTString_shallow(key), NULL);
23 }
24
25 void KeyValue::encode(ByteBuffer *bb) {
26         bb->putInt(key->length());
27
28         if (value != NULL) {
29                 bb->putInt(value->length());
30         } else {
31                 bb->putInt(0);
32         }
33
34         bb->put(key->internalBytes());
35
36         if (value != NULL) {
37                 bb->put(value->internalBytes());
38         }
39 }
40
41 int KeyValue::getSize() {
42         if (value != NULL) {
43                 return 2 * sizeof(int32_t) + key->length() + value->length();
44         }
45
46         return 2 * sizeof(int32_t) + key->length();
47 }
48
49 KeyValue *KeyValue::getCopy() {
50         return new KeyValue(key, value);
51 }