open_address_hash_dictionary_handler.h File Reference

Description

The handler for a hash table using linear probing.

Author
Scott Ronald Fazackerley
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.
3.Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without specific prior written permission.
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 HOLDER 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.

Definition in file open_address_hash_dictionary_handler.h.

Include dependency graph for open_address_hash_dictionary_handler.h:
This graph shows which files directly or indirectly include this file:

Classes

struct  oa_dictionary
 Struct used to for instance of a given dictionary. More...
 
struct  oadict_equality_cursor
 Dictionary cursor for equality queries. More...
 

Typedefs

typedef struct oa_dictionary ion_oa_dictionary_t
 Struct used to for instance of a given dictionary. More...
 
typedef struct oadict_equality_cursor ion_oadict_equality_cursor_t
 Dictionary cursor for equality queries. More...
 

Functions

void oadict_init (ion_dictionary_handler_t *handler)
 Registers a specific handler for a dictionary instance. More...
 
ion_status_t oadict_insert (ion_dictionary_t *dictionary, ion_key_t key, ion_value_t value)
 Inserts a key and value into the dictionary. More...
 
ion_err_t oadict_create_dictionary (ion_dictionary_id_t id, ion_key_type_t key_type, ion_key_size_t key_size, ion_value_size_t value_size, ion_dictionary_size_t dictionary_size, ion_dictionary_compare_t compare, ion_dictionary_handler_t *handler, ion_dictionary_t *dictionary)
 Creates an instance of a dictionary. More...
 
ion_status_t oadict_delete (ion_dictionary_t *dictionary, ion_key_t key)
 Deletes the key and assoicated value from the dictionary instance. More...
 
ion_err_t oadict_delete_dictionary (ion_dictionary_t *dictionary)
 Deletes an instance of the dictionary and associated data. More...
 
ion_err_t oadict_destroy_dictionary (ion_dictionary_id_t id)
 Cleans up all files created by the dictionary, and frees any allocated memory, for an already closed dictionary. More...
 
ion_status_t oadict_update (ion_dictionary_t *dictionary, ion_key_t key, ion_value_t value)
 Updates the value for a given key. More...
 
int oadict_compare (ion_key_t first_key, ion_key_t second_key)
 Compares two key and returns the difference. More...
 
ion_cursor_status_t oadict_next (ion_dict_cursor_t *cursor, ion_record_t *record)
 Next function to query and retrieve the next <K,V> that stratifies the predicate of the cursor. More...
 
ion_boolean_t oadict_is_equal (ion_dictionary_t *dict, ion_key_t key1, ion_key_t key2)
 Compares two keys and determines if they are equal assuming that they are equal is length (in size). More...
 
void oadict_destroy_cursor (ion_dict_cursor_t **cursor)
 Destroys the cursor. More...
 

Typedef Documentation

Struct used to for instance of a given dictionary.

Dictionary cursor for equality queries.

Used when a dictionary supports multiple vvalues for a given key.

        This subtype should be extended when supported for a given
        dictionary.

Function Documentation

int oadict_compare ( ion_key_t  first_key,
ion_key_t  second_key 
)

Compares two key and returns the difference.

Compares two key and returns the difference depending on the type of the key defined for the dictionary instance. If the keys are of numeric type, the return value is the difference between the keys. If the value is negative, first_key is smaller than second_key. If return value is positive, then first_key is larger than second_key. If the return value is 0 then first_key is equal to second_key.

If the key type is key_type_char_array then the function memcmp compares the size bytes of memory beginning at a1 against the size bytes of memory beginning at a2. The value returned has the same sign as the difference between the first differing pair of bytes (interpreted as unsigned char objects, then promoted to int).

Parameters
first_keyThe first key in the comparison.
second_keyThe second key in the comparison.
Returns
The difference between the keys.
ion_err_t oadict_create_dictionary ( ion_dictionary_id_t  id,
ion_key_type_t  key_type,
ion_key_size_t  key_size,
ion_value_size_t  value_size,
ion_dictionary_size_t  dictionary_size,
ion_dictionary_compare_t  compare,
ion_dictionary_handler_t handler,
ion_dictionary_t dictionary 
)

Creates an instance of a dictionary.

Creates as instance of a dictionary given a key_size and value_size, in bytes as well as the dictionary size which is the number of buckets available in the hashmap.

Parameters
id
key_type
key_sizeThe size of the key in bytes.
value_sizeThe size of the value in bytes.
dictionary_sizeThe size of the hashmap in discrete units
compareFunction pointer for the comparison function for the dictionary.
handlerTHe handler for the specific dictionary being created.
dictionaryThe pointer declared by the caller that will reference the instance of the dictionary created.
Returns
The status of the creation of the dictionary.

Definition at line 349 of file open_address_hash_dictionary_handler.c.

358  {
359  UNUSED(id);
360  /* this is the instance of the hashmap */
361  dictionary->instance = malloc(sizeof(ion_hashmap_t));
362 
363  dictionary->instance->compare = compare;
365 
366  /* this registers the dictionary the dictionary */
367  oah_initialize((ion_hashmap_t *) dictionary->instance, oah_compute_simple_hash, key_type, key_size, value_size, dictionary_size); /* just pick an arbitary size for testing atm */
368 
369  /*TODO The correct comparison operator needs to be bound at run time
370  * based on the type of key defined
371  */
372 
373  if (NULL == handler) {
374  return err_uninitialized;
375  }
376 
377  /* register the correct handler */
378  dictionary->handler = handler;
379 
380  return 0;
381 }
Struct used to maintain an instance of an in memory hashmap.
ion_err_t oah_initialize(ion_hashmap_t *hashmap, ion_hash_t(*hashing_function)(ion_hashmap_t *, ion_key_t, int), ion_key_type_t key_type, ion_key_size_t key_size, ion_value_size_t value_size, int size)
This function initializes an open address in memory hash map.
ion_dictionary_handler_t * handler
ion_dictionary_parent_t * instance
ion_hash_t oah_compute_simple_hash(ion_hashmap_t *hashmap, ion_key_t key, int size_of_key)
A simple hashing algorithm implementation.
ion_dictionary_type_t type
#define UNUSED(x)
Definition: kv_system.h:102
ion_dictionary_compare_t compare

Here is the call graph for this function:

Here is the caller graph for this function:

ion_status_t oadict_delete ( ion_dictionary_t dictionary,
ion_key_t  key 
)

Deletes the key and assoicated value from the dictionary instance.

Parameters
dictionaryThe instance of the dictionary to delete from.
keyThe key that is to be deleted.
Returns
The status of the deletion

Definition at line 384 of file open_address_hash_dictionary_handler.c.

387  {
388  return oah_delete((ion_hashmap_t *) dictionary->instance, key);
389 }
Struct used to maintain an instance of an in memory hashmap.
ion_dictionary_parent_t * instance
#define key(k)
Definition: bpp_tree.c:75
ion_status_t oah_delete(ion_hashmap_t *hash_map, ion_key_t key)
Deletes item from map.

Here is the call graph for this function:

Here is the caller graph for this function:

ion_err_t oadict_delete_dictionary ( ion_dictionary_t dictionary)

Deletes an instance of the dictionary and associated data.

Parameters
dictionaryThe instance of the dictionary to delete.
Returns
The status of the dictionary deletion.

Definition at line 392 of file open_address_hash_dictionary_handler.c.

394  {
395  ion_err_t result = oah_destroy((ion_hashmap_t *) dictionary->instance);
396 
397  free(dictionary->instance);
398  dictionary->instance = NULL;/* When releasing memory, set pointer to NULL */
399  return result;
400 }
Struct used to maintain an instance of an in memory hashmap.
ion_dictionary_parent_t * instance
ion_err_t oah_destroy(ion_hashmap_t *hash_map)
Destroys the map in memory.
char ion_err_t
The error type used to store error codes.
Definition: kv_system.h:226

Here is the call graph for this function:

Here is the caller graph for this function:

void oadict_destroy_cursor ( ion_dict_cursor_t **  cursor)

Destroys the cursor.

Destroys the cursor when the user is finished with it. The destroy function will free up internally allocated memory as well as freeing up any reference to the cursor itself. Cursor pointers will be set to NULL as per ION_DB specification for de-allocated pointers.

Parameters
cursorpointer to cursor.

Definition at line 487 of file open_address_hash_dictionary_handler.c.

489  {
490  (*cursor)->predicate->destroy(&(*cursor)->predicate);
491  free(*cursor);
492  *cursor = NULL;
493 }

Here is the caller graph for this function:

ion_err_t oadict_destroy_dictionary ( ion_dictionary_id_t  id)

Cleans up all files created by the dictionary, and frees any allocated memory, for an already closed dictionary.

Parameters
idThe identifier identifying the dictionary to delete.
Returns
The resulting status of the operation.

Definition at line 403 of file open_address_hash_dictionary_handler.c.

405  {
406  UNUSED(id);
407  return err_not_implemented;
408 }
#define UNUSED(x)
Definition: kv_system.h:102

Here is the caller graph for this function:

void oadict_init ( ion_dictionary_handler_t handler)

Registers a specific handler for a dictionary instance.

Registers functions for handlers. This only needs to be called once for each type of dictionary that is present.

Parameters
handlerThe handler for the dictionary instance that is to be initialized.

Definition at line 324 of file open_address_hash_dictionary_handler.c.

326  {
327  handler->insert = oadict_insert;
329  handler->get = oadict_get;
330  handler->update = oadict_update;
331  handler->find = oadict_find;
332  handler->remove = oadict_delete;
337 }
ion_status_t(* insert)(ion_dictionary_t *, ion_key_t, ion_value_t)
ion_err_t oadict_open_dictionary(ion_dictionary_handler_t *handler, ion_dictionary_t *dictionary, ion_dictionary_config_info_t *config, ion_dictionary_compare_t compare)
Opens a specific open address hash instance of a dictionary.
ion_status_t oadict_update(ion_dictionary_t *dictionary, ion_key_t key, ion_value_t value)
Updates the value for a given key.
ion_err_t oadict_destroy_dictionary(ion_dictionary_id_t id)
Cleans up all files created by the dictionary, and frees any allocated memory, for an already closed ...
ion_status_t(* remove)(ion_dictionary_t *, ion_key_t)
ion_status_t oadict_delete(ion_dictionary_t *dictionary, ion_key_t key)
Deletes the key and assoicated value from the dictionary instance.
ion_err_t(* destroy_dictionary)(ion_dictionary_id_t id)
ion_err_t oadict_find(ion_dictionary_t *dictionary, ion_predicate_t *predicate, ion_dict_cursor_t **cursor)
Finds multiple instances of a keys that satisfy the provided predicate in the dictionary.
ion_status_t(* update)(ion_dictionary_t *, ion_key_t, ion_value_t)
ion_err_t oadict_delete_dictionary(ion_dictionary_t *dictionary)
Deletes an instance of the dictionary and associated data.
ion_err_t(* close_dictionary)(ion_dictionary_t *)
ion_err_t(* open_dictionary)(ion_dictionary_handler_t *, ion_dictionary_t *, ion_dictionary_config_info_t *, ion_dictionary_compare_t)
ion_err_t oadict_create_dictionary(ion_dictionary_id_t id, ion_key_type_t key_type, ion_key_size_t key_size, ion_value_size_t value_size, ion_dictionary_size_t dictionary_size, ion_dictionary_compare_t compare, ion_dictionary_handler_t *handler, ion_dictionary_t *dictionary)
Creates an instance of a dictionary.
ion_err_t(* create_dictionary)(ion_dictionary_id_t, ion_key_type_t, ion_key_size_t, ion_value_size_t, ion_dictionary_size_t, ion_dictionary_compare_t, ion_dictionary_handler_t *, ion_dictionary_t *)
ion_err_t(* delete_dictionary)(ion_dictionary_t *)
ion_status_t oadict_get(ion_dictionary_t *dictionary, ion_key_t key, ion_value_t value)
Queries a dictionary instance for the given key and returns the associated value. ...
ion_err_t(* find)(ion_dictionary_t *, ion_predicate_t *, ion_dict_cursor_t **)
ion_err_t oadict_close_dictionary(ion_dictionary_t *dictionary)
Closes an open address hash instance of a dictionary.
ion_status_t oadict_insert(ion_dictionary_t *dictionary, ion_key_t key, ion_value_t value)
Inserts a key and value into the dictionary.
ion_status_t(* get)(ion_dictionary_t *, ion_key_t, ion_value_t)

Here is the call graph for this function:

Here is the caller graph for this function:

ion_status_t oadict_insert ( ion_dictionary_t dictionary,
ion_key_t  key,
ion_value_t  value 
)

Inserts a key and value into the dictionary.

Parameters
dictionaryThe dictionary instance to insert the value into.
keyThe key to use.
valueThe value to use.
Returns
The status on the insertion of the record.

Definition at line 340 of file open_address_hash_dictionary_handler.c.

344  {
345  return oah_insert((ion_hashmap_t *) dictionary->instance, key, value);
346 }
Struct used to maintain an instance of an in memory hashmap.
ion_dictionary_parent_t * instance
#define key(k)
Definition: bpp_tree.c:75
ion_status_t oah_insert(ion_hashmap_t *hash_map, ion_key_t key, ion_value_t value)
Insert record into hashmap.

Here is the call graph for this function:

Here is the caller graph for this function:

ion_boolean_t oadict_is_equal ( ion_dictionary_t dict,
ion_key_t  key1,
ion_key_t  key2 
)

Compares two keys and determines if they are equal assuming that they are equal is length (in size).

Parameters
dictThe map the keys are associated with.
key1The first key for comparison.
key2The second key for comparison.
Returns
If the keys are equal.

Definition at line 473 of file open_address_hash_dictionary_handler.c.

477  {
478  if (memcmp(key1, key2, (((ion_hashmap_t *) dict->instance)->super.record.key_size)) == ION_IS_EQUAL) {
479  return boolean_true;
480  }
481  else {
482  return boolean_false;
483  }
484 }
Struct used to maintain an instance of an in memory hashmap.
ion_dictionary_parent_t * instance
#define ION_IS_EQUAL
Definition: kv_system.h:64
ion_cursor_status_t oadict_next ( ion_dict_cursor_t cursor,
ion_record_t record 
)

Next function to query and retrieve the next <K,V> that stratifies the predicate of the cursor.

Parameters
cursorThe cursor to iterate over the results.
Returns
The status of the cursor. Next function to query and retrieve the next <K,V> that stratifies the predicate of the cursor.
Parameters
cursorThe cursor to iterate over the results.
record
Returns
The status of the cursor.

Definition at line 420 of file open_address_hash_dictionary_handler.c.

423  {
425 
426  /* check the status of the cursor and if it is not valid or at the end, just exit */
427  if (cursor->status == cs_cursor_uninitialized) {
428  return cursor->status;
429  }
430  else if (cursor->status == cs_end_of_results) {
431  return cursor->status;
432  }
433  else if ((cursor->status == cs_cursor_initialized) || (cursor->status == cs_cursor_active)) {
434  /* cursor is active and results have never been accessed */
435  /* extract reference to map */
436  ion_hashmap_t *hash_map = ((ion_hashmap_t *) cursor->dictionary->instance);
437 
438  /* assume that the value has been pre-allocated */
439  /* compute length of data record stored in map */
440  int data_length = hash_map->super.record.key_size + hash_map->super.record.value_size;
441 
442  if (cursor->status == cs_cursor_active) {
443  /* find the next valid entry */
444 
445  /* scan and determine what to do? */
446  if (cs_end_of_results == oadict_scan(oadict_cursor)) {
447  /* Then this is the end and there are no more results */
448  cursor->status = cs_end_of_results;
449  return cursor->status;
450  }
451  }
452  else {
453  /* if the cursor is initialized but not active, then just read the data and set cursor active */
454  cursor->status = cs_cursor_active;
455  }
456 
457  /* the results are now ready //reference item at given position */
458  ion_hash_bucket_t *item = (((ion_hash_bucket_t *) ((hash_map->entry + (data_length + SIZEOF(STATUS)) * oadict_cursor->current /*idx*/))));
459 
460  memcpy(record->key, (item->data), hash_map->super.record.key_size);
461 
462  memcpy(record->value, (item->data + hash_map->super.record.key_size), hash_map->super.record.value_size);
463 
464  /* and update current cursor position */
465  return cursor->status;
466  }
467 
468  /* and if you get this far, the cursor is invalid */
469  return cs_invalid_cursor;
470 }
ion_record_info_t record
char * entry
Struct used to maintain an instance of an in memory hashmap.
ion_dictionary_parent_t * instance
ion_value_t value
Definition: kv_system.h:318
ion_dictionary_t * dictionary
ion_byte_t data[]
ion_dictionary_parent_t super
ion_cursor_status_t status
ion_key_t key
Definition: kv_system.h:316
Struct used to maintain individual records in the hashmap.
ion_key_size_t key_size
Definition: kv_system.h:307
#define SIZEOF(STATUS)
ion_err_t oadict_scan(ion_oadict_cursor_t *cursor)
Starts scanning map looking for conditions that match predicate and returns result.
ion_value_size_t value_size
Definition: kv_system.h:309

Here is the call graph for this function:

Here is the caller graph for this function:

ion_status_t oadict_update ( ion_dictionary_t dictionary,
ion_key_t  key,
ion_value_t  value 
)

Updates the value for a given key.

Updates the value for a given key. If the key does not currently exist in the hashmap, it will be created and the value sorted.

Parameters
dictionaryThe instance of the dictionary to be updated.
keyThe key that is to be updated.
valueThe value that is to be updated.
Returns
The status of the update.

Definition at line 411 of file open_address_hash_dictionary_handler.c.

415  {
416  return oah_update((ion_hashmap_t *) dictionary->instance, key, value);
417 }
Struct used to maintain an instance of an in memory hashmap.
ion_dictionary_parent_t * instance
#define key(k)
Definition: bpp_tree.c:75
ion_status_t oah_update(ion_hashmap_t *hash_map, ion_key_t key, ion_value_t value)
Updates a value in the map.

Here is the call graph for this function:

Here is the caller graph for this function: