Align Zebra API more so that ZEBRA_RES is used to signal error/OK.
[idzebra-moved-to-github.git] / test / api / t4.c
1 /* $Id: t4.c,v 1.17 2005-05-11 12:39:37 adam Exp $
2    Copyright (C) 1995-2005
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 /* t4 - insert a small pile of records, search and fetch them */
24
25 #include "testlib.h"
26
27 const char *myrec[] = {
28         "<gils>\n"
29         "  <title>My title</title>\n"
30         "</gils>\n",
31         0};
32         
33 #define NUMBER_TO_FETCH_MAX 1000
34
35 int main(int argc, char **argv)
36 {
37     int i;
38     int number_to_be_inserted = 5;
39     int number_to_fetch = 5;
40
41     ZebraService zs = start_up(0, argc, argv);
42     ZebraHandle zh = zebra_open(zs);
43
44     init_data(zh, myrec);
45
46     zebra_begin_trans (zh, 1);
47     for (i = 0; i< number_to_be_inserted-1; i++)
48     {  /* -1 since already inserted one in init_data */
49         zebra_add_record(zh, myrec[0], strlen(myrec[0]));
50     }
51     zebra_end_trans(zh);
52     zebra_close(zh);
53     zebra_stop(zs);
54
55     zs = start_service("");
56     zh = zebra_open(zs);
57     zebra_select_database(zh, "Default");
58     zebra_set_resource(zh, "sortmax", "3"); /* make small sort boundary */
59
60     for (i = 0; i<2; i++)
61     {
62         ZEBRA_RES ret;
63         ZebraRetrievalRecord retrievalRecord[NUMBER_TO_FETCH_MAX];
64         char setname[20];
65         int j;
66         ODR odr_input = odr_createmem(ODR_DECODE);    
67         ODR odr_output = odr_createmem(ODR_DECODE);    
68         YAZ_PQF_Parser parser = yaz_pqf_create();
69         Z_RPNQuery *query = yaz_pqf_parse(parser, odr_input, "@attr 1=4 my");
70         zint hits;
71         
72         sprintf(setname, "s%d", i+1);
73         ret = zebra_search_RPN(zh, odr_input, query, setname, &hits);
74         if (ret != ZEBRA_OK)
75         {
76             int code = zebra_errCode(zh);
77             yaz_log(YLOG_WARN, "Unexpected error code=%d", code);
78             exit(1);
79         }
80         if (hits != number_to_be_inserted)
81         {
82             yaz_log(YLOG_WARN, "Unexpected hit count " ZINT_FORMAT 
83                     "(should be %d)", hits, number_to_be_inserted);
84             exit(1);
85         }
86
87         yaz_pqf_destroy(parser);
88
89         odr_destroy(odr_input);
90
91         zebra_begin_trans(zh, 1);
92
93         for (j = 0; j < number_to_fetch; j++)
94             retrievalRecord[j].position = j+1;
95
96         ret = zebra_records_retrieve(zh, odr_output, setname, 0,
97                                      VAL_TEXT_XML, number_to_fetch,
98                                      retrievalRecord);
99         if (ret != ZEBRA_OK)
100         {
101             int code = zebra_errCode(zh);
102             yaz_log(YLOG_FATAL, "zebra_records_retrieve returned error %d",
103                     code);
104             exit(1);
105         }
106         
107         for (j = 0; j < number_to_fetch; j++)
108         {
109             if (!retrievalRecord[j].buf)
110             {
111                 yaz_log(YLOG_FATAL, "No record buf at position %d", j);
112                 exit(1);
113             }
114             if (!retrievalRecord[j].len)
115             {
116                 yaz_log(YLOG_FATAL, "No record len at position %d", j);
117                 exit(1);
118             }
119         }
120         odr_destroy(odr_output);
121
122         zebra_end_trans(zh);
123
124     }
125     zebra_commit(zh);
126     return close_down(zh, zs, 0);
127 }