DS3 C SDK  5.0.0
Provides access to the Spectra S3 API with C
ds3_uint64_string_map.c
Go to the documentation of this file.
1 /*
2  * ******************************************************************************
3  * Copyright 2014-2018 Spectra Logic Corporation. All Rights Reserved.
4  * Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5  * this file except in compliance with the License. A copy of the License is located at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * or in the "license" file accompanying this file.
10  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
12  * specific language governing permissions and limitations under the License.
13  * ****************************************************************************
14  */
15 
16 #include <glib.h>
17 
18 #include "ds3_uint64_string_map.h"
19 
20 struct _ds3_uint64_string_map {
21  GHashTable* hash; //key is uint64_t*, value is ds3_str*
22 };
23 
24 // Used to iterate through a ds3_uint64_string_map
25 struct _ds3_uint64_string_map_iter {
26  GHashTableIter* g_iter;
27 };
28 
29 static void _internal_uint64_free(gpointer data) {
30  g_free((uint64_t*) data);
31 }
32 
33 static void _internal_str_free(gpointer data) {
34  ds3_str_free((ds3_str*) data);
35 }
36 
38  // Create a hash table with uint64_t* as the key, and ds3_str* as the value
39  struct _ds3_uint64_string_map* map = g_new0(struct _ds3_uint64_string_map, 1);
40  map->hash = g_hash_table_new_full(g_int64_hash, g_int64_equal, _internal_uint64_free, _internal_str_free);
41  return (ds3_uint64_string_map*) map;
42 }
43 
44 // Inserts a safe copy of the key-value pair into the map. Returns true if the key did not exist yet.
45 ds3_bool ds3_uint64_string_map_insert(ds3_uint64_string_map* map, const uint64_t* key, const ds3_str* value) {
46  if (map == NULL || map->hash == NULL) {
47  return FALSE;
48  }
49  ds3_str* value_cpy = ds3_str_dup(value);
50  uint64_t* key_cpy = g_new0(uint64_t, 1);
51  *key_cpy = *key;
52  gboolean result = g_hash_table_insert(map->hash, key_cpy, value_cpy);
53  if (result == FALSE) {
54  return False;
55  }
56  return True;
57 }
58 
60  if (map == NULL || map->hash == NULL) {
61  return FALSE;
62  }
63  gboolean result = g_hash_table_contains(map->hash, key);
64  if (result == FALSE) {
65  return False;
66  }
67  return True;
68 }
69 
70 // Looks up the value for the provided key and returns a duplicate of the ds3_str value if found.
72  if (map == NULL || map->hash == NULL) {
73  return NULL;
74  }
75  GPtrArray* value = g_hash_table_lookup(map->hash, key);
76  if (value == NULL) {
77  return NULL;
78  }
79  return ds3_str_dup((ds3_str*)value);
80 }
81 
83  return (uint64_t) g_hash_table_size(map->hash);
84 }
85 
87  if (map == NULL) {
88  return;
89  }
90  if (map->hash != NULL) {
91  // Release all elements in the hash
92  g_hash_table_remove_all(map->hash);
93 
94  // Release the hash
95  g_hash_table_unref(map->hash);
96  }
97 
98  // Release the map
99  g_free(map);
100 }
101 
103  // Create iterator for ds3_uint64_string_map
104  struct _ds3_uint64_string_map_iter* iter = g_new0(struct _ds3_uint64_string_map_iter, 1);
105  iter->g_iter = g_new0(GHashTableIter, 1);
106  g_hash_table_iter_init(iter->g_iter, map->hash);
107  return (ds3_uint64_string_map_iter*) iter;
108 }
109 
111  if (iter == NULL) {
112  return;
113  }
114  if (iter->g_iter != NULL) {
115  g_free(iter->g_iter);
116  }
117  // Release the iterator
118  g_free(iter);
119 }
120 
121 // Returns the next pair of key-value in the map if exists. The pair must be deallocate
122 // using ds3_uint64_string_pair_free. The returned pair is a copy of the values within
123 // the map, so modification of the pair does not affect the map. If there is no next
124 // entry then NULL is returned.
126  gpointer g_key;
127  gpointer g_value;
128  gboolean has_next = g_hash_table_iter_next(iter->g_iter, &g_key, &g_value);
129  if (has_next == FALSE) {
130  return NULL;
131  }
132  // Allocate the pair ds3_uint64_string_pair
133  struct _ds3_uint64_string_pair* pair = g_new0(struct _ds3_uint64_string_pair, 1);
134 
135  // Set key and value of the pair
136  uint64_t* key_ptr = (uint64_t*) g_key;
137  pair->key = *key_ptr;
138  pair->value = ds3_str_dup((ds3_str*)g_value);
139 
140  return (ds3_uint64_string_pair*) pair;
141 }
142 
144  if (pair == NULL) {
145  return;
146  }
147  if (pair->value != NULL) {
148  ds3_str_free(pair->value);
149  }
150  g_free(pair);
151 }
struct _ds3_uint64_string_map_iter ds3_uint64_string_map_iter
ds3_bool ds3_uint64_string_map_insert(ds3_uint64_string_map *map, const uint64_t *key, const ds3_str *value)
void ds3_uint64_string_map_free(ds3_uint64_string_map *map)
Definition: ds3_bool.h:24
uint64_t ds3_uint64_string_map_size(ds3_uint64_string_map *map)
ds3_uint64_string_pair * ds3_uint64_string_map_iter_next(ds3_uint64_string_map_iter *iter)
ds3_uint64_string_map_iter * ds3_uint64_string_map_iter_init(ds3_uint64_string_map *map)
void ds3_uint64_string_pair_free(ds3_uint64_string_pair *pair)
void ds3_uint64_string_map_iter_free(ds3_uint64_string_map_iter *iter)
void ds3_str_free(ds3_str *string)
Definition: ds3_string.c:50
ds3_str * ds3_str_dup(const ds3_str *string)
Definition: ds3_string.c:32
static void _internal_uint64_free(gpointer data)
ds3_uint64_string_map * ds3_uint64_string_map_init(void)
Definition: ds3_bool.h:24
ds3_bool ds3_uint64_string_map_contains(ds3_uint64_string_map *map, uint64_t *key)
struct _ds3_uint64_string_map ds3_uint64_string_map
ds3_str * ds3_uint64_string_map_lookup(ds3_uint64_string_map *map, uint64_t *key)
static void _internal_str_free(gpointer data)
ds3_bool
Definition: ds3_bool.h:23