de48f897b4aecc3985d99580b0107e7e5e90f900
[idzebra-moved-to-github.git] / test / api / t4.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 /* t4 - insert a small pile of records, search and fetch them */
21
22 #include "testlib.h"
23
24 const char *myrec[] = {
25         "<gils>\n"
26         "  <title>My title</title>\n"
27         "</gils>\n",
28         0};
29         
30 #define NUMBER_TO_FETCH_MAX 1000
31
32 static void tst(int argc, char **argv)
33 {
34     int i;
35     int number_to_be_inserted = 5;
36     int number_to_fetch = 5;
37
38     ZebraService zs = tl_start_up(0, argc, argv);
39     ZebraHandle zh = zebra_open(zs, 0);
40
41     YAZ_CHECK(tl_init_data(zh, myrec));
42
43     YAZ_CHECK(zebra_begin_trans (zh, 1) == ZEBRA_OK);
44
45     for (i = 0; i< number_to_be_inserted-1; i++)
46     {  /* -1 since already inserted one in init_data */
47         zebra_add_record(zh, myrec[0], strlen(myrec[0]));
48     }
49     YAZ_CHECK(zebra_end_trans(zh) == ZEBRA_OK);
50
51     zebra_close(zh);
52     zebra_stop(zs);
53
54     zs = tl_zebra_start("");
55     zh = zebra_open(zs, 0);
56     YAZ_CHECK(zebra_select_database(zh, "Default") == ZEBRA_OK);
57     zebra_set_resource(zh, "sortmax", "3"); /* make small sort boundary */
58
59     for (i = 0; i<2; i++)
60     {
61         ZEBRA_RES ret;
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         ret = zebra_search_RPN(zh, odr_input, query, setname, &hits);
73         if (ret != ZEBRA_OK)
74         {
75             int code = zebra_errCode(zh);
76             yaz_log(YLOG_WARN, "Unexpected error code=%d", code);
77             exit(1);
78         }
79         if (hits != number_to_be_inserted)
80         {
81             yaz_log(YLOG_WARN, "Unexpected hit count " ZINT_FORMAT 
82                     "(should be %d)", hits, number_to_be_inserted);
83             exit(1);
84         }
85
86         yaz_pqf_destroy(parser);
87
88         odr_destroy(odr_input);
89
90         YAZ_CHECK(zebra_begin_trans(zh, 1) == ZEBRA_OK);
91
92         for (j = 0; j < number_to_fetch; j++)
93             retrievalRecord[j].position = j+1;
94
95         ret = zebra_records_retrieve(zh, odr_output, setname, 0,
96                                      yaz_oid_recsyn_xml, number_to_fetch,
97                                      retrievalRecord);
98         if (ret != ZEBRA_OK)
99         {
100             int code = zebra_errCode(zh);
101             yaz_log(YLOG_FATAL, "zebra_records_retrieve returned error %d",
102                     code);
103             exit(1);
104         }
105         
106         for (j = 0; j < number_to_fetch; j++)
107         {
108             if (!retrievalRecord[j].buf)
109             {
110                 yaz_log(YLOG_FATAL, "No record buf at position %d", j);
111                 exit(1);
112             }
113             if (!retrievalRecord[j].len)
114             {
115                 yaz_log(YLOG_FATAL, "No record len at position %d", j);
116                 exit(1);
117             }
118         }
119         odr_destroy(odr_output);
120
121         YAZ_CHECK(zebra_end_trans(zh) == ZEBRA_OK);
122     }
123     zebra_commit(zh);
124     YAZ_CHECK(tl_close_down(zh, zs));
125 }
126
127 TL_MAIN
128
129 /*
130  * Local variables:
131  * c-basic-offset: 4
132  * indent-tabs-mode: nil
133  * End:
134  * vim: shiftwidth=4 tabstop=8 expandtab
135  */
136