Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rgw / rgw_es_main.cc
1 #include <list>
2 #include <string>
3 #include <iostream>
4
5 #include "global/global_init.h"
6 #include "global/global_context.h"
7
8 #include "common/ceph_argparse.h"
9 #include "common/ceph_json.h"
10 #include "rgw_es_query.h"
11
12 using namespace std;
13
14 int main(int argc, char *argv[])
15 {
16   vector<const char*> args;
17   argv_to_vec(argc, (const char **)argv, args);
18   env_to_vec(args);
19
20   auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
21                          CODE_ENVIRONMENT_UTILITY, 0);
22
23   common_init_finish(g_ceph_context);
24
25   string expr;
26
27   if (argc > 1) {
28     expr = argv[1];
29   } else {
30     expr = "age >= 30";
31   }
32
33   ESQueryCompiler es_query(expr, nullptr, "x-amz-meta-");
34
35   map<string, string, ltstr_nocase> aliases = { { "key", "name" },
36                                   { "etag", "meta.etag" },
37                                   { "size", "meta.size" },
38                                   { "mtime", "meta.mtime" },
39                                   { "lastmodified", "meta.mtime" },
40                                   { "contenttype", "meta.contenttype" },
41   };
42   es_query.set_field_aliases(&aliases);
43
44   map<string, ESEntityTypeMap::EntityType> generic_map = { {"bucket", ESEntityTypeMap::ES_ENTITY_STR},
45                                                            {"name", ESEntityTypeMap::ES_ENTITY_STR},
46                                                            {"instance", ESEntityTypeMap::ES_ENTITY_STR},
47                                                            {"meta.etag", ESEntityTypeMap::ES_ENTITY_STR},
48                                                            {"meta.contenttype", ESEntityTypeMap::ES_ENTITY_STR},
49                                                            {"meta.mtime", ESEntityTypeMap::ES_ENTITY_DATE},
50                                                            {"meta.size", ESEntityTypeMap::ES_ENTITY_INT} };
51   ESEntityTypeMap gm(generic_map);
52   es_query.set_generic_type_map(&gm);
53
54   map<string, ESEntityTypeMap::EntityType> custom_map = { {"str", ESEntityTypeMap::ES_ENTITY_STR},
55                                                           {"int", ESEntityTypeMap::ES_ENTITY_INT},
56                                                           {"date", ESEntityTypeMap::ES_ENTITY_DATE} };
57   ESEntityTypeMap em(custom_map);
58   es_query.set_custom_type_map(&em);
59
60   string err;
61   
62   bool valid = es_query.compile(&err);
63   if (!valid) {
64     cout << "failed to compile query: " << err << std::endl;
65     return EINVAL;
66   }
67
68   JSONFormatter f;
69   encode_json("root", es_query, &f);
70
71   f.flush(cout);
72
73   return 0;
74 }
75