Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / index / mphf_index.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * $XConsortium: mphf_index.cc /main/5 1996/07/18 14:38:37 drk $
25  *
26  * Copyright (c) 1992 HAL Computer Systems International, Ltd.
27  * All rights reserved.  Unpublished -- rights reserved under
28  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
29  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
30  * OR DISCLOSURE.
31  * 
32  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
34  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
36  * INTERNATIONAL, LTD.
37  * 
38  *                         RESTRICTED RIGHTS LEGEND
39  * Use, duplication, or disclosure by the Government is subject
40  * to the restrictions as set forth in subparagraph (c)(l)(ii)
41  * of the Rights in Technical Data and Computer Software clause
42  * at DFARS 252.227-7013.
43  *
44  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
45  *                  1315 Dell Avenue
46  *                  Campbell, CA  95008
47  * 
48  */
49
50
51 #include "index/mphf_index.h"
52
53 #ifdef C_API
54 #define v_static_key (*v_static_key_ptr) 
55 #endif
56
57
58 mphf_index::mphf_index() : c_index(MPHF_INDEX_CODE), f_key_file_name(0)
59 {
60 //MESSAGE(cerr, " mphf_index::mphf_index():");
61 //debug(cerr, (void*)this);
62
63    v_mphf = 0;
64    v_inv_lists_hd = 0;
65 }
66
67 void mphf_index::init_data_member(fast_mphf_handler* mp, 
68                                   inv_lists_handler* inv) 
69 {
70 /*
71 MESSAGE(cerr, " mphf_index::init_data_member():");
72 debug(cerr, (void*)v_mphf);
73 debug(cerr, (void*)(v_mphf->operator->()));
74 */
75    v_mphf = mp;
76    v_inv_lists_hd = inv;
77    set_mode(HEALTH, true);
78 }
79
80    
81 mphf_index::~mphf_index()
82 {
83    delete f_key_file_name;
84
85 ////////////////////////////////////////////////////////
86 // Need not to delete index object handlers, as they 
87 // are handled by the object cache.  qfc 6-17-93
88 ////////////////////////////////////////////////////////
89 }
90
91 Boolean 
92 mphf_index::insert_key_loc(const oid_t& id, const oid_t& loc) 
93 {
94     get_key_string(id);
95
96     *v_key_loc_pair_out << v_static_key << "\n";
97     loc.asciiOut(*v_key_loc_pair_out);
98     *v_key_loc_pair_out << "\n";
99
100     return true;
101 }
102
103 Boolean 
104 mphf_index::insert_key_loc(const handler& hd, const oid_t& loc) 
105 {
106     get_key_string(hd);
107
108     *v_key_loc_pair_out << v_static_key << "\n";
109     loc.asciiOut(*v_key_loc_pair_out);
110     *v_key_loc_pair_out << "\n";
111
112     return true;
113 }
114
115 Boolean mphf_index::remove_loc(const oid_t&, const oid_t&)
116 {
117    throw(stringException("mphf_index::remove_index(): not applicable"));
118    return false;
119 }
120
121 Boolean mphf_index::remove_loc(handler&, const oid_t&)
122 {
123    throw(stringException("remove_index(): not applicable"));
124    return false;
125 }
126
127 Boolean mphf_index::update_streampos(handler& ,
128                                      handler& ,
129                                      mmdb_pos_t
130                                     ) 
131 {
132    throw(stringException("update_index(): not applicable"));
133    return false;
134 }
135
136
137 Boolean mphf_index::load(const char* key_set)
138 {
139 /*
140 MESSAGE(cerr, " mphf_index::load():");
141 debug(cerr, (void*)this);
142 debug(cerr, (void*)(v_mphf->operator->()));
143 */
144
145    (*v_mphf) -> build(key_set);
146
147    fstream in(key_set, ios::in);
148    
149    v_sz = (*v_mphf) -> keysetsize();
150    (*v_inv_lists_hd) -> expand_space(v_sz);
151    
152    key_type key(LBUFSIZ);
153    
154 #ifdef DEBUG
155    int i = 0;
156 #endif
157
158    while ( in >> key ) {
159
160       oid_t key_id(c_code_t(0), 0);
161    
162       key_id.asciiIn(in);
163
164 #ifdef DEBUG
165 if ( i % 1000 == 0 && i != 0 ) {
166 cerr << i;
167 MESSAGE(cerr, " keys processed");
168 MESSAGE(cerr, "========");
169 }
170 i++;
171 #endif
172
173
174       int hash = (*v_mphf) -> hashTo(key);
175    
176       (*v_inv_lists_hd) -> insert_to_list(hash+1, key_id);
177
178    }
179    
180 #ifndef MPHF_DEBUG
181    del_file(key_set, 0);
182 #endif
183    
184    set_mode(HEALTH, true);
185    set_mode(UPDATE, true);
186    
187    return true;
188 }
189
190 oid_list_handler* mphf_index::get_loc_list(const oid_t& ) 
191 {
192    MESSAGE(cerr, "get_loc_list(): not implemented yet"); 
193    return 0;
194 }
195
196 int mphf_index::handler_to_inv_idx(const handler& t) 
197 {
198    get_key_string(t);
199 //debug(cerr, v_static_key);
200    return (*v_mphf) -> hashTo(v_static_key);
201 }
202
203 oid_list_handler* mphf_index::get_loc_list(const handler& t) 
204 {
205    return (*this)(handler_to_inv_idx(t) + 1) ;
206 }
207
208 int mphf_index::invlist_length(handler& t) 
209 {
210    oid_list_handler* x = get_loc_list(t);
211    return (*x) -> count();
212 }
213
214 ostream& operator << (ostream&s, mphf_index& v)
215 {
216    int ind = v.first();
217    while (ind != 0) {
218      v.next(ind);
219    }
220    return s;
221 }
222
223 Boolean mphf_index::batch_index_begin()
224 {
225 /*
226 MESSAGE(cerr, "batch_index_begin()");
227 debug(cerr, cmp_selector);
228 debug(cerr, count());
229 debug(cerr, int(this));
230 */
231
232    if ( count() > 0 ) {
233       throw(stringException("batch_index_begin(): index exists"));
234    } 
235
236    f_key_file_name = 
237      strdup(form("%s/keys.%d_%d", 
238                 get_store() -> my_path(), f_oid.ccode(), f_oid.icode()));
239
240    v_key_loc_pair_out = new fstream(f_key_file_name, ios::out);
241
242    return (( *v_key_loc_pair_out ) ?  true : false );
243 }
244
245 Boolean mphf_index::batch_index_end()
246 {
247    v_key_loc_pair_out -> close();
248    delete v_key_loc_pair_out;
249
250    return load(f_key_file_name);
251 }
252
253 oid_t mphf_index::first_of_invlist(int ind)
254 {
255    oid_t x((*v_inv_lists_hd) -> operator()(ind+1));
256
257    if ( x.ccode() == OID_LIST_CODE ) {
258       oid_list_handler* y = (*this)(ind);
259       if ( (*y) -> count() > 0 ) {
260          return (*y) -> operator()(1);
261       } else {
262          throw(stringException("first_of_invlist(): empty list"));
263       }
264    } else {
265       return x;
266    }
267 }
268
269 oid_t mphf_index::first_of_invlist(const handler& t)
270 {
271    int hash = handler_to_inv_idx(t);
272 /*
273 debug(cerr, hash);
274 */
275
276
277    oid_t x((*v_inv_lists_hd) -> operator()(hash+1));
278
279 //debug(cerr, x);
280
281    if ( x.ccode() == OID_LIST_CODE ) {
282       oid_list_handler* y = (*this)(hash);
283       if ( (*y) -> count() > 0 ) {
284          return (*y) -> operator()(1);
285       } else {
286          throw(stringException("first_of_invlist(): empty list"));
287       }
288    } else {
289       return x;
290    }
291 }
292
293 MMDB_BODIES(mphf_index)
294 HANDLER_BODIES(mphf_index)