This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
/
db.c
87 lines (75 loc) · 1.62 KB
/
db.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <assert.h>
#include "bitwise.h"
#include "storage.h"
#include "db.h"
#include "llru.h"
#define IDX_PRIME (16785407)
static struct btree _btree;
static struct bloom _bloom;
/*Sequential read,and mark keys in BloomFilter
* This is very important to preheat datas.
*/
static void db_loadbloom()
{
int i,l,super_size=sizeof(struct btree_super);
uint64_t alloc=_btree.alloc-super_size,offset;
int newsize=(sizeof(struct btree_table));
lseek64(_btree.fd,super_size, SEEK_SET);
while(alloc>0){
struct btree_table *table=malloc(newsize);
int r=read(_btree.fd,table, newsize) ;
if(table->size>0){
for(l=0;l<table->size;l++){
if(get_H(offset)==0)
bloom_add(&_bloom,table->items[l].sha1);
}
}
free(table);
alloc-=newsize;
}
}
void db_init(int bufferpool_size)
{
btree_init(&_btree);
llru_init(bufferpool_size);
bloom_init(&_bloom,IDX_PRIME);
db_loadbloom();
}
int db_add(char* key,char* value)
{
uint64_t off=btree_insert(&_btree,(const uint8_t*)key,(const void*)value,strlen(value));
if(off==0)
return (0);
bloom_add(&_bloom,key);
return (1);
}
void *db_get(char* key)
{
int b=bloom_get(&_bloom,(const char*)key);
if(b!=0)
return NULL;
void *v=llru_get((const char*)key);
if(v==NULL){
v=btree_get(&_btree,key);
char *k_tmp=strdup(key);
char *v_tmp=strdup((char*)v);
llru_set(k_tmp,v_tmp,strlen(k_tmp),strlen(v_tmp));
return v;
}else{
return strdup((char*)v);
}
}
void db_remove(char* key)
{
btree_delete(&_btree,key);
llru_remove(key);
}
void db_destroy()
{
llru_free();
btree_close(&_btree);
}