1 /* ------------------------------------------------
2 Copyright 2014 AT&T Intellectual Property
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
7 http://www.apache.org/licenses/LICENSE-2.0
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ------------------------------------------- */
16 #include"schemaparser_impl.h"
17 #include"schemaparser.h"
19 #include "parse_fta.h"
20 #include "parse_schema.h"
21 #include"generate_utils.h"
23 #include"host_tuple.h"
31 // Interface to FTA definition lexer and parser ...
33 extern int FtaParserparse(void);
34 extern FILE *FtaParserin;
35 extern int FtaParserdebug;
37 // This will need to be moved to a parse_fta.cc file.
38 fta_parse_t *fta_parse_result;
39 var_defs_t *fta_parse_defines;
46 static gs_int8_t tmpstr[20000]; // for returning const char* values.
48 vector<query_rep *> schema_list; // The schemas parsed thus far.
50 /////////////////////////////////////////////////////////////
53 static gs_int32_t curr_version = 4;
54 static gs_int32_t accepted_versions[] = { 4, 3, 2, 1, 0 }; // must end with zero.
56 gs_int32_t get_schemaparser_version(){ return curr_version; }
58 gs_int32_t *get_schemaparser_accepted_versions(){return accepted_versions;}
60 gs_int32_t schemaparser_accepts_version(gs_int32_t v){
62 for(i=0;accepted_versions[i]>0;++i){
63 if(accepted_versions[i] == v) return 1;
69 /////////////////////////////////////////////////////////////////////
70 //////////// Utility Functions
73 int fta_field_size(int dt, int *is_udef){
80 return(sizeof(unsigned int));
82 return(sizeof(unsigned long long int));
84 return(sizeof(long long int));
86 return(sizeof(double));
88 return(sizeof(timeval));
90 return(sizeof(vstring));
100 /////////////////////////////////////////////////////////////
104 //////////// Schema management
106 static gs_schemahandle_t ftaschema_parse(int prot_ok);
108 gs_schemahandle_t ftaschema_parse_string(gs_csp_t f){
109 // prot_ok is by default false in schemaparser.h
111 fta_parse_result = new fta_parse_t();
112 gs_sp_t schema = strdup(f);
115 FtaParser_setstringinput(schema);
117 if(FtaParserparse()){
118 fprintf(stderr,"FTA parse failed.\n");
124 return ftaschema_parse(false);
126 gs_schemahandle_t ftaschema_parse_string_prot(gs_csp_t f){
127 // prot_ok is by default false in schemaparser.h
129 fta_parse_result = new fta_parse_t();
130 gs_sp_t schema = strdup(f);
133 FtaParser_setstringinput(schema);
135 if(FtaParserparse()){
136 fprintf(stderr,"FTA parse failed.\n");
142 return ftaschema_parse(true);
145 gs_schemahandle_t ftaschema_parse_file(FILE *f){
146 // prot_ok is by default false in schemaparser.h
148 fta_parse_result = new fta_parse_t();
151 FtaParser_setfileinput(f);
153 if(FtaParserparse()){
154 fprintf(stderr,"FTA parse failed.\n");
158 return ftaschema_parse(false);
160 gs_schemahandle_t ftaschema_parse_file_prot(FILE *f){
161 // prot_ok is by default false in schemaparser.h
163 fta_parse_result = new fta_parse_t();
166 FtaParser_setfileinput(f);
168 if(FtaParserparse()){
169 fprintf(stderr,"FTA parse failed.\n");
173 return ftaschema_parse(true);
180 static gs_schemahandle_t ftaschema_parse(int prot_ok){
182 if(fta_parse_result->parse_type != STREAM_PARSE){
183 if(!(fta_parse_result->parse_type == TABLE_PARSE && prot_ok!=0)){
184 fprintf(stderr,"ERROR, input is not a stream file.\n");
190 // Get the tuple information.
191 if(fta_parse_result->tables->size() != 1){
192 fprintf(stderr,"ERROR parsing schema file: %d tables, expecting 1.\n",
193 fta_parse_result->tables->size() );
197 string table_name = fta_parse_result->tables->get_table_name(0);
198 vector<field_entry *> tuple_flds = fta_parse_result->tables->get_fields(table_name);
199 int n_tuples = tuple_flds.size();
201 // get the parameter info.
203 string query_name = table_name;
204 vector<var_pair_t *> query_params;
205 if(fta_parse_result->fta_parse_tree){
206 query_params = fta_parse_result->fta_parse_tree->query_params;
207 n_params = query_params.size();
209 // Get the query name
210 if(fta_parse_result->fta_parse_tree->nmap.count("query_name") == 0){
211 fprintf(stderr,"WARNING: query name is empty. using default_query.\n");
212 query_name = "default_query";
214 query_name = fta_parse_result->fta_parse_tree->nmap["query_name"];
217 if(query_name != table_name){
218 fprintf(stderr,"WARNING table name (%s) is different than query name (%s).\n",
219 table_name.c_str(), query_name.c_str() );
224 // Construct the query representation.
226 query_rep *qrep = new query_rep(query_name, n_tuples, n_params);
228 // Pack the tuple information.
230 for(fi=0;fi<n_tuples;++fi){
231 if((qrep->set_field_info(fi,tuple_flds[fi])) == UNDEFINED_TYPE){
232 fprintf(stderr,"ERROR tuple field %s (number %d) has undefined type %s.\n",
233 tuple_flds[fi]->get_name().c_str(), fi, tuple_flds[fi]->get_type().c_str());
236 if(qrep->finalize_field_info()){
237 fprintf(stderr,"ERROR undefined type in tuple.\n");
241 // Pack the param info
243 for(pi=0;pi<n_params;++pi){
244 if((qrep->set_param_info(pi, query_params[pi])) == UNDEFINED_TYPE){
245 fprintf(stderr,"ERROR parameter %s (number %d) has undefined type %s.\n",
246 query_params[pi]->name.c_str(), pi, query_params[pi]->val.c_str());
249 if(qrep->finalize_param_info()){
250 fprintf(stderr,"ERROR undefined type in parameter block.\n");
256 schema_list.push_back(qrep);
258 return(schema_list.size()-1);
262 /* Release memory used by the schema representation.
263 return non-zero on error.
265 // Currently, do nothing. I'll need to
266 // systematically plug memory leaks and write destructors
267 // to make implementing this function worth while.
268 gs_int32_t ftaschema_free(gs_schemahandle_t sh){
273 /* name of fta schema null terminated */
274 /* Returns NULL if sh is out of bounds. */
275 /* NO ALLOCATION IS PERFORMED! Must treat result as const. */
276 gs_sp_t ftaschema_name(gs_schemahandle_t sh){
277 if(sh < 0 || sh >= schema_list.size()){
280 strcpy(tmpstr,schema_list[sh]->name.c_str());
285 ///////////// Tuple management
288 /* number of entries in a tuple */
289 /* Return -1 if the schema handle is out of range. */
290 gs_int32_t ftaschema_tuple_len(gs_schemahandle_t sh){
291 if(sh < 0 || sh >= schema_list.size()){
294 return(schema_list[sh]->field_info.size());
297 /* field entry name */
298 /* Returns NULL if sh or index is out of bounds. */
299 /* NO ALLOCATION IS PERFORMED! Must treat result as const. */
300 gs_sp_t ftaschema_field_name(gs_schemahandle_t sh, gs_uint32_t index){
301 if(sh < 0 || sh >= schema_list.size()){
304 if(index >= schema_list[sh]->field_info.size()){
307 strcpy(tmpstr,schema_list[sh]->field_info[index].field_name.c_str());
311 /* Direct tuple access functions.
313 gs_uint32_t fta_unpack_uint(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
315 if(offset+sizeof(gs_uint32_t) > len){
319 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint32_t));
320 // return(htonl(retval));
323 gs_uint32_t fta_unpack_uint_nocheck(void *data, gs_uint32_t offset){
325 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint32_t));
329 gs_uint32_t fta_unpack_ushort(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
331 if(offset+sizeof(gs_uint32_t) > len){
335 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint32_t));
336 // return(htonl(retval));
339 gs_uint32_t fta_unpack_ushort_nocheck(void *data, gs_uint32_t offset){
341 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint32_t));
345 gs_uint32_t fta_unpack_bool(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
347 if(offset+sizeof(gs_uint32_t) > len){
351 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint32_t));
352 // return(htonl(retval));
355 gs_uint32_t fta_unpack_bool_nocheck(void *data, gs_uint32_t offset){
357 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint32_t));
358 // return(htonl(retval));
362 gs_int32_t fta_unpack_int(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
364 if(offset+sizeof(gs_int32_t) > len){
368 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_int32_t));
369 // return(htonl(retval));
372 gs_int32_t fta_unpack_int_nocheck(void *data, gs_uint32_t offset){
374 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_int32_t));
378 gs_uint64_t fta_unpack_ullong(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
380 if(offset+sizeof(gs_uint64_t) > len){
384 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint64_t));
385 // return(htonll(retval));
388 gs_uint64_t fta_unpack_ullong_nocheck(void *data, gs_uint32_t offset){
390 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint64_t));
394 gs_int64_t fta_unpack_llong(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
396 if(offset+sizeof( gs_int64_t) > len){
400 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_int64_t));
401 // return(htonl(retval));
404 gs_int64_t fta_unpack_llong_nocheck(void *data, gs_uint32_t offset){
406 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_int64_t));
410 gs_float_t fta_unpack_float(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
412 if(offset+sizeof( gs_float_t) > len){
416 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_float_t));
417 // return(ntohf(retval));
420 gs_float_t fta_unpack_float_nocheck(void *data, gs_uint32_t offset){
422 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_float_t));
426 timeval fta_unpack_timeval(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
428 if(offset+sizeof( timeval) > len){
432 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(timeval));
433 // retval.tv_sec = htonl(retval.tv_sec);
434 // retval.tv_usec = htonl(retval.tv_usec);
435 retval.tv_sec = retval.tv_sec;
436 retval.tv_usec = retval.tv_usec;
439 timeval fta_unpack_timeval_nocheck(void *data, gs_uint32_t offset){
441 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(timeval));
442 retval.tv_sec = retval.tv_sec;
443 retval.tv_usec = retval.tv_usec;
447 vstring fta_unpack_vstr(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
451 if(offset+sizeof( vstring32) > len){
456 memcpy(&unpack_s, ((gs_sp_t)data)+offset, sizeof(vstring32));
458 // retval.length = htonl(unpack_s.length);
459 // unpack_s.offset = htonl(unpack_s.offset);
460 retval.length = unpack_s.length;
461 unpack_s.offset = unpack_s.offset;
462 retval.reserved = SHALLOW_COPY;
464 if(unpack_s.offset + retval.length > len){
468 retval.offset = (gs_p_t)data + unpack_s.offset;
472 gs_sp_t fta_unpack_fstring(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
473 return( (gs_sp_t)(data)+offset);
476 struct hfta_ipv6_str fta_unpack_ipv6(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
477 struct hfta_ipv6_str retval;
478 if(offset+sizeof(hfta_ipv6_str) > len){
482 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(hfta_ipv6_str));
483 // htonl(retval.v[0]);
484 // htonl(retval.v[1]);
485 // htonl(retval.v[2]);
486 // htonl(retval.v[3]);
489 struct hfta_ipv6_str fta_unpack_ipv6_nocheck(void *data, gs_uint32_t offset){
490 struct hfta_ipv6_str retval;
491 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(hfta_ipv6_str));
497 Direct tuple access functions, but no ntoh xform.
499 gs_uint32_t fta_unpack_uint_noxf(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
501 if(offset+sizeof(gs_uint32_t) > len){
505 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint32_t));
508 gs_uint32_t fta_unpack_uint_noxf_nocheck(void *data, gs_uint32_t offset){
510 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint32_t));
514 gs_uint32_t fta_unpack_ushort_noxf(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
516 if(offset+sizeof(gs_uint32_t) > len){
520 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint32_t));
523 gs_uint32_t fta_unpack_ushort_noxf_nocheck(void *data, gs_uint32_t offset){
525 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint32_t));
529 gs_uint32_t fta_unpack_bool_noxf(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
531 if(offset+sizeof(gs_uint32_t) > len){
535 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint32_t));
538 gs_uint32_t fta_unpack_bool_noxf_nocheck(void *data, gs_uint32_t offset){
540 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint32_t));
544 gs_int32_t fta_unpack_int_noxf(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
546 if(offset+sizeof(gs_int32_t) > len){
550 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_int32_t));
553 gs_int32_t fta_unpack_int_noxf_nocheck(void *data, gs_uint32_t offset){
555 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_int32_t));
559 gs_uint64_t fta_unpack_ullong_noxf(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
561 if(offset+sizeof( gs_uint64_t) > len){
565 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint64_t));
568 gs_uint64_t fta_unpack_ullong_noxf_nocheck(void *data, gs_uint32_t offset){
570 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_uint64_t));
574 gs_int64_t fta_unpack_llong_noxf(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
576 if(offset+sizeof( gs_int64_t) > len){
580 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_int64_t));
583 gs_int64_t fta_unpack_llong_noxf_nocheck(void *data, gs_uint32_t offset){
585 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_int64_t));
589 gs_float_t fta_unpack_float_noxf(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
591 if(offset+sizeof( gs_float_t) > len){
595 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_float_t));
598 gs_float_t fta_unpack_float_noxf_nocheck(void *data, gs_uint32_t offset){
600 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(gs_float_t));
604 timeval fta_unpack_timeval_noxf_nocheck(void *data, gs_uint32_t offset){
606 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(timeval));
610 vstring fta_unpack_vstr_noxf(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
614 if(offset+sizeof( vstring32) > len){
619 memcpy(&unpack_s, ((gs_sp_t)data)+offset, sizeof(vstring32));
621 retval.length = unpack_s.length;
622 retval.reserved = SHALLOW_COPY;
624 if(unpack_s.offset + unpack_s.length > len){
628 retval.offset = (gs_p_t)data + unpack_s.offset;
632 gs_sp_t fta_unpack_fstring_noxf(void *data, gs_int32_t len, gs_uint32_t offset, gs_int32_t *problem){
633 return( (gs_sp_t)(data)+offset);
636 struct hfta_ipv6_str fta_unpack_ipv6_noxf_nocheck(void *data, gs_uint32_t offset){
637 struct hfta_ipv6_str retval;
638 memcpy(&retval, ((gs_sp_t)data)+offset, sizeof(hfta_ipv6_str));
643 /* returns fields offset by name */
644 /* if sh is out of bounds, or if fieldname is not the name of a field,
648 gs_int32_t ftaschema_get_field_offset_by_name(gs_schemahandle_t sh, gs_csp_t fieldname){
649 if(sh < 0 || sh >= schema_list.size()){
654 for(f=0;f<schema_list[sh]->field_info.size();++f){
655 if(strcmp(fieldname, schema_list[sh]->field_info[f].field_name.c_str()) == 0)
656 return(schema_list[sh]->field_info[f].offset);
663 /* field offset by index, return -1 if sh or idx is out-of-bounds */
664 gs_int32_t ftaschema_get_field_offset_by_index(gs_schemahandle_t sh, gs_int32_t idx){
665 if(sh < 0 || sh >= schema_list.size()){
668 if(idx < 0 || idx >= schema_list[sh]->field_info.size()){
672 return(schema_list[sh]->field_info[idx].offset);
677 /* returns fields type by name */
678 /* if sh is out of bounds, or if fieldname is not the name of a field,
680 return value is UNDEFINED_TYPE
682 gs_int32_t ftaschema_get_field_type_by_name(gs_schemahandle_t sh, gs_csp_t fieldname){
683 if(sh < 0 || sh >= schema_list.size()){
684 return(UNDEFINED_TYPE);
688 for(f=0;f<schema_list[sh]->field_info.size();++f){
689 if(strcmp(fieldname, schema_list[sh]->field_info[f].field_name.c_str()) == 0)
690 return(schema_list[sh]->field_info[f].pdt->type_indicator());
694 return( UNDEFINED_TYPE);
696 /* field type by index, rteturn value is UNDEFINED_TYPE if sh or idx
699 gs_int32_t ftaschema_get_field_type_by_index(gs_schemahandle_t sh, gs_int32_t idx){
700 if(sh < 0 || sh >= schema_list.size()){
701 return(UNDEFINED_TYPE);
703 if(idx < 0 || idx >= schema_list[sh]->field_info.size()){
704 return(UNDEFINED_TYPE);
707 return(schema_list[sh]->field_info[idx].pdt->type_indicator());
711 /* returns tuple value based on name */
712 /* if sh is out of bounds, or if fieldname is not the name of a field,
714 return value is of type UNDEFINED_TYPE
716 access_result ftaschema_get_field_by_name(gs_schemahandle_t sh, gs_csp_t fieldname,
717 void * data, gs_uint32_t len){
718 access_result retval;
719 retval.field_data_type = UNDEFINED_TYPE;
721 if(sh < 0 || sh >= schema_list.size()){
726 for(f=0;f<schema_list[sh]->field_info.size();++f){
727 if(strcmp(fieldname, schema_list[sh]->field_info[f].field_name.c_str()) == 0)
728 return(ftaschema_get_field_by_index(sh,f,data,len));
736 /* return tuple value by index */
737 /* if sh is out of bounds, or if fieldname is not the name of a field,
739 return value is of type UNDEFINED_TYPE
741 access_result ftaschema_get_field_by_index(gs_schemahandle_t sh, gs_uint32_t index,
742 void * data, gs_uint32_t len){
743 access_result retval;
744 retval.field_data_type = UNDEFINED_TYPE;
745 gs_int32_t problem = 0;
747 if(sh < 0 || sh >= schema_list.size()){
750 if(index >= schema_list[sh]->field_info.size()){
754 switch(schema_list[sh]->field_info[index].pdt->get_type()){
756 retval.r.ui = fta_unpack_uint(data, len,
757 schema_list[sh]->field_info[index].offset, &problem);
758 if(!problem) retval.field_data_type = UINT_TYPE;
761 retval.r.ui = fta_unpack_uint(data, len,
762 schema_list[sh]->field_info[index].offset, &problem);
763 if(!problem) retval.field_data_type = IP_TYPE;
766 retval.r.i = fta_unpack_int(data, len,
767 schema_list[sh]->field_info[index].offset, &problem);
768 if(!problem) retval.field_data_type = INT_TYPE;
771 retval.r.ul = fta_unpack_ullong(data, len,
772 schema_list[sh]->field_info[index].offset, &problem);
773 if(!problem) retval.field_data_type = ULLONG_TYPE;
776 retval.r.l = fta_unpack_llong(data, len,
777 schema_list[sh]->field_info[index].offset, &problem);
778 if(!problem) retval.field_data_type = LLONG_TYPE;
781 retval.r.ui = fta_unpack_ushort(data, len,
782 schema_list[sh]->field_info[index].offset, &problem);
783 if(!problem) retval.field_data_type = USHORT_TYPE;
786 retval.r.f = fta_unpack_float(data, len,
787 schema_list[sh]->field_info[index].offset, &problem);
788 if(!problem) retval.field_data_type = FLOAT_TYPE;
791 retval.r.ui = fta_unpack_bool(data, len,
792 schema_list[sh]->field_info[index].offset, &problem);
793 if(!problem) retval.field_data_type = BOOL_TYPE;
796 retval.r.vs = fta_unpack_vstr(data, len,
797 schema_list[sh]->field_info[index].offset, &problem);
798 if(!problem) retval.field_data_type = VSTR_TYPE;
801 retval.r.fs.data = fta_unpack_fstring(data, len,
802 schema_list[sh]->field_info[index].offset, &problem);
803 retval.r.fs.size = schema_list[sh]->field_info[index].pdt->type_size();
804 if(!problem) retval.field_data_type = FSTRING_TYPE;
806 retval.r.t = fta_unpack_timeval(data, len,
807 schema_list[sh]->field_info[index].offset, &problem);
808 if(!problem) retval.field_data_type = TIMEVAL_TYPE;
811 retval.r.ip6 = fta_unpack_ipv6(data, len,
812 schema_list[sh]->field_info[index].offset, &problem);
813 if(!problem) retval.field_data_type = IPV6_TYPE;
822 // Get location of eof, temporal-tuple metadata.
823 gs_int32_t ftaschema_get_tuple_metadata_offset(gs_schemahandle_t sh){
824 if(sh < 0 || sh >= schema_list.size())
825 return 0; // probably need to return a error instead of just telling its not eof tuple
827 return ( schema_list[sh]->min_tuple_size);
831 /* checks whether tuple is temporal
832 return value 1 indicates that tuple istemporal, 0 - not temporal
834 gs_int32_t ftaschema_is_temporal_tuple(gs_schemahandle_t sh, void *data) {
836 if(sh < 0 || sh >= schema_list.size())
837 return 0; // probably need to return a error instead of just telling its not temporal
839 return (*((gs_sp_t)data + schema_list[sh]->min_tuple_size) == TEMPORAL_TUPLE);
841 //inline gs_int32_t ftaschema_is_temporal_tuple_offset(int metadata_offset, void *data) {
842 // return (*((gs_sp_t)data + metadata_offset) == TEMPORAL_TUPLE);
847 /* checks whether tuple is special end-of_file tuple
848 return value 1 indicates that tuple is eof_tuple, 0 - otherwise
850 gs_int32_t ftaschema_is_eof_tuple(gs_schemahandle_t sh, void *data) {
851 if(sh < 0 || sh >= schema_list.size())
852 return 0; // probably need to return a error instead of just telling its not eof tuple
854 return (*((gs_sp_t)data + schema_list[sh]->min_tuple_size) == EOF_TUPLE);
857 inline gs_int32_t ftaschema_is_eof_tuple_offset(int metadata_offset, void *data) {
858 return (*((gs_sp_t)data + metadata_offset) == EOF_TUPLE);
862 /* extracts the trace from the temporal tuple
863 return value 0 indicates success, non-zero - error
865 gs_int32_t ftaschema_get_trace(gs_schemahandle_t sh, void* data, gs_int32_t len,
866 gs_uint64_t* trace_id, gs_uint32_t* sz, fta_stat** trace ) {
868 if(sh < 0 || sh >= schema_list.size() || *((gs_sp_t)data + schema_list[sh]->min_tuple_size) != TEMPORAL_TUPLE)
871 memcpy(trace_id, (gs_sp_t)data + schema_list[sh]->min_tuple_size + sizeof(gs_int8_t), sizeof(gs_uint64_t));
872 *trace = (fta_stat*)((gs_sp_t)data + schema_list[sh]->min_tuple_size + sizeof(gs_int8_t) + sizeof(gs_uint64_t));
873 *sz = (len - schema_list[sh]->min_tuple_size - sizeof(gs_int8_t)- sizeof(gs_uint64_t)) / sizeof(fta_stat);
879 //////////// Param block management
881 /* number of parameters */
882 /* Return -1 if sh is out of bounds */
883 gs_int32_t ftaschema_parameter_len(gs_schemahandle_t sh){
884 if(sh < 0 || sh >= schema_list.size()){
887 return(schema_list[sh]->param_info.size());
890 /* parameter entry name */
891 /* Return NULL if sh or index is out of bounds. */
892 /* NO COPYING IS PERFORMED */
893 gs_sp_t ftaschema_parameter_name(gs_schemahandle_t sh, gs_uint32_t index){
894 if(sh < 0 || sh >= schema_list.size()){
897 if(index >= schema_list[sh]->param_info.size()){
900 strcpy(tmpstr,schema_list[sh]->param_info[index].param_name.c_str());
904 /* set parameter value for parameter handle */
905 /* Pass in the parameter in its char string representation. */
906 /* Return value is -1 on error, else 0 */
907 gs_int32_t ftaschema_setparam_by_name(gs_schemahandle_t sh, gs_sp_t param_name,
908 gs_sp_t param_val, gs_int32_t len){
909 if(sh < 0 || sh >= schema_list.size()){
914 for(f=0;f<schema_list[sh]->param_info.size();++f){
915 if(strcmp(param_name, schema_list[sh]->param_info[f].param_name.c_str()) == 0)
916 return(ftaschema_setparam_by_index(sh,f,param_val,len));
923 /* set parameter value for parameter handle */
924 /* Pass in the parameter in its char string representation. */
925 /* Return value is -1 on error, else 0 */
926 gs_int32_t ftaschema_setparam_by_index(gs_schemahandle_t sh, gs_int32_t index,
927 gs_sp_t param_val, gs_int32_t len){
930 unsigned int ui,d1,d2,d3,d4;
932 unsigned long long int ulli;
937 if(sh < 0 || sh >= schema_list.size()){
940 if(index >= schema_list[sh]->param_info.size()){
944 param_block_info *pb = &(schema_list[sh]->param_info[index]);
945 switch(pb->pdt->get_type()){
947 if(sscanf(param_val,"%d",&i) == 1){
949 pb->value_set = true;
957 if(sscanf(param_val,"%u",&ui) == 1){
959 pb->value_set = true;
965 if(sscanf(param_val,"%d.%d.%d.%d",&d1,&d2,&d3,&d4) == 4){
966 pb->value.r.ui = (d1 << 24)+(d2 << 16)+(d3 << 8)+d4;
967 pb->value_set = true;
972 if(sscanf(param_val,"%llu",&ulli) == 1){
974 pb->value_set = true;
980 if(sscanf(param_val,"%lld",&lli) == 1){
982 pb->value_set = true;
988 if(sscanf(param_val,"%lf",&f) == 1){
990 pb->value_set = true;
996 if(sscanf(param_val,"(%d,%d)",&d1, &d2) == 2){
997 pb->value.r.t.tv_sec = d1;
998 pb->value.r.t.tv_usec = d2;
999 pb->value_set = true;
1005 if(pb->value.r.vs.offset != 0){
1006 tmp_ptr = (void *)(pb->value.r.vs.offset);
1009 tmp_ptr = malloc(len);
1010 pb->value.r.vs.offset = (gs_p_t)tmp_ptr;
1011 memcpy(tmp_ptr,param_val, len);
1012 pb->value.r.vs.length = len;
1013 // pb->value.r.vs.reserved = 0;
1014 pb->value.r.vs.reserved = INTERNAL;
1015 pb->value_set = true;
1019 fprintf(stderr,"ERROR, fstring parameters not supported, use vstring.\n");
1023 fprintf(stderr,"ERROR, ipv6_t parameters not supported.\n");
1027 fprintf(stderr,"INTERNAL ERROR undefined_t type in ftaschema_setparam_by_index\n");
1030 fprintf(stderr,"INTERNAL ERROR unknown type in ftaschema_setparam_by_index\n");
1037 gs_int32_t ftaschema_create_param_block(gs_schemahandle_t sh, void ** block, gs_int32_t * size){
1038 if(sh < 0 || sh >= schema_list.size()){
1042 *size = schema_list[sh]->min_param_size;
1045 for(p=0;p<schema_list[sh]->param_info.size();++p){
1046 if(! schema_list[sh]->param_info[p].value_set) return 1;
1048 switch(schema_list[sh]->param_info[p].pdt->get_type()){
1050 *size += schema_list[sh]->param_info[p].value.r.vs.length;
1053 fprintf(stderr,"ERROR, fstring parameters not supported, use vstring.\n");
1064 *block = malloc(*size);
1065 if(*block == NULL) return(-1);
1067 int data_pos = schema_list[sh]->min_param_size;
1070 gs_uint32_t tmp_uint;
1072 gs_uint64_t tmp_ull;
1078 for(p=0;p<schema_list[sh]->param_info.size();++p){
1079 param_block_info *pb = &(schema_list[sh]->param_info[p]);
1080 switch(pb->pdt->get_type()){
1082 // tmp_int = htonl(pb->value.r.i);
1083 tmp_int = pb->value.r.i;
1084 memcpy(((gs_sp_t)(*block))+pb->offset,&tmp_int,sizeof(gs_int32_t));
1090 // tmp_uint = htonl(pb->value.r.ui);
1091 tmp_uint = pb->value.r.ui;
1092 memcpy(((gs_sp_t)(*block))+pb->offset,&tmp_uint,sizeof(gs_uint32_t));
1095 // tmp_ull = htonll(pb->value.r.ul);
1096 tmp_ull = pb->value.r.ul;
1097 memcpy(((gs_sp_t)(*block))+pb->offset,&tmp_ull,sizeof(gs_uint64_t));
1100 // tmp_ll = htonll(pb->value.r.l);
1101 tmp_ll = pb->value.r.l;
1102 memcpy(((gs_sp_t)(*block))+pb->offset,&tmp_ll,sizeof(gs_int64_t));
1105 // tmp_f = ntohf(pb->value.r.f);
1106 tmp_f = pb->value.r.f;
1107 memcpy(((gs_sp_t)(*block))+pb->offset,&tmp_f,sizeof(gs_float_t));
1110 // tmp_tv.tv_sec = htonl(pb->value.r.t.tv_sec);
1111 // tmp_tv.tv_usec = htonl(pb->value.r.t.tv_usec);
1112 tmp_tv.tv_sec = pb->value.r.t.tv_sec;
1113 tmp_tv.tv_usec =pb->value.r.t.tv_usec;
1114 memcpy(((gs_sp_t)(*block))+pb->offset,&tmp_tv,sizeof(timeval));
1117 // tmp_vstr.offset = htonl(data_pos);
1118 // tmp_vstr.length = htonl(pb->value.r.vs.length);
1119 tmp_vstr.offset = data_pos;
1120 tmp_vstr.length = pb->value.r.vs.length;
1121 // tmp_vstr.reserved = htonl(pb->value.r.vs.reserved);
1122 // tmp_vstr.reserved = htonl(PACKED);
1123 tmp_vstr.reserved = PACKED;
1124 memcpy(((gs_sp_t)(*block))+pb->offset,&tmp_vstr,sizeof(vstring32));
1125 tmp_ptr = (void *)(pb->value.r.vs.offset);
1126 memcpy(((gs_sp_t)(*block))+data_pos, tmp_ptr, pb->value.r.vs.length);
1127 data_pos += pb->value.r.vs.length;
1130 fprintf(stderr,"ERROR, fstring parameters not supported, use vstring.\n");
1134 fprintf(stderr,"ERROR, ipv6_t parameters not supported.\n");
1138 fprintf(stderr,"INTERNAL ERROR undefined_t type in ftaschema_setparam_by_index\n");
1141 fprintf(stderr,"INTERNAL ERROR unknown type in ftaschema_create_param_block\n");
1150 ///////////////////////////////////////////////////////////////
1151 // Diagnostic functions
1153 void ftaschema_debugdump(gs_int32_t handle){
1155 printf("ftaschema_debugdump: handle is negative.\n");
1158 if(handle >= schema_list.size()){
1159 printf("ftaschema_debugdump: handle out of bounds (%d, max is %lu)\n",
1160 handle, schema_list.size());
1163 query_rep *q = schema_list[handle];
1165 printf("Handle is %d, query name is <%s>\n",handle,q->name.c_str());
1166 printf("Output tuple has %lu fields:\n",q->field_info.size());
1168 for(f=0;f<q->field_info.size();++f){
1169 printf("\t%s (%s) : %d\n",q->field_info[f].field_name.c_str(),
1170 (q->field_info[f].pdt->get_type_str()).c_str(),
1171 q->field_info[f].offset
1174 printf("Min tuple size is %d\n\n",q->min_tuple_size);
1175 printf("Param block is:\n");
1177 for(p=0;p<q->param_info.size();++p){
1178 printf("\t%s (%s) : %d\n",q->param_info[p].param_name.c_str(),
1179 (q->param_info[p].pdt->get_type_str()).c_str(),
1180 q->param_info[p].offset
1183 printf("Min param block size is %d\n\n",q->min_param_size);