1 |
/* |
---|
2 |
* Copyright (c) 2005-2008, Message Systems, Inc. |
---|
3 |
* All rights reserved. |
---|
4 |
* |
---|
5 |
* Redistribution and use in source and binary forms, with or without |
---|
6 |
* modification, are permitted provided that the following conditions are |
---|
7 |
* met: |
---|
8 |
* |
---|
9 |
* * Redistributions of source code must retain the above copyright |
---|
10 |
* notice, this list of conditions and the following disclaimer. |
---|
11 |
* * Redistributions in binary form must reproduce the above |
---|
12 |
* copyright notice, this list of conditions and the following |
---|
13 |
* disclaimer in the documentation and/or other materials provided |
---|
14 |
* with the distribution. |
---|
15 |
* * Neither the name Message Systems, Inc. nor the names |
---|
16 |
* of its contributors may be used to endorse or promote products |
---|
17 |
* derived from this software without specific prior written |
---|
18 |
* permission. |
---|
19 |
* |
---|
20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
31 |
*/ |
---|
32 |
|
---|
33 |
#ifndef _JLOG_PRIVATE_H |
---|
34 |
#define _JLOG_PRIVATE_H |
---|
35 |
/* vim:se ts=2 sw=2 et: */ |
---|
36 |
|
---|
37 |
#include "jlog_config.h" |
---|
38 |
#include "jlog.h" |
---|
39 |
#include "jlog_io.h" |
---|
40 |
|
---|
41 |
#define DEFAULT_FILE_MODE 0640 |
---|
42 |
#define DEFAULT_UNIT_LIMIT (4*1024*1024) |
---|
43 |
/* 4 Megabytes */ |
---|
44 |
#define DEFAULT_SAFETY JLOG_ALMOST_SAFE |
---|
45 |
#define INDEX_EXT ".idx" |
---|
46 |
#define MAXLOGPATHLEN (MAXPATHLEN - (8+sizeof(INDEX_EXT))) |
---|
47 |
|
---|
48 |
static const char __jlog_hexchars[] = "0123456789abcdef"; |
---|
49 |
|
---|
50 |
typedef enum { |
---|
51 |
JLOG_NEW = 0, |
---|
52 |
JLOG_INIT, |
---|
53 |
JLOG_READ, |
---|
54 |
JLOG_APPEND, |
---|
55 |
JLOG_INVALID |
---|
56 |
} jlog_mode; |
---|
57 |
|
---|
58 |
struct _jlog_ctx { |
---|
59 |
jlog_safety safety; |
---|
60 |
jlog_mode context_mode; |
---|
61 |
size_t unit_limit; |
---|
62 |
char *path; |
---|
63 |
int file_mode; |
---|
64 |
u_int32_t current_log; |
---|
65 |
jlog_file *data; |
---|
66 |
jlog_file *index; |
---|
67 |
jlog_file *checkpoint; |
---|
68 |
jlog_file *metastore; |
---|
69 |
void *mmap_base; |
---|
70 |
size_t mmap_len; |
---|
71 |
jlog_id storage; |
---|
72 |
char *subscriber_name; |
---|
73 |
int last_error; |
---|
74 |
int last_errno; |
---|
75 |
jlog_error_func error_func; |
---|
76 |
void *error_ctx; |
---|
77 |
}; |
---|
78 |
|
---|
79 |
struct _jlog_meta_info { |
---|
80 |
u_int32_t storage_log; |
---|
81 |
u_int32_t unit_limit; |
---|
82 |
u_int32_t safety; |
---|
83 |
}; |
---|
84 |
|
---|
85 |
/* macros */ |
---|
86 |
|
---|
87 |
#define STRLOGID(s, logid) do { \ |
---|
88 |
int __i; \ |
---|
89 |
for(__i=0;__i<8;__i++) \ |
---|
90 |
(s)[__i] = __jlog_hexchars[((logid) >> (32 - ((__i+1)*4))) & 0xf]; \ |
---|
91 |
(s)[__i] = '\0'; \ |
---|
92 |
} while(0) |
---|
93 |
|
---|
94 |
#define STRSETDATAFILE(ctx, file, log) do { \ |
---|
95 |
int __len; \ |
---|
96 |
__len = strlen((ctx)->path); \ |
---|
97 |
memcpy((file), (ctx)->path, __len); \ |
---|
98 |
(file)[__len] = IFS_CH; \ |
---|
99 |
STRLOGID((file)+(__len+1), log); \ |
---|
100 |
} while(0) |
---|
101 |
|
---|
102 |
#define SYS_FAIL_EX(a, dowarn) do { \ |
---|
103 |
if (ctx) { \ |
---|
104 |
ctx->last_error = (a); \ |
---|
105 |
ctx->last_errno = errno; \ |
---|
106 |
if(ctx->error_func && dowarn) { \ |
---|
107 |
ctx->error_func(ctx->error_ctx, \ |
---|
108 |
"JLOG-%d error: %d (%s) errno: %d (%s)\n", __LINE__, \ |
---|
109 |
ctx->last_error, jlog_ctx_err_string(ctx), \ |
---|
110 |
ctx->last_errno, strerror(ctx->last_errno)); \ |
---|
111 |
} \ |
---|
112 |
} \ |
---|
113 |
goto finish; \ |
---|
114 |
} while(0) |
---|
115 |
|
---|
116 |
#define SYS_FAIL(a) SYS_FAIL_EX(a, 1) |
---|
117 |
|
---|
118 |
/** |
---|
119 |
* repairs a damaged datafile |
---|
120 |
* @return 0 OK, >0 number of damaged segments removed, -1 repair failed |
---|
121 |
* @internal |
---|
122 |
*/ |
---|
123 |
JLOG_API(int) jlog_repair_datafile(jlog_ctx *ctx, u_int32_t log); |
---|
124 |
/** |
---|
125 |
* prints detailed info about the log segment to stderr |
---|
126 |
* @return 0 OK, 1 segment damaged, -1 other error |
---|
127 |
* @internal |
---|
128 |
*/ |
---|
129 |
JLOG_API(int) jlog_inspect_datafile(jlog_ctx *ctx, u_int32_t log); |
---|
130 |
/** |
---|
131 |
* fetches the last marker in the index and the closedness thereof |
---|
132 |
* @return 0 OK, -1 error |
---|
133 |
* @internal |
---|
134 |
*/ |
---|
135 |
JLOG_API(int) jlog_idx_details(jlog_ctx *ctx, u_int32_t log, |
---|
136 |
u_int32_t *marker, int *closed); |
---|
137 |
|
---|
138 |
|
---|
139 |
#ifdef _WIN32 |
---|
140 |
#include "ec_win32.h" |
---|
141 |
#endif |
---|
142 |
|
---|
143 |
#endif |
---|