-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmydb.c~
99 lines (85 loc) · 2.34 KB
/
mydb.c~
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "mydb.h"
#include "btree.h"
//int fseek(FILE *filename, long int offset, int origin);
struct DB *dbcreate(const char *file, const struct DBC conf)
{
struct DB* new_base = (struct DB*)malloc(sizeof(struct DB));
new_base->fd = NULL;
new_base->fd = fopen(file,"wb+");
if(new_base->fd == NULL)
printf("Error! File wasn't created!\n");
new_base->head = (struct BTreeNode*)malloc(sizeof(struct BTreeNode));
memset(new_base->head,0,sizeof(new_base->head));
fprintf(new_base->fd,"%d%d%d%d",BTREE_KEY_CNT,BTREE_KEY_LEN,BTREE_VAL_LEN,BTREE_CHLD_CNT);
fprintf(new_base->fd,"%d%d",(int)conf.db_size,(int)conf.chunk_size);
int offset = ftell(new_base->fd); //Узнаем текущее смещение
fprintf(new_base->fd,"%d",(int)(offset + sizeof(offset)) );//Пишем смещение главного листа B-дерева
fwrite(new_base->head,1,sizeof(new_base->head),new_base->fd);
head_offset = offset + sizeof(offset);
return new_base;
}
struct DB *dbopen (const char *file)
{
struct DB* base = (struct DB*)malloc(sizeof(struct DB));
memset(base,0,sizeof(base));
return base;
}
int close(struct DB *db);
{
if(db->head != NULL)
{
free(db->head);
db->head = NULL;
}
if(db->fd != NULL)
{
fclose(db->fd);
}
return 0;
}
int get(const struct DB *db, struct DBT *key, struct DBT *data)
{
int num = 0;
BTreeNode* res = search_key(db->head,key->data,num,db);
if(res == NULL)return -1;
data->data = res->data; //BTreeNode data - ?
return 0;
}
//----------------------------Код для поддержки тестирования---------------------------------------
int db_close(struct DB *db) {
db->close(db);
}
int db_del(const struct DB *db, void *key, size_t key_len) {
struct DBT keyt = {
.data = key,
.size = key_len
};
return db->del(db, &keyt);
}
int db_get(const struct DB *db, void *key, size_t key_len,
void **val, size_t *val_len) {
struct DBT keyt = {
.data = key,
.size = key_len
};
struct DBT valt = {0, 0};
int rc = db->get(db, &keyt, &valt);
*val = valt.data;
*val_len = valt.size;
return rc;
}
int db_put(const struct DB *db, void *key, size_t key_len,
void *val, size_t val_len) {
struct DBT keyt = {
.data = key,
.size = key_len
};
struct DBT valt = {
.data = val,
.size = val_len
};
return db->put(db, &keyt, &valt);
}