~vkolesnikov/pbxt/pbxt-07-diskfull

« back to all changes in this revision

Viewing changes to pbxt/src/xt_datalog.h

  • Committer: paul-mccullagh
  • Date: 2006-10-23 09:14:04 UTC
  • Revision ID: paul-mccullagh-918861e03d351978a9541168a96e58cc826734ee
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2005 SNAP Innovation GmbH
 
2
 *
 
3
 * PrimeBase XT
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * 2005-01-24   Paul McCullagh
 
20
 *
 
21
 * H&G2JCtL
 
22
 */
 
23
#ifndef __xt_datalog_h__
 
24
#define __xt_datalog_h__
 
25
 
 
26
#include "xt_filesys.h"
 
27
 
 
28
/*
 
29
 * We the size of a data log exceeds the rollover size, and the amount of
 
30
 * garbage in the file is greater than the garbage threshold, then
 
31
 * the garbage collector is activated.
 
32
 */
 
33
#define XT_GARBAGE_THRESHOLD    ((double) 30.0)
 
34
 
 
35
struct XTDatabase;
 
36
struct XTDataLog;
 
37
struct XTThread;
 
38
struct XTTable;
 
39
struct XTOpenTable;
 
40
 
 
41
/* The mode for opening a datalog depends in the value of "space_required" */
 
42
#define DL_MODE_NO_OPEN                 ((off_t) 0)
 
43
#define DL_MODE_GC_SOURCE               ((off_t) 1)
 
44
#define DL_MODE_EXCLUSIVE               ((off_t) 2)
 
45
#define DL_MODE_SHARED                  ((off_t) 3)
 
46
 
 
47
#define XT_DL_STATUS_DELETED    0
 
48
#define XT_DL_STATUS_RECORD             10
 
49
 
 
50
typedef struct XTDataLogHeader {
 
51
        XTDiskValue4                            lh_magic_4;
 
52
        XTDiskValue4                            lh_size_4;                      /* Must be set to sizeof(XTDataLogHeaderDRec). */
 
53
        XTDiskValue8                            lh_free_space_8;        /* The accumulated free space in this file. */
 
54
        XTDiskValue8                            lh_file_len_8;          /* The last confirmed correct file length (always set on close). */
 
55
} XTDataLogHeaderDRec, *XTDataLogHeaderDPtr;
 
56
 
 
57
typedef struct XTDataLogBuffer {
 
58
        xtWord1                                         lb_status_1;
 
59
        XTDiskValue4                            lb_data_size_4;         /* Size of this record data area only. */
 
60
        XTDiskValue6                            lb_record_6;            /* A reference to the record (XT_RECORD_REF_SIZE). */
 
61
        xtWord1                                         lb_data[XT_VAR_LENGTH];
 
62
} XTDataLogBufferDRec, *XTDataLogBufferDPtr;
 
63
 
 
64
#define XT_LOG_REC_HEADER_SIZE  offsetof(XTDataLogBufferDRec, lb_data)
 
65
 
 
66
typedef struct XTDataLogRefD {
 
67
        XTDiskValue4                            lr_offset_4;            /* No record will be beyond this offset because logs may not grow so big. */
 
68
        XTDiskValue2                            lr_log_no_2;            /* This is enough logs per table! */
 
69
} XTDataLogRefDRec, *XTDataLogRefDPtr;
 
70
 
 
71
#define XT_SET_LOG_REF_6(d, l, o)               XT_SET_DISK_6((xtWord1 *) (d), (((xtWord8) (l)) << 32) | ((xtWord8) (o)))               
 
72
#define XT_GET_LOG_REF_6(l, o, s)               do { xtWord8 t8 = XT_GET_DISK_6((xtWord1 *) (s)); l = t8 >> 32; o = t8 & (xtWord8) 0xFFFFFFFF; } while (0)
 
73
 
 
74
typedef struct XTGCData {
 
75
        struct XTThread                         *gc_thread;                             /* The data log garbage collector thread. */
 
76
        xt_mutex_type                           gc_lock;
 
77
        xt_cond_type                            gc_cond;
 
78
} XTGCDataRec, *XTGCDataPtr;
 
79
 
 
80
 
 
81
typedef struct XTDataLog {
 
82
        struct XTTable                          *dl_tab;                        /* The table of this data log. */
 
83
        xtWord4                                         dl_number;
 
84
        xtInt4                                          dl_ref_count;           /* The number of times this data log is open. */
 
85
 
 
86
        /* Field only updated by the sweeper thread: */
 
87
        off_t                                           dl_free_space;          /* This is the amount of free space (garbage) in the log. */
 
88
 
 
89
        xt_mutex_type                           dl_shlock;                      /* This lock is required to use the shared handle (assuming multiple pread are not allowed) */
 
90
        XTOpenFilePtr                           dl_shfile;                      /* The shared log handle (this handle is always open) */
 
91
 
 
92
        /* Fields only updated by the exclusive locker thread: */
 
93
#ifdef DEBUG
 
94
        struct XTThread                         *dl_exthr;
 
95
#endif
 
96
        XTOpenFilePtr                           dl_exfile;                                                      /* Non-null of the log is opened exclusively (this handle is append only) */
 
97
        xt_rwlock_type                  dl_rwlock;                                                      /* Lock for reading and writing the log buffer. */
 
98
        off_t                                           dl_file_size;                                           /* This is the size of the file (i.e. current EOF). */
 
99
        u_int                                           dl_offset;                                                      /* Current write position in write buffer. */
 
100
        xtWord1                                         dl_buffer[XT_DATA_LOG_BUFFER_SIZE];     /* The log write buffer. */
 
101
} XTDataLogRec, *XTDataLogPtr;
 
102
 
 
103
void    xt_dl_init_tab(struct XTThread *self, struct XTTable *tab);
 
104
void    xt_dl_exit_tab(struct XTTable *tab);
 
105
void    xt_dl_init_open_tab(struct XTThread *self, struct XTOpenTable *ot, off_t space_required);
 
106
void    xt_dl_exit_open_tab(struct XTOpenTable *ot);
 
107
 
 
108
xtBool  xt_dl_flush_log(XTDataLogPtr dlog);
 
109
xtBool  xt_dl_get_log_offset(struct XTOpenTable *ot, off_t *out_offset);
 
110
xtBool  xt_dl_append_log(struct XTOpenTable *ot, size_t size, void *data, xtBool may_switch);
 
111
xtBool  xt_dl_read_log(struct XTOpenTable *ot, xtWord4 log_no, off_t offset, size_t size, void *data);
 
112
xtBool  xt_dl_delete_log(struct XTOpenTable *ot, xtWord4 log_no, off_t offset, size_t size, off_t tab_offset);
 
113
 
 
114
void    xt_dl_init_db(struct XTThread *self, struct XTDatabase *db);
 
115
void    xt_dl_exit_db(struct XTThread *self, struct XTDatabase *db);
 
116
void    xt_quit_dl_daemons(struct XTThread *self, struct XTDatabase *db);
 
117
void    xt_gc_main(struct XTThread *self, xtBool once_off);
 
118
 
 
119
#endif
 
120