IDZEBRA  2.1.2
tstbfile2.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) 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 #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 #include <yaz/test.h>
29 #include <idzebra/bfile.h>
30 
31 void tst(void)
32 {
33  int r;
34  BFiles bfs;
35  BFile bf;
36  char buf[256];
37  int block_size = 16;
38  zint max_block = 200000;
39 
40  YAZ_CHECK(block_size <= sizeof(buf));
41  if (!(block_size <= sizeof(buf)))
42  return;
43 
44  YAZ_CHECK(max_block * block_size < 4 * 1000000); /* 4M */
45 
46  r = mkdir("register", 0777);
47  YAZ_CHECK(r == 0 || (r == -1 && errno == EEXIST));
48 
49  r = mkdir("shadow", 0777);
50  YAZ_CHECK(r == 0 || (r == -1 && errno == EEXIST));
51 
52  bfs = bfs_create("register.bad:4M", 0 /* base: current dir */);
53  YAZ_CHECK(!bfs);
54  if (bfs)
55  return;
56 
57  bfs = bfs_create("register:4M", 0 /* base: current dir */);
58  YAZ_CHECK(bfs);
59  if (!bfs)
60  return;
61 
62  r = bf_cache(bfs, "shadow.bad:4M");
63  YAZ_CHECK_EQ(r, ZEBRA_FAIL);
64 
65  r = bf_cache(bfs, "shadow:4M");
66  YAZ_CHECK_EQ(r, ZEBRA_OK);
67 
68  bf_reset(bfs);
69 
70 #if 1
71  /* we have to destroy bfs after reset. Unfortunately! */
72  bfs_destroy(bfs);
73 
74  bfs = bfs_create("register:4M", 0 /* base: current dir */);
75  YAZ_CHECK(bfs);
76  if (!bfs)
77  return;
78 
79  r = bf_cache(bfs, "shadow:4M");
80  YAZ_CHECK_EQ(r, ZEBRA_OK);
81 #endif
82 
83  yaz_log(YLOG_LOG, "writing file 1");
84  bf = bf_open(bfs, "file", block_size, 1);
85  YAZ_CHECK(bf);
86  if (bf)
87  {
88  zint bno[2];
89  memset(buf, ' ', block_size);
90 
91  bno[0] = 0;
92  bno[1] = 1;
93  while (bno[0] < max_block)
94  {
95  zint next = bno[0] + bno[1];
96 
97  sprintf(buf, ZINT_FORMAT, bno[0]);
98  YAZ_CHECK_EQ(bf_write(bf, bno[0], 0, 0, buf), 0);
99 
100  bno[0] = bno[1];
101  bno[1] = next;
102  }
103  bf_close(bf);
104  }
105 
106  yaz_log(YLOG_LOG, "reading file 1");
107  bf = bf_open(bfs, "file", block_size, 0);
108  YAZ_CHECK(bf);
109  if (bf)
110  {
111  zint bno[2];
112 
113  bno[0] = 0;
114  bno[1] = 1;
115  while (bno[0] < max_block)
116  {
117  zint next = bno[0] + bno[1];
118  memset(buf, ' ', block_size);
119 
120  YAZ_CHECK_EQ(bf_read(bf, bno[0], 0, 0, buf), 1);
121  YAZ_CHECK_EQ(atoi(buf), bno[0]);
122 
123  bno[0] = bno[1];
124  bno[1] = next;
125  }
126  bf_close(bf);
127  }
128 
129 #if 1
130  yaz_log(YLOG_LOG, "writing file 2");
131  bf = bf_open(bfs, "file", block_size, 1);
132  YAZ_CHECK(bf);
133  if (bf)
134  {
135  zint bno = 0;
136  while (bno < max_block)
137  {
138  memset(buf, ' ', block_size);
139 
140  sprintf(buf, ZINT_FORMAT, bno);
141  YAZ_CHECK_EQ(bf_write(bf, bno, 0, 0, buf), 0);
142 
143  bno = bno + 2;
144  }
145  bf_close(bf);
146  }
147 
148  yaz_log(YLOG_LOG, "reading file 2");
149  bf = bf_open(bfs, "file", block_size, 0);
150  YAZ_CHECK(bf);
151  if (bf)
152  {
153  zint bno = 0;
154  int step = max_block / 50;
155 
156  while (bno < max_block)
157  {
158  memset(buf, ' ', block_size);
159 
160  YAZ_CHECK_EQ(bf_read(bf, bno, 0, 0, buf), 1);
161  YAZ_CHECK_EQ(atoi(buf), bno);
162 
163  bno = bno + 2*step;
164  }
165  bf_close(bf);
166  }
167 #endif
168  bfs_destroy(bfs);
169 }
170 
171 
172 int main(int argc, char **argv)
173 {
174  YAZ_CHECK_INIT(argc, argv);
175  YAZ_CHECK_LOG();
176  tst();
177  YAZ_CHECK_TERM;
178 }
179 
#define ZEBRA_OK
Definition: util.h:82
void tst(void)
Definition: tstbfile2.c:31
char * buf
Definition: trunc.c:39
#define ZEBRA_FAIL
Definition: util.h:81
Zebra Block File Layer.
int main(int argc, char **argv)
Definition: tstbfile2.c:172
BFiles bfs_create(const char *spec, const char *base)
creates a Block files collection
Definition: bfile.c:63
void bf_reset(BFiles bfs)
Removes register and shadow completely.
Definition: bfile.c:382
void bf_close(BFile bf)
closes a Block file (may call exit)
Definition: bfile.c:151
ZEBRA_RES bf_cache(BFiles bfs, const char *spec)
enables or disables shadow for block files
Definition: bfile.c:105
int bf_read(BFile bf, zint no, int offset, int nbytes, void *buf)
read from block file (may call exit)
Definition: bfile.c:319
void bfs_destroy(BFiles bfiles)
destroys a block files handle
Definition: bfile.c:80
long zint
Zebra integer.
Definition: util.h:66
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:346
BFile bf_open(BFiles bfs, const char *name, int block_size, int wflag)
opens and returns a Block file handle
Definition: bfile.c:261
#define ZINT_FORMAT
Definition: util.h:72