14 #define _XOPEN_SOURCE 700
21 #include <sys/types.h>
30 #include "sion_error_handler.h"
39 #define POS_BEHIND_END -302
76 #define DFUNCTION "_sion_keyvalue_keymngr_init"
80 DPRINTFP((2, DFUNCTION, -1,
"enter init size=%d\n",size));
83 if (keymngr == NULL) {
84 _sion_errorprint(0,_SION_ERROR_RETURN,
"cannot allocate internal keyvalue keymngr of size %lu , aborting ...\n", (
unsigned long)
sizeof(
_sion_keyvalue_keymngr));
96 keymngr->
key_table = _sion_keyvalue_table_init(size);
98 _sion_errorprint(0,_SION_ERROR_RETURN,
"cannot allocate internal keyvalue table of for %lu entries , aborting ...\n", (
unsigned long) size);
103 DPRINTFP((2, DFUNCTION, -1,
"leave\n"));
109 #define DFUNCTION "_sion_keyvalue_keymngr_destroy"
111 size_t rc=SION_SUCCESS;
114 DPRINTFP((2, DFUNCTION, -1,
"enter\n"));
115 block = (*keymngr)->block_inwriteorder_head;
116 while (block != NULL) {
122 if((*keymngr)->key_table) {
123 rc=_sion_keyvalue_table_destroy(&((*keymngr)->key_table));
128 DPRINTFP((2, DFUNCTION, -1,
"leave rc=%d\n",rc));
133 #define DFUNCTION "_sion_keyvalue_keymngr_add_block"
134 int _sion_keyvalue_keymngr_add_block(
_sion_keyvalue_keymngr* keymngr, sion_table_key_t key,
size_t offset,
size_t len) {
135 size_t rc=SION_SUCCESS;
140 DPRINTFP((2, DFUNCTION, -1,
"enter\n"));
143 p=_sion_keyvalue_table_lookup(keymngr->
key_table,key);
149 return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
"cannot allocate internal keyvalue table entry, aborting ...\n"));
165 rc=_sion_keyvalue_table_store(keymngr->
key_table,key,p);
166 if (rc != SION_SUCCESS)
return(rc);
174 if (new_block == NULL) {
175 return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
"cannot allocate internal keyvalue table block entry, aborting ...\n"));
177 new_block->
entry = entry;
178 new_block->
offset = offset;
179 new_block->
len = len;
180 new_block->
next = NULL;
220 DPRINTFP((2, DFUNCTION, -1,
"leave rc=%d\n",rc));
225 #define DFUNCTION "_sion_keyvalue_keymngr_update_read_pos"
227 size_t bytes_read, sion_int64 current_pos) {
228 size_t rc=SION_SUCCESS;
235 DPRINTFP((2, DFUNCTION, -1,
"its the last key %ld\n",(
long) key));
242 DPRINTFP((2, DFUNCTION, -1,
"lookup for key %ld\n",(
long) key));
243 p=_sion_keyvalue_table_lookup(keymngr->
key_table,key);
245 DPRINTFP((2, DFUNCTION, -1,
"key %ld found\n",(
long) key));
248 DPRINTFP((2, DFUNCTION, -1,
"key %ld not found\n",(
long) key));
254 if(bytes_read<=entry->bytes_left) {
257 DPRINTFP((2, DFUNCTION, -1,
"advance key counters by %ld bytes\n",(
long) bytes_read));
263 DPRINTFP((2, DFUNCTION, -1,
"block is completely read, move to next if possible\n"));
272 DPRINTFP((2, DFUNCTION, -1,
"another block found (%ld,%ld)\n", (
long) entry->
current_pos, (
long) entry->
bytes_left ));
276 DPRINTFP((2, DFUNCTION, -1,
"no other block found so far ...\n"));
284 DPRINTFP((2, DFUNCTION, -1,
"bytes_read > entry->bytes_left (%zu > %zu)\n", bytes_read, entry->
bytes_left));
289 DPRINTFP((2, DFUNCTION, -1,
"leave rc=%d\n",rc));
296 #define DFUNCTION "_sion_keyvalue_keymngr_set_next_scan_pos"
298 size_t rc=SION_SUCCESS;
299 DPRINTFP((2, DFUNCTION, -1,
"enter pos=%ld\n",rc, (
long) pos));
301 DPRINTFP((2, DFUNCTION, -1,
"leave rc=%d next_scan_pos=%ld\n",rc, (
long) keymngr->
next_scan_pos));
306 #define DFUNCTION "_sion_keyvalue_keymngr_get_next_scan_pos"
308 size_t rc=SION_SUCCESS;
309 DPRINTFP((2, DFUNCTION, -1,
"enter\n"));
317 DPRINTFP((2, DFUNCTION, -1,
"leave rc=%d pos=%ld\n",rc, (
long) *pos));
322 #define DFUNCTION "_sion_keyvalue_keymngr_set_scan_done"
324 size_t rc=SION_SUCCESS;
326 DPRINTFP((2, DFUNCTION, -1,
"leave rc=%d scan_done=%d\n",rc, (
int) keymngr->
scan_done));
331 #define DFUNCTION "_sion_keyvalue_keymngr_is_scan_done"
334 DPRINTFP((2, DFUNCTION, -1,
"leave rc=%d\n",rc));
340 #define DFUNCTION "_sion_keyvalue_keymngr_lookup"
341 int _sion_keyvalue_keymngr_lookup(
_sion_keyvalue_keymngr* keymngr, sion_table_key_t key,
size_t *current_pos,
size_t *bytes_left) {
342 size_t rc=SION_SUCCESS;
346 DPRINTFP((2, DFUNCTION, -1,
"enter\n"));
349 p=_sion_keyvalue_table_lookup(keymngr->
key_table,key);
355 DPRINTFP((2, DFUNCTION, -1,
"found entry with %d blocks\n",entry->
blocks_avail));
360 DPRINTFP((2, DFUNCTION, -1,
"found block #%d --> (%ld,%ld)\n",(
int) entry->
blocklist_current->
blocknum,(
long) *current_pos,(
long) *bytes_left));
367 DPRINTFP((2, DFUNCTION, -1,
"leave rc=%d\n",rc));
373 #define DFUNCTION "_sion_keyvalue_keymngr_lookup_and_set_pos"
374 int _sion_keyvalue_keymngr_lookup_and_set_pos(
_sion_keyvalue_keymngr* keymngr, sion_table_key_t key,
int blocknum, sion_int64 posinblock,
375 size_t *current_pos,
size_t *bytes_left) {
376 size_t rc=SION_SUCCESS;
379 sion_int64 absolutepos;
382 DPRINTFP((2, DFUNCTION, -1,
"enter\n"));
388 p=_sion_keyvalue_table_lookup(keymngr->
key_table,key);
391 DPRINTFP((2, DFUNCTION, -1,
"leave key not found rc=%d\n",rc));
396 DPRINTFP((2, DFUNCTION, -1,
"found entry with %d blocks\n",entry->
blocks_avail));
402 DPRINTFP((2, DFUNCTION, -1,
"dummy call (SION_CURRENT_BLOCK,SION_CURRENT_POS), leave position unchanged rc=%d\n",rc));
406 DPRINTFP((2, DFUNCTION, -1,
"leave, seek position is already current pos in block %d\n",blocknum));
409 DPRINTFP((2, DFUNCTION, -1,
"leave, currently behind last block rc=%d\n",rc));
416 DPRINTFP((2, DFUNCTION, -1,
"seek in current_block request\n"));
422 DPRINTFP((2, DFUNCTION, -1,
"seek position is in block %d\n",blocknum));
427 DPRINTFP((2, DFUNCTION, -1,
"leave, currently behind last block rc=%d\n",rc));
437 if ( (blocknum<0) || (blocknum >= entry->
blocks_avail)) {
439 DPRINTFP((2, DFUNCTION, -1,
"leave, blocknum outside known range rc=%d\n",rc));
451 while ((block!=NULL) && (blocknum != block->
blocknum)) {
455 DPRINTFP((2, DFUNCTION, -1,
"internal error: somethink went wrong, seek, aborting, blocknum position block is NULL\n"));
456 return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
"internal error: somethink went wrong, seek, aborting ...\n"));
460 DPRINTFP((2, DFUNCTION, -1,
"position is in this block %d\n",block->
blocknum));
462 if((posinblock>=0) && (posinblock < block->len)) {
466 DPRINTFP((2, DFUNCTION, -1,
"set current pointer for entry: current_pos=%d bytes_left=%d blocknum=%d\n",(
int) entry->
current_pos,(
int) entry->
bytes_left, (
int) block->
blocknum));
468 DPRINTFP((2, DFUNCTION, -1,
"wrong pos in block %d\n",(
int) posinblock));
474 absolutepos=posinblock;
475 if ( (absolutepos < 0)
479 DPRINTFP((2, DFUNCTION, -1,
"leave, absolute position outside known range rc=%d\n",rc));
495 DPRINTFP((2, DFUNCTION, -1,
"internal error: somethink went wrong, seek, aborting, absolute position block is NULL\n"));
496 return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
"internal error: somethink went wrong, seek, aborting ...\n"));
505 DPRINTFP((2, DFUNCTION, -1,
"set current pointer for entry: current_pos=%d bytes_left=%d blocknum=%d\n",(
int) entry->
current_pos,(
int) entry->
bytes_left, (
int) block->
blocknum));
511 DPRINTFP((2, DFUNCTION, -1,
"found block --> (%ld,%ld)\n",(
long) *current_pos,(
long) *bytes_left));
513 DPRINTFP((2, DFUNCTION, -1,
"leave rc=%d\n",rc));
519 #define DFUNCTION "_sion_keyvalue_keymngr_iterator_reset"
524 DPRINTFP((2, DFUNCTION, -1,
"enter\n"));
528 DPRINTFP((2, DFUNCTION, -1,
"leave\n"));
536 #define DFUNCTION "_sion_keyvalue_keymngr_iterator_next"
537 int _sion_keyvalue_keymngr_iterator_next(
_sion_keyvalue_keymngr* keymngr, sion_table_key_t *key,
size_t *current_pos,
size_t *offset,
size_t *len) {
541 DPRINTFP((2, DFUNCTION, -1,
"enter\n"));
556 return(_sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,
"internal error: somethink went wrong, checking end of list , aborting ...\n"));
583 DPRINTFP((2, DFUNCTION, -1,
"leave rc=%d\n",rc));
588 #define DFUNCTION "_sion_keyvalue_keymngr_key_list_iterator_reset"
593 DPRINTFP((2, DFUNCTION, -1,
"enter\n"));
595 _sion_keyvalue_table_iterator_reset(keymngr->
key_table);
597 DPRINTFP((2, DFUNCTION, -1,
"leave\n"));
602 #define DFUNCTION "_sion_keyvalue_keymngr_key_list_iterator_next"
603 int _sion_keyvalue_keymngr_key_list_iterator_next(
_sion_keyvalue_keymngr* keymngr, sion_table_key_t *key) {
607 DPRINTFP((2, DFUNCTION, -1,
"enter\n"));
609 rc=_sion_keyvalue_table_iterator_next_in_store_order(keymngr->
key_table, key, &data);
611 DPRINTFP((2, DFUNCTION, -1,
"leave rc=%d key=%ld\n",rc, (
long) *key));
618 #define DFUNCTION "_sion_keyvalue_keymngr_dup"
625 DPRINTFP((2, DFUNCTION, -1,
"enter dup\n"));
628 if (keymngr == NULL) {
629 _sion_errorprint(0,_SION_ERROR_RETURN,
"cannot allocate internal keyvalue keymngr of size %lu , aborting ...\n", (
unsigned long)
sizeof(
_sion_keyvalue_keymngr));
641 keymngr->size = keymngr_orig->size;
642 keymngr->
key_table = _sion_keyvalue_table_init(keymngr_orig->size);
644 _sion_errorprint(0,_SION_ERROR_RETURN,
"cannot allocate internal keyvalue table of for %lu entries , aborting ...\n", (
unsigned long) keymngr->size);
651 DPRINTFP((2, DFUNCTION, -1,
"start duplicating entry for key %llu\n",sel_key));
653 p=_sion_keyvalue_table_lookup(keymngr_orig->
key_table,sel_key);
659 DPRINTFP((2, DFUNCTION, -1,
"add block to keymngr key=%ld offset=%ld len=%ld\n",
661 _sion_keyvalue_keymngr_add_block(keymngr,block->
entry->
key,block->
offset,block->
len);
665 DPRINTFP((2, DFUNCTION, -1,
"end duplicating one entry\n"));
669 DPRINTFP((2, DFUNCTION, -1,
"start duplicating all entries\n"));
673 DPRINTFP((2, DFUNCTION, -1,
"add block to keymngr key=%ld offset=%ld len=%ld\n",
675 _sion_keyvalue_keymngr_add_block(keymngr,block->
entry->
key,block->
offset,block->
len);
678 DPRINTFP((2, DFUNCTION, -1,
"end duplicating entries\n"));
681 DPRINTFP((2, DFUNCTION, -1,
"leave\n"));
686 #define DFUNCTION "_sion_keyvalue_keymngr_key_get_stat"
688 size_t rc=SION_SUCCESS;
692 DPRINTFP((2, DFUNCTION, -1,
"enter key=%ld\n",(
long) key));
695 p=_sion_keyvalue_table_lookup(keymngr->
key_table,key);
700 DPRINTFP((2, DFUNCTION, -1,
"found entry with %d blocks\n",entry->
blocks_avail));
703 keystat->key = (uint64_t) key;
707 DPRINTFP((2, DFUNCTION, -1,
"found entry for key %ld (%ld,%ld)\n",(
long) keystat->key, (
long) keystat->num_blocks, (
long) keystat->total_size ));
713 DPRINTFP((2, DFUNCTION, -1,
"leave rc=%d\n",rc));
719 #define DFUNCTION "_sion_keyvalue_keymngr_key_get_sizeof"
721 size_t bytes=0, help_bytes;
725 DPRINTFP((2, DFUNCTION, -1,
"enter\n"));
727 DPRINTFP((2, DFUNCTION, -1,
" sizeof key_keymgr= %5d\n", help_bytes));
731 while (block != NULL) {
735 DPRINTFP((2, DFUNCTION, -1,
" sizeof key_blocks= %5d\n", help_bytes));
739 help_bytes=_sion_keyvalue_table_get_size(keymngr->
key_table);
740 DPRINTFP((2, DFUNCTION, -1,
" sizeof key_blocks= %5d\n", help_bytes));
744 DPRINTFP((2, DFUNCTION, -1,
"leave bytes=%d\n",bytes));
#define SION_ABSOLUTE_POS
#define SION_CURRENT_BLOCK
#define SION_DESCSTATE_DUP_SEL_RANK_KEY
_sion_key_block_entry * next_inwriteorder
_sion_key_block_entry * next
_sion_key_block_entry * blocklist_head
_sion_key_block_entry * blocklist_current
_sion_key_block_entry * blocklist_tail
_sion_key_block_entry * block_inwriteorder_tail
_sion_key_block_entry * block_inwriteorder_head
_sion_key_entry * lastentry_used
_sion_key_block_entry * iterator_last_block
_sion_keyvalue_table * key_table