Test case for retrieval out of range of sortmax.
[idzebra-moved-to-github.git] / test / api / t4.c
1 /* $Id: t4.c,v 1.16 2005-04-14 12:01:50 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         ZebraRetrievalRecord retrievalRecord[NUMBER_TO_FETCH_MAX];
63         char setname[20];
64         int j;
65         ODR odr_input = odr_createmem(ODR_DECODE);    
66         ODR odr_output = odr_createmem(ODR_DECODE);    
67         YAZ_PQF_Parser parser = yaz_pqf_create();
68         Z_RPNQuery *query = yaz_pqf_parse(parser, odr_input, "@attr 1=4 my");
69         zint hits;
70         
71         sprintf(setname, "s%d", i+1);
72         zebra_search_RPN(zh, odr_input, query, setname, &hits);
73         if (hits != number_to_be_inserted)
74         {
75             yaz_log(YLOG_WARN, "Unexpected hit count " ZINT_FORMAT 
76                     "(should be %d)", hits, number_to_be_inserted);
77             exit(1);
78         }
79
80         yaz_pqf_destroy(parser);
81
82         odr_destroy(odr_input);
83
84         zebra_begin_trans(zh, 1);
85
86         for (j = 0; j < number_to_fetch; j++)
87             retrievalRecord[j].position = j+1;
88
89         zebra_records_retrieve(zh, odr_output, setname, 0,
90                                VAL_TEXT_XML, number_to_fetch, retrievalRecord);
91         
92         if (zebra_errCode(zh))
93         {
94             yaz_log(YLOG_FATAL, "zebra_records_retrieve returned error %d",
95                     zebra_errCode(zh));
96             exit(1);
97         }
98         
99         for (j = 0; j < number_to_fetch; j++)
100         {
101             if (!retrievalRecord[j].buf)
102             {
103                 yaz_log(YLOG_FATAL, "No record buf at position %d", j);
104                 exit(1);
105             }
106             if (!retrievalRecord[j].len)
107             {
108                 yaz_log(YLOG_FATAL, "No record len at position %d", j);
109                 exit(1);
110             }
111         }
112         odr_destroy(odr_output);
113
114         zebra_end_trans(zh);
115
116     }
117     zebra_commit(zh);
118     return close_down(zh, zs, 0);
119 }