summaryrefslogtreecommitdiff
path: root/src/key-value-store/hashtable
diff options
context:
space:
mode:
Diffstat (limited to 'src/key-value-store/hashtable')
-rw-r--r--src/key-value-store/hashtable/md5.h50
-rw-r--r--src/key-value-store/hashtable/md5c.c296
-rw-r--r--src/key-value-store/hashtable/qhash.c155
-rw-r--r--src/key-value-store/hashtable/qhash.h64
-rw-r--r--src/key-value-store/hashtable/qhasharr.c869
-rw-r--r--src/key-value-store/hashtable/qhasharr.h136
-rw-r--r--src/key-value-store/hashtable/qlibc.h55
-rw-r--r--src/key-value-store/hashtable/qtype.h123
8 files changed, 1748 insertions, 0 deletions
diff --git a/src/key-value-store/hashtable/md5.h b/src/key-value-store/hashtable/md5.h
new file mode 100644
index 0000000..cfcf6c6
--- /dev/null
+++ b/src/key-value-store/hashtable/md5.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
+ * rights reserved.
+ *
+ * License to copy and use this software is granted provided that it
+ * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
+ * Algorithm" in all material mentioning or referencing this software
+ * or this function.
+ *
+ * License is also granted to make and use derivative works provided
+ * that such works are identified as "derived from the RSA Data
+ * Security, Inc. MD5 Message-Digest Algorithm" in all material
+ * mentioning or referencing the derived work.
+ *
+ * RSA Data Security, Inc. makes no representations concerning either
+ * the merchantability of this software or the suitability of this
+ * software for any particular purpose. It is provided "as is"
+ * without express or implied warranty of any kind.
+ *
+ * These notices must be retained in any copies of any part of this
+ * documentation and/or software.
+ */
+
+#ifndef _Q_MD5_H_
+#define _Q_MD5_H_
+
+#define MD5_BLOCK_LENGTH 64
+#define MD5_DIGEST_LENGTH 16
+#define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1)
+
+/* MD5 context. */
+typedef struct MD5Context {
+ u_int32_t state[4]; /* state (ABCD) */
+ u_int32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
+ unsigned char buffer[64]; /* input buffer */
+} MD5_CTX;
+
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+void MD5Init(MD5_CTX *);
+void MD5Update(MD5_CTX *, const unsigned char *, unsigned int);
+void MD5Final(unsigned char[16], MD5_CTX *);
+char * MD5End(MD5_CTX *, char *);
+char * MD5File(const char *, char *);
+char * MD5FileChunk(const char *, char *, off_t, off_t);
+char * MD5Data(const unsigned char *, unsigned int, char *);
+__END_DECLS
+
+#endif /* _Q_MD5_H_ */
diff --git a/src/key-value-store/hashtable/md5c.c b/src/key-value-store/hashtable/md5c.c
new file mode 100644
index 0000000..de9e47f
--- /dev/null
+++ b/src/key-value-store/hashtable/md5c.c
@@ -0,0 +1,296 @@
+/*
+ * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
+ *
+ * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
+ * rights reserved.
+ *
+ * License to copy and use this software is granted provided that it
+ * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
+ * Algorithm" in all material mentioning or referencing this software
+ * or this function.
+ *
+ * License is also granted to make and use derivative works provided
+ * that such works are identified as "derived from the RSA Data
+ * Security, Inc. MD5 Message-Digest Algorithm" in all material
+ * mentioning or referencing the derived work.
+ *
+ * RSA Data Security, Inc. makes no representations concerning either
+ * the merchantability of this software or the suitability of this
+ * software for any particular purpose. It is provided "as is"
+ * without express or implied warranty of any kind.
+ *
+ * These notices must be retained in any copies of any part of this
+ * documentation and/or software.
+ *
+ * This code is the same as the code published by RSA Inc. It has been
+ * edited for clarity and style only.
+ */
+
+#include <sys/cdefs.h>
+#include <sys/types.h>
+#include <string.h>
+#include "md5.h"
+
+static void MD5Transform( u_int32_t[4], const unsigned char[64]);
+
+#if (BYTE_ORDER == LITTLE_ENDIAN)
+#define Encode memcpy
+#define Decode memcpy
+#else
+
+/*
+ * Encodes input (u_int32_t) into output (unsigned char). Assumes len is
+ * a multiple of 4.
+ */
+
+static void Encode (unsigned char *output, u_int32_t *input, unsigned int len) {
+ unsigned int i;
+ u_int32_t *op = (u_int32_t *)output;
+
+ for (i = 0; i < len / 4; i++)
+ op[i] = htole32(input[i]);
+}
+
+/*
+ * Decodes input (unsigned char) into output (u_int32_t). Assumes len is
+ * a multiple of 4.
+ */
+
+static void Decode (u_int32_t *output, const unsigned char *input, unsigned int len) {
+ unsigned int i;
+ const u_int32_t *ip = (const u_int32_t *)input;
+
+ for (i = 0; i < len / 4; i++)
+ output[i] = le32toh(ip[i]);
+}
+#endif
+
+static unsigned char PADDING[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0 };
+
+/* F, G, H and I are basic MD5 functions. */
+#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
+#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
+#define H(x, y, z) ((x) ^ (y) ^ (z))
+#define I(x, y, z) ((y) ^ ((x) | (~z)))
+
+/* ROTATE_LEFT rotates x left n bits. */
+#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
+
+/*
+ * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
+ * Rotation is separate from addition to prevent recomputation.
+ */
+#define FF(a, b, c, d, x, s, ac) { \
+ (a) += F ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+ }
+#define GG(a, b, c, d, x, s, ac) { \
+ (a) += G ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+ }
+#define HH(a, b, c, d, x, s, ac) { \
+ (a) += H ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+ }
+#define II(a, b, c, d, x, s, ac) { \
+ (a) += I ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+ }
+
+/* MD5 initialization. Begins an MD5 operation, writing a new context. */
+
+void MD5Init(MD5_CTX *context) {
+
+ context->count[0] = context->count[1] = 0;
+
+ /* Load magic initialization constants. */
+ context->state[0] = 0x67452301;
+ context->state[1] = 0xefcdab89;
+ context->state[2] = 0x98badcfe;
+ context->state[3] = 0x10325476;
+}
+
+/*
+ * MD5 block update operation. Continues an MD5 message-digest
+ * operation, processing another message block, and updating the
+ * context.
+ */
+
+void MD5Update(MD5_CTX *context, const unsigned char *input,
+ unsigned int inputLen) {
+ unsigned int i, idx, partLen;
+
+ /* Compute number of bytes mod 64 */
+ idx = (unsigned int) ((context->count[0] >> 3) & 0x3F);
+
+ /* Update number of bits */
+ if ((context->count[0] += ((u_int32_t) inputLen << 3))
+ < ((u_int32_t) inputLen << 3))
+ context->count[1]++;
+ context->count[1] += ((u_int32_t) inputLen >> 29);
+
+ partLen = 64 - idx;
+
+ /* Transform as many times as possible. */
+ if (inputLen >= partLen) {
+ memcpy((void *) &context->buffer[idx], (const void *) input, partLen);
+ MD5Transform(context->state, context->buffer);
+
+ for (i = partLen; i + 63 < inputLen; i += 64)
+ MD5Transform(context->state, &input[i]);
+
+ idx = 0;
+ } else
+ i = 0;
+
+ /* Buffer remaining input */
+ memcpy((void *) &context->buffer[idx], (const void *) &input[i],
+ inputLen - i);
+}
+
+/*
+ * MD5 padding. Adds padding followed by original length.
+ */
+
+static void MD5Pad(MD5_CTX *context) {
+ unsigned char bits[8];
+ unsigned int idx, padLen;
+
+ /* Save number of bits */
+ Encode(bits, context->count, 8);
+
+ /* Pad out to 56 mod 64. */
+ idx = (unsigned int) ((context->count[0] >> 3) & 0x3f);
+ padLen = (idx < 56) ? (56 - idx) : (120 - idx);
+ MD5Update(context, PADDING, padLen);
+
+ /* Append length (before padding) */
+ MD5Update(context, bits, 8);
+}
+
+/*
+ * MD5 finalization. Ends an MD5 message-digest operation, writing the
+ * the message digest and zeroizing the context.
+ */
+
+void MD5Final(unsigned char digest[16], MD5_CTX *context) {
+ /* Do padding. */
+ MD5Pad(context);
+
+ /* Store state in digest */
+ Encode(digest, context->state, 16);
+
+ /* Zeroize sensitive information. */
+ memset((void *) context, 0, sizeof(*context));
+}
+
+/* MD5 basic transformation. Transforms state based on block. */
+
+static void MD5Transform(u_int32_t state[4], const unsigned char block[64]) {
+ u_int32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
+
+ Decode(x, block, 64);
+
+ /* Round 1 */
+#define S11 7
+#define S12 12
+#define S13 17
+#define S14 22
+ FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
+ FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
+ FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
+ FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
+ FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
+ FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
+ FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
+ FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
+ FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
+ FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
+ FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
+ FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
+ FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
+ FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
+ FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
+ FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
+
+ /* Round 2 */
+#define S21 5
+#define S22 9
+#define S23 14
+#define S24 20
+ GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
+ GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
+ GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
+ GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
+ GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
+ GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
+ GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
+ GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
+ GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
+ GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
+ GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
+ GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
+ GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
+ GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
+ GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
+ GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
+
+ /* Round 3 */
+#define S31 4
+#define S32 11
+#define S33 16
+#define S34 23
+ HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
+ HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
+ HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
+ HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
+ HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
+ HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
+ HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
+ HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
+ HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
+ HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
+ HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
+ HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
+ HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
+ HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
+ HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
+ HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
+
+ /* Round 4 */
+#define S41 6
+#define S42 10
+#define S43 15
+#define S44 21
+ II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
+ II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
+ II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
+ II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
+ II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
+ II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
+ II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
+ II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
+ II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
+ II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
+ II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
+ II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
+ II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
+ II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
+ II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
+ II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
+
+ state[0] += a;
+ state[1] += b;
+ state[2] += c;
+ state[3] += d;
+
+ /* Zeroize sensitive information. */
+ memset((void *) x, 0, sizeof(x));
+}
diff --git a/src/key-value-store/hashtable/qhash.c b/src/key-value-store/hashtable/qhash.c
new file mode 100644
index 0000000..e7709aa
--- /dev/null
+++ b/src/key-value-store/hashtable/qhash.c
@@ -0,0 +1,155 @@
+/******************************************************************************
+ * qLibc
+ *
+ * Copyright (c) 2010-2014 Seungyoung Kim.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *****************************************************************************/
+
+/**
+ * @file qhash.c Hash APIs.
+ */
+/*
+ * Modified parts of this file by XS Embedded GmbH, 2014
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include "md5.h"
+#include "qhash.h"
+
+/**
+ * Calculate 128-bit(16-bytes) MD5 hash.
+ *
+ * @param data source object
+ * @param nbytes size of data
+ * @param retbuf user buffer. It must be at leat 16-bytes long.
+ *
+ * @return true if successful, otherwise false.
+ *
+ * @code
+ * // get MD5
+ * unsigned char md5hash[16];
+ * qhashmd5((void*)"hello", 5, md5hash);
+ *
+ * // hex encode
+ * char *md5ascii = qhex_encode(md5hash, 16);
+ * printf("Hex encoded MD5: %s\n", md5ascii);
+ * free(md5ascii);
+ * @endcode
+ */
+bool qhashmd5(const void *data, size_t nbytes, void *retbuf) {
+ if (data == NULL || retbuf == NULL) {
+ errno = EINVAL;
+ return false;
+ }
+
+ MD5_CTX context;
+ MD5Init(&context);
+ MD5Update(&context, (unsigned char *) data, (unsigned int) nbytes);
+ MD5Final(retbuf, &context);
+
+ return true;
+}
+
+
+/**
+ * Get 32-bit Murmur3 hash.
+ *
+ * @param data source data
+ * @param nbytes size of data
+ *
+ * @return 32-bit unsigned hash value.
+ *
+ * @code
+ * uint32_t hashval = qhashmurmur3_32((void*)"hello", 5);
+ * @endcode
+ *
+ * @code
+ * MurmurHash3 was created by Austin Appleby in 2008. The initial
+ * implementation was published in C++ and placed in the public.
+ * https://sites.google.com/site/murmurhash/
+ * Seungyoung Kim has ported its implementation into C language
+ * in 2012 and published it as a part of qLibc component.
+ * @endcode
+ */
+uint32_t qhashmurmur3_32(const void *data, size_t nbytes) {
+ if (data == NULL || nbytes == 0)
+ return 0;
+
+ const uint32_t c1 = 0xcc9e2d51;
+ const uint32_t c2 = 0x1b873593;
+
+ const int nblocks = nbytes / 4;
+ const uint32_t *blocks = (const uint32_t *) (data);
+ const uint8_t *tail = (const uint8_t *) (data + (nblocks * 4));
+
+ uint32_t h = 0;
+
+ int i;
+ uint32_t k;
+ for (i = 0; i < nblocks; i++) {
+ k = blocks[i];
+
+ k *= c1;
+ k = (k << 15) | (k >> (32 - 15));
+ k *= c2;
+
+ h ^= k;
+ h = (h << 13) | (h >> (32 - 13));
+ h = (h * 5) + 0xe6546b64;
+ }
+
+ k = 0;
+ switch (nbytes & 3) {
+ case 3:
+ k ^= tail[2] << 16;
+ case 2:
+ k ^= tail[1] << 8;
+ case 1:
+ k ^= tail[0];
+ k *= c1;
+ k = (k << 15) | (k >> (32 - 15));
+ k *= c2;
+ h ^= k;
+ };
+
+ h ^= nbytes;
+
+ h ^= h >> 16;
+ h *= 0x85ebca6b;
+ h ^= h >> 13;
+ h *= 0xc2b2ae35;
+ h ^= h >> 16;
+
+ return h;
+}
+
diff --git a/src/key-value-store/hashtable/qhash.h b/src/key-value-store/hashtable/qhash.h
new file mode 100644
index 0000000..b873799
--- /dev/null
+++ b/src/key-value-store/hashtable/qhash.h
@@ -0,0 +1,64 @@
+/******************************************************************************
+ * qLibc
+ *
+ * Copyright (c) 2010-2014 Seungyoung Kim.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *****************************************************************************/
+
+/**
+ * qhash header file.
+ *
+ * @file qhash.h
+ */
+
+/*
+ * Modified parts of this file by XS Embedded GmbH, 2014
+ */
+
+#ifndef _QHASH_H
+#define _QHASH_H
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern bool qhashmd5(const void *data, size_t nbytes, void *retbuf);
+//extern bool qhashmd5_file(const char *filepath, off_t offset, ssize_t nbytes,
+// void *retbuf);
+
+//extern uint32_t qhashfnv1_32(const void *data, size_t nbytes);
+//extern uint64_t qhashfnv1_64(const void *data, size_t nbytes);
+
+extern uint32_t qhashmurmur3_32(const void *data, size_t nbytes);
+//extern bool qhashmurmur3_128(const void *data, size_t nbytes, void *retbuf);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /*_QHASH_H */
diff --git a/src/key-value-store/hashtable/qhasharr.c b/src/key-value-store/hashtable/qhasharr.c
new file mode 100644
index 0000000..97169bf
--- /dev/null
+++ b/src/key-value-store/hashtable/qhasharr.c
@@ -0,0 +1,869 @@
+/******************************************************************************
+ * qLibc
+ *
+ * Copyright (c) 2010-2014 Seungyoung Kim.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *****************************************************************************/
+
+/**
+ * @file qhasharr.c Static(array) hash-table implementation.
+ *
+ * qhasharr implements a hash-table which maps keys to values and stores into
+ * fixed size static memory like shared-memory and memory-mapped file.
+ * The creator qhasharr() initializes static memory to makes small slots in it.
+ * The default slot size factors are defined in _Q_HASHARR_KEYSIZE and
+ * _Q_HASHARR_VALUESIZE. And they are applied at compile time.
+ *
+ * The value part of an element will be stored across several slots if it's size
+ * exceeds the slot size. But the key part of an element will be truncated if
+ * the size exceeds and it's length and more complex MD5 hash value will be
+ * stored with the key. So to look up a particular key, first we find an element
+ * which has same hash value. If the key was not truncated, we just do key
+ * comparison. But if the key was truncated because it's length exceeds, we do
+ * both md5 and key comparison(only stored size) to verify that the key is same.
+ * So please be aware of that, theoretically there is a possibility we pick
+ * wrong element in case a key exceeds the limit, has same length and MD5 hash
+ * with lookup key. But this possibility is extreamly low and almost never
+ * happen in practice. If you happpen to want to make sure everything,
+ * you set _Q_HASHARR_KEYSIZE big enough at compile time to make sure all keys
+ * fits in it.
+ *
+ * qhasharr hash-table does not support thread-safe. So users should handle
+ * race conditions on application side by raising user lock before calling
+ * functions which modify the table data.
+ *
+ * @code
+ * [Data Structure Diagram]
+ *
+ * +--[Static Flat Memory Area]-----------------------------------------------+
+ * | +-[Header]---------+ +-[Slot 0]---+ +-[Slot 1]---+ +-[Slot N]---+ |
+ * | |Private table data| |KEY A|DATA A| |KEY B|DATA B| .... |KEY N|DATA N| |
+ * | +------------------+ +------------+ +------------+ +------------+ |
+ * +--------------------------------------------------------------------------+
+ *
+ * Below diagram shows how a big value is stored.
+ * +--[Static Flat Memory Area------------------------------------------------+
+ * | +--------+ +-[Slot 0]---+ +-[Slot 1]---+ +-[Slot 2]---+ +-[Slot 3]-----+ |
+ * | |TBL INFO| |KEY A|DATA A| |DATA A cont.| |KEY B|DATA B| |DATA A cont. | |
+ * | +--------+ +------------+ +------------+ +------------+ +--------------+ |
+ * | ^~~link~~^ ^~~~~~~~~~link~~~~~~~~~^ |
+ * +--------------------------------------------------------------------------+
+ * @endcode
+ *
+ * @code
+ * // initialize hash-table.
+ * char memory[1000 * 10];
+ * qhasharr_t *tbl = qhasharr(memory, sizeof(memory));
+ * if(tbl == NULL) return;
+ *
+ * // insert elements (key duplication does not allowed)
+ * tbl->putstr(tbl, "e1", "a");
+ * tbl->putstr(tbl, "e2", "b");
+ * tbl->putstr(tbl, "e3", "c");
+ *
+ * // debug print out
+ * tbl->//DEBUG(tbl, stdout);
+ *
+ * char *e2 = tbl->getstr(tbl, "e2");
+ * if(e2 != NULL) {
+ * printf("getstr('e2') : %s\n", e2);
+ * free(e2);
+ * }
+ *
+ * // Release reference object.
+ * tbl->free(tbl);
+ * @endcode
+ *
+ * An example for using hash table over shared memory.
+ *
+ * @code
+ * [CREATOR SIDE]
+ * int maxslots = 1000;
+ * int memsize = qhasharr_calculate_memsize(maxslots);
+ *
+ * // create shared memory
+ * int shmid = qshm_init("/tmp/some_id_file", 'q', memsize, true);
+ * if(shmid < 0) return -1; // creation failed
+ * void *memory = qshm_get(shmid);
+ *
+ * // initialize hash-table
+ * qhasharr_t *tbl = qhasharr(memory, memsize);
+ * if(hasharr == NULL) return -1;
+ *
+ * (...your codes with your own locking mechanism...)
+ *
+ * // Release reference object
+ * tbl->free(tbl);
+ *
+ * // destroy shared memory
+ * qshm_free(shmid);
+ *
+ * [USER SIDE]
+ * int shmid = qshm_getid("/tmp/some_id_file", 'q');
+ *
+ * // get shared memory
+ * void *memory = qshm_get(shmid);
+ *
+ * // map existing memory into table
+ * qhasharr_t *tbl = qhasharr(memory, 0);
+ *
+ * (...your codes with your own locking mechanism...)
+ *
+ * // Release reference object
+ * tbl->free(tbl);
+ * @endcode
+ */
+
+/*
+ * Modified parts of this file by XS Embedded GmbH, 2014
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+#include "qhash.h"
+#include "qhasharr.h"
+
+#ifndef _DOXYGEN_SKIP
+
+static bool put(qhasharr_t *tbl, const char *key, const void *value,
+ size_t size);
+
+static void *get(qhasharr_t *tbl, const char *key, size_t *size);
+
+static bool getnext(qhasharr_t *tbl, qnobj_t *obj, int *idx);
+
+static bool remove_(qhasharr_t *tbl, const char *key);
+
+static int size(qhasharr_t *tbl, int *maxslots, int *usedslots);
+
+static void free_(qhasharr_t *tbl);
+
+// internal usages
+static int _find_empty(qhasharr_t *tbl, int startidx);
+static int _get_idx(qhasharr_t *tbl, const char *key, unsigned int hash);
+static void *_get_data(qhasharr_t *tbl, int idx, size_t *size);
+static bool _put_data(qhasharr_t *tbl, int idx, unsigned int hash,
+ const char *key, const void *value, size_t size,
+ int count);
+static bool _copy_slot(qhasharr_t *tbl, int idx1, int idx2);
+static bool _remove_slot(qhasharr_t *tbl, int idx);
+static bool _remove_data(qhasharr_t *tbl, int idx);
+
+#endif
+
+/**
+ * Get how much memory is needed for N slots.
+ *
+ * @param max a number of maximum internal slots
+ *
+ * @return memory size needed
+ *
+ * @note
+ * This can be used for calculating minimum memory size for N slots.
+ */
+size_t qhasharr_calculate_memsize(int max) {
+ size_t memsize = sizeof(qhasharr_data_t)
+ + (sizeof(qhasharr_slot_t) * (max));
+ return memsize;
+}
+
+/**
+ * Initialize static hash table
+ *
+ * @param memory a pointer of data memory.
+ * @param memsize a size of data memory, 0 for using existing data.
+ *
+ * @return qhasharr_t container pointer, otherwise returns NULL.
+ * @retval errno will be set in error condition.
+ * - EINVAL : Assigned memory is too small. It must bigger enough to allocate
+ * at least 1 slot.
+ *
+ * @code
+ * // initialize hash-table with 100 slots.
+ * // A single element can take several slots.
+ * char memory[112 * 100];
+ *
+ * // Initialize new table.
+ * qhasharr_t *tbl = qhasharr(memory, sizeof(memory));
+ *
+ * // Use existing table.
+ * qhasharr_t *tbl2 = qhasharr(memory, 0);
+ * @endcode
+ */
+qhasharr_t *qhasharr(void *memory, size_t memsize) {
+ // Structure memory.
+ qhasharr_data_t *data = (qhasharr_data_t *) memory;
+
+ // Initialize data if memsize is set or use existing data.
+ if (memsize > 0) {
+ // calculate max
+ int maxslots = (memsize - sizeof(qhasharr_data_t))
+ / sizeof(qhasharr_slot_t);
+ if (maxslots < 1 || memsize <= sizeof(qhasharr_t)) {
+ //errno = EINVAL;
+ return NULL;
+ }
+
+ // Set memory.
+ //memset((void *) data, 0, memsize); //TODO check if initialisation is needed
+ data->maxslots = maxslots;
+ data->usedslots = 0;
+ data->num = 0;
+ }
+
+ // Set data address. Shared memory returns virtul address.
+ data->slots = (qhasharr_slot_t *) (memory + sizeof(qhasharr_data_t));
+
+ // Create the table object.
+ qhasharr_t *tbl = (qhasharr_t *) malloc(sizeof(qhasharr_t));
+ if (tbl == NULL) {
+ //errno = ENOMEM;
+ return NULL;
+ }
+ memset((void *) tbl, 0, sizeof(qhasharr_t));
+
+ // assign methods
+ tbl->put = put;
+
+ tbl->get = get;
+
+ tbl->getnext = getnext;
+
+ tbl->remove = remove_;
+
+ tbl->size = size;
+
+ tbl->free = free_;
+
+ tbl->data = data;
+
+ return tbl;
+}
+
+/**
+ * qhasharr->put(): Put an object into this table.
+ *
+ * @param tbl qhasharr_t container pointer.
+ * @param key key string
+ * @param value value object data
+ * @param size size of value
+ *
+ * @return true if successful, otherwise returns false
+ * @retval errno will be set in error condition.
+ * - ENOBUFS : Table doesn't have enough space to store the object.
+ * - EINVAL : Invalid argument.
+ * - EFAULT : Unexpected error. Data structure is not constant.
+ */
+static bool put(qhasharr_t *tbl, const char *key, const void *value,
+ size_t size) {
+ if (tbl == NULL || key == NULL || value == NULL) {
+ //errno = EINVAL;
+ return false;
+ }
+
+ qhasharr_data_t *data = tbl->data;
+
+ // check full
+ if (data->usedslots >= data->maxslots || ((data->usedslots + (size / 32)) >= data->maxslots)) {
+ //DEBUG("hasharr: put %s - FULL", key);
+ //errno = ENOBUFS;
+ return false;
+ }
+
+ // get hash integer
+ unsigned int hash = qhashmurmur3_32(key, strlen(key)) % data->maxslots;
+
+ // check, is slot empty
+ if (data->slots[hash].count == 0) { // empty slot
+ // put data
+ if (_put_data(tbl, hash, hash, key, value, size, 1) == false) {
+ //DEBUG("hasharr: FAILED put(new) %s", key);
+ return false;
+ } //DEBUG("hasharr: put(new) %s (idx=%d,hash=%u,tot=%d)",
+ // key, hash, hash, data->usedslots);
+ } else if (data->slots[hash].count > 0) { // same key or hash collision
+ // check same key;
+ int idx = _get_idx(tbl, key, hash);
+ if (idx >= 0) { // same key
+ // remove and recall
+ remove_(tbl, key);
+ //printf("overwriting existent key 2%s\n", key);
+ return put(tbl, key, value, size);
+ } else { // no same key, just hash collision
+ // find empty slot
+ int idx = _find_empty(tbl, hash);
+ if (idx < 0) {
+ //errno = ENOBUFS;
+ return false;
+ }
+
+ // put data. -1 is used for collision resolution (idx != hash);
+ if (_put_data(tbl, idx, hash, key, value, size, -1) == false) {
+ //DEBUG("hasharr: FAILED put(col) %s", key);
+ return false;
+ }
+
+ // increase counter from leading slot
+ data->slots[hash].count++;
+
+ //DEBUG("hasharr: put(col) %s (idx=%d,hash=%u,tot=%d)",
+ // key, idx, hash, data->usedslots);
+ }
+ } else {
+ // in case of -1 or -2, move it. -1 used for collision resolution,
+ // -2 used for oversized value data.
+
+ // find empty slot
+ int idx = _find_empty(tbl, hash + 1);
+ if (idx < 0) {
+ //errno = ENOBUFS;
+ return false;
+ }
+
+ // move dup slot to empty
+ _copy_slot(tbl, idx, hash);
+ _remove_slot(tbl, hash);
+
+ // in case of -2, adjust link of mother
+ if (data->slots[idx].count == -2) {
+ data->slots[data->slots[idx].hash].link = idx;
+ if (data->slots[idx].link != -1) {
+ data->slots[data->slots[idx].link].hash = idx;
+ }
+ }
+
+ // store data
+ if (_put_data(tbl, hash, hash, key, value, size, 1) == false) {
+ //DEBUG("hasharr: FAILED put(swp) %s", key);
+ return false;
+ }
+
+ //DEBUG("hasharr: put(swp) %s (idx=%u,hash=%u,tot=%d)",
+ // key, hash, hash, data->usedslots);
+ }
+ return true;
+}
+
+
+
+
+/**
+ * qhasharr->get(): Get an object from this table
+ *
+ * @param tbl qhasharr_t container pointer.
+ * @param key key string
+ * @param size if not NULL, oject size will be stored
+ *
+ * @return malloced object pointer if successful, otherwise(not found)
+ * returns NULL
+ * @retval errno will be set in error condition.
+ * - ENOENT : No such key found.
+ * - EINVAL : Invalid argument.
+ * - ENOMEM : Memory allocation failed.
+ *
+ * @note
+ * returned object must be freed after done using.
+ */
+static void *get(qhasharr_t *tbl, const char *key, size_t *size) {
+ if (tbl == NULL || key == NULL) {
+ //errno = EINVAL;
+ return NULL;
+ }
+
+ qhasharr_data_t *data = tbl->data;
+
+ // get hash integer
+ unsigned int hash = qhashmurmur3_32(key, strlen(key)) % data->maxslots;
+
+ int idx = _get_idx(tbl, key, hash);
+ if (idx < 0) {
+ //errno = ENOENT;
+ return NULL;
+ }
+
+ return _get_data(tbl, idx, size);
+}
+
+/**
+ * qhasharr->getnext(): Get next element.
+ *
+ * @param tbl qhasharr_t container pointer.
+ * @param idx index pointer
+ *
+ * @return key name string if successful, otherwise(end of table) returns NULL
+ * @retval errno will be set in error condition.
+ * - ENOENT : No next element.
+ * - EINVAL : Invald argument.
+ * - ENOMEM : Memory allocation failed.
+ *
+ * @code
+ * int idx = 0;
+ * qnobj_t obj;
+ * while(tbl->getnext(tbl, &obj, &idx) == true) {
+ * printf("NAME=%s, DATA=%s, SIZE=%zu\n",
+ * obj.name, (char*)obj.data, obj.size);
+ * free(obj.name);
+ * free(obj.data);
+ * }
+ * @endcode
+ *
+ * @note
+ * Please be aware a key name will be returned with truncated length
+ * because key name is truncated when it put into the table if it's length is
+ * longer than _Q_HASHARR_KEYSIZE.
+ */
+static bool getnext(qhasharr_t *tbl, qnobj_t *obj, int *idx) {
+ if (tbl == NULL || obj == NULL || idx == NULL) {
+ //errno = EINVAL;
+ return NULL;
+ }
+
+ qhasharr_data_t *data = tbl->data;
+
+ for (; *idx < data->maxslots; (*idx)++) {
+ if (data->slots[*idx].count == 0 || data->slots[*idx].count == -2) {
+ continue;
+ }
+
+ size_t keylen = data->slots[*idx].data.pair.keylen;
+ if (keylen > _Q_HASHARR_KEYSIZE)
+ keylen = _Q_HASHARR_KEYSIZE;
+
+ obj->name = (char *) malloc(keylen + 1);
+ if (obj->name == NULL) {
+ //errno = ENOMEM;
+ return false;
+ }
+ memcpy(obj->name, data->slots[*idx].data.pair.key, keylen);
+ obj->name[keylen] = '\0';
+
+ obj->data = _get_data(tbl, *idx, &obj->size);
+ if (obj->data == NULL) {
+ free(obj->name);
+ //errno = ENOMEM;
+ return false;
+ }
+
+ *idx += 1;
+ return true;
+ }
+
+ //errno = ENOENT;
+ return false;
+}
+
+/**
+ * qhasharr->remove(): Remove an object from this table.
+ *
+ * @param tbl qhasharr_t container pointer.
+ * @param key key string
+ *
+ * @return true if successful, otherwise(not found) returns false
+ * @retval errno will be set in error condition.
+ * - ENOENT : No such key found.
+ * - EINVAL : Invald argument.
+ * - EFAULT : Unexpected error. Data structure is not constant.
+ */
+static bool remove_(qhasharr_t *tbl, const char *key) {
+ if (tbl == NULL || key == NULL) {
+ //errno = EINVAL;
+ return false;
+ }
+
+ qhasharr_data_t *data = tbl->data;
+
+ // get hash integer
+ unsigned int hash = qhashmurmur3_32(key, strlen(key)) % data->maxslots;
+
+ int idx = _get_idx(tbl, key, hash);
+ if (idx < 0) {
+ //DEBUG("not found %s", key);
+ //errno = ENOENT;
+ return false;
+ }
+
+ if (data->slots[idx].count == 1) {
+ // just remove
+ _remove_data(tbl, idx);
+ //DEBUG("hasharr: rem %s (idx=%d,tot=%d)", key, idx, data->usedslots);
+ } else if (data->slots[idx].count > 1) { // leading slot and has dup
+ // find dup
+ int idx2;
+ for (idx2 = idx + 1;; idx2++)
+ {
+ if (idx2 >= data->maxslots)
+ idx2 = 0;
+ if (idx2 == idx) {
+ //DEBUG("hasharr: [BUG] failed to remove dup key %s.", key);
+ //errno = EFAULT;
+ return false;
+ }
+ if (data->slots[idx2].count == -1 && data->slots[idx2].hash == hash)
+ {
+ break;
+ }
+ }
+
+ // move to leading slot
+ int backupcount = data->slots[idx].count;
+ _remove_data(tbl, idx); // remove leading data
+ _copy_slot(tbl, idx, idx2); // copy slot
+ _remove_slot(tbl, idx2); // remove moved slot
+
+ data->slots[idx].count = backupcount - 1; // adjust collision counter
+ if (data->slots[idx].link != -1) {
+ data->slots[data->slots[idx].link].hash = idx;
+ }
+
+ //DEBUG("hasharr: rem(lead) %s (idx=%d,tot=%d)",
+ // key, idx, data->usedslots);
+ } else { // in case of -1. used for collision resolution
+ // decrease counter from leading slot
+ if (data->slots[data->slots[idx].hash].count <= 1) {
+ //DEBUG("hasharr: [BUG] failed to remove %s. "
+ // "counter of leading slot mismatch.", key);
+ //errno = EFAULT;
+ return false;
+ }
+ data->slots[data->slots[idx].hash].count--;
+
+ // remove data
+ _remove_data(tbl, idx);
+ //DEBUG("hasharr: rem(dup) %s (idx=%d,tot=%d)", key, idx, data->usedslots);
+ }
+
+ return true;
+}
+
+/**
+ * qhasharr->size(): Returns the number of objects in this table.
+ *
+ * @param tbl qhasharr_t container pointer.
+ *
+ * @return a number of elements stored.
+ */
+static int size(qhasharr_t *tbl, int *maxslots, int *usedslots) {
+ if (tbl == NULL) {
+ //errno = EINVAL;
+ return -1;
+ }
+
+ qhasharr_data_t *data = tbl->data;
+
+ if (maxslots != NULL)
+ *maxslots = data->maxslots;
+ if (usedslots != NULL)
+ *usedslots = data->usedslots;
+
+ return data->num;
+}
+
+
+/**
+ * qhasharr->free(): De-allocate table reference object.
+ *
+ * @param tbl qhashtbl_t container pointer.
+ *
+ * @note
+ * This does not de-allocate memory but only function reference object.
+ * Data memory such as shared memory must be de-allocated separately.
+ */
+void free_(qhasharr_t *tbl) {
+ free(tbl);
+}
+
+#ifndef _DOXYGEN_SKIP
+
+// find empty slot : return empty slow number, otherwise returns -1.
+static int _find_empty(qhasharr_t *tbl, int startidx) {
+ qhasharr_data_t *data = tbl->data;
+
+ if (startidx >= data->maxslots)
+ startidx = 0;
+
+ int idx = startidx;
+ while (true) {
+ if (data->slots[idx].count == 0)
+ return idx;
+
+ idx++;
+ if (idx >= data->maxslots)
+ idx = 0;
+ if (idx == startidx)
+ break;
+ }
+
+ return -1;
+}
+
+static int _get_idx(qhasharr_t *tbl, const char *key, unsigned int hash) {
+ qhasharr_data_t *data = tbl->data;
+
+ if (data->slots[hash].count > 0) {
+ int count, idx;
+ for (count = 0, idx = hash; count < data->slots[hash].count;) {
+ if (data->slots[idx].hash == hash
+ && (data->slots[idx].count > 0
+ || data->slots[idx].count == -1)) {
+ // same hash
+ count++;
+
+ // is same key?
+ size_t keylen = strlen(key);
+ // first check key length
+ if (keylen == data->slots[idx].data.pair.keylen) {
+ if (keylen <= _Q_HASHARR_KEYSIZE) {
+ // original key is stored
+ if (!memcmp(key, data->slots[idx].data.pair.key, keylen))
+ {
+ return idx;
+ }
+ } else {
+ // key is truncated, compare MD5 also.
+ unsigned char keymd5[16];
+ qhashmd5(key, keylen, keymd5);
+ if (!memcmp(key, data->slots[idx].data.pair.key, _Q_HASHARR_KEYSIZE) && !memcmp(keymd5, data->slots[idx].data.pair.keymd5, 16))
+ {
+ return idx;
+ }
+ }
+ }
+ }
+
+ // increase idx
+ idx++;
+ if (idx >= data->maxslots)
+ idx = 0;
+
+ // check loop
+ if (idx == hash)
+ break;
+
+ continue;
+ }
+ }
+
+ return -1;
+}
+
+static void *_get_data(qhasharr_t *tbl, int idx, size_t *size) {
+ if (idx < 0) {
+ //errno = ENOENT;
+ return NULL;
+ }
+
+ qhasharr_data_t *data = tbl->data;
+
+ int newidx;
+ size_t valsize;
+ for (newidx = idx, valsize = 0;; newidx = data->slots[newidx].link)
+ {
+ valsize += data->slots[newidx].size;
+ if (data->slots[newidx].link == -1)
+ break;
+ }
+
+ void *value, *vp;
+ value = malloc(valsize);
+ if (value == NULL) {
+ //errno = ENOMEM;
+ return NULL;
+ }
+
+ for (newidx = idx, vp = value;; newidx = data->slots[newidx].link) {
+ if (data->slots[newidx].count == -2) {
+ // extended data block
+ memcpy(vp, (void *) data->slots[newidx].data.ext.value,
+ data->slots[newidx].size);
+ } else {
+ // key/value pair data block
+ memcpy(vp, (void *) data->slots[newidx].data.pair.value,
+ data->slots[newidx].size);
+ }
+
+ vp += data->slots[newidx].size;
+ if (data->slots[newidx].link == -1)
+ break;
+ }
+
+ if (size != NULL)
+ {
+ *size = valsize;
+ }
+ return value;
+}
+
+static bool _put_data(qhasharr_t *tbl, int idx, unsigned int hash,
+ const char *key, const void *value, size_t size,
+ int count) {
+ qhasharr_data_t *data = tbl->data;
+
+ // check if used
+ if (data->slots[idx].count != 0) {
+ //DEBUG("hasharr: BUG found.");
+ //errno = EFAULT;
+ return false;
+ }
+
+ size_t keylen = strlen(key);
+ unsigned char keymd5[16];
+ qhashmd5(key, keylen, keymd5);
+
+ // store key
+ data->slots[idx].count = count;
+ data->slots[idx].hash = hash;
+ strncpy(data->slots[idx].data.pair.key, key, _Q_HASHARR_KEYSIZE);
+ memcpy((char *) data->slots[idx].data.pair.keymd5, (char *) keymd5, 16);
+ data->slots[idx].data.pair.keylen = keylen;
+ data->slots[idx].link = -1;
+
+ // store value
+ int newidx;
+ size_t savesize;
+ for (newidx = idx, savesize = 0; savesize < size;) {
+ if (savesize > 0) { // find next empty slot
+ int tmpidx = _find_empty(tbl, newidx + 1);
+ if (tmpidx < 0) {
+ //DEBUG("hasharr: Can't expand slot for key %s.", key);
+ _remove_data(tbl, idx);
+ //errno = ENOBUFS;
+ return false;
+ }
+
+ // clear & set
+ memset((void *) (&data->slots[tmpidx]), '\0',
+ sizeof(qhasharr_slot_t));
+
+ data->slots[tmpidx].count = -2; // extended data block
+ data->slots[tmpidx].hash = newidx; // prev link
+ data->slots[tmpidx].link = -1; // end block mark
+ data->slots[tmpidx].size = 0;
+
+ data->slots[newidx].link = tmpidx; // link chain
+
+ //DEBUG("hasharr: slot %d is linked to slot %d for key %s.",
+ // tmpidx, newidx, key);
+ newidx = tmpidx;
+ }
+
+ // copy data
+ size_t copysize = size - savesize;
+
+ if (data->slots[newidx].count == -2) {
+ // extended value
+ if (copysize > sizeof(struct _Q_HASHARR_SLOT_EXT)) {
+ copysize = sizeof(struct _Q_HASHARR_SLOT_EXT);
+ }
+ memcpy(data->slots[newidx].data.ext.value, value + savesize,
+ copysize);
+ } else {
+ // first slot
+ if (copysize > _Q_HASHARR_VALUESIZE) {
+ copysize = _Q_HASHARR_VALUESIZE;
+ }
+ memcpy(data->slots[newidx].data.pair.value, value + savesize,
+ copysize);
+
+ // increase stored key counter
+ data->num++;
+ }
+ data->slots[newidx].size = copysize;
+ savesize += copysize;
+
+ // increase used slot counter
+ data->usedslots++;
+ }
+
+ return true;
+}
+
+static bool _copy_slot(qhasharr_t *tbl, int idx1, int idx2) {
+ qhasharr_data_t *data = tbl->data;
+
+ if (data->slots[idx1].count != 0 || data->slots[idx2].count == 0) {
+ //DEBUG("hasharr: BUG found.");
+ //errno = EFAULT;
+ return false;
+ }
+
+ memcpy((void *) (&data->slots[idx1]), (void *) (&data->slots[idx2]),
+ sizeof(qhasharr_slot_t));
+
+ // increase used slot counter
+ data->usedslots++;
+
+ return true;
+}
+
+static bool _remove_slot(qhasharr_t *tbl, int idx)
+{
+ qhasharr_data_t *data = tbl->data;
+
+ if (data->slots[idx].count == 0)
+ {
+ //DEBUG("hasharr: BUG found.");
+ //errno = EFAULT;
+ return false;
+ }
+
+ data->slots[idx].count = 0;
+
+ // decrease used slot counter
+ data->usedslots--;
+
+ return true;
+}
+
+static bool _remove_data(qhasharr_t *tbl, int idx) {
+ qhasharr_data_t *data = tbl->data;
+
+ if (data->slots[idx].count == 0) {
+ //DEBUG("hasharr: BUG found.");
+ //errno = EFAULT;
+ return false;
+ }
+
+ while (true) {
+ int link = data->slots[idx].link;
+ _remove_slot(tbl, idx);
+
+ if (link == -1)
+ break;
+
+ idx = link;
+ }
+
+ // decrease stored key counter
+ data->num--;
+
+ return true;
+}
+
+#endif /* _DOXYGEN_SKIP */
diff --git a/src/key-value-store/hashtable/qhasharr.h b/src/key-value-store/hashtable/qhasharr.h
new file mode 100644
index 0000000..37d52f3
--- /dev/null
+++ b/src/key-value-store/hashtable/qhasharr.h
@@ -0,0 +1,136 @@
+/******************************************************************************
+ * qLibc
+ *
+ * Copyright (c) 2010-2014 Seungyoung Kim.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *****************************************************************************/
+
+/**
+ * Static Hash Table container that works in preallocated fixed size memory.
+ *
+ * @file qhasharr.h
+ */
+
+/*
+ * Modified parts of this file by XS Embedded GmbH, 2014
+ */
+
+
+#ifndef _QHASHARR_H
+#define _QHASHARR_H
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include "qtype.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* tunable knobs */
+#define _Q_HASHARR_KEYSIZE (128) /*!< knob for maximum key size. */
+#define _Q_HASHARR_VALUESIZE (32) /*!< knob for maximum data size in a slot. */
+
+#define PERS_CACHE_MAX_SLOTS 100000 /**< Max. number of slots in the cache */
+#define PERS_CACHE_MEMSIZE (sizeof(qhasharr_data_t)+ (sizeof(qhasharr_slot_t) * (PERS_CACHE_MAX_SLOTS))) //approximately 2 MB
+
+/* types */
+typedef struct qhasharr_slot_s qhasharr_slot_t;
+typedef struct qhasharr_data_s qhasharr_data_t;
+typedef struct qhasharr_s qhasharr_t;
+
+/* public functions */
+extern qhasharr_t *qhasharr(void *memory, size_t memsize);
+extern size_t qhasharr_calculate_memsize(int max);
+
+/**
+ * qhasharr internal data slot structure
+ */
+struct qhasharr_slot_s {
+ short count; /*!< hash collision counter. 0 indicates empty slot,
+ -1 is used for collision resolution, -2 is used for
+ indicating linked block */
+ uint32_t hash; /*!< key hash. we use FNV32 */
+
+ uint8_t size; /*!< value size in this slot*/
+ int link; /*!< next link */
+
+ union {
+ /*!< key/value data */
+ struct _Q_HASHARR_SLOT_KEYVAL {
+ unsigned char value[_Q_HASHARR_VALUESIZE]; /*!< value */
+
+ char key[_Q_HASHARR_KEYSIZE]; /*!< key string, can be cut */
+ uint16_t keylen; /*!< original key length */
+ unsigned char keymd5[16]; /*!< md5 hash of the key */
+ } pair;
+
+ /*!< extended data block, used only when the count value is -2 */
+ struct _Q_HASHARR_SLOT_EXT {
+ unsigned char value[sizeof(struct _Q_HASHARR_SLOT_KEYVAL)];
+ } ext;
+ } data;
+};
+
+/**
+ * qhasharr memory structure
+ */
+struct qhasharr_data_s {
+ int maxslots; /*!< number of maximum slots */
+ int usedslots; /*!< number of used slots */
+ int num; /*!< number of stored keys */
+ qhasharr_slot_t *slots; /*!< data area pointer */
+};
+
+/**
+ * qhasharr container object
+ */
+struct qhasharr_s {
+ /* encapsulated member functions */
+ bool (*put) (qhasharr_t *tbl, const char *key, const void *value,
+ size_t size);
+
+ void *(*get) (qhasharr_t *tbl, const char *key, size_t *size);
+
+ bool (*getnext) (qhasharr_t *tbl, qnobj_t *obj, int *idx);
+
+ bool (*remove) (qhasharr_t *tbl, const char *key);
+
+ int (*size) (qhasharr_t *tbl, int *maxslots, int *usedslots);
+
+ void (*clear) (qhasharr_t *tbl);
+
+ void (*free) (qhasharr_t *tbl);
+
+ /* private variables */
+ qhasharr_data_t *data;
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /*_QHASHARR_H */
+
diff --git a/src/key-value-store/hashtable/qlibc.h b/src/key-value-store/hashtable/qlibc.h
new file mode 100644
index 0000000..0e35e48
--- /dev/null
+++ b/src/key-value-store/hashtable/qlibc.h
@@ -0,0 +1,55 @@
+ /******************************************************************************
+ * Project Persistency
+ * (c) copyright 2014
+ * Company XS Embedded GmbH
+ *****************************************************************************/
+/******************************************************************************
+ * qLibc
+ *
+ * Copyright (c) 2010-2014 Seungyoung Kim.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *****************************************************************************/
+
+/**
+ * qlibc header file.
+ *
+ * @file qlibc.h
+ */
+
+/*
+ * Modified parts of this file by XS Embedded GmbH, 2014
+ */
+
+#ifndef _QLIBC_H
+#define _QLIBC_H
+
+/* containers */
+#include "qtype.h"
+#include "qhasharr.h"
+
+/* utilities */
+#include "qhash.h"
+
+#endif /*_QLIBC_H */
+
diff --git a/src/key-value-store/hashtable/qtype.h b/src/key-value-store/hashtable/qtype.h
new file mode 100644
index 0000000..5da24e3
--- /dev/null
+++ b/src/key-value-store/hashtable/qtype.h
@@ -0,0 +1,123 @@
+/******************************************************************************
+ * qLibc
+ *
+ * Copyright (c) 2010-2014 Seungyoung Kim.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *****************************************************************************/
+
+/**
+ * Defines basic object types that are commonly used in various containers.
+ *
+ * @file qtype.h
+ */
+
+#ifndef _QTYPE_H
+#define _QTYPE_H
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <pthread.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* types */
+typedef struct qmutex_s qmutex_t; /*!< qlibc pthread mutex type*/
+typedef struct qobj_s qobj_t; /*!< object type*/
+typedef struct qnobj_s qnobj_t; /*!< named-object type*/
+typedef struct qdlobj_s qdlobj_t; /*!< doubly-linked-object type*/
+typedef struct qdlnobj_s qdlnobj_t; /*!< doubly-linked-named-object type*/
+typedef struct qhnobj_s qhnobj_t; /*!< hashed-named-object type*/
+
+/**
+ * qlibc pthread mutex data structure.
+ */
+struct qmutex_s {
+ pthread_mutex_t mutex; /*!< pthread mutex */
+ pthread_t owner; /*!< mutex owner thread id */
+ int count; /*!< recursive lock counter */
+};
+
+/**
+ * object data structure.
+ */
+struct qobj_s {
+ void *data; /*!< data */
+ size_t size; /*!< data size */
+ uint8_t type; /*!< data type */
+};
+
+/**
+ * named-object data structure.
+ */
+struct qnobj_s {
+ char *name; /*!< object name */
+ void *data; /*!< data */
+ size_t size; /*!< data size */
+};
+
+/**
+ * doubly-linked-object data structure.
+ */
+struct qdlobj_s {
+ void *data; /*!< data */
+ size_t size; /*!< data size */
+
+ qdlobj_t *prev; /*!< previous link */
+ qdlobj_t *next; /*!< next link */
+};
+
+/**
+ * doubly-linked-named-object data structure.
+ */
+struct qdlnobj_s {
+ uint32_t hash; /*!< 32bit-hash value of object name */
+ char *name; /*!< object name */
+ void *data; /*!< data */
+ size_t size; /*!< data size */
+
+ qdlnobj_t *prev; /*!< previous link */
+ qdlnobj_t *next; /*!< next link */
+};
+
+/**
+ * hashed-named-object data structure.
+ */
+struct qhnobj_s {
+ uint32_t hash; /*!< 32bit-hash value of object name */
+ char *name; /*!< object name */
+ void *data; /*!< data */
+ size_t size; /*!< data size */
+
+ qhnobj_t *next; /*!< for chaining next collision object */
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /*_QTYPE_H */
+