Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / storage / page_storage.h
1 /*
2  * $XConsortium: page_storage.h /main/7 1996/07/18 14:55:30 drk $
3  *
4  * Copyright (c) 1993 HAL Computer Systems International, Ltd.
5  * All rights reserved.  Unpublished -- rights reserved under
6  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
7  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
8  * OR DISCLOSURE.
9  * 
10  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
11  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
12  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
13  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
14  * INTERNATIONAL, LTD.
15  * 
16  *                         RESTRICTED RIGHTS LEGEND
17  * Use, duplication, or disclosure by the Government is subject
18  * to the restrictions as set forth in subparagraph (c)(l)(ii)
19  * of the Rights in Technical Data and Computer Software clause
20  * at DFARS 252.227-7013.
21  *
22  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
23  *                  1315 Dell Avenue
24  *                  Campbell, CA  95008
25  * 
26  */
27
28
29 #ifndef _page_storage_h
30 #define _page_storage_h 1
31
32 #define NUM_PAGES 10
33
34 #include "utility/pm_random.h"
35 #include "dstr/heap.h"
36 #include "dstr/void_ptr_array.h"
37 #include "dynhash/imp_die.h"
38 #include "storage/unixf_storage.h"
39 #include "storage/heap_comp_funcs.h"
40 #include "storage/page_cache.h"
41 #include "storage/spointer.h"
42
43 enum direction { positive, negative };
44
45 class page;
46
47 class str_index_record_t : public root
48 {
49
50 public:
51    int str_offset;
52    mmdb_pos_t loc;
53
54    str_index_record_t(int offset, mmdb_pos_t lc = 0);
55    virtual ~str_index_record_t() {};
56 };
57
58 void delete_str_index_record(const void* str_index_record_ptr);
59
60 typedef str_index_record_t* str_index_record_tPtr;
61
62 ////////////////////////////////////////////
63 ////////////////////////////////////////////
64 class store_trans
65 {
66
67 protected:
68    enum trans_t { ENABLED, DISABLED };
69
70    int page_sz;
71    int max_pages;
72    trans_t status;
73    unixf_storage* log_store;
74    imp_die* log_index;
75
76    char* path;
77    char* name;
78
79 public:
80    store_trans(char* path, char*name, int page_sz); 
81    ~store_trans();
82
83    void init(rep_policy*);
84    void quit();
85
86    void set_max_pages(int max_pgs) {
87       max_pages = max_pgs;
88    };
89
90    friend class page_storage;
91 };
92
93 /******************************************************/
94 // cached page storage. Implemented on Unix file class.
95 /******************************************************/
96
97 #ifdef C_API
98 #define f_global_pcache (*f_global_pcache_ptr)
99 #endif
100
101
102 class page_storage : public abs_storage 
103 {
104
105 protected:
106    buffer* v_buf;          // aux. buf.
107
108    int page_sz ;           // page size
109
110    static int dv_sz ;
111    static int abs_off ;
112
113 #ifndef C_API
114    static page_cache_global_part f_global_pcache;
115 #else
116    static page_cache_global_part* f_global_pcache_ptr;
117 #endif
118
119    page_cache_local_part f_local_pcache;
120
121    int total_pages;
122
123    int pagings ;
124    int total_page_access;
125
126    store_trans trans_info;
127
128 // byte order 
129    int v_server_order;
130    int v_db_order;
131
132 protected:
133
134    Boolean seek_loc_negative(mmdb_pos_t& loc, int smd);
135    Boolean seek_loc_positive(mmdb_pos_t& loc, int smd);
136
137 public:
138    page_storage(char* path, char* name,
139                 unixf_storage* store, 
140                 int page_sz = PAGSIZ, 
141                 int num_cached_pages = NUM_PAGES,
142                 mmdb_byte_order_t db_order_when_create_store = mmdb_big_endian
143                );
144    virtual ~page_storage();
145
146    void remove(); // remove all pages in the store
147
148    void sync();
149    void sync(int pagenum);
150    void sync(page*);
151
152    void begin_trans();
153    void commit_trans();
154    void roll_back();
155    void save_to_log(page* page_ptr);
156
157 // get server and db order
158    int server_order() { return v_server_order; } ;
159    int db_order() { return v_db_order; };
160
161
162 // i/o functions
163    int readString (mmdb_pos_t loc, char* base, int len, int str_offset = 0); 
164    int get_str_ptr(mmdb_pos_t loc, char*&, int& len) ;
165
166    int updateString(mmdb_pos_t loc, const char* base, int len, int string_ofst = 0, Boolean flush = false);
167    int deleteString (mmdb_pos_t loc, Boolean flush = false);
168    int insertString(mmdb_pos_t& loc, const char* base, int len, Boolean flush = false);
169    int allocString (mmdb_pos_t& loc, int len, char*&, int mode = 0);
170    int appendString(mmdb_pos_t loc, const char*, int len, Boolean flush_opt = false);
171
172    int set_page_dirty(mmdb_pos_t loc);
173
174 // iteration functions
175    typedef enum access { READ, WRITE } access_t;
176    int first() const;             // first page's index in the store
177    page* operator()(int page_num, enum access intent) ;    //get the page
178    void next(int&) const;         // next page's index. 
179
180
181 // format the store to contain extra empty 'pages' pages
182    int add_page_frames(int pages);   
183
184
185 // get locs of the pieces that a long string is broken into
186 // The array should be deleted after use.
187    int get_str_locs(mmdb_pos_t str_loc, str_index_record_tPtr*& locs, int& vector_leng);
188
189 // seek loc to (loc +/- 1) position 
190    Boolean seek_loc(mmdb_pos_t& loc, const direction = positive, int = spointer_t::FIRST_RECD );
191    mmdb_pos_t first_loc(); // return first loc in the store
192    mmdb_pos_t last_loc(); // return last loc in the store
193
194 // store status query functions
195    int page_size() { return page_sz; }; 
196
197 //
198    void set_page_size(int pgsz) { page_sz = pgsz; }; 
199
200 // how many pages in total in the store
201    int pages() const { return total_pages; }; 
202
203 // paging counting function
204    void reset_paging_count();
205    int paging_count() const;
206
207    Boolean io_mode(int mode) ;
208
209 // return an aux. buf.
210    buffer& aux_buf();
211
212 // printing functions
213    virtual io_status asciiOut(ostream&) ;
214  
215    friend void close_file(const void*);
216    friend void remove_from_global_cache(const void*);
217
218    friend class storage_mgr_t;
219    friend class handler;
220    friend class page_cache_local_part;
221    friend class page_cache_global_part;
222
223 #ifdef C_API
224    friend void initialize_MMDB();
225    friend void quit_MMDB();
226 #endif
227 };
228
229 typedef page_storage* page_storagePtr;
230
231 #endif