Not functional yet
[idzebra-moved-to-github.git] / isam / isam.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: isam.c,v $
7  * Revision 1.1  1994-09-12 08:02:13  quinn
8  * Not functional yet
9  *
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15
16 #include <util.h>
17 #include "isutil.h"
18 #include <bfile.h>
19 #include <isam.h>
20 #include <common.h>
21
22 static int splitargs(const char *s, char *bf[], int max)
23 {
24     int ct = 0;
25     for (;;)
26     {
27         while (*s && isspace(*s))
28             s++;
29         bf[ct] = (char *) s;
30         if (!*s)
31                 return ct;
32         ct++;
33         if (ct > max)
34         {
35             log(LOG_WARN, "Ignoring extra args to is resource");
36             bf[ct] = '\0';
37             return(ct - 1);
38         }
39         while (*s && !isspace(*s))
40             s++;
41     }
42 }
43
44 /*
45  * Open isam file.
46  * Process resources.
47  */
48 ISAM is_open(const char *name, int writeflag)
49 {
50     ISAM new;
51     char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
52     int num, size, rs, tmp, i;
53
54     log(LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
55     new = xmalloc(sizeof(*new));
56     new->writeflag = writeflag;
57
58     /* determine number and size of blocktypes */
59     if (!(r = res_get(common_resource, nm = strconcat(name, ".",
60         "blocktypes", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
61     {
62         log(LOG_FATAL, "Failed to locate resource %s", nm);
63         return 0;
64     }
65     new->num_types = num;
66     for (i = 0; i < num; i++)
67     {
68         if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
69         {
70             log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
71             return 0;
72         }
73         if (rs == 1)
74                 *m = 'b';
75         switch (*m)
76         {
77                 case 'b': case 'B':
78                     new->types[i].blocksize = size; break;
79                 case 'k': case 'K':
80                     new->types[i].blocksize = size * 1024; break;
81                 case 'm': case 'M':
82                     new->types[i].blocksize = size * 1048576; break;
83                 default:
84                     log(LOG_FATAL, "Illegal size suffix: %c", *m);
85                     return 0;
86         }
87         m[0] = 'A' + i;
88         m[1] = '\0';
89         if (!(new->types[i].bf = bf_open(strconcat(name, m, 0), 
90             new->types[i].blocksize, writeflag)))
91         {
92             log(LOG_FATAL, "bf_open failed");
93             return 0;
94         }
95     }
96
97     /* determine nice fill rates */
98     if (!(r = res_get(common_resource, nm = strconcat(name, ".",
99         "nicefill", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
100     {
101         log(LOG_FATAL, "Failed to locate resource %s", nm);
102         return 0;
103     }
104     if (num < new->num_types)
105     {
106         log(LOG_FATAL, "Not enough elements in %s", nm);
107         return 0;
108     }
109     for (i = 0; i < num; i++)
110     {
111         if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
112         {
113             log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
114             return 0;
115         }
116         new->types[i].nice_keys_block = tmp;
117     }
118
119     /* determine max keys/blocksize */
120     if (!(r = res_get(common_resource, nm = strconcat(name, ".",
121         "maxkeys", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
122     {
123         log(LOG_FATAL, "Failed to locate resource %s", nm);
124         return 0;
125     }
126     if (num < new->num_types -1)
127     {
128         log(LOG_FATAL, "Not enough elements in %s", nm);
129         return 0;
130     }
131     for (i = 0; i < num; i++)
132     {
133         if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
134         {
135             log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
136             return 0;
137         }
138         new->types[i].max_keys = tmp;
139     }
140     return new;
141 }
142
143 /*
144  * Close isam file.
145  */
146 int is_close(ISAM is)
147 {
148     log(LOG_DEBUG, "is_close()");
149     log(LOG_LOG, "is_close needs to close individual files.");
150     xfree(is);
151     return 0;
152 }