Add new function nmem_strsplitx.
[yaz-moved-to-github.git] / include / yaz / solr.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data.
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /** \file solr.h
29     \brief Header with public definitions about SOLR.
30 */
31
32 #ifndef SOLR_H_INCLUDED
33 #define SOLR_H_INCLUDED
34 #include <stdio.h>
35 #include <yaz/nmem.h>
36
37 YAZ_BEGIN_CDECL
38
39 /** \brief SOLR parser handle (opaque pointer) */
40 typedef struct solr_parser *SOLR_parser;
41
42 /** \brief creates a SOLR parser.
43     \returns CCL parser
44     
45     Returns SOLR parser or NULL if parser could not be created.
46  */
47 YAZ_EXPORT 
48 SOLR_parser solr_parser_create(void);
49
50 /** \brief destroys a SOLR parser.
51     \param cp SOLR parser
52
53     This function does nothing if NULL if received.
54  */
55 YAZ_EXPORT 
56 void solr_parser_destroy(SOLR_parser cp);
57
58 /** \brief parses a SOLR query (string)
59     \param cp SOLR parser
60     \param str SOLR string
61     \retval 0 success
62     \retval !=0 failure
63  */
64 YAZ_EXPORT 
65 int solr_parser_string(SOLR_parser cp, const char *str);
66
67 /** \brief parses SOLR query (query stream)
68     \param cp SOLR parser
69     \param getbyte function which reads one character from stream
70     \param ungetbyte function which unreads one character from stream
71     \param client_data data to be passed to stream functions
72     \retval 0 success
73     \retval !=0 failure
74     
75     This function is similar to solr_parser_string but takes a
76     functions to read each query character from a stream.
77     
78     The functions pointers getbytes, ungetbyte are similar to
79     that known from stdios getc, ungetc.
80 */
81 YAZ_EXPORT 
82 int solr_parser_stream(SOLR_parser cp,
83                       int (*getbyte)(void *client_data),
84                       void (*ungetbyte)(int b, void *client_data),
85                       void *client_data);
86
87 /** \brief parses SOLR query (from FILE)
88     \param cp SOLR parser
89     \param f file where query is read from
90     \retval 0 success
91     \retval !=0 failure
92     
93     This function is similar to solr_parser_string but reads from
94     stdio FILE handle instead.
95 */
96 YAZ_EXPORT
97 int solr_parser_stdio(SOLR_parser cp, FILE *f);
98
99 /** \brief Node type: search term */
100 #define SOLR_NODE_ST 1
101 /** \brief Node type: boolean */
102 #define SOLR_NODE_BOOL 2
103 /** \brief SOLR parse tree (node)
104  */
105 struct solr_node {
106     /** node type */
107     int which;
108     union {
109         /** which == SOLR_NODE_ST */
110         struct {
111             /** SOLR index */
112             char *index;
113             /** SOLR index URI or NULL if no URI */
114             char *index_uri;
115             /** Search term */
116             char *term;
117             /** relation */
118             char *relation;
119             /** relation URL or NULL if no relation URI) */
120             char *relation_uri;
121             /** relation modifiers */
122             struct solr_node *modifiers;
123             /** term list */
124             struct solr_node *extra_terms;
125         } st;
126         /** which == SOLR_NODE_BOOL */
127         struct {
128             /** operator name "and", "or", ... */
129             char *value;
130             /** left operand */
131             struct solr_node *left;
132             /** right operand */ 
133             struct solr_node *right;
134             /** modifiers (NULL for no list) */
135             struct solr_node *modifiers;
136         } boolean;
137     } u;
138 };
139
140 /** \brief Private structure that describes the SOLR properties (profile)
141  */
142 struct solr_properties;
143
144 /** \brief Structure used by solr_buf_write_handler
145  */
146 struct solr_buf_write_info {
147     int max;
148     int off;
149     char *buf;
150 };
151
152 /** \brief Handler for solr_buf_write_info
153  */
154 YAZ_EXPORT
155 void solr_buf_write_handler(const char *b, void *client_data);
156
157 /** \brief Prints a SOLR node and all sub nodes.
158     Hence this function prints the parse tree which is as returned by
159     solr_parser_result.
160 */
161 YAZ_EXPORT
162 void solr_node_print(struct solr_node *cn);
163
164 /** \brief creates a search clause node (st). */
165 YAZ_EXPORT
166 struct solr_node *solr_node_mk_sc(NMEM nmem, const char *index,
167                                 const char *relation, const char *term);
168
169 /** \brief applies a prefix+uri to "unresolved" index and relation URIs.
170     "unresolved" URIs are those nodes where member index_uri / relation_uri
171     is NULL.
172 */
173 YAZ_EXPORT
174 struct solr_node *solr_apply_prefix(NMEM nmem, struct solr_node *cn,
175                                   const char *prefix, const char *uri);
176
177 /** \brief creates a boolean node. */
178 YAZ_EXPORT
179 struct solr_node *solr_node_mk_boolean(NMEM nmem, const char *op);
180
181 /** \brief destroys a node and its children. */
182 YAZ_EXPORT
183 void solr_node_destroy(struct solr_node *cn);
184
185 /** duplicates a node (returns a copy of supplied node) . */
186 YAZ_EXPORT
187 struct solr_node *solr_node_dup (NMEM nmem, struct solr_node *cp);
188
189 /** \brief returns the parse tree of the most recently parsed SOLR query.
190     \param cp SOLR parser
191     \returns SOLR node or NULL for failure
192 */
193 YAZ_EXPORT
194 struct solr_node *solr_parser_result(SOLR_parser cp);
195
196 /** \brief converts SOLR tree to XSOLR and writes to user-defined stream
197     \param cn SOLR node (tree)
198     \param pr print function
199     \param client_data data to be passed to pr function
200  */
201 YAZ_EXPORT
202 void solr_to_xml(struct solr_node *cn,
203                 void (*pr)(const char *buf, void *client_data),
204                 void *client_data);
205 /** \brief converts SOLR tree to XSOLR and writes to file
206     \param cn SOLR node (tree)
207     \param f file handle
208  */
209 YAZ_EXPORT
210 void solr_to_xml_stdio(struct solr_node *cn, FILE *f);
211
212 /** \brief converts SOLR tree to XSOLR and writes result to buffer
213     \param cn SOLR node (tree)
214     \param out buffer
215     \param max size of buffer (max chars to write)
216     \returns length of resulting buffer
217  */
218 YAZ_EXPORT
219 int solr_to_xml_buf(struct solr_node *cn, char *out, int max);
220
221 /** \brief stream handle for file (used by solr_to_xml_stdio) */
222 YAZ_EXPORT
223 void solr_fputs(const char *buf, void *client_data);
224
225 /** \brief SOLR transform handle.
226     The transform describes how to convert from SOLR to PQF (Type-1 AKA RPN).
227 */
228 typedef struct solr_transform_t_ *solr_transform_t;
229
230 /** \brief creates a SOLR transform handle
231     \returns transform handle or NULL for failure
232 */
233 YAZ_EXPORT
234 solr_transform_t solr_transform_create(void);
235
236 /** \brief creates a SOLR transform handle from am opened file handle
237     \param f file where transformation spec is read
238     \returns transform handle or NULL for failure
239
240     The transformation spec is read from a FILE handle which is assumed
241     opened for reading.
242 */
243 YAZ_EXPORT
244 solr_transform_t solr_transform_open_FILE (FILE *f);
245
246 /** \brief creates a SOLR transform handle from a file
247     \param fname name of where transformation spec is read
248     \returns transform handle or NULL for failure
249 */
250 YAZ_EXPORT
251 solr_transform_t solr_transform_open_fname(const char *fname);
252
253
254 /** \brief defines SOLR transform pattern
255     \param ct SOLR transform handle
256     \param pattern pattern string
257     \param value pattern value
258     \returns 0 for succes; -1 for failure
259 */
260 YAZ_EXPORT
261 int solr_transform_define_pattern(solr_transform_t ct, const char *pattern,
262                                  const char *value);
263     
264
265
266 /** \brief destroys a SOLR transform handle
267     \param ct SOLR transform handle
268  */
269 YAZ_EXPORT
270 void solr_transform_close(solr_transform_t ct);
271
272 /** \brief tranforms PQF given a SOLR tree
273     \param ct SOLR transform handle
274     \param cn SOLR node tree
275     \param pr print function
276     \param client_data data to be passed to pr
277     \retval 0 success
278     \retval != 0 error
279
280     The result is written to a user-defined stream.
281 */
282 YAZ_EXPORT
283 int solr_transform(solr_transform_t ct,
284                   struct solr_node *cn,
285                   void (*pr)(const char *buf, void *client_data),
286                   void *client_data);
287
288 /** \brief transforms PQF given a SOLR tree (from FILE)
289     \param ct SOLR transform handle
290     \param cn SOLR tree
291     \param f FILE where output is written
292     \retval 0 success
293     \retval !=0 failure (error code)
294
295     The result is written to a file specified by FILE handle (which must
296     be opened for writing.
297 */
298 YAZ_EXPORT
299 int solr_transform_FILE(solr_transform_t ct,
300                        struct solr_node *cn, FILE *f);
301
302 /** \brief transforms PQF given a SOLR tree (from FILE)
303     \param ct SOLR transform handle
304     \param cn SOLR tree
305     \param out buffer for output
306     \param max maximum bytes for output (size of buffer)
307     \retval 0 success
308     \retval !=0 failure (error code)
309  */
310 YAZ_EXPORT
311 int solr_transform_buf(solr_transform_t ct,
312                       struct solr_node *cn, char *out, int max);
313
314 /** \brief returns additional information for last transform
315     \param ct SOLR transform handle
316     \param addinfo additional info (result)
317     \returns error code
318  */
319 YAZ_EXPORT
320 int solr_transform_error(solr_transform_t ct, const char **addinfo);
321
322 /** \brief sets error and addinfo for transform
323     \param ct SOLR transform handle
324     \param error error code
325     \param addinfo additional info
326  */
327 YAZ_EXPORT
328 void solr_transform_set_error(solr_transform_t ct, int error, const char *addinfo);
329
330 /** \brief returns the SOLR message corresponding to a given error code.
331     \param code error code
332     \returns text message
333 */
334 YAZ_EXPORT
335 const char *solr_strerror(int code);
336
337 /** \brief returns the standard SOLR context set URI.
338     \returns SOLR URI string
339 */
340 YAZ_EXPORT
341 const char *solr_uri(void);
342
343 /** \brief compares two SOLR strings (ala strcmp)
344     \param s1 string 1
345     \param s2 string 2
346     \returns comparison value
347     Compares two SOLR strings (for relations, operators, etc)
348     (unfortunately defined as case-insensitive unlike XML etc)
349 */
350 YAZ_EXPORT
351 int solr_strcmp(const char *s1, const char *s2);
352
353 /** \brief compares two SOLR strings (ala strncmp)
354     \param s1 string 1
355     \param s2 string 2
356     \param n size
357     \returns comparison value
358     Compares two SOLR strings at most n bytes
359     (unfortunately defined as case-insensitive unlike XML etc)
360  */
361 YAZ_EXPORT
362 int solr_strncmp(const char *s1, const char *s2, size_t n);
363
364 YAZ_END_CDECL
365
366 #endif
367 /* SOLR_H_INCLUDED */
368 /*
369  * Local variables:
370  * c-basic-offset: 4
371  * c-file-style: "Stroustrup"
372  * indent-tabs-mode: nil
373  * End:
374  * vim: shiftwidth=4 tabstop=8 expandtab
375  */
376