321f00eed24281e191e10b6aad96bf772f3c8442
[ric-app/mc.git] / schemaparser / mc_extract.cc
1 #include<stdlib.h>
2 #include<stdio.h>
3 #include<string.h>
4 #include<string>
5 #include<iostream>
6 #include<fstream>
7
8 #include"schemaparser.h"
9
10 #include <sdl/syncstorage.hpp>
11
12 using namespace std;
13
14 //      data type definitions from sdl
15 using Namespace = std::string;
16 using Key = std::string;
17 using Data = std::vector<uint8_t>;
18 using DataMap = std::map<Key, Data>;
19 using Keys = std::set<Key>;
20
21 int main(int argc, char **argv){
22
23 //              MCNIB setup
24         Namespace ns("mcnib");  
25         std::unique_ptr<shareddatalayer::SyncStorage> sdl(shareddatalayer::SyncStorage::create());
26
27
28         if(argc< 2){
29                 fprintf(stderr,"Error, usage is %s schema [prefix] [directory]\n", argv[0]);
30                 exit(1);
31         }
32         string schema = argv[1];
33         string prefix("");
34         if(argc>2)
35                 prefix = argv[2];
36
37 //              Get the nib.json file
38         string nib_str;
39
40         if(argc>3){
41                 string directory = argv[3];
42         
43                 string inflnm = directory + "/" + string("nib.json");
44
45                 ifstream infl(inflnm);
46                 if(!infl){
47                         cerr << "Error, can't open " << inflnm << endl;
48                         exit(1);
49                 }
50                 string line;
51                 while(getline(infl, line)){
52                         nib_str += line;
53                 }
54                 infl.close();
55         }else{
56 //fprintf(stderr,"Fetching from sdl\n");
57                 Key k = "_schema";
58                 Keys K;
59                 K.insert(k);
60                 DataMap Dk = sdl->get(ns, K);
61                 if(Dk.count(k)>0){
62                         vector<uint8_t> val_v = Dk[k];
63                         char val[val_v.size()+1];                               // from Data
64                         int i;
65                         for(i=0;i<val_v.size();++i) val[i] = (char)(val_v[i]);
66                         val[i]='\0';
67                         nib_str = val;
68                 }
69         }
70
71 //              Load the schemas, get the representation for the desired schema
72         mc_schema::mc_schemas *mcs = mc_schema::new_mc_schemas(nib_str);
73         if(mcs->has_errors()){  // TODO some schemas might have been loaded
74                 fprintf(stderr, "Errors loading the schemas:\n%s\n",mcs->get_errors().c_str());
75                 exit(1);
76         }
77         mc_schema::query_rep *qr = mcs->get_query_rep(schema);
78         if(qr==NULL){
79                 fprintf(stderr,"Error, schema %s not found, available schemas are:\n",schema.c_str());
80                 vector<string> streams = mcs->get_streams();
81                 for(int i=0;i<streams.size(); ++i){
82                         printf("\t%s\n",streams[i].c_str());
83                 }
84                 exit(1);
85         }
86
87 //      Get the table representation
88         vector<string> fields;
89         vector<mc_schema::field_handle> handles;
90         int n_fields = qr->get_num_fields();
91         for(int i=0;i<n_fields;++i){
92                 fields.push_back(qr->get_field_name(i));
93                 handles.push_back(qr->get_handle_of_field(fields[i]));
94         }
95
96 //      CSV header
97         printf("Key_");
98         for(int i=0;i<n_fields;++i){
99                 printf(",%s",fields[i].c_str());
100         }
101         printf("\n");
102
103 //              
104         string pk =  schema+":"+prefix;
105         Keys K = sdl->findKeys(ns, pk); // prefix keys in schema
106         
107         DataMap Dk = sdl->get(ns, K);
108         for(auto si=K.begin();si!=K.end();++si){
109
110                 std::vector<uint8_t> val_v = Dk[(*si)]; // 5 lines to unpack a string
111                 int len = val_v.size();
112                 char val[len];                          // from Data
113                 for(int i=0;i<val_v.size();++i)
114                         val[i] = (char)(val_v[i]);
115
116                 printf("%s",(*si).c_str());
117                 for(int i=0;i<n_fields;++i){
118                         string s = mc_schema::get_field_string(handles[i], val, len);
119                         printf(",%s",s.c_str());
120                 }
121                 printf("\n");
122         }
123
124         delete mcs;
125
126 }