2323#include < plugins/pluginStructs.h>
2424#include < plugins/udaPlugin.h>
2525#include < structures/struct.h>
26+ #include < clientserver/compressDim.h>
2627
2728#include " map_types/data_source_mapping.hpp"
2829#include " map_types/map_arguments.hpp"
@@ -89,79 +90,38 @@ int json_plugin::UDADataSource::call_plugins(DATA_BLOCK* data_block, const libto
8990 return err;
9091 } // Return 1 if no request receieved
9192
92- /*
93- *
94- * generate subset info then remove subset syntax from
95- * request string
96- *
97- */
98-
9993 REQUEST_DATA request = {0 };
10094 strcpy (request.signal , request_str.c_str ());
10195
10296 ENVIRONMENT * environment = getIdamClientEnvironment ();
10397 makeRequestData (&request, *m_plugin_list, environment);
10498
105- // if (m_cache_enabled) {
106- // std::string key_found = ram_cache->has_entry(request_str) ? "True" : "False";
107- // ram_cache->log(libtokamap:LogLevel::DEBUG, "key, \"" + request_str + "\" in cache? " + key_found);
108- // }
109-
110- /*
111- *
112- * CACHING GOES HERE
113- *
114- */
115-
116- // check cache for request string and only get data if it's not already there
117- // currently copies whole datablock (data, error, and dims)
118- // if (m_cache_enabled) {
119- // ram_cache->log(libtokamap::LogLevel::DEBUG, "caching disabled");
120- // }
121-
122- bool cache_hit = false ;
123- if (m_cache_enabled && ram_cache != nullptr ) {
124- cache_hit = json_plugin::copy_from_cache (*ram_cache, request_str, data_block);
125- }
126- if (cache_hit) {
127- // ram_cache->log(libtokamap:LogLevel::INFO, "Adding cached datablock onto plugin_interface");
128- // ram_cache->log(libtokamap:LogLevel::INFO,
129- // "data on plugin_interface (data_n): " + std::to_string(data_block->data_n));
130- err = 0 ;
131- } else {
132- IDAM_PLUGIN_INTERFACE interface = {0 };
133- CLIENT_BLOCK client_block;
134- DATA_SOURCE data_source;
135- SIGNAL_DESC signal_desc;
136- initClientBlock (&client_block, 0 , " " );
137- initDataSource (&data_source);
138- initSignalDesc (&signal_desc);
139-
140- interface.request_data = &request;
141- interface.pluginList = m_plugin_list;
142- interface.data_block = data_block;
143- interface.environment = environment;
144- interface.client_block = &client_block;
145- interface.data_source = &data_source;
146- interface.signal_desc = &signal_desc;
147-
148- err = callPlugin (m_plugin_list, request_str.c_str (), &interface);
149-
150- if (err != 0 ) {
151- // add check of int udaNumErrors() and if more than one, don't wipe
152- // 220 situation when UDA tries to get data and cannot find it
153- if (err == 220 ) {
154- closeUdaError ();
155- }
156- return err;
157- } // return code if failure, no need to proceed
99+ IDAM_PLUGIN_INTERFACE interface = {0 };
100+ CLIENT_BLOCK client_block;
101+ DATA_SOURCE data_source;
102+ SIGNAL_DESC signal_desc;
103+ initClientBlock (&client_block, 0 , " " );
104+ initDataSource (&data_source);
105+ initSignalDesc (&signal_desc);
106+
107+ interface.request_data = &request;
108+ interface.pluginList = m_plugin_list;
109+ interface.data_block = data_block;
110+ interface.environment = environment;
111+ interface.client_block = &client_block;
112+ interface.data_source = &data_source;
113+ interface.signal_desc = &signal_desc;
158114
159- // Add retrieved datablock to cache. data is copied from datablock into a new libtokamap:data_entry. original
160- // data remains on block (on plugin_interface structure) for return.
161- if (m_cache_enabled && ram_cache != nullptr ) {
162- json_plugin::copy_to_cache (*ram_cache, request_str, data_block);
115+ err = callPlugin (m_plugin_list, request_str.c_str (), &interface);
116+
117+ if (err != 0 ) {
118+ // add check of int udaNumErrors() and if more than one, don't wipe
119+ // 220 situation when UDA tries to get data and cannot find it
120+ if (err == 220 ) {
121+ closeUdaError ();
163122 }
164- }
123+ return err;
124+ } // return code if failure, no need to proceed
165125
166126 return err;
167127}
@@ -174,10 +134,106 @@ libtokamap::TypedDataArray set_return_data(DataBlock& data_block, size_t size, s
174134 auto array = libtokamap::TypedDataArray{reinterpret_cast <T*>(data_block.data ), size, std::move (shape), false };
175135 // we set the data_block.data to nullptr to avoid double deletion
176136 data_block.data = nullptr ;
137+ freeDataBlock (&data_block);
177138 return array;
178139}
140+
141+ void expand_compressed_dim (DIMS & dim)
142+ {
143+ if (dim.compressed == 0 )
144+ {
145+ return ;
146+ }
147+ uncompressDim (&dim);
148+ dim.compressed = 0 ;
149+ dim.method = 0 ;
150+ if (dim.sams != nullptr ) {
151+ free (dim.sams );
152+ dim.sams = nullptr ;
153+ }
154+ if (dim.offs != nullptr ) {
155+ free (dim.offs );
156+ dim.offs = nullptr ;
157+ }
158+ if (dim.ints != nullptr ) {
159+ free (dim.ints );
160+ dim.ints = nullptr ;
161+ }
162+ dim.udoms = 0 ;
163+ }
164+
165+ void free_all_dims (DATA_BLOCK & data_block)
166+ {
167+ if (data_block.dims == nullptr ) {
168+ return ;
169+ }
170+
171+ for (unsigned int i = 0 ; i<data_block.rank ; i++)
172+ {
173+ auto dim = data_block.dims [i];
174+ if (dim.dim != nullptr ) free (dim.dim );
175+ if (dim.errhi != nullptr ) free (dim.errhi );
176+ if (dim.errlo != nullptr ) free (dim.errlo );
177+ if (dim.sams != nullptr ) free (dim.sams );
178+ if (dim.offs != nullptr ) free (dim.offs );
179+ if (dim.ints != nullptr ) free (dim.ints );
180+ }
181+ free (data_block.dims );
182+ data_block.dims = nullptr ;
183+ data_block.rank = 0 ;
184+ data_block.order = -1 ;
185+ }
186+
187+ void replace_data_with_dim (DATA_BLOCK & data_block, size_t index)
188+ {
189+ if (data_block.rank == 0 ){
190+ throw std::runtime_error{" Dims requested for data of rank 0. No dimension data exists" };
191+ }
192+ if (index >= data_block.rank ){
193+ throw std::runtime_error{" dimension index requested is out-of-bounds" };
194+ }
195+ if (data_block.dims == nullptr or data_block.dims [index].dim == nullptr ){
196+ throw std::runtime_error{" No dimension data exists for index requested" };
197+ }
198+
199+ // just free the previous data for now
200+ // can alter behaviour if we need to add caching later
201+ if (data_block.data != nullptr ){
202+ free (data_block.data );
203+ data_block.data = nullptr ;
204+ }
205+
206+ // copy dim data onto data_block
207+ auto dim = data_block.dims [index];
208+ expand_compressed_dim (dim);
209+ data_block.data = dim.dim ;
210+ dim.dim = nullptr ;
211+ data_block.data_n = dim.dim_n ;
212+ data_block.data_type = dim.data_type ;
213+
214+ // avoid any confusion during later cleaup
215+ free_all_dims (data_block);
216+
217+ // "get" function just returns the data array to the calling scope
218+ // no need to add compressed dims and set rank to 1 for normal return
219+ // of this data_block.
220+ }
221+
222+ void replace_data_with_time (DATA_BLOCK & data_block)
223+ {
224+ if (data_block.order < 0 ){
225+ throw std::runtime_error{" No time data exists on datablack where requested" };
226+ }
227+ if (data_block.order >= data_block.rank ){
228+ throw std::runtime_error{" corrupt datablock. time index is out-of-bounds" };
229+ }
230+
231+ replace_data_with_dim (data_block, data_block.order );
232+ }
233+
179234} // namespace
180235
236+
181237libtokamap::TypedDataArray json_plugin::UDADataSource::get (const libtokamap::DataSourceArgs& data_source_args,
182238 const libtokamap::MapArguments& arguments,
183239 libtokamap::RamCache* ram_cache)
@@ -189,9 +245,8 @@ libtokamap::TypedDataArray json_plugin::UDADataSource::get(const libtokamap::Dat
189245 return {};
190246 }
191247
192- // temporary solution to the slice functionality returning arrays of 1 element
193- if (data_block.rank == 1 && data_block.data_n == 1 ) {
194- data_block.rank = 0 ;
248+ if (data_source_args.count (" time" ) != 0 && data_source_args.at (" time" ).get <bool >()){
249+ replace_data_with_time (data_block);
195250 }
196251
197252 size_t size = data_block.data_n ;
@@ -200,6 +255,7 @@ libtokamap::TypedDataArray json_plugin::UDADataSource::get(const libtokamap::Dat
200255 shape[i] = data_block.dims [i].dim_n ;
201256 }
202257
258+ // note set_return_data destroys the data_block after moving the data out of it
203259 switch (data_block.data_type ) {
204260 case UDA_TYPE_INT :
205261 return set_return_data<int >(data_block, size, std::move (shape));
0 commit comments