source: tags/ms_r17q2/ARBDB/gb_main.h

Last change on this file was 15640, checked in by westram, 7 years ago
File size: 7.0 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : gb_main.h                                         //
4//   Purpose   : GB_MAIN_TYPE                                      //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#ifndef GB_MAIN_H
12#define GB_MAIN_H
13
14#ifndef GB_LOCAL_H
15#include "gb_local.h"
16#endif
17#ifndef GB_CB_H
18#include "gb_cb.h"
19#endif
20
21// ------------------------------
22//      forward declare types
23
24struct g_b_undo_mgr;
25struct gb_close_callback_list;
26struct gb_user;
27struct gb_project;
28struct gb_Key;
29struct gb_server_data;
30struct gb_hierarchy_callback_list;
31class  gb_hierarchy_location;
32
33// --------------------------------------------------------------------------------
34
35enum gb_open_types {
36    gb_open_all             = 0,
37    gb_open_read_only_all   = 16,
38    gb_open_read_only_small = 17
39};
40
41struct gb_quick_save {
42    char *quick_save_disabled;                      // if set, quick save is not possible and text describes reason why
43    int   last_index;
44
45    gb_quick_save()
46        : quick_save_disabled(NULL),
47          last_index(0)
48    {}
49};
50
51// --------------------------------------------------------------------------------
52
53#if defined(DEBUG)
54// #define GEN_CACHE_STATS // unit tests will fail if enabled
55#endif // DEBUG
56
57typedef uint16_t gb_cache_idx;
58
59struct gb_cache_entry;
60struct gb_cache : virtual Noncopyable {
61    gb_cache_entry *entries;
62
63    gb_cache_idx firstfree_entry;
64    gb_cache_idx newest_entry;
65    gb_cache_idx oldest_entry;
66   
67    size_t sum_data_size;
68    size_t max_data_size;
69    size_t big_data_min_size;
70
71#if defined(GEN_CACHE_STATS)
72    GB_HASH *not_reused; // key = DB_path, value = number of cache entries not reused
73    GB_HASH *reused;     // key = DB_path, value = number of cache entries reused
74    GB_HASH *reuse_sum;  // key = DB_path, value = how often entries were reused
75#endif
76
77    gb_cache();
78    ~gb_cache();
79};
80
81// --------------------------------------------------------------------------------
82//      root structure (one for each database)
83
84#define ALLOWED_KEYS  15000
85#define ALLOWED_DATES 256
86
87class GB_MAIN_TYPE : virtual Noncopyable {
88    inline GB_ERROR start_transaction() __ATTR__USERESULT;
89    GB_ERROR check_quick_save() const;
90    GB_ERROR initial_client_transaction() __ATTR__USERESULT;
91
92    int transaction_level;
93    int aborted_transaction;
94
95    bool i_am_server;
96
97    struct callback_group : virtual Noncopyable {
98        gb_hierarchy_callback_list *hierarchy_cbs; // defined hierarchy callbacks
99        gb_pending_callbacks        pending;       // collect triggered callbacks (will be called by commit; discarded by abort)
100
101        callback_group() : hierarchy_cbs(NULL) {}
102
103        inline void add_hcb(const gb_hierarchy_location& loc, const TypedDatabaseCallback& dbcb);
104        inline void remove_hcb(const gb_hierarchy_location& loc, const TypedDatabaseCallback& dbcb);
105        inline void forget_hcbs();
106
107        void trigger(GBDATA *gbd, GB_CB_TYPE type, gb_callback_list *dataCBs);
108    };
109
110    callback_group changeCBs; // all but GB_CB_DELETE
111    callback_group deleteCBs; // GB_CB_DELETE
112
113    friend class ArbDBWriter;
114
115public:
116
117    gbcmc_comm     *c_link;
118    gb_server_data *server_data;
119    GBCONTAINER    *dummy_father;
120    GBCONTAINER    *root_container;
121    GBCONTAINER    *gb_key_data;
122    char           *path;
123    gb_open_types   opentype;
124    char           *disabled_path;
125    int             allow_corrupt_file_recovery;
126
127    gb_quick_save qs;
128    gb_cache      cache;
129    int           compression_mask;
130
131    int      keycnt;                                // first non used key
132    long     sizeofkeys;                            // malloc size
133    long     first_free_key;                        // index of first gap
134    gb_Key  *keys;
135    GB_HASH *key_2_index_hash;
136    long     key_clock;                             // trans. nr. of last change
137    bool     mapped;                                // true -> loaded via mapfile
138
139    unsigned int last_updated;
140    long         last_saved_time;
141    long         last_saved_transaction;
142    long         last_main_saved_transaction;
143
144    GB_UNDO_TYPE requested_undo_type;
145    GB_UNDO_TYPE undo_type;
146
147    g_b_undo_mgr *undo;
148
149    char         *dates[ALLOWED_DATES];           // @@@ saved to DB, but never used
150    unsigned int  security_level;
151    int           old_security_level;
152    int           pushed_security_level;
153    long          clock;
154    GB_NUMHASH   *remote_hash;
155
156    GB_HASH *command_hash;
157
158    gb_close_callback_list *close_callbacks;
159
160    gb_user *users[GB_MAX_USERS];                   // user 0 is server
161    gb_user *this_user;
162
163    // --------------------
164
165private:
166    GBCONTAINER*& gb_main_ref() { return root_container; }
167
168    GB_ERROR check_saveable(const char *new_path, const char *flags) const;
169    GB_ERROR check_quick_saveable(const char *new_path, const char *flags) const {
170        GB_ERROR error = check_quick_save();
171        return error ? error : check_saveable(new_path, flags);
172    }
173public:
174
175    GB_MAIN_TYPE(const char *db_path);
176    ~GB_MAIN_TYPE();
177
178    void free_all_keys();
179    void release_main_idx();
180
181    int get_transaction_level() const { return transaction_level; }
182
183    GBDATA *gb_main() const { return (GBDATA*)root_container; }
184
185    GB_ERROR login_remote(const char *db_path, const char *opent) __ATTR__USERESULT;
186
187    inline GB_ERROR begin_transaction() __ATTR__USERESULT;
188    inline GB_ERROR commit_transaction() __ATTR__USERESULT;
189    inline GB_ERROR abort_transaction() __ATTR__USERESULT;
190
191    inline GB_ERROR push_transaction() __ATTR__USERESULT;
192    inline GB_ERROR pop_transaction() __ATTR__USERESULT;
193
194    inline GB_ERROR no_transaction();
195
196    __ATTR__USERESULT GB_ERROR send_update_to_server(GBDATA *gbd) __ATTR__USERESULT;
197
198    GB_ERROR save_quick(const char *refpath);
199
200    GB_ERROR save_as(const char *as_path, const char *savetype);
201    GB_ERROR save_quick_as(const char *as_path);
202
203    GB_ERROR panic_save(const char *db_panic);
204
205    void mark_as_server() { i_am_server = true; }
206
207    bool is_server() const { return i_am_server; }
208    bool is_client() const { return !is_server(); }
209
210    void call_pending_callbacks();
211
212    bool has_pending_change_callback() const { return changeCBs.pending.pending(); }
213    bool has_pending_delete_callback() const { return deleteCBs.pending.pending(); }
214
215    GB_ERROR add_hierarchy_cb(const gb_hierarchy_location& loc, const TypedDatabaseCallback& dbcb);
216    GB_ERROR remove_hierarchy_cb(const gb_hierarchy_location& loc, const TypedDatabaseCallback& dbcb);
217    void forget_hierarchy_cbs();
218
219    inline void trigger_change_callbacks(GBDATA *gbd, GB_CB_TYPE type);
220    void trigger_delete_callbacks(GBDATA *gbd);
221};
222
223#else
224#error gb_main.h included twice
225#endif // GB_MAIN_H
226
Note: See TracBrowser for help on using the repository browser.