iinq.c
Go to the documentation of this file.
1 /******************************************************************************/
35 /******************************************************************************/
36 
37 #include <stdio.h>
38 #include "iinq.h"
39 #include "../dictionary/bpp_tree/bpp_tree_handler.h"
40 
43  char *schema_file_name,
44  ion_key_type_t key_type,
45  ion_key_size_t key_size,
46  ion_value_size_t value_size
47 ) {
49  FILE *schema_file;
52 
53  dictionary.handler = &handler;
54 
55  error = ion_init_master_table();
56 
57  if (err_ok != error) {
58  return error;
59  }
60 
61  /* Load the handler. */
62  bpptree_init(&handler);
63 
64  /* If the file exists, fail. */
65  if (NULL != (schema_file = fopen(schema_file_name, "rb"))) {
66  if (0 != fclose(schema_file)) {
67  return err_file_close_error;
68  }
69 
71  }
72  /* Otherwise, we are creating the dictionary for the first time. */
73  else if (NULL != (schema_file = fopen(schema_file_name, "w+b"))) {
74  if (0 != fseek(schema_file, 0, SEEK_SET)) {
75  return err_file_bad_seek;
76  }
77 
78  error = ion_master_table_create_dictionary(&handler, &dictionary, key_type, key_size, value_size, -1);
79 
80  if (err_ok != error) {
81  return error;
82  }
83 
84  if (1 != fwrite(&dictionary.instance->id, sizeof(dictionary.instance->id), 1, schema_file)) {
85  return err_file_read_error;
86  }
87 
88  if (0 != fclose(schema_file)) {
89  return err_file_close_error;
90  }
91 
92  ion_close_dictionary(&dictionary);
93 
94  error = err_ok;
95  }
96  else {
97  error = err_file_open_error;
98  }
99 
101 
102  return error;
103 }
104 
105 ion_err_t
107  char *schema_file_name,
109  ion_dictionary_handler_t *handler
110 ) {
112  FILE *schema_file;
114 
115  error = ion_init_master_table();
116 
117  if (err_ok != error) {
118  return error;
119  }
120 
121  /* Load the handler. */
122  bpptree_init(handler);
123 
124  /* If the schema file already exists. */
125  if (NULL != (schema_file = fopen(schema_file_name, "rb"))) {
126  if (0 != fseek(schema_file, 0, SEEK_SET)) {
127  return err_file_bad_seek;
128  }
129 
130  if (1 != fread(&id, sizeof(id), 1, schema_file)) {
131  return err_file_read_error;
132  }
133 
134  error = ion_open_dictionary(handler, dictionary, id);
135 
136  if (err_ok != error) {
137  return error;
138  }
139 
140  if (0 != fclose(schema_file)) {
141  return err_file_close_error;
142  }
143 
144  error = err_ok;
145  }
146  else {
147  error = err_file_open_error;
148  }
149 
151 
152  return error;
153 }
154 
157  char *schema_file_name,
158  ion_key_t key,
159  ion_value_t value
160 ) {
164  ion_dictionary_handler_t handler;
165 
166  dictionary.handler = &handler;
167 
168  error = iinq_open_source(schema_file_name, &dictionary, &handler);
169 
170  if (err_ok != error) {
171  status = ION_STATUS_ERROR(error);
172  goto RETURN;
173  }
174 
175  status = dictionary_insert(&dictionary, key, value);
176  error = ion_close_dictionary(&dictionary);
177 
178  if ((err_ok == status.error) && (err_ok != error)) {
179  status.error = error;
180  }
181 
182 RETURN: return status;
183 }
184 
187  char *schema_file_name,
188  ion_key_t key,
189  ion_value_t value
190 ) {
194  ion_dictionary_handler_t handler;
195 
196  dictionary.handler = &handler;
197 
198  error = iinq_open_source(schema_file_name, &dictionary, &handler);
199 
200  if (err_ok != error) {
201  status = ION_STATUS_ERROR(error);
202  goto RETURN;
203  }
204 
205  status = dictionary_update(&dictionary, key, value);
206  error = ion_close_dictionary(&dictionary);
207 
208  if ((err_ok == status.error) && (err_ok != error)) {
209  status.error = error;
210  }
211 
212 RETURN: return status;
213 }
214 
217  char *schema_file_name,
218  ion_key_t key
219 ) {
223  ion_dictionary_handler_t handler;
224 
225  dictionary.handler = &handler;
226 
227  error = iinq_open_source(schema_file_name, &dictionary, &handler);
228 
229  if (err_ok != error) {
230  status = ION_STATUS_ERROR(error);
231  goto RETURN;
232  }
233 
234  status = dictionary_delete(&dictionary, key);
235  error = ion_close_dictionary(&dictionary);
236 
237  if ((err_ok == status.error) && (err_ok != error)) {
238  status.error = error;
239  }
240 
241 RETURN: return status;
242 }
243 
244 ion_err_t
246  char *schema_file_name
247 ) {
250  ion_dictionary_handler_t handler;
251 
252  dictionary.handler = &handler;
253 
254  error = iinq_open_source(schema_file_name, &dictionary, &handler);
255 
256  if (err_ok != error) {
257  return error;
258  }
259 
260  error = dictionary_delete_dictionary(&dictionary);
261 
262  fremove(schema_file_name);
263 
264  return error;
265 }
#define ION_STATUS_ERROR(error)
Definition: kv_system.h:110
enum ION_KEY_TYPE ion_key_type_t
This is the available key types for ION_DB. All types will be based on system defines.
This code contains definitions for iinq functions.
ion_err_t ion_master_table_create_dictionary(ion_dictionary_handler_t *handler, ion_dictionary_t *dictionary, ion_key_type_t key_type, ion_key_size_t key_size, ion_value_size_t value_size, ion_dictionary_size_t dictionary_size)
Creates a dictionary through use of the master table.
int ion_value_size_t
The size (length) of a dictionary value in bytes.
Definition: kv_system.h:256
ion_dictionary_handler_t * handler
ion_dictionary_parent_t * instance
ion_status_t iinq_delete(char *schema_file_name, ion_key_t key)
Definition: iinq.c:216
ion_err_t iinq_drop(char *schema_file_name)
Definition: iinq.c:245
unsigned int ion_dictionary_id_t
A type used to identify dictionaries, specifically in the master table.
#define key(k)
Definition: bpp_tree.c:75
ion_status_t dictionary_update(ion_dictionary_t *dictionary, ion_key_t key, ion_value_t value)
Update all records with a given key.
Definition: dictionary.c:169
ion_err_t error
Definition: kv_system.h:291
ion_status_t dictionary_delete(ion_dictionary_t *dictionary, ion_key_t key)
Delete a value given a key.
Definition: dictionary.c:199
ion_status_t dictionary_insert(ion_dictionary_t *dictionary, ion_key_t key, ion_value_t value)
Insert a value into a dictionary.
Definition: dictionary.c:151
#define fremove(x)
Definition: kv_system.h:56
char ion_err_t
The error type used to store error codes.
Definition: kv_system.h:226
void * ion_key_t
A dictionary key.
Definition: kv_system.h:241
ion_status_t iinq_insert(char *schema_file_name, ion_key_t key, ion_value_t value)
Definition: iinq.c:156
void * ion_value_t
A dictionary value.
Definition: kv_system.h:246
A dictionary contains information regarding an instance of the storage element and the associated han...
ion_dictionary_id_t id
ion_err_t ion_close_dictionary(ion_dictionary_t *dictionary)
Closes a given dictionary.
ion_err_t ion_init_master_table(void)
Opens the master table.
ion_status_t iinq_update(char *schema_file_name, ion_key_t key, ion_value_t value)
Definition: iinq.c:186
ion_err_t dictionary_delete_dictionary(ion_dictionary_t *dictionary)
Destroys dictionary.
Definition: dictionary.c:178
void bpptree_init(ion_dictionary_handler_t *handler)
Registers a specific handler for a dictionary instance.
ion_err_t ion_open_dictionary(ion_dictionary_handler_t *handler, ion_dictionary_t *dictionary, ion_dictionary_id_t id)
Finds the target dictionary and opens it.
ion_err_t ion_close_master_table(void)
Closes the master table.
#define error(rc)
Definition: bpp_tree.c:151
int ion_key_size_t
The size (length) of a dictionary key in bytes.
Definition: kv_system.h:251
ion_err_t iinq_create_source(char *schema_file_name, ion_key_type_t key_type, ion_key_size_t key_size, ion_value_size_t value_size)
Definition: iinq.c:42
ion_err_t iinq_open_source(char *schema_file_name, ion_dictionary_t *dictionary, ion_dictionary_handler_t *handler)
Definition: iinq.c:106
#define ION_STATUS_INITIALIZE
Definition: kv_system.h:107
A dictionary_handler is responsible for dealing with the specific interface for an underlying diction...
A status object that describes the result of a dictionary operation.
Definition: kv_system.h:290