-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibdb.h
149 lines (128 loc) · 3.13 KB
/
libdb.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
144
145
146
147
#ifndef LIBDB_H
#define LIBDB_H
#include <string.h>
/* */
/* */
#define MIN_BLOCK_SIZE 1024
/* check `man dbopen` */
struct DBT {
void *data;
size_t size;
};
struct DBC {
/* Maximum on-disk file size */
/* 512MB by default */
size_t db_size;
/* Maximum chunk (node/data chunk) size */
/* 4KB by default */
size_t chunk_size; // block size
/* Maximum memory size */
/* 16MB by default */
size_t mem_size;
};
typedef struct _cache_block {
int block_id;
int pos_in_cache;
struct _cache_block *prev;
struct _cache_block *next;
} cache_block;
typedef struct _bin_tree_node { // структура для представления узлов дерева
int key;
int height;
cache_block *cache_block_ptr;
struct _bin_tree_node* left;
struct _bin_tree_node* right;
} bin_tree_node;
typedef struct _db_cache {
cache_block *first;
cache_block *last;
int total_blocks;
int occuped_blocks;
void *cache_memory;
bin_tree_node *bin_tree;
} db_cache;
typedef struct {
size_t db_size;
size_t chunk_size;
size_t mem_size;
int root_id;
int bitmap_size; /*in bytes*/
int bitmap_start_index;
int free_block_count;
int tree_depth;
} db_header;
typedef struct {
int fd;
size_t db_size;
size_t chunk_size;
size_t mem_size;
void * root_node;
int root_id;
/*bit map*/
char * bitmap;
/*bitmap size in bytes*/
size_t bitmap_size;
/*first iterating byte in bitmap*/
int bitmap_start_index;
int free_block_count;
int tree_depth;
} db_info_in_DB;
struct DB {
/* Public API */
int (*close)(struct DB *db);
int (*del)(struct DB *db, struct DBT *key);
int (*get)(struct DB *db, struct DBT *key, struct DBT *data);
int (*put)(struct DB *db, struct DBT *key, struct DBT *data);
int (*sync)(struct DB *db);
/* Private API */
//db_info_in_DB db_info;
/* ... */
}; /* Need for supporting multiple backends (HASH/BTREE) */
struct MY_DB {
/* Public API */
int (*close)(struct DB *db);
int (*del)(struct DB *db, struct DBT *key);
int (*get)(struct DB *db, struct DBT *key, struct DBT *data);
int (*put)(struct DB *db, struct DBT *key, struct DBT *data);
int (*sync)(struct DB *db);
/* Private API */
db_info_in_DB db_info;
db_cache cache;
/* ... */
}; /* Need for supporting multiple backends (HASH/BTREE) */
struct DB *dbcreate(const char *file, struct DBC conf);
struct DB *dbopen (const char *file); /* Metadata in file */
int db_close(struct DB *db);
int del(struct DB *db, struct DBT *key);
int get(struct DB *db, struct DBT *key, struct DBT *data);
int put(struct DB *db, struct DBT *key, struct DBT *data);
int db_del(struct DB *db, void *k, size_t size)
{
struct DBT key;
key.data = k;
key.size = size;
return del(db, &key);
}
int db_get(struct DB *db, void *k, size_t k_s, void **v, size_t *v_s)
{
struct DBT key;
struct DBT value;
int res;
key.data = k;
key.size = k_s;
res = get(db, &key, &value);
*v = value.data;
*v_s = value.size;
return res;
}
int db_put(struct DB *db, void *k, size_t k_s, void * v, size_t v_s )
{
struct DBT key;
struct DBT value;
key.data = k;
key.size = k_s;
value.data = v;
value.size = v_s;
return put(db, &key, &value);
}
#endif