1 /* This file is part of the Zebra server.
2 Copyright (C) 2004-2013 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 /* insert a small pile of records, search and fetch them */
21
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
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
tst(int argc,char ** argv)35 static void tst(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 = tl_start_up(0, argc, argv);
42 ZebraHandle zh = zebra_open(zs, 0);
43
44 YAZ_CHECK(tl_init_data(zh, myrec));
45
46 YAZ_CHECK(zebra_begin_trans (zh, 1) == ZEBRA_OK);
47
48 for (i = 0; i< number_to_be_inserted-1; i++)
49 { /* -1 since already inserted one in init_data */
50 zebra_add_record(zh, myrec[0], strlen(myrec[0]));
51 }
52 YAZ_CHECK(zebra_end_trans(zh) == ZEBRA_OK);
53
54 zebra_close(zh);
55 zebra_stop(zs);
56
57 zs = tl_zebra_start("");
58 zh = zebra_open(zs, 0);
59 YAZ_CHECK(zebra_select_database(zh, "Default") == ZEBRA_OK);
60 zebra_set_resource(zh, "sortmax", "3"); /* make small sort boundary */
61
62 for (i = 0; i<2; i++)
63 {
64 ZEBRA_RES ret;
65 ZebraRetrievalRecord retrievalRecord[NUMBER_TO_FETCH_MAX];
66 char setname[20];
67 int j;
68 ODR odr_input = odr_createmem(ODR_DECODE);
69 ODR odr_output = odr_createmem(ODR_DECODE);
70 YAZ_PQF_Parser parser = yaz_pqf_create();
71 Z_RPNQuery *query = yaz_pqf_parse(parser, odr_input, "@attr 1=4 my");
72 zint hits;
73
74 sprintf(setname, "s%d", i+1);
75 ret = zebra_search_RPN(zh, odr_input, query, setname, &hits);
76 if (ret != ZEBRA_OK)
77 {
78 int code = zebra_errCode(zh);
79 yaz_log(YLOG_WARN, "Unexpected error code=%d", code);
80 exit(1);
81 }
82 if (hits != number_to_be_inserted)
83 {
84 yaz_log(YLOG_WARN, "Unexpected hit count " ZINT_FORMAT
85 "(should be %d)", hits, number_to_be_inserted);
86 exit(1);
87 }
88
89 yaz_pqf_destroy(parser);
90
91 odr_destroy(odr_input);
92
93 YAZ_CHECK(zebra_begin_trans(zh, 1) == ZEBRA_OK);
94
95 for (j = 0; j < number_to_fetch; j++)
96 retrievalRecord[j].position = j+1;
97
98 ret = zebra_records_retrieve(zh, odr_output, setname, 0,
99 yaz_oid_recsyn_xml, number_to_fetch,
100 retrievalRecord);
101 if (ret != ZEBRA_OK)
102 {
103 int code = zebra_errCode(zh);
104 yaz_log(YLOG_FATAL, "zebra_records_retrieve returned error %d",
105 code);
106 exit(1);
107 }
108
109 for (j = 0; j < number_to_fetch; j++)
110 {
111 if (!retrievalRecord[j].buf)
112 {
113 yaz_log(YLOG_FATAL, "No record buf at position %d", j);
114 exit(1);
115 }
116 if (!retrievalRecord[j].len)
117 {
118 yaz_log(YLOG_FATAL, "No record len at position %d", j);
119 exit(1);
120 }
121 }
122 odr_destroy(odr_output);
123
124 YAZ_CHECK(zebra_end_trans(zh) == ZEBRA_OK);
125 }
126 zebra_commit(zh);
127 YAZ_CHECK(tl_close_down(zh, zs));
128 }
129
130 TL_MAIN
131
132 /*
133 * Local variables:
134 * c-basic-offset: 4
135 * c-file-style: "Stroustrup"
136 * indent-tabs-mode: nil
137 * End:
138 * vim: shiftwidth=4 tabstop=8 expandtab
139 */
140
141