-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfs.c
313 lines (273 loc) · 9.88 KB
/
sfs.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "sfs.h"
#include "crc32.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAGIC (0x534653aa)
#define VERSION (0x01000000)
static char errmsg[8192];
static uint32_t ptrOffset(void *x, void *y){
return (char*)y > (char*)x ?
(char*)y - (char*)x:
(char*)x - (char*)y;
}
static void* offsetPtr(void* x, uint32_t y){
return (char*)x + y;
}
static void* negOffsetPtr(void* x, uint32_t y){
return (char*)x - y;
}
int sfsVarcharCons(SFSVarchar *varchar, const char* src){
memcpy(varchar->buf, src, varchar->len);
return 1;
}
SFSVarchar* sfsVarcharCreate(uint32_t varcharLen, const char* src){
SFSVarchar *varchar = (SFSVarchar*)malloc(sizeof(SFSVarchar)+varcharLen);
varchar->len = varcharLen;
if(src) memcpy(varchar->buf, src, varcharLen);
return varchar;
}
int sfsVarcharRelease(SFSVarchar *varchar){
free(varchar);
return 1;
}
static uint32_t calcRecordSize(const SFSVarchar *recordMeta){
uint32_t recordSize = 0;
for(int i=0; i < recordMeta->len; i++){
uint8_t x = (uint8_t)recordMeta->buf[i];
recordSize += x ? x: 4;
}
return recordSize;
}
static int calcTableSize(uint32_t storeSize, const SFSVarchar *recordMeta){
return sizeof(SFSTable) + storeSize + sizeof(SFSVarchar) + recordMeta->len;
}
int sfsTableCons(SFSTable *table,
uint32_t initStorSize,
const SFSVarchar *recordMeta,
SFSDatabase *db){
if(initStorSize == 0) initStorSize = 16 * calcRecordSize(recordMeta);
table->size = calcTableSize(initStorSize, recordMeta);
table->storSize = initStorSize;
table->freeSpace = initStorSize + sizeof(SFSVarchar) + recordMeta->len;
table->varcharNum = -1; // to insert recordMeta
table->recordNum = 0;
table->recordSize = calcRecordSize(recordMeta);
table->lastVarchar= offsetPtr(table, table->size);
sfsTableAddVarchar(&table, recordMeta->len, recordMeta->buf);
table->recordMeta = table->lastVarchar;
table->database = db;
return 1;
}
SFSTable* sfsTableCreate(uint32_t initStorSize,
const SFSVarchar *recordMeta,
SFSDatabase *db){
if(initStorSize == 0) initStorSize = 16 * calcRecordSize(recordMeta);
uint32_t tableSize = calcTableSize(initStorSize, recordMeta);
SFSTable *table = malloc(tableSize);
sfsTableCons(table, initStorSize, recordMeta, db);
return table;
}
int sfsTableRelease(SFSTable *table){
free(table);
return 1;
}
int sfsTableReserve(SFSTable **ptable, uint32_t storSize){
SFSTable *table = *ptable;
uint32_t newTableSize = calcTableSize(storSize, table->recordMeta);
uint32_t oldTableSize = table->size;
if(newTableSize <= oldTableSize) return 1;
uint32_t delta = newTableSize - oldTableSize;
const SFSVarchar *recordMeta = table->recordMeta;
SFSTable *newTable = sfsTableCreate(storSize, recordMeta, table->database);
memcpy(newTable, table, sizeof(SFSTable) + table->recordNum * table->recordSize);
uint32_t tailLen = table->size - ptrOffset(table, table->lastVarchar);
memcpy(offsetPtr(newTable, newTableSize - tailLen),
table->lastVarchar,
tailLen);
newTable->size = newTableSize;
newTable->storSize = storSize;
newTable->freeSpace += delta;
newTable->lastVarchar = offsetPtr(newTable, newTableSize - tailLen);
newTable->recordMeta =
offsetPtr(newTable, newTableSize - sizeof(SFSVarchar) - recordMeta->len);
char* st = newTable->buf;
for(uint32_t i=0; i < recordMeta->len; i++){
uint8_t type = (uint8_t)recordMeta->buf[i];
if(type){
st = offsetPtr(st, type);
} else {
void* *cur = (void **)st;
for(uint32_t j=0; j < newTable->recordNum; j++){
uint32_t offset = ptrOffset(table, (*cur) ) + delta;
*cur = offsetPtr(newTable, offset);
cur = offsetPtr(cur, newTable->recordSize);
}
st = offsetPtr(st, 4);
}
}
free(table);
if(newTable->database) newTable->database->size += delta;
*ptable = newTable;
return 1;
}
#define tableForeach(table, ptr) \
void* ptr = table->buf; \
for(int i = 0; i < table->recordNum; i++, ptr = offsetPtr(ptr, table->recordSize))
static int tableExpand(SFSTable **ptable, uint32_t addSize){
uint32_t oldStorSize = (*ptable)->storSize;
uint32_t newStorSize;
if(oldStorSize + (*ptable)->freeSpace >= addSize) newStorSize = oldStorSize *2;
else newStorSize = oldStorSize + addSize;
return sfsTableReserve(ptable, newStorSize);
}
void* sfsTableAddRecord(SFSTable **ptable){
uint32_t recordSize = (*ptable)->recordSize;
if((*ptable)->freeSpace < recordSize) tableExpand(ptable, recordSize);
SFSTable *table = *ptable;
void* retPtr = offsetPtr(table->buf, table->recordNum * recordSize);
table->recordNum++;
table->freeSpace -= table->recordSize;
return retPtr;
}
SFSVarchar* sfsTableAddVarchar(SFSTable **ptable, uint32_t varcharLen, const char* src){
uint32_t varcharSize = sizeof(SFSVarchar) + varcharLen;
if((*ptable)->freeSpace < varcharSize) tableExpand(ptable, varcharSize);
SFSTable *table = *ptable;
SFSVarchar *retPtr = table->lastVarchar;
retPtr = negOffsetPtr(retPtr, varcharSize);
retPtr->len = varcharLen;
if(src) memcpy(retPtr->buf, src, varcharLen);
table->lastVarchar = retPtr;
table->varcharNum++;
table->freeSpace -= varcharSize;
return retPtr;
}
SFSDatabase* sfsDatabaseCreate(){
SFSDatabase *db = malloc(sizeof(SFSDatabase));
db->magic = MAGIC;
db->version = VERSION;
db->size = sizeof(SFSDatabase);
db->tableNum = 0;
return db;
}
void sfsDatabaseRelease(SFSDatabase* db){
for(int i=0; i<db->tableNum; i++){
sfsTableRelease(db->table[i]);
}
free(db);
}
//model==1 convert ptr to offset, vice versa
static void recordVarcharAndPtrConvert(SFSTable *table, void *record, int model){
const SFSVarchar *recordMeta = table->recordMeta;
uint32_t *cur = (uint32_t*)record;
for(int i=0; i < recordMeta->len; i++){
uint8_t type = (uint8_t)recordMeta->buf[i];
if(type){
cur = offsetPtr(cur, type);
} else {
if(model){
*cur = ptrOffset(table, (void*)(*cur));
} else{
*cur = (uint32_t)offsetPtr(table, *cur);
}
cur = offsetPtr(cur, 4);
}
}
}
static void tablePtrToOffsetConvert(SFSTable *table, uint32_t databaseOffset){
table->database = (SFSDatabase*)databaseOffset;
table->lastVarchar = (SFSVarchar*)ptrOffset(table, table->lastVarchar);
table->recordMeta = (SFSVarchar*)ptrOffset(table, table->recordMeta);
tableForeach(table, record){
recordVarcharAndPtrConvert(table, record, 1);
}
}
static void tableOffsetToPtrConvert(SFSTable *table, SFSDatabase *db){
table->database = db;
table->lastVarchar = offsetPtr(table, (uint32_t)table->lastVarchar);
table->recordMeta = offsetPtr(table, (uint32_t)table->recordMeta);
tableForeach(table, record){
recordVarcharAndPtrConvert(table, record, 0);
}
}
int sfsDatabaseSave(char *fileName, SFSDatabase* db){
if(strlen(fileName) > 4096) {
sprintf(errmsg, "file name is too long, is .4096%s", fileName);
return 0;
}
FILE *fp = fopen(fileName, "wb");
if(!fp){
int errnum = errno;
sprintf(errmsg, "error in open file: %s, file name is %s",strerror(errnum), fileName);
return 0;
}
uint32_t tableOffset = sizeof(SFSDatabase);
SFSTable* table[16];
for(int i=0; i < db->tableNum; i++){
table[i] = db->table[i];
tablePtrToOffsetConvert(table[i], tableOffset);
db->table[i] = (SFSTable*)tableOffset;
tableOffset += table[i]->size;
}
muti_crc32_init();
muti_crc32_update(&(db->version), sizeof(SFSDatabase));
for(int i=0; i < db->tableNum; i++) muti_crc32_update(table[i], table[i]->size);
db->crc = muti_crc32_get();
fwrite(db, sizeof(SFSDatabase), 1, fp);
for(int i=0; i < db->tableNum; i++) {
fwrite(table[i], table[i]->size, 1, fp);
}
fclose(fp);
for(int i=0; i < db->tableNum; i++){
db->table[i] = table[i];
tableOffsetToPtrConvert(table[i], db);
}
return 1;
}
SFSDatabase* sfsDatabaseCreateLoad(char *fileName){
if(strlen(fileName) > 4096) {
sprintf(errmsg, "file name is too long, is .4096%s", fileName);
return NULL;
}
FILE *fp = fopen(fileName, "rb");
if(!fp){
int errnum = errno;
sprintf(errmsg, "error in open file: %s, file name is %s",strerror(errnum), fileName);
return NULL;
}
uint32_t magic = 0;
fread(&magic, sizeof(uint32_t), 1, fp);
if(magic!=MAGIC) {
sprintf(errmsg, "the file %s is not a sfs file", fileName);
return NULL;
}
fseek(fp, 0L, SEEK_END);
uint32_t fileSize = ftell(fp);
SFSDatabase * db = malloc(fileSize);
fseek(fp, 0L, SEEK_SET);
fread(&db, fileSize, 1, fp);
uint32_t crc = crc32(&(db->version), fileSize - 2*sizeof(uint32_t));
if(crc != db->crc){
sprintf(errmsg, "the file crc is wrong, file name is %s", fileName);
free(db);
return NULL;
}
for(int i=0; i < db->tableNum; i++){
db->table[i] = offsetPtr(db, (uint32_t)db->table[i]);
tableOffsetToPtrConvert(db->table[i], db);
db->table[i]->database = db;
}
}
SFSTable* sfsDatabaseAddTable(SFSDatabase *db, uint32_t initStorSize, const SFSVarchar *recordMeta){
if(initStorSize == 0) initStorSize = 16 * calcRecordSize(recordMeta);
uint32_t tableSize = calcTableSize(initStorSize, recordMeta);
SFSTable *table = sfsTableCreate(initStorSize, recordMeta, db);
db->table[db->tableNum] = table;
db->tableNum++;
return table;
}
// return the lastest err
char *sfsErrMsg(){
return errmsg;
}