IDZEBRA 2.2.8
tstbfile2.c
Go to the documentation of this file.
1/* This file is part of the Zebra server.
2 Copyright (C) Index Data
3
4Zebra is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18*/
19
20#if HAVE_CONFIG_H
21#include <config.h>
22#endif
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <errno.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include <yaz/snprintf.h>
30#include <yaz/test.h>
31#include <idzebra/bfile.h>
32
33void tst(void)
34{
35 int r;
36 BFiles bfs;
37 BFile bf;
38 char buf[256];
39 int block_size = 16;
40 zint max_block = 200000;
41
42 YAZ_CHECK(block_size <= sizeof(buf));
43 if (!(block_size <= sizeof(buf)))
44 return;
45
46 YAZ_CHECK(max_block * block_size < 4 * 1000000); /* 4M */
47
48 r = mkdir("register", 0777);
49 YAZ_CHECK(r == 0 || (r == -1 && errno == EEXIST));
50
51 r = mkdir("shadow", 0777);
52 YAZ_CHECK(r == 0 || (r == -1 && errno == EEXIST));
53
54 bfs = bfs_create("register.bad:4M", 0 /* base: current dir */);
55 YAZ_CHECK(!bfs);
56 if (bfs)
57 return;
58
59 bfs = bfs_create("register:4M", 0 /* base: current dir */);
60 YAZ_CHECK(bfs);
61 if (!bfs)
62 return;
63
64 r = bf_cache(bfs, "shadow.bad:4M");
65 YAZ_CHECK_EQ(r, ZEBRA_FAIL);
66
67 r = bf_cache(bfs, "shadow:4M");
68 YAZ_CHECK_EQ(r, ZEBRA_OK);
69
70 bf_reset(bfs);
71
72#if 1
73 /* we have to destroy bfs after reset. Unfortunately! */
74 bfs_destroy(bfs);
75
76 bfs = bfs_create("register:4M", 0 /* base: current dir */);
77 YAZ_CHECK(bfs);
78 if (!bfs)
79 return;
80
81 r = bf_cache(bfs, "shadow:4M");
82 YAZ_CHECK_EQ(r, ZEBRA_OK);
83#endif
84
85 yaz_log(YLOG_LOG, "writing file 1");
86 bf = bf_open(bfs, "file", block_size, 1);
87 YAZ_CHECK(bf);
88 if (bf)
89 {
90 zint bno[2];
91 memset(buf, ' ', block_size);
92
93 bno[0] = 0;
94 bno[1] = 1;
95 while (bno[0] < max_block)
96 {
97 zint next
98 = bno[0] + bno[1];
99
100 yaz_snprintf(buf, sizeof(buf), ZINT_FORMAT, bno[0]);
101 YAZ_CHECK_EQ(bf_write(bf, bno[0], 0, 0, buf), 0);
102
103 bno[0] = bno[1];
104 bno[1] = next;
105 }
106 bf_close(bf);
107 }
108
109 yaz_log(YLOG_LOG, "reading file 1");
110 bf = bf_open(bfs, "file", block_size, 0);
111 YAZ_CHECK(bf);
112 if (bf)
113 {
114 zint bno[2];
115
116 bno[0] = 0;
117 bno[1] = 1;
118 while (bno[0] < max_block)
119 {
120 zint next = bno[0] + bno[1];
121 memset(buf, ' ', block_size);
122
123 YAZ_CHECK_EQ(bf_read(bf, bno[0], 0, 0, buf), 1);
124 YAZ_CHECK_EQ(atoi(buf), bno[0]);
125
126 bno[0] = bno[1];
127 bno[1] = next;
128 }
129 bf_close(bf);
130 }
131
132#if 1
133 yaz_log(YLOG_LOG, "writing file 2");
134 bf = bf_open(bfs, "file", block_size, 1);
135 YAZ_CHECK(bf);
136 if (bf)
137 {
138 zint bno = 0;
139 while (bno < max_block)
140 {
141 memset(buf, ' ', block_size);
142
143 yaz_snprintf(buf, sizeof(buf), ZINT_FORMAT, bno);
144 YAZ_CHECK_EQ(bf_write(bf, bno, 0, 0, buf), 0);
145
146 bno = bno + 2;
147 }
148 bf_close(bf);
149 }
150
151 yaz_log(YLOG_LOG, "reading file 2");
152 bf = bf_open(bfs, "file", block_size, 0);
153 YAZ_CHECK(bf);
154 if (bf)
155 {
156 zint bno = 0;
157 int step = max_block / 50;
158
159 while (bno < max_block)
160 {
161 memset(buf, ' ', block_size);
162
163 YAZ_CHECK_EQ(bf_read(bf, bno, 0, 0, buf), 1);
164 YAZ_CHECK_EQ(atoi(buf), bno);
165
166 bno = bno + 2*step;
167 }
168 bf_close(bf);
169 }
170#endif
171 bfs_destroy(bfs);
172}
173
174
175int main(int argc, char **argv)
176{
177 YAZ_CHECK_INIT(argc, argv);
178 YAZ_CHECK_LOG();
179 tst();
180 YAZ_CHECK_TERM;
181}
182
Zebra Block File Layer.
int bf_read(BFile bf, zint no, int offset, int nbytes, void *buf)
read from block file (may call exit)
Definition bfile.c:205
ZEBRA_RES bf_cache(BFiles bfs, const char *spec)
enables or disables shadow for block files
Definition bfile.c:95
void bf_close(BFile bf)
closes a Block file (may call exit)
Definition bfile.c:139
BFile bf_open(BFiles bfs, const char *name, int block_size, int wflag)
opens and returns a Block file handle
Definition bfile.c:150
int bf_write(BFile bf, zint no, int offset, int nbytes, const void *buf)
writes block of bytes to file (may call exit)
Definition bfile.c:232
void bf_reset(BFiles bfs)
Removes register and shadow completely.
Definition bfile.c:268
BFiles bfs_create(const char *spec, const char *base)
creates a Block files collection
Definition bfile.c:56
void bfs_destroy(BFiles bfiles)
destroys a block files handle
Definition bfile.c:73
int main(int argc, char **argv)
Definition tstbfile2.c:175
void tst(void)
Definition tstbfile2.c:33
long zint
Zebra integer.
Definition util.h:66
#define ZEBRA_FAIL
Definition util.h:81
#define ZINT_FORMAT
Definition util.h:72
#define ZEBRA_OK
Definition util.h:82