-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtndb_int.h
145 lines (111 loc) · 4.12 KB
/
tndb_int.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifndef TNDB_INTERNAL_H
#define TNDB_INTERNAL_H
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
/* FreeBSD does not provide PATH_MAX (syslimits.h is not for user) */
#ifndef PATH_MAX
# if defined(FILENAME_MAX)
# define PATH_MAX FILENAME_MAX
# elif defined(_POSIX_PATH_MAX)
# defined PATH_MAX _POSIX_PATH_MAX
# endif
#endif
#include <trurl/narray.h>
#include <trurl/nstream.h>
#include <trurl/nmalloc.h>
#define TNDB_FILEFMT_MAJOR 1
#define TNDB_FILEFMT_MINOR 0
uint32_t tndb_hash(const void *d, register uint8_t size);
#define TNDBSIGN_OFFSET 9 /* hdr[8] + sizeof(flsgs) */
struct tndb_sign {
void *ctx;
unsigned char md[20]; /* sha */
};
void tndb_sign_init(struct tndb_sign *sign);
void tndb_sign_update(struct tndb_sign *sign, const void *buf, unsigned int size);
void tndb_sign_update_int32(struct tndb_sign *sign, uint32_t v);
void tndb_sign_final(struct tndb_sign *sign);
int tndb_sign_store(struct tndb_sign *sign, tn_stream *st, uint32_t flags);
struct tndb_hdr {
unsigned char hdr[8];
uint8_t flags;
struct tndb_sign sign; /* signatures, variable length */
uint32_t ts; /* */
uint32_t nrec; /* number of records */
uint32_t doffs; /* offset of first data record */
};
void tndb_hdr_init(struct tndb_hdr *hdr, unsigned flags);
int tndb_hdr_store(struct tndb_hdr *hdr, tn_stream *st);
int tndb_hdr_compute_digest(struct tndb_hdr *hdr);
int tndb_hdr_store_sizeof(struct tndb_hdr *hdr);
int tndb_hdr_restore(struct tndb_hdr *hdr, tn_stream *st);
#define tndb_hdr_upsign(hdr, buf, size) \
do { if (hdr->flags & TNDBHDR_SIGN) \
tndb_sign_update(hdr->sign, buf, size); \
} while(0);
/* hash entry */
struct tndb_hent {
uint32_t val; /* hashed key */
uint32_t offs; /* offset in file */
};
struct tndb;
struct tndb_hent *tndb_hent_new(struct tndb *db, uint32_t val, uint32_t offs);
void tndb_hent_free(void *ptr);
int tndb_hent_cmp(const struct tndb_hent *h1, struct tndb_hent *h2);
int tndb_hent_cmp_store(const struct tndb_hent *h1, struct tndb_hent *h2);
#define TNDB_HTSIZE 256
#define TNDB_HTBYTESIZE (TNDB_HTSIZE * sizeof(uint32_t))
#define TNDB_R_MODE_R (1 << 0)
#define TNDB_R_MODE_W (1 << 1)
#define TNDB_R_HTT_LOADED (1 << 2)
#define TNDB_R_SIGN_VRFIED (1 << 3)
#define TNDB_R_UNLINKED (1 << 10)
struct tndb {
unsigned rtflags; /* runtime flags */
char *path;
tn_stream *st;
struct tndb_hdr hdr;
union {
uint32_t htt; /* offset of hash table; computed
basing on hdr end position */
uint32_t current; /* used in rw mode only */
} offs;
tn_array *htt[TNDB_HTSIZE]; /* arary of tn_array ptr of
tndb_hent */
char errmsg[128];
tn_alloc *na;
int _refcnt;
};
struct tndb *tndb_new(unsigned flags);
void tndb_free(struct tndb *db);
static inline
int nn_stream_read_offs(tn_stream *st, void *buf, unsigned int size, uint32_t offs)
{
if (st->st_seek(st->stream, offs, SEEK_SET) == -1)
return -1;
return st->st_read(st->stream, buf, size);
}
static inline
int nn_stream_read_uint32_offs(tn_stream *st, uint32_t *val, uint32_t offs)
{
int rc;
*val = 0;
if (st->st_seek(st->stream, offs, SEEK_SET) == -1)
return 0;
rc = n_stream_read_uint32(st, val);
return rc;
}
#ifndef ENABLE_TRACE
# define ENABLE_TRACE 0
#endif
#if ENABLE_TRACE
# define DBGF(fmt, args...) fprintf(stdout, "%-18s: " fmt, __FUNCTION__ , ## args)
# define DBG(fmt, args...) fprintf(stdout, fmt, ## args)
#else
# define DBGF(fmt, args...) ((void) 0)
# define DBG(fmt, args...) ((void) 0)
#endif
#define DBGF_NULL(fmt, args...) ((void) 0)
#define DBGF_F(fmt, args...) fprintf(stdout, "%-18s: " fmt, __FUNCTION__ , ## args)
#endif