Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / index / dyn_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: dyn_index.cc /main/5 1996/07/18 14:34:38 drk $
25  *
26  * Copyright (c) 1993 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
52 #include "index/dyn_index.h"
53
54 #ifdef C_API
55 #define v_static_key (*v_static_key_ptr)
56 #endif
57
58
59 dyn_index::dyn_index(c_code_t x) : c_index(x)
60 {
61    v_inv_lists_hd = 0;
62    v_idx_agent_ptr = 0;
63 }
64
65
66 dyn_index::~dyn_index()
67 {
68 ////////////////////////////////////////////////////////
69 // Need not to delete index object handlers, as they 
70 // are handled by the object cache.  qfc 6-17-93
71 ////////////////////////////////////////////////////////
72 }
73  
74 data_t* dyn_index::hd_to_key(const handler& t)
75 {
76    get_key_string(t);
77 /*
78 MESSAGE(cerr, "dyn_index::hd_to_key():");
79 if ( v_static_key.get() )
80    debug(cerr, v_static_key.get());
81 MESSAGE(cerr, "************");
82 */
83
84    return new data_t(v_static_key.get(), v_static_key.size());
85 }
86
87 Boolean 
88 dyn_index::insert_key_loc(const handler& t, const oid_t& id) 
89 {
90
91 //MESSAGE(cerr, "dyn_index::insert_key_loc()");
92 //id.asciiOut(cerr); cerr << "\n";
93
94
95    data_t* intKey = hd_to_key(t);
96
97    _insert_loc( *intKey, id );
98
99    delete intKey;
100    return true;
101 }
102    
103 Boolean 
104 dyn_index::insert_key_loc(const oid_t& t, const oid_t& id) 
105 {
106     data_t intKey(int(t.icode()), voidPtr(-1));
107     return _insert_loc( intKey, id );
108 }
109
110 Boolean 
111 dyn_index::_insert_loc(data_t& intKey, const oid_t& id) 
112 {
113 /*
114 MESSAGE(cerr, "in dyn_index insert()");
115 debug(cerr, intKey);
116 debug(cerr, id);
117 debug(cerr, pos);
118 */
119
120     if ( v_idx_agent_ptr -> member( intKey ) == true ) {
121
122        int hash = int(long((intKey.dt)));
123
124 //MESSAGE(cerr, "update inv list");
125 //debug(cerr, hash);
126
127 /**************************/
128 // just update the inv list
129 /**************************/
130        oid_list_handler* list_hd = 
131             (*v_inv_lists_hd) -> get_list(hash);
132
133        if ( list_hd == 0 ) 
134           throw(stringException("NULL oidlist ptr"));
135
136 /*
137 MESSAGE(cerr, "insert to list before");
138 (*list_hd) -> asciiOut(cerr); cerr << "\n";
139 */
140
141        (*list_hd) -> insert_component(id);
142        //list_hd -> commit();
143
144 /*
145 MESSAGE(cerr, "insert to list after");
146 (*list_hd) -> asciiOut(cerr); cerr << "\n";
147 */
148       
149        delete list_hd;
150
151
152     } else {
153 //MESSAGE(cerr, "create inv list");
154
155 /**************************/
156 // create a new inv list
157 /**************************/
158        oid_list list;
159        list.insert_component(id);
160
161        (*v_inv_lists_hd) -> append_list(list);
162
163        intKey.dt = voidPtr((*v_inv_lists_hd) -> count());
164 //debug(cerr, int(intKey.dt));
165
166        v_idx_agent_ptr -> insert( intKey );
167
168     }
169 /*
170 MESSAGE(cerr, "dyn_index::_insert_loc(): ");
171 v_idx_agent_ptr -> asciiOut(cerr);
172 MESSAGE(cerr, "=========================");
173 */
174
175     return true;
176 }
177    
178 Boolean 
179 dyn_index::remove_loc(handler& t, const oid_t& id)
180 {
181    data_t* intKey = hd_to_key(t);
182
183    if ( intKey == 0 ) 
184       return false;
185    else {
186       Boolean ok = _remove_loc(*intKey, id);
187       delete intKey;
188       return ok;
189    }
190 }
191
192 Boolean 
193 dyn_index::remove_loc(const oid_t& t, const oid_t& id) 
194 {
195    data_t intKey(int(t.icode()), voidPtr(-1));
196    return _remove_loc(intKey, id);
197 }
198
199 Boolean dyn_index::_remove_loc(data_t& intKey, const oid_t& id) 
200 {
201 //MESSAGE(cerr, "_remove_loc()");
202
203    Boolean ok = v_idx_agent_ptr -> member( intKey );
204
205    if ( ok == false ) return true; // no key in the hash table.
206
207 //MESSAGE(cerr, "in hash table");
208    int hash = int(long(intKey.dt));
209
210    oid_list_handler *list_hd = 
211         (*v_inv_lists_hd) -> get_list(hash);
212
213    if ( list_hd == 0 )
214        throw(stringException("NULL oid list ptr"));
215
216 //MESSAGE(cerr, "remove_component():");
217    (*list_hd) -> remove_component(id);
218
219    //list_hd -> commit();
220
221 //MESSAGE(cerr, "remove_component() done");
222
223    delete list_hd;
224     
225 //MESSAGE(cerr, "_remove_loc() done");
226    return true;
227 }
228
229 Boolean dyn_index::update_streampos(handler& /* old_obj */,     
230                                     handler& /* new_obj */,
231                                     mmdb_pos_t /* pos */
232                                    ) 
233 {
234    return false;
235 }
236
237 oid_list_handler* dyn_index::get_loc_list(const oid_t& id)
238 {
239    data_t intKey((int)id.icode());
240
241    if ( v_idx_agent_ptr -> member( intKey ) == true ) {
242
243       return  (*v_inv_lists_hd) -> get_list(int(long(intKey.dt))); 
244
245    } else {
246
247       return 0;
248
249    }
250 }
251
252 oid_list_handler* dyn_index::get_loc_list(const handler& t)
253 {
254 //MESSAGE(cerr, "dyn_index::get_loc_list()");
255 //debug(cerr, (*v_inv_lists_hd) -> get_store() -> my_path());
256 //debug(cerr, (*v_inv_lists_hd) -> get_store() -> my_name());
257
258    if ( OK() == false ) {
259       MESSAGE(cerr, 
260               "dyn_index::get_loc_list(): bad index object status");
261       return 0;
262    }
263
264    data_t* intKey = 0;
265
266    if ( ( intKey = hd_to_key(t)) != 0 &&
267         v_idx_agent_ptr -> member( *intKey ) == true 
268       ) {
269 //MESSAGE(cerr, "in hash table");
270
271       int pos = int(long(intKey -> dt));
272 //debug(cerr, pos);
273       delete intKey;
274 //MESSAGE(cerr, "dyn_index::get_loc_list() done");
275
276       return  (*v_inv_lists_hd) -> get_list(pos); 
277
278    } else {
279 //MESSAGE(cerr, "not in hash table");
280       delete intKey;
281       return 0;
282    }
283 }
284
285 int dyn_index::invlist_length(handler& t) 
286 {
287    oid_list_handler* locs = get_loc_list(t);
288
289    if ( locs == 0 )
290       throw(stringException("NULL oid list ptr"));
291
292    return (*locs) -> count();
293 }
294
295 io_status dyn_index::asciiOut(ostream& )
296 {
297    return done;
298 }
299