5ae44021f4fbb220252bc10d3141ed460233ea8e
[idzebra-moved-to-github.git] / dict / dcompact.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: dcompact.c,v $
7  * Revision 1.6  1999-09-07 08:13:08  adam
8  * Removed log messages.
9  *
10  * Revision 1.5  1999/05/26 07:49:12  adam
11  * C++ compilation.
12  *
13  * Revision 1.4  1999/05/15 14:36:37  adam
14  * Updated dictionary. Implemented "compression" of dictionary.
15  *
16  * Revision 1.3  1999/05/12 13:08:06  adam
17  * First version of ISAMS.
18  *
19  * Revision 1.2  1999/03/09 16:27:49  adam
20  * More work on SDRKit integration.
21  *
22  * Revision 1.1  1999/03/09 13:07:06  adam
23  * Work on dict_compact routine.
24  *
25  */
26
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 #include <log.h>
33 #include <dict.h>
34
35 static void dict_copy_page(Dict dict, char *to_p, char *from_p, int *map)
36 {
37     int i, slen, no = 0;
38     short *from_indxp, *to_indxp;
39     char *from_info, *to_info;
40     
41     from_indxp = (short*) ((char*) from_p+DICT_bsize(from_p));
42     to_indxp = (short*) ((char*) to_p+DICT_bsize(to_p));
43     to_info = (char*) to_p + DICT_infoffset;
44     for (i = DICT_nodir (from_p); --i >= 0; )
45     {
46         if (*--from_indxp > 0) /* tail string here! */
47         {
48             /* string (Dict_char *) DICT_EOS terminated */
49             /* unsigned char        length of information */
50             /* char *               information */
51
52             from_info = (char*) from_p + *from_indxp;
53             *--to_indxp = to_info - to_p;
54             slen = (dict_strlen((Dict_char*) from_info)+1)*sizeof(Dict_char);
55             memcpy (to_info, from_info, slen);
56             from_info += slen;
57             to_info += slen;
58         }
59         else
60         {
61             Dict_ptr subptr;
62             Dict_char subchar;
63             /* Dict_ptr             subptr */
64             /* Dict_char            sub char */
65             /* unsigned char        length of information */
66             /* char *               information */
67
68             *--to_indxp = -(to_info - to_p);
69             from_info = (char*) from_p - *from_indxp;
70
71             memcpy (&subptr, from_info, sizeof(subptr));
72             subptr = map[subptr];
73             from_info += sizeof(Dict_ptr);
74             memcpy (&subchar, from_info, sizeof(subchar));
75             from_info += sizeof(Dict_char);
76                             
77             memcpy (to_info, &subptr, sizeof(Dict_ptr));
78             to_info += sizeof(Dict_ptr);
79             memcpy (to_info, &subchar, sizeof(Dict_char));
80             to_info += sizeof(Dict_char);
81         }
82         assert (to_info < (char*) to_indxp);
83         slen = *from_info+1;
84         memcpy (to_info, from_info, slen);
85         to_info += slen;
86         ++no;
87     }
88     DICT_size(to_p) = to_info - to_p;
89     DICT_type(to_p) = 0;
90     DICT_nodir(to_p) = no;
91 }
92
93 int dict_copy_compact (BFiles bfs, const char *from_name, const char *to_name)
94 {
95     int no_dir = 0;
96     Dict dict_from, dict_to;
97     int *map, i;
98     dict_from = dict_open (bfs, from_name, 0, 0, 0);
99     if (!dict_from)
100         return -1;
101     map = (int *) xmalloc ((dict_from->head.last+1) * sizeof(*map));
102     for (i = 0; i <= (int) (dict_from->head.last); i++)
103         map[i] = -1;
104     dict_to = dict_open (bfs, to_name, 0, 1, 1);
105     if (!dict_to)
106         return -1;
107     map[0] = 0;
108     map[1] = dict_from->head.page_size;
109     
110     for (i = 1; i < (int) (dict_from->head.last); i++)
111     {
112         void *buf;
113         int size;
114 #if 0
115         logf (LOG_LOG, "map[%d] = %d", i, map[i]);
116 #endif
117         dict_bf_readp (dict_from->dbf, i, &buf);
118         size = ((DICT_size(buf)+sizeof(short)-1)/sizeof(short) +
119                 DICT_nodir(buf))*sizeof(short);
120         map[i+1] = map[i] + size;
121         no_dir += DICT_nodir(buf);
122     }
123 #if 0
124     logf (LOG_LOG, "map[%d] = %d", i, map[i]);
125     logf (LOG_LOG, "nodir = %d", no_dir);
126 #endif
127     dict_to->head.root = map[1];
128     dict_to->head.last = map[i];
129     for (i = 1; i< (int) (dict_from->head.last); i++)
130     {
131         void *old_p, *new_p;
132         dict_bf_readp (dict_from->dbf, i, &old_p);
133
134         logf (LOG_LOG, "dict_bf_newp no=%d size=%d", map[i],
135               map[i+1] - map[i]);
136         dict_bf_newp (dict_to->dbf, map[i], &new_p, map[i+1] - map[i]);
137
138         DICT_type(new_p) = 0;
139         DICT_backptr(new_p) = map[i-1];
140         DICT_bsize(new_p) = map[i+1] - map[i];
141
142         dict_copy_page(dict_from, (char*) new_p, (char*) old_p, map);
143     }
144     dict_close (dict_from);
145     dict_close (dict_to);
146     return 0;
147 }