Use cache in dict - not in bfile.
[idzebra-moved-to-github.git] / dict / insert.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: insert.c,v $
7  * Revision 1.2  1994-08-17 13:32:19  adam
8  * Use cache in dict - not in bfile.
9  *
10  * Revision 1.1  1994/08/16  16:26:48  adam
11  * Added dict.
12  *
13  */
14
15 #include <stdlib.h>
16 #include <string.h>
17 #include <stdio.h>
18 #include <assert.h>
19
20 #include <dict.h>
21
22 static Dict_ptr new_page (Dict dict, Dict_ptr back_ptr, void **pp)
23 {
24     void *p;
25     Dict_ptr ptr = dict->head.free_list;
26     if (dict->head.free_list == dict->head.last)
27     {
28         dict->head.free_list++;
29         dict->head.last = dict->head.free_list;
30         dict_bf_newp (dict->dbf, ptr, &p);
31     }
32     else
33     {
34         dict_bf_readp (dict->dbf, dict->head.free_list, &p);
35         dict->head.free_list = DICT_nextptr(p);
36         if (dict->head.free_list == 0)
37             dict->head.free_list = dict->head.last;
38     }
39     assert (p);
40     DICT_type(p) = 1;
41     DICT_backptr(p) = back_ptr;
42     DICT_nextptr(p) = 0;
43     DICT_nodir(p) = 0;
44     DICT_size(p) = 0;
45     *pp = p;
46     return ptr;
47 }
48
49 static int dict_ins (Dict dict, const Dict_char *str, Dict_ptr back_ptr,
50                      void *p, void *userinfo)
51 {
52     Dict_ptr ptr = back_ptr, subptr;
53     short *indxp, *indxp1, *indxp2;
54     short newsize;
55     if (ptr == 0)
56         ptr = new_page (dict, back_ptr, &p);
57     assert (p);
58     assert (ptr);
59
60     indxp = (short*) ((char*) p+DICT_PAGESIZE);
61     while (*str != DICT_EOS)
62     {
63         char *info;
64         if (*--indxp > 0) /* tail string here! */
65         {
66             int cmp;
67             info = DICT_info(p) + *indxp;
68             cmp = dict_strcmp ((Dict_char*)
69                               (info+sizeof(Dict_info)+sizeof(Dict_ptr)),
70                                str);
71             if (!cmp)
72             {
73                 if (memcmp (info+sizeof(Dict_ptr), userinfo, sizeof(userinfo)))
74                 {
75                     memcpy (info+sizeof(Dict_ptr), userinfo, sizeof(userinfo));
76                     dict_bf_touch (dict->dbf, ptr);
77                 }
78                 return 0;
79             }
80             else if(cmp < 0)
81                 break;
82             
83         }
84         else if(*indxp < 0)  /* tail of string in sub page */
85         {
86             int cmp;
87             info = DICT_info(p) - *indxp;
88             cmp = memcmp (info+sizeof(Dict_info)+sizeof(Dict_ptr), str, 
89                          sizeof(Dict_char));
90             if (!cmp)
91             {
92                 Dict_ptr subptr;
93                 void *pp;
94                 memcpy (&subptr, info, sizeof(subptr));
95                 if (subptr == 0)
96                 {
97                     subptr = new_page (dict, ptr, &pp);
98                     memcpy (info, &subptr, sizeof(subptr));
99                     dict_bf_touch (dict->dbf, ptr);
100                 }
101                 return dict_ins (dict, str+1, ptr, pp, userinfo);
102             }
103             else if(cmp < 0)
104                 break;
105         }
106         else
107             break;
108     }
109     newsize = DICT_size(p);
110     subptr = 0;
111     memcpy (DICT_info(p) + newsize, &subptr, sizeof(subptr));
112     memcpy (DICT_info(p) + newsize + sizeof(Dict_ptr), userinfo,
113             sizeof(Dict_info));
114     memcpy (DICT_info(p) + newsize + sizeof(Dict_ptr)+sizeof(Dict_info),
115             str, dict_strlen (str));
116     newsize = DICT_size(p) +
117         sizeof(Dict_info) + sizeof(Dict_ptr) + dict_strlen (str);
118     DICT_size (p) = newsize;
119
120     DICT_nodir(p) = DICT_nodir(p)+1;
121     indxp2 = (short*)((char*) p + DICT_PAGESIZE - DICT_nodir(p)*sizeof(short));
122     for (indxp1 = indxp2; indxp1 != indxp; indxp1++)
123         indxp[0] = indxp[1];
124     *indxp = -newsize;
125     return 0;
126 }
127
128 int dict_insert (Dict dict, const Dict_char *str, void *userinfo)
129 {
130     dict_ins (dict, str, 0, NULL, userinfo);
131     return 0;
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145