summaryrefslogtreecommitdiff
path: root/src/key-value-store/database/kissdb.c
blob: 4c8e7b67d8f9830908c9a6de08f0bd1459eab36d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
 /******************************************************************************
 * Project         Persistency
 * (c) copyright   2014
 * Company         XS Embedded GmbH
 *****************************************************************************/
/* (Keep It) Simple Stupid Database
*
* Written by Adam Ierymenko <adam.ierymenko@zerotier.com>
* Modified by Simon Disch <simon.disch@xse.de>
*
* KISSDB is in the public domain and is distributed with NO WARRANTY.
*
* http://creativecommons.org/publicdomain/zero/1.0/ */

/* Compile with KISSDB_TEST to build as a test program. */

/* Note: big-endian systems will need changes to implement byte swapping
* on hash table file I/O. Or you could just use it as-is if you don't care
* that your database files will be unreadable on little-endian systems. */

#define _FILE_OFFSET_BITS 64
#define TMP_BUFFER_LENGTH 128
#define KISSDB_HEADER_SIZE sizeof(Header_s)
#define __useBackups
//#define __useFileMapping
//#define __writeThrough
#define __checkerror

#include "./kissdb.h"
#include "../crc32.h"
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdint.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/time.h>

#include "dlt.h"

DLT_DECLARE_CONTEXT(persComLldbDLTCtx);

#ifdef __showTimeMeasurements
inline long long getNsDuration(struct timespec* start, struct timespec* end)
{
   return ((end->tv_sec * SECONDS2NANO) + end->tv_nsec) - ((start->tv_sec * SECONDS2NANO) + start->tv_nsec);
}
#endif

/* djb2 hash function */
static uint64_t KISSDB_hash(const void *b, unsigned long len)
{
   unsigned long i;
   uint64_t hash = 5381;
   for (i = 0; i < len; ++i)
      hash = ((hash << 5) + hash) + (uint64_t) (((const uint8_t *) b)[i]);
   return hash;
}

//returns a name for shared memory objects beginning with a slash followed by "path" (non alphanumeric chars are replaced with '_')  appended with "tailing"
char * kdbGetShmName(const char *tailing, const char * path)
{
   char * result = (char *) malloc(1 +  strlen(path) + strlen(tailing) + 1); //free happens at lifecycle shutdown
   int i =0;
   int x = 1;

   if (result != NULL)
   {
      result[0] = '/';
      for (i = 0; i < strlen(path); i++)
      {
         if (!isalnum(path[i]))
            result[i + 1] = '_';
         else
            result[i + 1] = path[i];
      }
      for (x = 0; x < strlen(tailing); x++)
      {
         result[i + x + 1] = tailing[x];
      }
      result[i + x + 1] = '\0';
   }
   return result;
}

//returns -1 on error and positive value for success
int kdbShmemOpen(const char * name, size_t length, Kdb_bool* shmCreator)
{
   int result;
   result = shm_open(name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
   if (result < 0)
   {
      if (errno == EEXIST)
      {
         *shmCreator = Kdb_false;
         result = shm_open(name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
         if (result < 0)
            return -1;
      }
   }
   else
   {
      *shmCreator = Kdb_true;
      if (ftruncate(result, length) < 0)
         return -1;
   }
   return result;
}

void Kdb_wrlock(pthread_rwlock_t * wrlock)
{
   pthread_rwlock_wrlock(wrlock);
}

void Kdb_rdlock(pthread_rwlock_t * rdlock)
{
   pthread_rwlock_rdlock(rdlock);
}

void Kdb_unlock(pthread_rwlock_t * lock)
{
   pthread_rwlock_unlock(lock);
}

Kdb_bool kdbShmemClose(int shmem, const char * shmName)
{
   if( close(shmem) == -1)
      return Kdb_false;
   if( shm_unlink(shmName) < 0)
      return Kdb_false;
   return Kdb_true;
}

void * getKdbShmemPtr(int shmem, size_t length)
{
   void* result = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, shmem, 0);
   if (result == MAP_FAILED)
      return ((void *) -1);
   return result;
}

Kdb_bool freeKdbShmemPtr(void * shmem_ptr, size_t length)
{
   if(munmap(shmem_ptr, length) == 0)
      return Kdb_true;
   else
      return Kdb_false;
}

Kdb_bool resizeKdbShmem(int shmem, Hashtable_slot_s** shmem_ptr, size_t oldLength, size_t newLength)
{
   //unmap shm with old size
   if( freeKdbShmemPtr(*shmem_ptr, oldLength) == Kdb_false)
      return Kdb_false;

   if (ftruncate(shmem, newLength) < 0)
      return Kdb_false;

   //get pointer to resized shm with new Length
   *shmem_ptr = getKdbShmemPtr(shmem, newLength);
   if(*shmem_ptr == ((void *) -1))
      return Kdb_false;
   return Kdb_true;
}

#ifdef __writeThrough
Kdb_bool remapKdbShmem(int shmem, uint64_t** shmem_ptr, size_t oldLength, size_t newLength)
{
   //unmap shm with old size
   if( freeKdbShmemPtr(*shmem_ptr, oldLength) == Kdb_false )
      return Kdb_false;
   //get pointer to resized shm with new Length
   *shmem_ptr = getKdbShmemPtr(shmem, newLength);
   if(*shmem_ptr == ((void *) -1))
      return Kdb_false;
   return Kdb_true;
}
#endif


int KISSDB_open(KISSDB *db, const char *path, int mode, uint16_t hash_table_size, uint64_t key_size,
      uint64_t value_size)
{
   Hashtable_slot_s *httmp;
   Kdb_bool tmp_creator;
   int ret = 0;

   //TODO check if usage of O_SYNC O_DIRECT flags is needed. If O_SYNC and O_DIrect is specified, no additional fsync calls are needed after fflush
   if(mode == KISSDB_OPEN_MODE_RWCREAT)
      db->fd = open(path, O_CREAT | O_RDWR , S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH  ); //gets closed when db->f is closed
   else
      db->fd = open(path, O_RDWR , S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH  ); //gets closed when db->f is closed

   if(db->fd == -1)
      return KISSDB_ERROR_IO;

   if (lseek(db->fd, 0, SEEK_END) == -1)
   {
      close(db->fd);
      return KISSDB_ERROR_IO;
   }
   if (lseek(db->fd, 0, SEEK_CUR) < KISSDB_HEADER_SIZE)
   {
      /* write header if not already present */
      if ((hash_table_size) && (key_size) && (value_size))
      {
         ret = writeHeader(db, &hash_table_size, &key_size, &value_size);
         if(0 != ret)
         {
            close(db->fd);
            return ret;
         }
         //Seek behind header
         if (lseek(db->fd, KISSDB_HEADER_SIZE, SEEK_SET) == -1)
         {
            close(db->fd);
            return KISSDB_ERROR_IO;
         }
      }
      else
      {
         close(db->fd);
         return KISSDB_ERROR_INVALID_PARAMETERS;
      }
   }
   else
   {
      //read existing header
      ret = readHeader(db, &hash_table_size, &key_size, &value_size);
      if( 0 != ret)
         return ret;

      if (lseek(db->fd, KISSDB_HEADER_SIZE, SEEK_SET) == -1)
      {
         close(db->fd);
         return KISSDB_ERROR_IO;
      } //Seek behind header
   }
   //store non shared db information
   db->hash_table_size = hash_table_size;
   db->key_size = key_size;
   db->value_size = value_size;
   db->hash_table_size_bytes = sizeof(Hashtable_slot_s) * (hash_table_size + 1); /* [hash_table_size] == next table */

   //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, DLT_STRING("Hashtable size in bytes: "), DLT_UINT64(db->hash_table_size_bytes));

   if (db->already_open == Kdb_false) //check if this instance has already opened the db before
   {
      db->shmem_cached_name = kdbGetShmName("-cache", path);
      if(db->shmem_cached_name == NULL)
         return KISSDB_ERROR_MALLOC;
      db->shmem_info_name = kdbGetShmName("-shm-info", path);
      if(db->shmem_info_name == NULL)
         return KISSDB_ERROR_MALLOC;
      db->shmem_info_fd = kdbShmemOpen(db->shmem_info_name, sizeof(Shared_Data_s), &db->shmem_creator);
      if(db->shmem_info_fd < 0)
         return KISSDB_ERROR_OPEN_SHM;
      db->shmem_info = (Shared_Data_s *) getKdbShmemPtr(db->shmem_info_fd, sizeof(Shared_Data_s));
      if(db->shmem_info == ((void *) -1))
         return KISSDB_ERROR_MAP_SHM;

      size_t first_mapping;
      if(db->shmem_info->shmem_size > db->hash_table_size_bytes )
         first_mapping = db->shmem_info->shmem_size;
      else
         first_mapping = db->hash_table_size_bytes;

      //open / create shared memory for first hashtable
      db->shmem_ht_name = kdbGetShmName("-ht", path);
      if(db->shmem_ht_name == NULL)
         return KISSDB_ERROR_MALLOC;
      db->shmem_ht_fd = kdbShmemOpen(db->shmem_ht_name,  first_mapping, &tmp_creator);
      if(db->shmem_ht_fd < 0)
         return KISSDB_ERROR_OPEN_SHM;
      db->hash_tables = (Hashtable_slot_s *) getKdbShmemPtr(db->shmem_ht_fd, first_mapping);
      if(db->hash_tables == ((void *) -1))
         return KISSDB_ERROR_MAP_SHM;
      db->old_mapped_size = first_mapping; //local information

      //if shared memory for rwlock was opened (created) with this call to KISSDB_open for the first time -> init rwlock
      if (db->shmem_creator == Kdb_true)
      {
         //[Initialize rwlock attributes]
         pthread_rwlockattr_t rwlattr, cache_rwlattr;
         pthread_rwlockattr_init(&rwlattr);
         pthread_rwlockattr_init(&cache_rwlattr);
         pthread_rwlockattr_setpshared(&rwlattr, PTHREAD_PROCESS_SHARED);
         pthread_rwlockattr_setpshared(&cache_rwlattr, PTHREAD_PROCESS_SHARED);
         pthread_rwlock_init(&db->shmem_info->rwlock, &rwlattr);
         pthread_rwlock_init(&db->shmem_info->cache_rwlock, &cache_rwlattr);
         Kdb_wrlock(&db->shmem_info->rwlock);

#ifdef __checkerror
         //CHECK POWERLOSS FLAGS
         ret = checkErrorFlags(db);
         if (0 != ret)
         {
            close(db->fd);
            Kdb_unlock(&db->shmem_info->rwlock);
            return ret;
         }
#endif
         db->shmem_info->num_hash_tables = 0;
      }
      else // already initialized
         Kdb_wrlock(&db->shmem_info->rwlock);

      db->already_open = Kdb_true;
   }
   else
      Kdb_wrlock(&db->shmem_info->rwlock);

   //only read header from file into memory for first caller of KISSDB_open
   if (db->shmem_creator == Kdb_true)
   {
      httmp = (Hashtable_slot_s*) malloc(db->hash_table_size_bytes);   //read hashtable from file
      if (!httmp)
      {
         close(db->fd);
         Kdb_unlock(&db->shmem_info->rwlock);
         return KISSDB_ERROR_MALLOC;
      }
      while (read(db->fd, httmp, db->hash_table_size_bytes) == db->hash_table_size_bytes)
      {
         Kdb_bool result = Kdb_false;
         //if new size would exceed old shared memory size-> allocate additional memory page to shared memory
         if (db->hash_table_size_bytes * (db->shmem_info->num_hash_tables + 1) > db->old_mapped_size)
         {
            Kdb_bool temp;
            if (db->shmem_ht_fd <= 0)
            {
               db->shmem_ht_fd = kdbShmemOpen(db->shmem_ht_name,  db->old_mapped_size, &temp);
               if(db->shmem_ht_fd < 0)
               {
                  free(httmp);
                  Kdb_unlock(&db->shmem_info->rwlock);
                  return KISSDB_ERROR_OPEN_SHM;
               }
            }
            result = resizeKdbShmem(db->shmem_ht_fd, &db->hash_tables, db->old_mapped_size, db->old_mapped_size + db->hash_table_size_bytes);
            if (result == Kdb_false)
            {
               free(httmp);
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_RESIZE_SHM;
            }
            else
            {
               db->shmem_info->shmem_size = db->old_mapped_size + db->hash_table_size_bytes;
               db->old_mapped_size = db->old_mapped_size + db->hash_table_size_bytes;
            }
         }
         // copy the current hashtable read from file to (htadress + (htsize  * htcount)) in memory
         memcpy(((uint8_t *) db->hash_tables) + (db->hash_table_size_bytes * db->shmem_info->num_hash_tables), httmp, db->hash_table_size_bytes);
         ++db->shmem_info->num_hash_tables;

         //read until all hash tables have been read
         if (httmp[db->hash_table_size].offsetA) //if httable[hash_table_size] contains a offset to a further hashtable
         {
            //ONE MORE HASHTABLE FOUND
            if (lseek(db->fd, httmp[db->hash_table_size].offsetA, SEEK_SET) == -1)
            { //move the filepointer to the next hashtable in the file
               KISSDB_close(db);
               free(httmp);
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_IO;
            }
         }
         else
            break; // no further hashtables exist
      }
      free(httmp);
   }

   //printSharedHashtable(db);

   Kdb_unlock(&db->shmem_info->rwlock);
   return 0;
}





int KISSDB_close(KISSDB *db)
{
   Kdb_wrlock(&db->shmem_info->rwlock);

   uint64_t crc = 0;
   Header_s* ptr = 0;
#ifdef __showTimeMeasurements
   long long KdbDuration = 0;
   struct timespec mmapStart, mmapEnd;
   KdbDuration = 0;
#endif

   //printSharedHashtable(db);
   if (db->shmem_creator == Kdb_true)
   {
      //free shared hashtable
      if( freeKdbShmemPtr(db->hash_tables, db->old_mapped_size) == Kdb_false)
      {
         Kdb_unlock(&db->shmem_info->rwlock);
         return KISSDB_ERROR_UNMAP_SHM;
      }
      if( kdbShmemClose(db->shmem_ht_fd, db->shmem_ht_name) == Kdb_false)
         return KISSDB_ERROR_CLOSE_SHM;

      free(db->shmem_ht_name);
      Kdb_unlock(&db->shmem_info->rwlock);
      pthread_rwlock_destroy(&db->shmem_info->rwlock);
      pthread_rwlock_destroy(&db->shmem_info->cache_rwlock);

      // free shared information
      if (freeKdbShmemPtr(db->shmem_info, sizeof(Kdb_bool)) == Kdb_false)
         return KISSDB_ERROR_UNMAP_SHM;
      if (kdbShmemClose(db->shmem_info_fd, db->shmem_info_name) == Kdb_false)
         return KISSDB_ERROR_CLOSE_SHM;
      free(db->shmem_info_name);

#ifdef __showTimeMeasurements
      clock_gettime(CLOCK_ID, &mmapStart);
#endif

      //update header (checksum and flags)
      int mapFlag = PROT_WRITE | PROT_READ;
      ptr = (Header_s*) mmap(NULL,KISSDB_HEADER_SIZE, mapFlag, MAP_SHARED, db->fd, 0);
      if (ptr == MAP_FAILED)
      {
         close(db->fd);
         return KISSDB_ERROR_IO;
      }
#ifdef __checkerror
      // generate checksum over database file (beginning at file offset [sizeof(ptr->KdbV) + sizeof(ptr->checksum)] up to EOF)
      if( db->fd )
      {
         crc = 0;
         crc = (uint64_t) pcoCalcCrc32Csum(db->fd, sizeof(Header_s) );
         ptr->checksum =  crc;
         //printf("CLOSING ------ DB: %s, WITH CHECKSUM CALCULATED: %" PRIu64 "  \n", db->shmem_ht_name, ptr->checksum);
      }
#endif
      ptr->closeFailed = 0x00; //remove closeFailed flag
      ptr->closeOk = 0x01;     //set closeOk flag

      //sync changes with file
      if( 0 != msync(ptr, KISSDB_HEADER_SIZE, MS_SYNC  | MS_INVALIDATE))
      {
         close(db->fd);
         return KISSDB_ERROR_IO;
      }
      //unmap memory
      if( 0 != munmap(ptr, KISSDB_HEADER_SIZE))
      {
         close(db->fd);
         return KISSDB_ERROR_IO;
      }
#ifdef __showTimeMeasurements
      clock_gettime(CLOCK_ID, &mmapEnd);
      KdbDuration += getNsDuration(&mmapStart, &mmapEnd);
      printf("mmap duration for => %f ms\n", (double)((double)KdbDuration/NANO2MIL));
#endif
      fsync(db->fd);

      if( db->fd)
         close(db->fd);

      db->already_open = Kdb_false;
      //memset(db, 0, sizeof(KISSDB)); //todo check if necessary
   }
   else
      //if caller is not the creator of the lock
      Kdb_unlock(&db->shmem_info->rwlock);
   return 0;
}


int KISSDB_get(KISSDB *db, const void *key, void *vbuf)
{
   Kdb_rdlock(&db->shmem_info->rwlock);

   uint8_t tmp[TMP_BUFFER_LENGTH];
   uint64_t current;
   const uint8_t *kptr;
   unsigned long klen, i;
   long n = 0;
   uint64_t checksum, backupChecksum, crc;
   uint64_t hash = KISSDB_hash(key, db->key_size) % (uint64_t) db->hash_table_size;
   int64_t offset, backupOffset, htoffset, checksumOffset, flagOffset; //lasthtoffset
   Hashtable_slot_s *cur_hash_table;

#ifdef __writeThrough
   //if new one or more hashtables were appended, remap shared memory block to adress space
   if (db->old_mapped_size < db->shmem_info->shmem_size)
   {
      Kdb_bool temp;
      db->shmem_ht_fd = kdbShmemOpen(db->shmem_ht_name, db->old_mapped_size, &temp);
      if(db->shmem_ht_fd < 0)
         return KISSDB_ERROR_OPEN_SHM;
      res = remapKdbShmem(db->shmem_ht_fd, &db->hash_tables, db->old_mapped_size, db->shmem_info->shmem_size);
      if (res == Kdb_false)
         return KISSDB_ERROR_REMAP_SHM;
      db->old_mapped_size = db->shmem_info->shmem_size;
   }
#endif

   htoffset = KISSDB_HEADER_SIZE; //lasthtoffset
   cur_hash_table = db->hash_tables;//pointer to current hashtable in memory
   for (i = 0; i < db->shmem_info->num_hash_tables; ++i)
   {
      offset = cur_hash_table[hash].offsetA;//get fileoffset where the data can be found in the file
#ifdef __useBackups
      //get information about current valid offset to latest written data
      if(cur_hash_table[hash].current == 0x00) //valid is offsetA
      {
         offset  = cur_hash_table[hash].offsetA;
         checksum = cur_hash_table[hash].checksumA;
      }
      else
      {
         offset = cur_hash_table[hash].offsetB;
         checksum = cur_hash_table[hash].checksumB;
      }
#endif

      if (offset >= KISSDB_HEADER_SIZE)       //if a valid offset is available in the slot
      {
         if (lseek(db->fd, offset, SEEK_SET) == -1) //move filepointer to this offset
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
         kptr = (const uint8_t *) key;
         klen = db->key_size;
         while (klen)
         {
            n = (long) read(db->fd, tmp, (klen > sizeof(tmp)) ? sizeof(tmp) : klen);
            if (n > 0)
            {
               if (memcmp(kptr, tmp, n))//if key does not match -> search in next hashtable
                  goto get_no_match_next_hash_table;
               kptr += n;
               klen -= (unsigned long) n;
            }
            else
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return 1; /* not found */
            }
         }
         if (read(db->fd, vbuf, db->value_size) == db->value_size) //if key matches at the fileoffset -> read the value
         {
            //crc check for file content
#ifdef __useBackups
            //only validate checksums at read if checksum of file is invalid
            if (db->shmem_info->crc_invalid == Kdb_true)
            {
               //verify checksum of current key/value pair
               crc = 0;
               crc = (uint64_t) pcoCrc32(crc, (unsigned char*) vbuf, db->value_size);
               if (checksum != crc)
               {
                  //printf("KISSDB_get: WARNING: checksum invalid -> try to read from valid data block \n");
                  //try to read valid data from backup
                  Hashtable_slot_s slot = cur_hash_table[hash];
                  if (cur_hash_table[hash].current == 0x00) //current is offsetA, but Data there is corrupt--> so use offsetB as backupOffset
                  {
                     backupOffset = cur_hash_table[hash].offsetB;
                     backupChecksum = cur_hash_table[hash].checksumB;
                     checksumOffset = htoffset + (sizeof(Hashtable_slot_s) * hash + sizeof(slot.offsetA)); //offset that points to checksumA
                     current = 0x01; //current is offsetB
                  }
                  else
                  {
                     backupOffset = cur_hash_table[hash].offsetA;
                     backupChecksum = cur_hash_table[hash].checksumA;
                     checksumOffset = htoffset
                           + (sizeof(Hashtable_slot_s) * hash + sizeof(slot.offsetA) + sizeof(slot.checksumA)
                                 + sizeof(slot.offsetB)); //offset that points to checksumB
                     current = 0x00;
                  }
                  flagOffset = htoffset
                        + (sizeof(Hashtable_slot_s) * hash + (sizeof(Hashtable_slot_s) - sizeof(slot.current))); //offset that points to currentflag

                  //seek to backup data
                  if (lseek(db->fd, backupOffset + db->key_size, SEEK_SET) == -1) //move filepointer to data of key-value pair //TODO make checksum over key AND data ??
                  {
                     Kdb_unlock(&db->shmem_info->rwlock);
                     return KISSDB_ERROR_IO;
                  }

                  //verify checksum of backup key/value pair
                  //read from backup data
                  if (read(db->fd, vbuf, db->value_size) == db->value_size) //read value of backup Data block
                  {
                     //generate checksum of backup
                     crc = 0;
                     crc = (uint64_t) pcoCrc32(crc, (unsigned char*) vbuf, db->value_size);
                     if (crc == backupChecksum)  //if checksum ok
                     {
                        //printf("KISSDB_get: WARNING: OVERWRITING CORRUPT DATA \n");
                        //seek to corrupt data
                        if (lseek(db->fd, offset + db->key_size, SEEK_SET) == -1) //move filepointer to data of corrupt key-value pair
                        {
                           Kdb_unlock(&db->shmem_info->rwlock);
                           return KISSDB_ERROR_IO;
                        }
                        //overwrite corrupt data
                        if (write( db->fd, vbuf, db->value_size) != db->value_size )  //write value
                        {
                           Kdb_unlock(&db->shmem_info->rwlock);
                           return KISSDB_ERROR_IO;
                        }
                        //seek to header slot and update checksum of corrupt data (do not modify offsets)
                        if (lseek(db->fd, checksumOffset, SEEK_SET) == -1) //move to checksumX in file
                        {
                           Kdb_unlock(&db->shmem_info->rwlock);
                           return KISSDB_ERROR_IO;
                        }
                        if (write( db->fd, &crc, sizeof(uint64_t)) != sizeof(uint64_t) )  //write checksumX to hashtbale slot
                        {
                           Kdb_unlock(&db->shmem_info->rwlock);
                           return KISSDB_ERROR_IO;
                        }
                        //update checksumX in memory
                        if (cur_hash_table[hash].current == 0x00) //current is offsetA, but Data there is corrupt--> so update checksumA with new checksum
                           cur_hash_table[hash].checksumA = crc;
                        else
                           cur_hash_table[hash].checksumB = crc;
                        //switch current valid to backup

                        if (lseek(db->fd, flagOffset, SEEK_SET) == -1) //move to current flag in file
                        {
                           Kdb_unlock(&db->shmem_info->rwlock);
                           return KISSDB_ERROR_IO;
                        }
                        if (write( db->fd, &current, sizeof(uint64_t)) != sizeof(uint64_t) )     //write current hashtable slot in file
                        {
                           Kdb_unlock(&db->shmem_info->rwlock);
                           return KISSDB_ERROR_IO;
                        }
                        //update current valid in memory
                        cur_hash_table[hash].current = current;
                        //fsync(db->fd)
                        Kdb_unlock(&db->shmem_info->rwlock);
                        return 0; /* success */
                     }
                     else //if checksum not valid, return NOT FOUND
                     {
                        Kdb_unlock(&db->shmem_info->rwlock);
                        return 1; /* not found */
                     }
                  }
                  else
                  {
                     Kdb_unlock(&db->shmem_info->rwlock);
                     return KISSDB_ERROR_IO;
                  }
               }
            }
#endif
            Kdb_unlock(&db->shmem_info->rwlock);
            return 0; /* success */
         }
         else
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
      }
      else
      {
         Kdb_unlock(&db->shmem_info->rwlock);
         return 1; /* not found */
      }
      //get_no_match_next_hash_table: cur_hash_table += db->hash_table_size + 1;
      get_no_match_next_hash_table:  //update lastht offset //lasthtoffset = htoffset
      htoffset = cur_hash_table[db->hash_table_size].offsetA; // fileoffset to the next file-hashtable
      cur_hash_table += (db->hash_table_size + 1); //pointer to the next memory-hashtable
   }
   Kdb_unlock(&db->shmem_info->rwlock);
   return 1; /* not found */
}


//TODO check current valid data to be deleted ?
int KISSDB_delete(KISSDB *db, const void *key)
{
   Kdb_wrlock(&db->shmem_info->rwlock);

   uint8_t tmp[TMP_BUFFER_LENGTH];
   //uint64_t current = 0x00;
   const uint8_t *kptr;
   long n;
   unsigned long klen, i;
   //uint64_t crc = 0x00;
   uint64_t hash = KISSDB_hash(key, db->key_size) % (uint64_t) db->hash_table_size;
   //int64_t empty_offset = 0;
   int64_t empty_offsetB = 0;
   int64_t offset = 0;
   int64_t htoffset = 0;
   Hashtable_slot_s *cur_hash_table;

#ifdef __writeThrough
   //if new hashtable was appended, remap shared memory block to adress space
   if (db->old_mapped_size < db->shmem_info->shmem_size)
   {
      Kdb_bool temp;
      db->shmem_ht_fd = kdbShmemOpen(db->shmem_ht_name, db->old_mapped_size, &temp);
      if(db->shmem_ht_fd < 0)
         return KISSDB_ERROR_OPEN_SHM;
      result = remapKdbShmem(db->shmem_ht_fd, &db->hash_tables, db->old_mapped_size, db->shmem_info->shmem_size);
      if (result == Kdb_false)
         return KISSDB_ERROR_REMAP_SHM;
      db->old_mapped_size = db->shmem_info->shmem_size;
   }
#endif

   htoffset = KISSDB_HEADER_SIZE;
   cur_hash_table = db->hash_tables; //pointer to current hashtable in memory

   for (i = 0; i < db->shmem_info->num_hash_tables; ++i)
   {
      offset = cur_hash_table[hash].offsetA; //get fileoffset where the data can be found in the file
      if (offset >= KISSDB_HEADER_SIZE)
      {
         if (lseek(db->fd, offset, SEEK_SET) == -1)
         {
            //set filepointer to Key value offset in file
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
         kptr = (const uint8_t *) key;
         klen = db->key_size;
         while (klen)
         {
            n = (long) read(db->fd, tmp, (klen > sizeof(tmp)) ? sizeof(tmp) : klen);
            if (n > 0)
            {
               if (memcmp(kptr, tmp, n))//if key does not match, search in next hashtable
                  goto get_no_match_next_hash_table;
               kptr += n;
               klen -= (unsigned long) n;
            }
            else
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return 1; /* not found */
            }
         }
         //TODO: mmap Hashtable slot structure to avoid seeking -> align hashtables at a multiple of a pagesize
#ifdef __useFileMapping
         empty_offsetB = -(offset + (db->key_size + db->value_size)); //todo check if offset is rewritten in put function !
         cur_hash_table[hash].offsetB = empty_offsetB;
         cur_hash_table[hash].checksumA = 0x00;
         cur_hash_table[hash].checksumB = 0x00;
         cur_hash_table[hash].current =   0x00;
         int testoffset= lseek(fd, 0, SEEK_CUR); //filepointer position
         int myoffset = htoffset + (sizeof(Hashtable_slot_s) * hash);

         printf("Endoffset in file: %d , Offset for mmap: %d , size for mmap: %d \n", testoffset, myoffset, sizeof(Hashtable_slot_s));

         //mmap the current hashtable slot
         int mapFlag = PROT_WRITE | PROT_READ;
         printf("In Delete: filedes: %d\n", db->fd);
         htSlot = (Hashtable_slot_s*) mmap(NULL, sizeof(Hashtable_slot_s), mapFlag, MAP_SHARED, db->fd, htoffset + (sizeof(Hashtable_slot_s) * hash) ); //TODO offset must be a multiple of pagesize
         if (htSlot == MAP_FAILED)
         {
            printf("MMAP ERROR !\n");
            close(db->fd);
            return KISSDB_ERROR_IO;
         }
         //do changes to slot in file
         htSlot->offsetA = empty_offset;
         htSlot->checksumA = 0x00;
         htSlot->offsetB = empty_offsetB;
         htSlot->checksumB = 0x00;
         htSlot->current = 0x00;

         //sync changes with file
         if (0 != msync(htSlot,  sizeof(Hashtable_slot_s), MS_SYNC | MS_INVALIDATE))
         {
            close(db->fd);
            return KISSDB_ERROR_IO;
         }
         //unmap memory
         if (0 != munmap(htSlot,  sizeof(Hashtable_slot_s)))
         {
            close(db->fd);
            return KISSDB_ERROR_IO;
         }
#endif
         if (lseek(db->fd, htoffset + (sizeof(Hashtable_slot_s) * hash), SEEK_SET) == -1) //move Filepointer to used slot in file-hashtable.
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }

#ifndef __useBackups
         cur_hash_table[hash].offsetA = -offset; //negate offset in hashtable that points to the data
         empty_offset = -offset;
         //update hashtable slot in file header (delete existing offset information)
         if (write( db->fd, &empty_offset, sizeof(int64_t)) != sizeof(int64_t) )  //mark slot in file-hashtable as deleted
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
#endif

#ifdef __useBackups
         //negate offsetB, delete checksums and current flag in memory
         cur_hash_table[hash].offsetA = -offset; //negate offset in hashtable that points to the data
         empty_offsetB = -(offset + (db->key_size + db->value_size));
         cur_hash_table[hash].checksumA = 0x00;
         cur_hash_table[hash].offsetB = empty_offsetB;
         cur_hash_table[hash].checksumB = 0x00;
         cur_hash_table[hash].current =   0x00;
         if (write( db->fd, &cur_hash_table[hash], sizeof(Hashtable_slot_s)) != sizeof(Hashtable_slot_s) )  //write updated data in the file-hashtable slot
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
#endif
         //TODO currently, no synchronus Filedescriptor is used!!!! fsync after fflush is needed to do synchronus writes
         //fsync(db->fd) // associating a file stream with a synchronous file descriptor means that an fsync() call is not needed on the file descriptor after the fflush()
         Kdb_unlock(&db->shmem_info->rwlock);
         return 0; /* success */
      }
      else
      {
         Kdb_unlock(&db->shmem_info->rwlock);
         return 1; /* not found */ //if no offset is found at hashed position in ht
      }
      get_no_match_next_hash_table: htoffset = cur_hash_table[db->hash_table_size].offsetA; // fileoffset to next ht in file
      cur_hash_table += (db->hash_table_size + 1); //pointer to next hashtable in memory
   }
   Kdb_unlock(&db->shmem_info->rwlock);
   return 1; /* not found */
}

int KISSDB_put(KISSDB *db, const void *key, const void *value)
{
   Kdb_wrlock(&db->shmem_info->rwlock);

   uint8_t tmp[TMP_BUFFER_LENGTH];
   uint64_t current = 0x00;
   const uint8_t *kptr;
   unsigned long klen, i;
   uint64_t hash = KISSDB_hash(key, db->key_size) % (uint64_t) db->hash_table_size;
   int64_t offset, endoffset, htoffset, lasthtoffset;
   Hashtable_slot_s *cur_hash_table;
   Kdb_bool result = Kdb_false;
   Kdb_bool temp = Kdb_false;
   uint64_t crc = 0x00;
   long n;
   char delimiter[8] = "||||||||";

#ifdef __writeThrough
   //if new hashtable was appended, remap shared memory block to adress space
   if(db->old_mapped_size < db->shmem_info->shmem_size)
   {
      db->shmem_ht_fd = kdbShmemOpen(db->shmem_ht_name,  db->old_mapped_size, &temp);
      if(db->shmem_ht_fd < 0)
         return KISSDB_ERROR_OPEN_SHM;
      res = remapKdbShmem(db->shmem_ht_fd, &db->hash_tables, db->old_mapped_size,db->shmem_info->shmem_size);
      if (res == Kdb_false)
         return KISSDB_ERROR_REMAP_SHM;
      db->old_mapped_size = db->shmem_info->shmem_size;
   }
#endif
   lasthtoffset = htoffset = KISSDB_HEADER_SIZE;
   cur_hash_table = db->hash_tables; //pointer to current hashtable in memory

   for (i = 0; i < db->shmem_info->num_hash_tables; ++i)
   {
      offset = cur_hash_table[hash].offsetA;   //fileoffset to data in file
      if (offset >= KISSDB_HEADER_SIZE || offset < 0) //if a key with same hash is already in this slot or the same key must be overwritten
      {
         // if slot is marked as deleted, use this slot and negate the offset in order to reuse the existing data block
         if(offset < 0)
         {
            offset = -offset; //get original offset where data was deleted
            //printf("Overwriting slot for key: [%s] which was deleted before, offsetA: %d \n",key, offset);
            if (lseek(db->fd, offset, SEEK_SET) == -1) //move filepointer to fileoffset where the key can be found
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_IO;
            }
            if (write( db->fd, key, db->key_size) != db->key_size )  //write key
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_IO;
            }
            if (write( db->fd, value, db->value_size) != db->value_size )  //write value
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_IO;
            }

            // write same key and value again here because slot was deleted an can be reused like an initial write
#ifdef __useBackups
            if (write( db->fd, key, db->key_size) != db->key_size )  //write key
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_IO;
            }
            if (write( db->fd, value, db->value_size) != db->value_size )  //write value
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_IO;
            }
#endif
            //seek back to hashtbale slot
            if (lseek(db->fd, htoffset + (sizeof(Hashtable_slot_s) * hash), SEEK_SET) == -1) //move  to beginning of hashtable slot in file
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_IO;
            }

#ifndef __useBackups
            cur_hash_table[hash].offsetA = offset; //write the offset to the data in the memory-hashtable slot
            if (write( db->fd, &offset, sizeof(int64_t)) != sizeof(int64_t) )  //write the offsetA to the data in the file-hashtable slot
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_IO;
            }
#endif

#ifdef __useBackups
            crc = 0x00;
            crc = (uint32_t) pcoCrc32(crc, (unsigned char*)value, db->value_size);
            cur_hash_table[hash].offsetA = offset; //write the offset to the data in the memory-hashtable slot
            cur_hash_table[hash].checksumA = crc;
            offset += (db->key_size + db->value_size);
            cur_hash_table[hash].offsetB = offset;          //write the offset to the data in the memory-hashtable slot
            cur_hash_table[hash].checksumB = crc;
            cur_hash_table[hash].current = 0x00;

            if (write( db->fd, &cur_hash_table[hash], sizeof(Hashtable_slot_s)) != sizeof(Hashtable_slot_s) )  //write updated data in the file-hashtable slot
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_IO;
            }
#endif
            //fsync(db->fd) //associating a file stream with a synchronous file descriptor means that an fsync() call is not needed on the file descriptor after the fflush()
            Kdb_unlock(&db->shmem_info->rwlock);
            return 0; /* success */
         }

         //overwrite existing if key matches
         // if cur_hash_table[hash].current == 0x00 -> offsetA is latest so write to offsetB else offsetB is latest and write to offsetA
#ifdef __useBackups
         if( cur_hash_table[hash].current == 0x00 )
            offset = cur_hash_table[hash].offsetA; //0x00 -> offsetA is latest
         else
            offset = cur_hash_table[hash].offsetB; //else offsetB is latest
#endif
         if (lseek(db->fd, offset, SEEK_SET) == -1) //move filepointer to fileoffset where valid data can be found
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }

         kptr = (const uint8_t *) key; //pointer to search key
         klen = db->key_size;
         while (klen)
         {
            n = (long) read(db->fd, tmp, (klen > sizeof(tmp)) ? sizeof(tmp) : klen);
            if (n > 0)
            {
               if (memcmp(kptr, tmp, n)) //if search key does not match with key in file
                  goto put_no_match_next_hash_table;
               kptr += n;
               klen -= (unsigned long) n;
            }
         }

         //if key matches -> seek to currently non valid data block for this key
#ifdef __useBackups
         if( cur_hash_table[hash].current == 0x00 )
            offset = cur_hash_table[hash].offsetB; // 0x00 -> offsetA is latest so write new data to offsetB which holds old data
         else
            offset = cur_hash_table[hash].offsetA; // offsetB is latest so write new data to offsetA which holds old data

         if (lseek(db->fd, offset, SEEK_SET) == -1)//move filepointer to fileoffset where backup data can be found
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
         if (write( db->fd, key, db->key_size) != db->key_size )  //write key
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
#endif
         if (write( db->fd, value, db->value_size) != db->value_size )
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
         // seek back to slot in header for update of checksum and flag
         if (lseek(db->fd, htoffset + (sizeof(Hashtable_slot_s) * hash), SEEK_SET) == -1) //move  to beginning of hashtable slot in file
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }

         //generate crc for value
         crc = 0x00;
         crc = (uint64_t) pcoCrc32(crc, (unsigned char*)value, db->value_size);
         current = 0x00;
         Hashtable_slot_s slot = cur_hash_table[hash];

         // check current flag and decide what parts of hashtable slot in file must be updated
         if( cur_hash_table[hash].current == 0x00 ) //offsetA is latest -> modify settings of B
         {
            int seek = sizeof(slot.offsetA) + sizeof(slot.checksumA) + sizeof(slot.offsetB);
            lseek(db->fd, seek , SEEK_CUR);                //move to checksumB in file
            if( write( db->fd, &crc, sizeof(uint64_t)) != sizeof(uint64_t))      //write checksumB to file
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_IO;
            }
            current = 0x01;
            if( write( db->fd, &current, sizeof(uint64_t)) != sizeof(uint64_t))   //write current  to hashtbale slot
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_IO;
            }
            cur_hash_table[hash].checksumB = crc;
            cur_hash_table[hash].current = current;
         }
         else //offsetB is latest -> modify settings of A
         {

            int seek = sizeof(slot.offsetA);
            lseek(db->fd, seek , SEEK_CUR); //move to checksumA in file
            if( write( db->fd, &crc, sizeof(uint64_t)) != sizeof(uint64_t))   //write checksumA to file
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_IO;
            }
            seek = sizeof(slot.offsetB) + sizeof(slot.checksumB);;
            lseek(db->fd, seek , SEEK_CUR); //move to checksumA in file
            current = 0x00;
            if( write( db->fd, &current, sizeof(uint64_t)) != sizeof(uint64_t))//write current  to hashtbale slot
            {
               Kdb_unlock(&db->shmem_info->rwlock);
               return KISSDB_ERROR_IO;
            }
            cur_hash_table[hash].checksumA = crc;
            cur_hash_table[hash].current = current;
         }
         Kdb_unlock(&db->shmem_info->rwlock);
         return 0; //success
      }
      else //if key is not already inserted
      {
         /* add new data if an empty hash table slot is discovered */
         if (lseek(db->fd, 0, SEEK_END) == -1) //filepointer to the end of the file
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
         endoffset = lseek(db->fd, 0, SEEK_CUR); //filepointer position
         if (write( db->fd, key, db->key_size) != db->key_size )
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
         if (write( db->fd, value, db->value_size) != db->value_size )
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }

         // write same key and value again here --> initial write
#ifdef __useBackups
         if (write( db->fd, key, db->key_size) != db->key_size )
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
         if (write( db->fd, value, db->value_size) != db->value_size )
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
#endif
         if (lseek(db->fd, htoffset + (sizeof(Hashtable_slot_s) * hash), SEEK_SET) == -1) //move filepointer to file-hashtable slot in file (offsetA)
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
#ifndef __useBackups
         if (write( db->fd, &endoffset, sizeof(int64_t)) != sizeof(int64_t) )  //write the offsetA to the data in the file-hashtable slot
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
         cur_hash_table[hash].offsetA = endoffset; //write the offsetA to the data in the memory-hashtable slot
#endif

#ifdef __useBackups
         crc = 0x00;
         crc = (uint64_t) pcoCrc32(crc, (unsigned char*) value, db->value_size);
         offset = endoffset + (db->key_size + db->value_size);
         cur_hash_table[hash].offsetA = endoffset; //write the offsetA to the data in the memory-hashtable slot
         cur_hash_table[hash].checksumA = crc;
         cur_hash_table[hash].offsetB = offset;          //write the offset to the data in the memory-hashtable slot
         cur_hash_table[hash].checksumB = crc;
         cur_hash_table[hash].current = 0x00;
         current = 0x00; //current

         if (write( db->fd, &cur_hash_table[hash], sizeof(Hashtable_slot_s)) != sizeof(Hashtable_slot_s) )  //write updated data in the file-hashtable slot
         {
            Kdb_unlock(&db->shmem_info->rwlock);
            return KISSDB_ERROR_IO;
         }
#endif
         //fsync(db->fd) // associating a file stream with a synchronous file descriptor means that an fsync() call is not needed on the file descriptor after the fflush()
         Kdb_unlock(&db->shmem_info->rwlock);
         return 0; /* success */
      }
      put_no_match_next_hash_table: lasthtoffset = htoffset;
      htoffset = cur_hash_table[db->hash_table_size].offsetA; // fileoffset to the next file-hashtable
      cur_hash_table += (db->hash_table_size + 1); //pointer to the next memory-hashtable
   }

   /* if no existing slots, add a new page of hash table entries */
   if (lseek(db->fd, 0, SEEK_END) == -1) //Filepointer to the end of file
   {
      Kdb_unlock(&db->shmem_info->rwlock);
      return KISSDB_ERROR_IO;
   }
   if(db->shmem_info->num_hash_tables > 0) //only write delimiter if first hashtable has been written (first delimiter is written by open call)
   {
      if (write( db->fd, &delimiter, sizeof(delimiter)) != sizeof(delimiter) )  //write delimiter
      {
         Kdb_unlock(&db->shmem_info->rwlock);
         return KISSDB_ERROR_IO;
      }
   }
   endoffset = lseek(db->fd, 0, SEEK_CUR);

   //if new size would exceed old shared memory size-> allocate additional memory to shared memory (+ db->hash_table_size_bytes)
   if( (db->hash_table_size_bytes * (db->shmem_info->num_hash_tables + 1)) > db->shmem_info->shmem_size)
   {
      if (db->shmem_ht_fd <= 0)
      {
         db->shmem_ht_fd = kdbShmemOpen(db->shmem_ht_name,  db->old_mapped_size, &temp);
         if(db->shmem_ht_fd < 0)
            return KISSDB_ERROR_OPEN_SHM;
      }
      result = resizeKdbShmem(db->shmem_ht_fd, &db->hash_tables, db->old_mapped_size, db->old_mapped_size + db->hash_table_size_bytes);
      if (result == Kdb_false)
      {
         return KISSDB_ERROR_RESIZE_SHM;
      }
      else
      {
         db->shmem_info->shmem_size = db->old_mapped_size + db->hash_table_size_bytes;
         db->old_mapped_size = db->shmem_info->shmem_size;
      }
   }

   //if( currentHtOffset <= db->old_mapped_size / sizeof(Hashtable_slot_s) )
   cur_hash_table = &(db->hash_tables[(db->hash_table_size + 1) * db->shmem_info->num_hash_tables]);
   //else
   //   return KISSDB_ERROR_ACCESS_VIOLATION;
   memset(cur_hash_table, 0, db->hash_table_size_bytes); //hashtable init
   cur_hash_table[hash].offsetA = endoffset + db->hash_table_size_bytes; /* where new entry will go (behind the new Ht that gets written)*/

#ifdef __useBackups
   crc = 0x00;
   crc = (uint64_t) pcoCrc32(crc, (unsigned char*)value, db->value_size);
   cur_hash_table[hash].checksumA = crc;
   cur_hash_table[hash].checksumB = crc;
   cur_hash_table[hash].offsetB = cur_hash_table[hash].offsetA + (db->key_size + db->value_size);//write the offset to the data in the memory-hashtable slot
   cur_hash_table[hash].current = 0x00;
#endif

   // write new hashtable at the end of the file
   if (write( db->fd, cur_hash_table, db->hash_table_size_bytes) != db->hash_table_size_bytes )
   {
      Kdb_unlock(&db->shmem_info->rwlock);
      return KISSDB_ERROR_IO;
   }
   // write key behind new hashtable
   if (write( db->fd, key, db->key_size) != db->key_size )
   {
      Kdb_unlock(&db->shmem_info->rwlock);
      return KISSDB_ERROR_IO;
   }
   // write value behind key
   if (write( db->fd, value, db->value_size) != db->value_size )
   {
      Kdb_unlock(&db->shmem_info->rwlock);
      return KISSDB_ERROR_IO;
   }
   // write same key and value again here --> initial write
#ifdef __useBackups
   if (write( db->fd, key, db->key_size) != db->key_size )
   {
      Kdb_unlock(&db->shmem_info->rwlock);
      return KISSDB_ERROR_IO;
   }
   if (write( db->fd, value, db->value_size) != db->value_size )
   {
      Kdb_unlock(&db->shmem_info->rwlock);
      return KISSDB_ERROR_IO;
   }
#endif

   //if a hashtable exists, update link to new hashtable
   if (db->shmem_info->num_hash_tables)
   {
      if (lseek(db->fd, lasthtoffset + (sizeof(Hashtable_slot_s) * db->hash_table_size), SEEK_SET) == -1)
      {
         Kdb_unlock(&db->shmem_info->rwlock);
         return KISSDB_ERROR_IO;
      }
      if (write( db->fd, &endoffset, sizeof(int64_t)) != sizeof(int64_t) )
      {
         Kdb_unlock(&db->shmem_info->rwlock);
         return KISSDB_ERROR_IO;
      }
      db->hash_tables[((db->hash_table_size + 1) * (db->shmem_info->num_hash_tables - 1)) + db->hash_table_size].offsetA = endoffset; //update link to new hashtable in old hashtable
   }
   ++db->shmem_info->num_hash_tables;
   //fsync(db->fd)
   Kdb_unlock(&db->shmem_info->rwlock);
   return 0; /* success */
}



#if 0
/*
 * prints the offsets stored in the shared Hashtable
 */
void printSharedHashtable(KISSDB *db)
{
   Hashtable_slot_s *cur_hash_table;
   cur_hash_table = db->hash_tables;
   unsigned long k;
   unsigned long x = (db->hash_table_size * db->shmem_info->num_hash_tables);
   //printf("Address of SHARED HT_NUMBER: %p \n", &db->shmem_info->num_hash_tables);
   printf("Address of SHARED HEADER: %p \n", &cur_hash_table);
   Header_s* ptr;
   printf("HT Struct sizes: %d, %d, %d, %d,%d, %d, %d, %d\n", sizeof(ptr->KdbV), sizeof(ptr->checksum), sizeof(ptr->closeFailed), sizeof(ptr->closeOk), sizeof(ptr->hash_table_size),sizeof(ptr->key_size),sizeof(ptr->value_size),sizeof(ptr->delimiter));
   printf("HEADER SIZE: %d \n", sizeof(Header_s));
   printf("Hashtable_slot_s SIZE: %d \n", sizeof(Hashtable_slot_s));
   for (k = 0; k < x; k++)
   {
      if (db->hash_tables[k].offsetA != 0)
      {
         printf("offsetA  [%lu]: %" PRId64 " \n", k, db->hash_tables[k].offsetA);
         printf("checksumA[%lu]: %" PRIu64 " \n", k, db->hash_tables[k].checksumA);
         printf("offsetB  [%lu]: %" PRId64 " \n", k, db->hash_tables[k].offsetB);
         printf("checksumB[%lu]: %" PRIu64 " \n", k, db->hash_tables[k].checksumB);
         printf("current  [%lu]: %" PRIu64 " \n", k, db->hash_tables[k].current);
      }
   }
}
#endif


void KISSDB_Iterator_init(KISSDB *db, KISSDB_Iterator *dbi)
{
   dbi->db = db;
   dbi->h_no = 0;  // number of read hashtables
   dbi->h_idx = 0; // index in current hashtable
}


int KISSDB_Iterator_next(KISSDB_Iterator *dbi, void *kbuf, void *vbuf)
{
   int64_t offset;
   Kdb_rdlock(&dbi->db->shmem_info->rwlock);

   if ((dbi->h_no < (dbi->db->shmem_info->num_hash_tables)) && (dbi->h_idx < dbi->db->hash_table_size))
   {
      //TODO check for currently valid data block flag and use this offset instead of offsetA
      while (!(offset = dbi->db->hash_tables[((dbi->db->hash_table_size + 1) * dbi->h_no) + dbi->h_idx].offsetA))
      {
         if (++dbi->h_idx >= dbi->db->hash_table_size)
         {
            dbi->h_idx = 0;
            if (++dbi->h_no >= (dbi->db->shmem_info->num_hash_tables))
            {
               Kdb_unlock(&dbi->db->shmem_info->rwlock);
               return 0;
            }
         }
      }

      if (lseek(dbi->db->fd, offset, SEEK_SET) == -1)
         return KISSDB_ERROR_IO;
      if (read(dbi->db->fd, kbuf, dbi->db->key_size) != dbi->db->key_size)
         return KISSDB_ERROR_IO;
      if (vbuf != NULL)
      {
         if (read(dbi->db->fd, vbuf, dbi->db->value_size) != dbi->db->value_size)
            return KISSDB_ERROR_IO;
      }
      else
      {
         if (lseek(dbi->db->fd, dbi->db->value_size, SEEK_CUR) == -1)
            return KISSDB_ERROR_IO;
      }

      if (++dbi->h_idx >= dbi->db->hash_table_size)
      {
         dbi->h_idx = 0;
         ++dbi->h_no;
      }
      Kdb_unlock(&dbi->db->shmem_info->rwlock);
      return 1;
   }
   Kdb_unlock(&dbi->db->shmem_info->rwlock);
   return 0;
}



int readHeader(KISSDB* db, uint16_t* hash_table_size, uint64_t* key_size, uint64_t* value_size)
{
   //set Filepointer to the beginning of the file
   if (lseek(db->fd, 0, SEEK_SET) == -1)
      return KISSDB_ERROR_IO;
   //mmap header from beginning of file
   int mapFlag = PROT_WRITE | PROT_READ;
   Header_s* ptr = 0;
   ptr = (Header_s*) mmap(NULL, KISSDB_HEADER_SIZE, mapFlag, MAP_SHARED, db->fd, 0);
   if (ptr == MAP_FAILED)
      return KISSDB_ERROR_IO;

   if ((ptr->KdbV[0] != 'K') || (ptr->KdbV[1] != 'd') || (ptr->KdbV[2] != 'B') || (ptr->KdbV[3] != KISSDB_VERSION))
      return KISSDB_ERROR_CORRUPT_DBFILE;

   if (!ptr->hash_table_size)
      return KISSDB_ERROR_CORRUPT_DBFILE;
   (*hash_table_size) = (uint16_t) ptr->hash_table_size;

   if (!ptr->key_size)
      return KISSDB_ERROR_CORRUPT_DBFILE;
   (*key_size) = (uint64_t) ptr->key_size;

   if (!ptr->value_size)
      return KISSDB_ERROR_CORRUPT_DBFILE;
   (*value_size) = (uint64_t) ptr->value_size;

   //sync changes with file
   if (0 != msync(ptr, KISSDB_HEADER_SIZE, MS_SYNC | MS_INVALIDATE))
      return KISSDB_ERROR_IO;

   //unmap memory
   if (0 != munmap(ptr, KISSDB_HEADER_SIZE))
      return KISSDB_ERROR_IO;
   return 0;
}




int writeHeader(KISSDB* db, uint16_t* hash_table_size, uint64_t* key_size, uint64_t* value_size)
{
   Header_s* ptr = 0;
   int ret= 0;

   //Seek to beginning of file
   if (lseek(db->fd, 0, SEEK_SET) == -1)
      return KISSDB_ERROR_IO;

   //ftruncate file to needed size for header
   ret = ftruncate(db->fd, KISSDB_HEADER_SIZE);
   if (ret < 0)
      return KISSDB_ERROR_IO;

   //mmap header from beginning of file
   int mapFlag = PROT_WRITE | PROT_READ;
   ptr = (Header_s*) mmap(NULL, KISSDB_HEADER_SIZE, mapFlag, MAP_SHARED, db->fd, 0);
   if (ptr == MAP_FAILED)
      return KISSDB_ERROR_IO;

   ptr->KdbV[0] = 'K';
   ptr->KdbV[1] = 'd';
   ptr->KdbV[2] = 'B';
   ptr->KdbV[3] = KISSDB_VERSION;
   ptr->KdbV[4] = '-';
   ptr->KdbV[5] = '-';
   ptr->KdbV[6] = '-';
   ptr->KdbV[7] = '-';
   ptr->checksum = 0x00;
   ptr->closeFailed = 0x00; //remove closeFailed flag
   ptr->closeOk = 0x01;     //set closeOk flag
   ptr->hash_table_size = (uint64_t)(*hash_table_size);
   ptr->key_size = (uint64_t)(*key_size);
   ptr->value_size = (uint64_t)(*value_size);
   memcpy(ptr->delimiter,"||||||||", 8);

   //sync changes with file
   if (0 != msync(ptr, KISSDB_HEADER_SIZE, MS_SYNC | MS_INVALIDATE))
      return KISSDB_ERROR_IO;

   //unmap memory
   if (0 != munmap(ptr, KISSDB_HEADER_SIZE))
      return KISSDB_ERROR_IO;
   return 0;
}


int checkErrorFlags(KISSDB* db)
{
   //mmap header from beginning of file
   int mapFlag = PROT_WRITE | PROT_READ;
   Header_s* ptr = 0;
   ptr = (Header_s*) mmap(NULL, KISSDB_HEADER_SIZE, mapFlag, MAP_SHARED, db->fd, 0);
   if (ptr == MAP_FAILED)
      return KISSDB_ERROR_IO;
   //uint64_t crc = 0;

#ifdef __checkerror
   //check if closeFailed flag is set
   if(ptr->closeFailed == 0x01)
   {
      //TODO implement verifyHashtableCS

      //if closeFailed flag is set, something went wrong at last close -> so check crc
      db->shmem_info->crc_invalid = Kdb_true; //check crc for further reads

#if 0
      DLT_LOG(persComLldbDLTCtx, DLT_LOG_WARN, DLT_STRING("OPENING DB -> closeFailed flag is set:  "), DLT_UINT64(ptr->closeFailed));
      crc = (uint64_t) pcoCalcCrc32Csum(db->fd, sizeof(Header_s));
      if(ptr->checksum != 0) //do not check if database is currently in creation
      {
         if (crc != ptr->checksum)
         {
            DLT_LOG(persComLldbDLTCtx, DLT_LOG_WARN, DLT_STRING("OPENING DB: "), DLT_STRING(db->shmem_ht_name), DLT_STRING(" CHECKSUM IN HEADER : "), DLT_UINT64(ptr->checksum), DLT_STRING(" != CHECKSUM CALCULATED: "), DLT_UINT64(crc));
            //db->shmem_info->crc_invalid = Kdb_true; //check datablocks at further reads
            //return KISSDB_ERROR_CORRUPT_DBFILE; //previous close failed and checksum invalid -> error state -> return error
         }
         else
         {
            DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, DLT_STRING("OPENING DB: "), DLT_STRING(db->shmem_ht_name), DLT_STRING(" CECHKSUM IN HEADER: "), DLT_UINT64(ptr->checksum), DLT_STRING(" == CHECKSUM CALCULATED: "), DLT_UINT64(crc));
            //db->shmem_info->crc_invalid = Kdb_false; //do not check datablocks at further reads
         }
      }
      else
         DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, DLT_STRING("Do not check checksum, database in creation: "), DLT_STRING(db->shmem_ht_name));
#endif
   }
   else
   {
      //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, DLT_STRING("OPENING DB: closeFailed flag is not set:  "), DLT_UINT64(ptr->closeFailed));
      ptr->closeFailed = 0x01; //NO: create close failed flag
   }


   //check if closeOk flag is set
   if(ptr->closeOk == 0x01)
   {
      //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, DLT_STRING("OPENING DB -> closeOk flag is set:  "), DLT_UINT64(ptr->closeOk));
      ptr->closeOk = 0x00;
   }
   else
   {
      //if closeOK is not set , something went wrong at last close
      db->shmem_info->crc_invalid = Kdb_true; //do crc check at read

#if 0
      crc = (uint64_t) pcoCalcCrc32Csum(db->fd, sizeof(Header_s));
      if(ptr->checksum != 0) //do not check if database is currently in creation
      {
         if (crc != ptr->checksum)
         {
            DLT_LOG(persComLldbDLTCtx, DLT_LOG_WARN, DLT_STRING("OPENING DB: "), DLT_STRING(db->shmem_ht_name), DLT_STRING(" CHECKSUM IN HEADER : "), DLT_UINT64(ptr->checksum), DLT_STRING(" != CHECKSUM CALCULATED: "), DLT_UINT64(crc));
            //db->shmem_info->crc_invalid = Kdb_true;
            //return KISSDB_ERROR_CORRUPT_DBFILE; //previous close failed and checksum invalid -> error state -> return error
         }
         else
         {
            DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, DLT_STRING("OPENING DB: "), DLT_STRING(db->shmem_ht_name), DLT_STRING(" CECHKSUM IN HEADER: "), DLT_UINT64(ptr->checksum), DLT_STRING(" == CHECKSUM CALCULATED: "), DLT_UINT64(crc));
            //db->shmem_info->crc_invalid = Kdb_false;
         }
      }
      else
      {
         DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, DLT_STRING("Do not check checksum, database in creation: "), DLT_STRING(db->shmem_ht_name));
      }

      DLT_LOG(persComLldbDLTCtx, DLT_LOG_WARN, DLT_STRING("OPENING DB -> closeOk flag is not set:  "), DLT_UINT64(ptr->closeOk));
#endif


   }
#endif
   //sync changes with file
   if (0 != msync(ptr, KISSDB_HEADER_SIZE, MS_SYNC | MS_INVALIDATE))
      return KISSDB_ERROR_IO;

   //unmap memory
   if (0 != munmap(ptr, KISSDB_HEADER_SIZE))
      return KISSDB_ERROR_IO;

   return 0;
}