-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnildb.h
120 lines (104 loc) · 2.61 KB
/
nildb.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
/* nildb
*
* Tiebing Zhang <[email protected]>,Adam Ierymenko <[email protected]>
* NILDB is in the public domain and is distributed with NO WARRANTY.
*
*/
#ifndef ___NILDB_H
#define ___NILDB_H
#include <stdio.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Version: 1
*
* This is the file format identifier, and changes any time the file
* format changes. The code version will be this dot something, and can
* be seen in tags in the git repository.
*/
#define NILDB_VERSION 1
//Key,data,next_offset,active/not
/**
* nildb database state
*
* These fields can be read by a user, e.g. to look up key_size and
* value_size, but should never be changed.
*/
typedef struct {
unsigned long hash_table_size;
unsigned long key_size;
unsigned long value_size;
unsigned long hash_table_size_bytes;
FILE *f;
} nildb;
/**
* I/O error or file not found
*/
#define NILDB_ERROR_IO -2
/**
* Out of memory
*/
#define NILDB_ERROR_MALLOC -3
/**
* Invalid paramters (e.g. missing _size paramters on init to create database)
*/
#define NILDB_ERROR_INVALID_PARAMETERS -4
/**
* Database file appears corrupt
*/
#define NILDB_ERROR_CORRUPT_DBFILE -5
/**
* Open database
*
* The three _size parameters must be specified if the database could
* be created or re-created. Otherwise an error will occur. If the
* database already exists, these parameters are ignored and are read
* from the database. You can check the struture afterwords to see what
* they were.
*
* @param db Database struct
* @param path Path to file
* @param hash_table_size Size of hash table in 64-bit entries (must be >0)
* @param key_size Size of keys in bytes
* @param value_size Size of values in bytes
* @return 0 on success, nonzero on error
*/
extern int nildb_open(
nildb *db,
const char *path,
uint32_t hash_table_size,
uint32_t key_size,
uint32_t value_size);
/**
* Close database
*
* @param db Database struct
*/
extern void nildb_close(nildb *db);
/**
* Get an entry
*
* @param db Database struct
* @param key Key (key_size bytes)
* @param vbuf Value buffer (value_size bytes capacity)
* @return -1 on I/O error, 0 on success, 1 on not found
*/
extern int nildb_get(nildb *db,const void *key,void *vbuf);
/**
* Put an entry (overwriting it if it already exists)
*
* In the already-exists case the size of the database file does not
* change.
*
* @param db Database struct
* @param key Key (key_size bytes)
* @param value Value (value_size bytes)
* @return -1 on I/O error, 0 on success
*/
extern int nildb_put(nildb *db,const void *key,const void *value);
#ifdef __cplusplus
}
#endif
#endif