1
/* Copyright (c) 2005 SNAP Innovation GmbH
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.
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.
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
19
* 2005-01-24 Paul McCullagh
23
#ifndef __xt_datalog_h__
24
#define __xt_datalog_h__
26
#include "xt_filesys.h"
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.
33
#define XT_GARBAGE_THRESHOLD ((double) 30.0)
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)
47
#define XT_DL_STATUS_DELETED 0
48
#define XT_DL_STATUS_RECORD 10
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;
57
typedef struct XTDataLogBuffer {
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;
64
#define XT_LOG_REC_HEADER_SIZE offsetof(XTDataLogBufferDRec, lb_data)
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;
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)
74
typedef struct XTGCData {
75
struct XTThread *gc_thread; /* The data log garbage collector thread. */
76
xt_mutex_type gc_lock;
78
} XTGCDataRec, *XTGCDataPtr;
81
typedef struct XTDataLog {
82
struct XTTable *dl_tab; /* The table of this data log. */
84
xtInt4 dl_ref_count; /* The number of times this data log is open. */
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. */
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) */
92
/* Fields only updated by the exclusive locker thread: */
94
struct XTThread *dl_exthr;
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;
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);
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);
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);