linked_file_bag.c
Go to the documentation of this file.
1 /******************************************************************************/
36 /******************************************************************************/
37 
38 #include "linked_file_bag.h"
39 
40 #if !defined(ION_NULL)
41 #define ION_NULL ((void *) 0)
42 #endif
43 
46  ion_lfb_t *bag,
47  ion_byte_t *to_write,
48  unsigned int num_bytes,
50  ion_file_offset_t *wrote_at
51 ) {
52  ion_file_offset_t next_empty;
54 
55  next_empty = ION_LFB_NULL;
56 
57  if (ION_LFB_NULL != bag->next_empty) {
58  error = ion_fread_at(bag->file_handle, bag->next_empty, sizeof(ion_file_offset_t), (ion_byte_t *) &next_empty);
59 
60  if (err_ok != error) {
61  return error;
62  }
63 
64  *wrote_at = bag->next_empty;
65  }
66  else {
67  *wrote_at = ion_fend(bag->file_handle);
68  }
69 
70  error = ion_fwrite_at(bag->file_handle, *wrote_at, sizeof(ion_file_offset_t), (ion_byte_t *) &next);
71 
72  if (err_ok != error) {
73  return error;
74  }
75 
76  error = ion_fwrite_at(bag->file_handle, *wrote_at + sizeof(ion_file_offset_t), num_bytes, to_write);
77 
78  if (err_ok != error) {
79  return error;
80  }
81 
82  bag->next_empty = next_empty;
83 
84  return err_ok;
85 }
86 
89  ion_lfb_t *bag,
90  ion_file_offset_t offset,
91  unsigned int num_bytes,
92  ion_byte_t *write_to,
94 ) {
96 
97  error = ion_fread_at(bag->file_handle, offset, sizeof(ion_file_offset_t), (ion_byte_t *) next);
98 
99  if (err_ok != error) {
100  return error;
101  }
102 
103  error = ion_fread_at(bag->file_handle, offset + sizeof(ion_file_offset_t), num_bytes, write_to);
104 
105  return error;
106 }
107 
120 ion_err_t
122  ion_lfb_t *bag,
123  ion_file_offset_t offset,
125 ) {
127 
128  error = ion_fwrite_at(bag->file_handle, offset, sizeof(ion_file_offset_t), (ion_byte_t *) &(next));
129 
130  if (err_ok == error) {
131  bag->next_empty = offset;
132  }
133 
134  return error;
135 }
136 
137 ion_err_t
139  ion_lfb_t *bag,
140  ion_file_offset_t offset
141 ) {
142  return lfb_update_next(bag, offset, bag->next_empty);
143 }
144 
145 ion_err_t
147  ion_lfb_t *bag,
148  ion_file_offset_t offset,
149  ion_result_count_t *count
150 ) {
153 
154  while (ION_LFB_NULL != offset) {
155  error = ion_fread_at(bag->file_handle, offset, sizeof(ion_file_offset_t), (ion_byte_t *) &next);
156 
157  if (err_ok != error) {
158  return error;
159  }
160 
161  error = lfb_delete(bag, offset);
162 
163  if (err_ok != error) {
164  return error;
165  }
166 
167  if (NULL != count) {
168  (*count)++;
169  }
170 
171  offset = next;
172  }
173 
174  return err_ok;
175 }
176 
177 ion_err_t
179  ion_lfb_t *bag,
180  ion_file_offset_t offset,
181  unsigned int num_bytes,
182  ion_byte_t *to_write,
184 ) {
186 
187  if (NULL != next) {
188  error = lfb_update_next(bag, offset, *next);
189 
190  if (err_ok != error) {
191  return error;
192  }
193  }
194 
195  error = ion_fwrite_at(bag->file_handle, offset + sizeof(ion_file_offset_t), num_bytes, to_write);
196 
197  return error;
198 }
199 
200 ion_err_t
202  ion_lfb_t *bag,
203  ion_file_offset_t offset,
204  unsigned int num_bytes,
205  ion_byte_t *to_write,
206  ion_result_count_t *count
207 ) {
210 
211  while (ION_LFB_NULL != offset) {
212  error = ion_fread_at(bag->file_handle, offset, sizeof(ion_file_offset_t), (ion_byte_t *) &next);
213 
214  if (err_ok != error) {
215  return error;
216  }
217 
218  error = lfb_update(bag, offset, num_bytes, to_write, NULL);
219 
220  if (err_ok != error) {
221  return error;
222  }
223 
224  if (NULL != count) {
225  (*count)++;
226  }
227 
228  offset = next;
229  }
230 
231  return err_ok;
232 }
unsigned char ion_byte_t
A byte type.
Definition: kv_system.h:232
API for a persistent bag. Items are linked together in a singly linked list.
ion_err_t lfb_delete(ion_lfb_t *bag, ion_file_offset_t offset)
Attempt to delete a record stored at a given offset.
#define ION_LFB_NULL
ion_err_t lfb_update(ion_lfb_t *bag, ion_file_offset_t offset, unsigned int num_bytes, ion_byte_t *to_write, ion_file_offset_t *next)
Attempt to update a record within a linked file bag at a given offset.
ion_err_t lfb_update_all(ion_lfb_t *bag, ion_file_offset_t offset, unsigned int num_bytes, ion_byte_t *to_write, ion_result_count_t *count)
Attempt to update all records kept within a specific bag, starting at some record at a given offset...
#define next(b)
Definition: bpp_tree.c:82
ion_file_handle_t file_handle
ion_err_t lfb_get(ion_lfb_t *bag, ion_file_offset_t offset, unsigned int num_bytes, ion_byte_t *write_to, ion_file_offset_t *next)
Add an item to the linked file bag.
char ion_err_t
The error type used to store error codes.
Definition: kv_system.h:226
long ion_file_offset_t
Definition: ion_file.h:47
ion_file_offset_t next_empty
#define error(rc)
Definition: bpp_tree.c:139
ion_err_t lfb_delete_all(ion_lfb_t *bag, ion_file_offset_t offset, ion_result_count_t *count)
Attempt to delete all contents from the bag starting at a given offset.
ion_err_t ion_fwrite_at(ion_file_handle_t file, ion_file_offset_t offset, unsigned int num_bytes, ion_byte_t *to_write)
Definition: ion_file.c:177
ion_err_t lfb_put(ion_lfb_t *bag, ion_byte_t *to_write, unsigned int num_bytes, ion_file_offset_t next, ion_file_offset_t *wrote_at)
Add an item to the linked file bag.
ion_file_offset_t ion_fend(ion_file_handle_t file)
Definition: ion_file.c:143
ion_err_t lfb_update_next(ion_lfb_t *bag, ion_file_offset_t offset, ion_file_offset_t next)
Update the next offset for the record stored at offset.
int ion_result_count_t
A type for the number of results changed during an operation.
Definition: kv_system.h:284
ion_err_t ion_fread_at(ion_file_handle_t file, ion_file_offset_t offset, unsigned int num_bytes, ion_byte_t *write_to)
Definition: ion_file.c:237
A handler struct for a linked file bag instance.