ctdb-server: Remove duplicate logic
[samba4-gss.git] / source3 / rpc_server / mdssvc / es_parser_test.c
blob7d88c67abffc95a0d732d6dd75c05a3d6fafacb1
1 /*
2 Unix SMB/CIFS implementation.
3 Main metadata server / Spotlight routines / ES backend
5 Copyright (C) Ralph Boehme 2019
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "rpc_server/mdssvc/mdssvc.h"
23 #include "rpc_server/mdssvc/mdssvc_es.h"
24 #include "rpc_server/mdssvc/es_parser.tab.h"
25 #include "rpc_server/mdssvc/es_mapping.h"
28 * Examples:
30 * $ ./spotlight2es '_kMDItemGroupId=="11"'
31 * ...
32 * $ ./spotlight2es '*=="test*"||kMDItemTextContent=="test*"'
33 * ...
36 int main(int argc, char **argv)
38 TALLOC_CTX *mem_ctx = NULL;
39 json_t *mappings = NULL;
40 json_error_t json_error;
41 char *default_path = NULL;
42 const char *path = NULL;
43 const char *query_string = NULL;
44 const char *path_scope = NULL;
45 char *es_query = NULL;
46 bool ok;
48 if (argc != 2) {
49 printf("usage: %s QUERY\n", argv[0]);
50 return 1;
52 query_string = argv[1];
53 path_scope = "/foo/bar";
55 lp_load_global(get_dyn_CONFIGFILE());
57 mem_ctx = talloc_init("es_parser_test");
58 if (mem_ctx == NULL) {
59 return 1;
62 default_path = talloc_asprintf(mem_ctx,
63 "%s/mdssvc/elasticsearch_mappings.json",
64 get_dyn_SAMBA_DATADIR());
65 if (default_path == NULL) {
66 TALLOC_FREE(mem_ctx);
67 return 1;
70 path = lp_parm_const_string(GLOBAL_SECTION_SNUM,
71 "elasticsearch",
72 "mappings",
73 default_path);
74 if (path == NULL) {
75 TALLOC_FREE(mem_ctx);
76 return 1;
79 mappings = json_load_file(path, 0, &json_error);
80 if (mappings == NULL) {
81 DBG_ERR("Opening mapping file [%s] failed: %s\n",
82 path, strerror(errno));
83 TALLOC_FREE(mem_ctx);
84 return 1;
87 ok = map_spotlight_to_es_query(mem_ctx,
88 mappings,
89 path_scope,
90 query_string,
91 &es_query);
92 printf("%s\n", ok ? es_query : "*mapping failed*");
94 json_decref(mappings);
95 talloc_free(mem_ctx);
96 return ok ? 0 : 1;