98da891ade2e8ff76a7b13f87681083f8b0c58d6
[idzebra-moved-to-github.git] / test / api / t4.c
1 /* $Id: t4.c,v 1.8.2.1 2005-05-04 10:40:19 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,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 <stdlib.h>
24 #include <yaz/log.h>
25 #include <yaz/pquery.h>
26 #include <zebraapi.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
47     yaz_log_init_file("t4.log");
48
49     nmem_init ();
50     
51     zs = start_service();
52     zh = zebra_open (zs);
53     zebra_init(zh);
54     zebra_select_database(zh, "Default");
55
56     zebra_begin_trans (zh, 1);
57     for (i = 0; i<1200; i++)
58         zebra_add_record (zh, myrec, strlen(myrec));
59     zebra_end_trans (zh);
60     zebra_close(zh);
61     zebra_stop(zs);
62
63     zs = start_service();
64     zh = zebra_open (zs);
65     zebra_select_database(zh, "Default");
66
67     for (i = 0; i<2; i++)
68     {
69         ZebraRetrievalRecord retrievalRecord[1001];
70         char setname[20];
71         int j;
72         ODR odr_input = odr_createmem (ODR_DECODE);    
73         ODR odr_output = odr_createmem (ODR_DECODE);    
74         YAZ_PQF_Parser parser = yaz_pqf_create();
75         Z_RPNQuery *query = yaz_pqf_parse(parser, odr_input, 
76                                           "@attr 1=4 my");
77         int hits;
78         
79         sprintf(setname, "s%d", i+1);
80         zebra_search_RPN (zh, odr_input, query, setname, &hits);
81
82         yaz_pqf_destroy(parser);
83
84         odr_destroy (odr_input);
85
86         zebra_begin_trans (zh, 1);
87
88         for (j = 0; j<1001; j++)
89             retrievalRecord[j].position = j+1;
90
91         zebra_records_retrieve (zh, odr_output, setname, 0,
92                                 VAL_TEXT_XML, 1001, retrievalRecord);
93
94
95         odr_destroy (odr_output);
96
97         zebra_end_trans (zh);
98
99     }
100     zebra_commit (zh);
101     zebra_close (zh);
102     zebra_stop (zs);
103
104     nmem_exit ();
105     xmalloc_trav ("x");
106     exit (0);
107 }