8 #include"schemaparser.h"
10 #include <sdl/syncstorage.hpp>
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>;
21 int main(int argc, char **argv){
24 Namespace ns("mcnib");
25 std::unique_ptr<shareddatalayer::SyncStorage> sdl(shareddatalayer::SyncStorage::create());
29 fprintf(stderr,"Error, usage is %s schema [prefix] [directory]\n", argv[0]);
32 string schema = argv[1];
37 // Get the nib.json file
41 string directory = argv[3];
43 string inflnm = directory + "/" + string("nib.json");
45 ifstream infl(inflnm);
47 cerr << "Error, can't open " << inflnm << endl;
51 while(getline(infl, line)){
56 //fprintf(stderr,"Fetching from sdl\n");
60 DataMap Dk = sdl->get(ns, K);
62 vector<uint8_t> val_v = Dk[k];
63 char val[val_v.size()+1]; // from Data
65 for(i=0;i<val_v.size();++i) val[i] = (char)(val_v[i]);
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());
77 mc_schema::query_rep *qr = mcs->get_query_rep(schema);
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());
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]));
98 for(int i=0;i<n_fields;++i){
99 printf(",%s",fields[i].c_str());
104 string pk = schema+":"+prefix;
105 Keys K = sdl->findKeys(ns, pk); // prefix keys in schema
107 DataMap Dk = sdl->get(ns, K);
108 for(auto si=K.begin();si!=K.end();++si){
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]);
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());