store.c
Go to the documentation of this file.
1 
10 #include "store.h"
11 #include "mcl_core/mcl_memory.h"
12 #include "mcl_core/mcl_list.h"
18 #include "event.h"
19 #include "timeseries.h"
20 #include "file.h"
21 #include "custom_data.h"
23 
24 // This function adds item to store.
25 static mcl_error_t _store_add_data(mcl_store_t *store, void *item);
26 
27 // This function destroys item.
28 static mcl_error_t _item_destroy(void **item);
29 
31 {
32  mcl_error_t code = MCL_OK;
33 
34  MCL_DEBUG_ENTRY("mcl_store_t **store = <%p>", store);
35 
36  // Null check.
37  MCL_ASSERT_NOT_NULL(store, code);
38 
39  // Allocate store.
40  MCL_NEW(*store);
41  MCL_ASSERT_CODE_MESSAGE(MCL_NULL != *store, MCL_OUT_OF_MEMORY, "Memory for store could not be allocated!");
42 
43  (*store)->item_base.preamble = MCL_ITEM_PREAMBLE;
44  (*store)->item_base.type = MCL_ITEM_TYPE_STORE;
45  (*store)->last_item_id = 0;
46 
47  // Initialize list containing mcl data types.
48  code = mcl_list_initialize(&((*store)->item_list));
49  MCL_ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, MCL_FREE(*store), code, "Initialization of store list failed!");
50 
51  MCL_DEBUG("Store list has been successfully initialized.");
52 
54  MCL_DEBUG_LEAVE("retVal = <%d>", code);
55  return code;
56 }
57 
59 {
60  mcl_error_t code;
61 
62  MCL_DEBUG_ENTRY("mcl_store_t *store = <%p>, void *item = <%p>", store, item);
63 
64  // Null check.
65  MCL_ASSERT_NOT_NULL(store, code);
66  MCL_ASSERT_NOT_NULL(item, code);
67 
68  if (MCL_ITEM_PREAMBLE != ((mcl_item_t *)item)->preamble)
69  {
70  MCL_DEBUG_LEAVE("retVal = <%d>", MCL_INVALID_PARAMETER);
71  return MCL_INVALID_PARAMETER;
72  }
73 
74  code = _store_add_data(store, item);
75 
77  MCL_DEBUG_LEAVE("retVal = <%d>", code);
78  return code;
79 }
80 
82 {
83  MCL_DEBUG_ENTRY("mcl_store_t **store = <%p>", store);
84 
85  // Make sure input argument is not NULL.
86  if ((MCL_NULL != store) && (MCL_NULL != *store))
87  {
89  MCL_FREE(*store);
90  }
91  else
92  {
93  MCL_DEBUG("Store is already NULL. Nothing will be destroyed.");
94  }
95 
96  MCL_DEBUG_LEAVE("retVal = <void>");
97 }
98 
99 static mcl_error_t _store_add_data(mcl_store_t *store, void *item)
100 {
101  mcl_error_t code;
102  store_item_t *store_item;
103 
104  MCL_DEBUG_ENTRY("mcl_store_t *store = <%p>, void *item = <%p>", store, item);
105 
106  // Check data fields if they are filled according to item type
107  switch (((mcl_item_t *) item)->type)
108  {
110  code = timeseries_validate((timeseries_t *) item);
111  break;
112 
113  case MCL_ITEM_TYPE_EVENT:
114  code = event_validate((event_t *) item);
115  break;
116 
117  case MCL_ITEM_TYPE_FILE:
118  code = file_validate((file_t *) item);
119  break;
120 
122  code = custom_data_validate((custom_data_t *) item);
123  break;
124 
127  break;
128 
129  default:
130  return MCL_INVALID_PARAMETER;
131  }
132 
133  MCL_ASSERT_CODE_MESSAGE(MCL_OK == code, MCL_INVALID_PARAMETER, "Item validation failed for the selected data type.");
134 
135  MCL_NEW(store_item);
136  MCL_ASSERT_CODE_MESSAGE(MCL_NULL != store_item, MCL_OUT_OF_MEMORY, "Not enough memory to create store_item!");
137 
138  store_item->item = item;
139  store_item->id = (store)->last_item_id + 1;
140  store_item->status = STORE_ITEM_STATUS_READY;
141 
142  code = mcl_list_add(store->item_list, store_item);
143  MCL_ASSERT_STATEMENT_CODE_MESSAGE(MCL_OK == code, MCL_FREE(store_item), code, "Add to list failed!");
144 
145  // Increase the last_item_id if the item is added to store.
146  (store)->last_item_id = store_item->id;
147 
148  MCL_DEBUG_LEAVE("retVal = <%d>", MCL_OK);
149  return MCL_OK;
150 }
151 
152 static mcl_error_t _item_destroy(void **item)
153 {
154  MCL_DEBUG_ENTRY("void **item = <%p>", item);
155 
156  switch (((mcl_item_t *) *item)->type)
157  {
160  break;
161 
162  case MCL_ITEM_TYPE_EVENT:
163  mcl_event_destroy((event_t **) item);
164  break;
165 
166  case MCL_ITEM_TYPE_FILE:
167  mcl_file_destroy((file_t **) item);
168  break;
169 
172  break;
173 
176  break;
177 
178  default:
179  break;
180  }
181 
182  MCL_DEBUG_LEAVE("retVal = <%d>", MCL_OK);
183  return MCL_OK;
184 }
185 
187 {
188  MCL_DEBUG_ENTRY("store_item_t **store_item = <%p>", store_item);
189 
190  // Destroy item.
191  _item_destroy(&((*store_item)->item));
192  MCL_FREE(*store_item);
193 
194  MCL_DEBUG_LEAVE("retVal = void");
195 }
#define MCL_FUNCTION_LEAVE_LABEL
mcl_size_t id
Id of the item in the store.
Definition: store.h:41
Data source configuration module header file.
mcl_error_t event_validate(event_t *event)
Definition: event.c:158
mcl_error_t mcl_store_initialize(mcl_store_t **store)
Definition: store.c:30
Item type custom data.
Definition: item.h:27
Item type data source configuration.
Definition: item.h:28
MCL_OK
#define MCL_DEBUG(...)
void * item
Item to be added to the store.
Definition: store.h:39
MCL_CONNECTIVITY_EXPORT void mcl_custom_data_destroy(mcl_custom_data_t **custom_data)
Definition: custom_data.c:122
void mcl_store_destroy(mcl_store_t **store)
Definition: store.c:81
mcl_int32_t mcl_error_t
void store_item_destroy(store_item_t **store_item)
Definition: store.c:186
#define MCL_DEBUG_ENTRY(...)
static mcl_error_t _item_destroy(void **item)
Definition: store.c:152
Store module header file.
mcl_error_t data_source_configuration_validate(data_source_configuration_t *data_source_configuration)
void(* mcl_list_item_destroy_callback)(void **item)
File module header file.
Data source configuration module interface header file.
#define MCL_ASSERT_CODE_MESSAGE(condition, return_code,...)
mcl_error_t timeseries_validate(timeseries_t *timeseries)
Definition: timeseries.c:109
Item type timeseries.
Definition: item.h:24
#define MCL_NEW(p)
#define MCL_NULL
struct mcl_store_t mcl_store_t
Definition: mcl_store.h:33
Timeseries module header file.
Event module interface header file.
MCL_CONNECTIVITY_EXPORT void mcl_timeseries_destroy(mcl_timeseries_t **timeseries)
Definition: timeseries.c:138
Definition: file.h:31
Custom data module interface header file.
MCL_CORE_EXPORT mcl_error_t mcl_list_add(mcl_list_t *list, void *data)
#define MCL_FREE(p)
MCL_CORE_EXPORT void mcl_list_destroy_with_content(mcl_list_t **list, mcl_list_item_destroy_callback callback)
Timeseries module interface header file.
#define MCL_ASSERT_STATEMENT_CODE_MESSAGE(condition, statement, return_code,...)
#define MCL_ASSERT_NOT_NULL(argument, return_variable)
static mcl_error_t _store_add_data(mcl_store_t *store, void *item)
Definition: store.c:99
This item is not processed before.
Definition: store.h:20
E_MCL_STORE_ITEM_STATUS status
Status of item in the store.
Definition: store.h:40
Item type store.
Definition: item.h:29
Item type event.
Definition: item.h:25
#define MCL_ITEM_PREAMBLE
Definition: item.h:17
Event module header file.
Custom data module header file.
mcl_error_t file_validate(file_t *file)
Definition: file.c:121
Store module interface header file.
mcl_error_t custom_data_validate(custom_data_t *custom_data)
Definition: custom_data.c:104
MCL_OUT_OF_MEMORY
MCL_CONNECTIVITY_EXPORT void mcl_data_source_configuration_destroy(mcl_data_source_configuration_t **data_source_configuration)
Item type file.
Definition: item.h:26
MCL_CORE_EXPORT mcl_error_t mcl_list_initialize(mcl_list_t **list)
MCL_CONNECTIVITY_EXPORT void mcl_file_destroy(mcl_file_t **file)
Definition: file.c:148
Definition: event.h:36
MCL_INVALID_PARAMETER
MCL_CONNECTIVITY_EXPORT void mcl_event_destroy(mcl_event_t **event)
Definition: event.c:186
mcl_error_t mcl_store_add(mcl_store_t *store, void *item)
Definition: store.c:58
File module interface header file.
#define MCL_DEBUG_LEAVE(...)