2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
24 * $XConsortium: template_mgr.C /main/10 1996/10/03 18:50:20 drk $
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
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
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.
44 * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
51 #include "mgrs/template_mgr.h"
53 #include "object/root.h"
54 #include "object/oid.h"
55 #include "object/integer.h"
56 #include "object/pstring.h"
57 #include "object/compressed_pstring.h"
58 #include "object/tuple.h"
59 #include "object/short_list.h"
60 #include "object/cset.h"
61 #include "index/mphf_index.h"
62 #include "index/btree_index.h"
63 #include "index/dyn_memory_index.h"
64 #include "index/dyn_disk_index.h"
65 #include "object/dl_list_cell.h"
66 #include "object/dl_list.h"
67 #include "compression/zip.h"
68 #include "compression/huffman.h"
69 #include "compression/lzss.h"
71 #include "api/transaction.h"
73 extern memory_pool g_memory_pool;
76 #define CLASS_CODE_BYTES ll4(sizeof(c_code_t))
78 #define CLASS_CODE_BYTES sizeof(c_code_t)
81 #define TEMP_OBJ_NUMS 18
82 static rootPtr template_obj_table[TEMP_OBJ_NUMS] ;
86 buffer* cdr_io_buf_ptr = 0;
87 #define cdr_io_buf (*cdr_io_buf_ptr)
89 static buffer cdr_io_buf(LBUFSIZ);
93 template_mgr_t::template_mgr_t() : v_template_objs(32801, 20)
96 if ( cdr_io_buf_ptr == 0 )
97 cdr_io_buf_ptr = new buffer(LBUFSIZ);
100 template_obj_table[0] = ::new compressed_pstring;
101 template_obj_table[1] = ::new oid;
102 template_obj_table[2] = ::new integer;
103 template_obj_table[3] = ::new pstring;
104 template_obj_table[4] = ::new tuple;
105 template_obj_table[5] = ::new oid_list;
106 template_obj_table[6] = ::new fast_mphf;
107 template_obj_table[7] = ::new inv_lists;
108 template_obj_table[8] = ::new mphf_index;
109 template_obj_table[9] = ::new btree_index;
110 template_obj_table[10] = ::new dyn_memory_index;
111 template_obj_table[11] = ::new dyn_disk_index;
112 template_obj_table[12] = ::new cset;
113 template_obj_table[13] = ::new short_list;
114 template_obj_table[14] = ::new dl_list_cell;
115 template_obj_table[15] = ::new dl_list;
116 template_obj_table[16] = ::new huff;
117 template_obj_table[17] = ::new lzss;
119 for ( int i=0; i<TEMP_OBJ_NUMS; i++ ) {
120 insert_template(template_obj_table[i]);
124 template_mgr_t::~template_mgr_t()
126 for ( int i=0; i<TEMP_OBJ_NUMS; i++ ) {
127 ::delete template_obj_table[i];
131 delete cdr_io_buf_ptr;
136 root* template_mgr_t::look_up(c_code_t c_code)
138 data_t key_data(c_code, 0);
139 if ( v_template_objs.member(key_data) == true &&
140 long(key_data.dt) != -1 ) {
141 return (root*)(key_data.dt);
149 template_mgr_t::_peek_slot(abs_storage* store, mmdb_pos_t pos,
154 if ( store == 0 || store -> OK() == false ) {
155 throw(stringException("template_mgr_t::_peek_obj() store in bad shape"));
158 //first read in the oid_t string.
160 if ( store -> get_str_ptr(pos, z, len) != 0 ) {
161 throw(stringException("_peek_obj(): can't get oid_t string"));
165 /*cerr << "len=" << len << endl;
166 for (int i=0; i<len; i++)
167 cerr << int(z[i]) << " ";
172 if ( len < (int) CLASS_CODE_BYTES || z == 0 )
173 throw(stringException("_peek_obj(): corrupted data"));
176 memcpy((char*)&class_code, z, CLASS_CODE_BYTES);
177 //debug(cerr, class_code);
180 if ( store -> swap_order() == true ) {
181 //MESSAGE(cerr, "swap class code order");
182 ORDER_SWAP_USHORT(class_code);
186 //debug(cerr, class_code);
191 c_code_t template_mgr_t::peek_slot(abs_storage* store, mmdb_pos_t pos)
193 char* z = 0; int len = 0;
194 c_code_t class_code = _peek_slot(store, pos, z, len);
195 RESET_BIT(class_code, CDR_FLAG);
199 /******************************************************/
200 // return an object in x. The object is fetched from
201 // store 'store' at location 'pos'.
202 // If the object has been previously deleted, an
203 // exception will be thrown.
204 /******************************************************/
206 template_mgr_t::init_obj(abs_storage* store, mmdb_pos_t pos, root*& x)
208 //debug(cerr, store -> my_name());
213 char* z = 0; int len = 0;
214 c_code_t class_code = _peek_slot(store, pos, z, len);
219 store->get_str_ptr(pos, dbg_char_ptr, dbg_int);
220 fprintf(stderr, "init_obj ptr=0x%lx len=%d\n", (long)dbg_char_ptr,dbg_int);
224 if ( class_code == 0 )
225 throw(stringException("init_obj(): class code == 0 "));
227 Boolean compacted_disk_rep =
228 ( BIT_TEST(class_code, CDR_FLAG) ) ? true : false;
230 //debug(cerr, int(compacted_disk_rep));
232 RESET_BIT(class_code, CDR_FLAG);
234 //debug(cerr, class_code);
235 //if ( pos == 16385 ) {
236 //MESSAGE(cerr, "STOP:");
239 rootPtr object_template = look_up(class_code);
241 if ( object_template == 0 ) {
242 debug(cerr, class_code);
243 debug(cerr, store -> my_name());
244 throw(stringException("init_obj(): can't find object template"));
247 /*****************************************/
248 // make sure that the string existing on
249 // the store is of sufficient length
250 /*****************************************/
252 int obj_len = len - CLASS_CODE_BYTES;
254 switch ( compacted_disk_rep ) {
259 ////////////////////////////////////
260 // compute and cache the cdr size
261 ////////////////////////////////////
262 if ( object_template -> get_cdr_size() == 0 ) {
264 object_template -> cdrOut(cdr_io_buf);
265 object_template -> set_cdr_size(cdr_io_buf.content_sz());
273 int cdr_sz = object_template -> get_cdr_size();
275 if ( obj_len != cdr_sz ) {
276 debug(cerr, obj_len);
278 debug(cerr, *(c_code_t*)z);
279 throw(stringException("corrupted data"));
286 if ( obj_len != object_template -> mem_sizeof() ) {
287 debug(cerr, obj_len);
288 debug(cerr, object_template -> mem_sizeof());
289 debug(cerr, *(c_code_t*)z);
290 throw(stringException("corrupted data"));
296 g_transac -> book(store);
299 //debug(cerr, int(store));
301 //debug(cerr, class_code);
303 persistent_info pinfo;
305 pinfo.storage = store;
306 pinfo.position = pos;
307 pinfo.persistent = true;
308 pinfo.old_object = true;
309 pinfo.class_code = class_code;
313 x = object_template -> cast_to( z + CLASS_CODE_BYTES );
315 //char* p = g_memory_pool.alloc( object_template -> mem_sizeof());
316 char* p = new char[object_template -> mem_sizeof()];
318 //MESSAGE(cerr, "template mgr: init case(), new char array");
319 //debug(cerr, (void*)p);
321 switch ( compacted_disk_rep ) {
324 memset((char*)p, char(0), object_template -> mem_sizeof());
327 memcpy((char*)p, z + CLASS_CODE_BYTES, obj_len);
331 x = object_template -> cast_to( p );
334 x -> init_persistent_info( &pinfo );
336 switch ( compacted_disk_rep ) {
340 buf.set_chunk(z+CLASS_CODE_BYTES, obj_len);
341 buf.set_content_sz(obj_len);
344 buf.set_swap_order(store -> swap_order());
354 x -> set_mode(UPDATE, false);
356 //x -> my_oid().asciiOut(cerr); cerr << "\n";
357 //MESSAGE(cerr, "template_mgr_t::init_obj() done");
363 template_mgr_t::commit_obj(abs_storage* store, root* obj_ptr)
365 if ( store && obj_ptr &&
366 obj_ptr -> get_mode(UPDATE) == true
370 MESSAGE(cerr, "COMMIT OBJECT");
371 debug(cerr, obj_ptr -> my_oid());
374 ////////////////////////////////
375 // update the object disk copy.
376 ////////////////////////////////
378 switch ( obj_ptr -> get_mode(CDR) ) {
383 cdr_io_buf.set_swap_order(store -> swap_order());
387 obj_ptr -> cdrOut(cdr_io_buf);
389 //debug(cerr, cdr_io_buf);
390 //debug(cerr, cdr_io_buf.content_sz());
393 updateString(mmdb_pos_t(obj_ptr -> my_oid().icode()),
394 cdr_io_buf.get_base(),
395 cdr_io_buf.content_sz(),
404 updateString(mmdb_pos_t(obj_ptr -> my_oid().icode()), (char*)obj_ptr,
405 obj_ptr -> mem_sizeof(),
411 obj_ptr -> set_mode(UPDATE, false);
419 template_mgr_t::quit_obj(abs_storage* store, root* obj_ptr)
421 //MESSAGE(cerr, "template_mgr_t::quit_obj()");
422 //debug(cerr, (void*)obj_ptr);
423 commit_obj(store, obj_ptr);
426 obj_ptr -> f_oid.become(ground);
433 /******************************************************/
434 // return an object in x. The object is
435 // saved on the store 'store'. Its location is the
436 // i_code of the object.
437 /******************************************************/
440 template_mgr_t::create_obj(abs_storage* store, c_code_t class_code, root*& new_obj)
442 //MESSAGE(cerr, "Create obj case");
443 //debug(cerr, class_code);
446 fprintf(stderr, "create_obj class_code=%d\n", class_code);
449 /*****************************************/
450 // Can we find a matched template object?
451 // The match is based on the class code.
452 /*****************************************/
453 root* object_template = look_up(class_code);
455 if ( object_template == 0 ) {
456 debug(cerr, class_code);
457 throw(stringException("get_obj(): unknown object instance"));
460 /*****************************************/
461 // ask the template to create a new object
463 /*****************************************/
465 char* store_str_ptr = 0;
467 #ifdef COMPACTED_DISK_REP
469 if ( object_template -> get_cdr_size() == 0 ) {
470 //////////////////////////////////
471 // compute and cache the cdr_size
472 //////////////////////////////////
474 object_template -> cdrOut(cdr_io_buf);
475 object_template -> set_cdr_size(cdr_io_buf.content_sz());
478 int disk_obj_len = object_template -> get_cdr_size();
481 int disk_obj_len = object_template -> mem_sizeof();
485 MESSAGE(cerr, "to allocate string");
486 debug(cerr, disk_obj_len);
487 debug(cerr, CLASS_CODE_BYTES);
491 g_transac -> book(store);
494 persistent_info pinfo;
497 store -> allocString (pinfo.position,
498 disk_obj_len + CLASS_CODE_BYTES,
499 store_str_ptr, spointer_t::IS_OBJECT
502 store -> allocString (pinfo.position,
503 disk_obj_len + CLASS_CODE_BYTES,
504 store_str_ptr, spointer_t::IS_OBJECT
510 store->get_str_ptr(pinfo.position, dbg_char_ptr, dbg_int);
511 fprintf(stderr, "create_obj ptr=0x%lx len=%d\n",
512 (long)dbg_char_ptr, dbg_int);
517 #ifdef COMPACTED_DISK_REP
518 SET_BIT(class_code, CDR_FLAG);
521 //debug(cerr, pinfo.position);
524 if ( store -> swap_order() == true ) {
525 //MESSAGE(cerr, "switch class code in create_obj");
526 c_code_t x = class_code;
528 ORDER_SWAP_USHORT(x);
530 memcpy(store_str_ptr, (char*)&x, sizeof(x));
532 //MESSAGE(cerr, "do not switch class code in create_obj");
533 //debug(cerr, class_code);
534 memcpy(store_str_ptr, (char*)&class_code, sizeof(class_code));
537 memcpy(store_str_ptr, (char*)&class_code, sizeof(class_code));
540 //debug(cerr, class_code);
541 //debug(cerr, long(store_str_ptr));
542 //for ( int i=0; i<sizeof(class_code); i++ )
543 // cerr << int(store_str_ptr[i]) << " ";
546 #ifdef COMPACTED_DISK_REP
547 RESET_BIT(class_code, CDR_FLAG);
550 pinfo.storage = store;
551 pinfo.old_object = false;
552 pinfo.persistent = true;
553 pinfo.class_code = class_code;
556 new_obj = object_template -> cast_to( store_str_ptr + CLASS_CODE_BYTES);
559 //MESSAGE(cerr, "to clean mem allocated");
561 int mem_obj_len = object_template -> mem_sizeof();
563 //char* p = g_memory_pool.alloc(mem_obj_len);
564 char* p = new char[mem_obj_len];
566 //MESSAGE(cerr, "template mgr: create case(), new char array");
567 //debug(cerr, (void*)p);
569 memset(p, char(0), mem_obj_len);
570 new_obj = object_template -> cast_to( p );
574 new_obj -> init_persistent_info( &pinfo );
576 #ifdef COMPACTED_DISK_REP
577 new_obj -> set_mode(CDR, true);
579 new_obj -> set_mode(CDR, false);
582 new_obj -> set_mode(OLD_OBJECT, true);
583 new_obj -> set_mode(UPDATE, true);
584 new_obj -> set_mode(HEALTH, true);
586 //debug(cerr, int(new_obj -> get_mode(BASE_DATA_INITED)));
587 //debug(cerr, int(new_obj -> get_mode(PERSISTENT)));
590 MESSAGE(cerr, "oid of the new created obj:");
591 debug(cerr, class_code);
592 cerr << "class_code=" << class_code ;
593 cerr << ", location=";
594 cerr << PAGE_ID((new_obj -> my_oid()).icode(), 8192) << ".";
595 cerr << PAGE_IDX((new_obj -> my_oid()).icode(), 8192) << "\n";
599 //MESSAGE(cerr, "create_obj done()");
604 Boolean template_mgr_t::destroy_obj(abs_storage* store, root* x)
607 //MESSAGE(cerr, "destroy_obj()");
608 //debug(cerr, x -> my_oid());
609 store -> deleteString( mmdb_pos_t(x -> my_oid().icode()) );
610 x -> f_oid.become(ground);
611 //MESSAGE(cerr, "destroy_obj() done");
617 Boolean template_mgr_t::insert_template(root* tmt)
619 if ( tmt == 0 ) return true;
621 c_code_t class_code = tmt -> my_oid().ccode();
623 if ( look_up(class_code) == 0 ) {
624 data_t key_data(class_code, tmt);
625 v_template_objs.insert(key_data);