Added a test for sorting
[idzebra-moved-to-github.git] / test / api / t7.c
1 /* $Id: t7.c,v 1.1 2004-09-22 11:59:25 heikki Exp $
2    Copyright (C) 2004
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <yaz/log.h>
24 #include <yaz/pquery.h>
25 #include <yaz/sortspec.h>
26 #include <idzebra/api.h>
27
28 /* read zebra.cfg from env var srcdir if it exists; otherwise current dir */
29 static ZebraService start_service()
30 {
31     char cfg[256];
32     char *srcdir = getenv("srcdir");
33     sprintf(cfg, "%.200s%szebra.cfg", srcdir ? srcdir : "", srcdir ? "/" : "");
34     return zebra_start(cfg);
35 }
36         
37 int main(int argc, char **argv)
38 {
39     int i;
40     ZebraService zs;
41     ZebraHandle zh;
42     const char *myrec =
43         "<gils>\n"
44         "  <title>My title</title>\n"
45         "</gils>\n";
46     const char *setname1="set1";
47     const char *setname2="set2";
48     const char *setname3="set3";
49     int status;
50     int rc;
51     ODR odr_input = odr_createmem (ODR_DECODE);    
52     ODR odr_output = odr_createmem (ODR_ENCODE);    
53     YAZ_PQF_Parser parser = yaz_pqf_create();
54     Z_RPNQuery *query = yaz_pqf_parse(parser, odr_input, 
55                                       "@attr 1=4 my");
56     Z_SortKeySpecList *spec = 
57           yaz_sort_spec (odr_output, "@attr 1=4 id");
58     int hits;
59
60     yaz_log_init_file("t7.log");
61
62     nmem_init ();
63     
64     zs = start_service();
65     zh = zebra_open (zs);
66     zebra_select_database(zh, "Default");
67     zebra_init(zh);
68
69     zebra_begin_trans (zh, 1);
70     zebra_add_record (zh, myrec, strlen(myrec));
71     zebra_end_trans (zh);
72
73     zebra_begin_trans (zh, 0);
74         
75     zebra_search_RPN (zh, odr_input, query, setname1, &hits);
76
77     rc=zebra_sort(zh, odr_output, 1, &setname1, setname2, spec, &status);
78     if (rc) { printf("sort A returned %d %d \n",rc,status); exit(1);}
79     rc=zebra_sort(zh, odr_output, 1, &setname2, setname3, spec, &status);
80     if (rc) { printf("sort B returned %d %d \n",rc,status); exit(1);}
81
82     zebra_end_trans (zh);
83     yaz_pqf_destroy(parser);
84
85     /*
86      zebra_deleleResultSet(zh, Z_DeleteRequest_list,
87                           1, &setnamep, &status);
88     */
89     odr_destroy (odr_input);
90     odr_destroy (odr_output);
91
92     zebra_commit (zh);
93     zebra_close (zh);
94     zebra_stop (zs);
95
96     nmem_exit ();
97     xmalloc_trav ("x");
98     exit (0);
99 }