-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemstorage.c
102 lines (86 loc) · 2.66 KB
/
memstorage.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
#include <memstorage.h>
#include <datacontainer.h>
#include <storagebase.h>
#include <stdlib.h>
#include <string.h>
#include <flatbitconfig.h>
extern const int maxNumberRecordsInSegment;
extern const int maxNumberOfSegments;
enum MemSegmentStatus {
MEM_SEG_OK, MEM_SEG_FULL
};
enum MemStorageStatus {
MEM_STORAGE_OK, MEM_STORAGE_FULL
};
const int segSize()
{
return maxNumberRecordsInSegment * sizeOfRecord;
}
enum MemStorageStatus memStorageStatus(struct Container * c)
{
return c->records <= maxNumberRecordsInSegment * maxNumberOfSegments ? MEM_STORAGE_OK : MEM_STORAGE_FULL;
}
int memStorageOpen(struct Container * c)
{
c->storage->base[MEM_BASE_IND]->header = malloc(sizeOfHeader);
c->storage->base[MEM_BASE_IND]->handle = calloc(c->records, segSize());
assert(c->storage->base[MEM_BASE_IND]->handle && c->storage->base[MEM_BASE_IND]->header);
printf("size of %d allocated mem at %p\n", c->records * sizeOfRecord, c->storage->base[MEM_BASE_IND]->handle);
return 0;
}
int memStorageClose(struct Container * c)
{
free(c->storage->base[MEM_BASE_IND]->handle); //oops should close Keys too if pointers
printf("size of %d free'd mem\n", c->records * sizeOfRecord);
return 0;
}
int memReadRecord(struct Container * c, struct Record * recordOut, unsigned int index)
{
//memcpy(recordOut, c->storage->base[MEM_BASE_IND]->handle + sizeOfRecord * index, sizeOfRecord);
recordOut = c->storage->base[MEM_BASE_IND]->handle + sizeOfRecord * index; //try without copy
return 0;
}
int memDelRecord(struct Container * c, unsigned int index)
{
struct Record * record;
record = c->storage->base[MEM_BASE_IND]->handle + sizeOfRecord * index;
record->status = 0;
return 0;
}
int memWriteRecord(struct Container * c, struct Record * record)
{
switch (memStorageStatus(c)) {
case MEM_STORAGE_OK:
break;
case MEM_STORAGE_FULL:
return MEM_STORAGE_FULL;
break;
}
struct Record * r = memcpy(c->storage->base[MEM_BASE_IND]->handle + sizeOfRecord * (c->records++), record, sizeOfRecord);
record->status = 1;
printf("wrote key %d ", r->key.pk);
printf("records %d after write mem\n", c->records);
return MEM_STORAGE_OK;
}
int memWriteHeader(struct Container * c)
{
struct Header *header = makeHeader();
memcpy(c->storage->base[MEM_BASE_IND]->header, header, sizeOfHeader);
return 0;
}
unsigned int memGetIndex(struct Container * c, struct Key * key)
{
struct Record rec;
unsigned int index=0;
for (; index <= c->records; index++)
{
memcpy(&rec, c->storage->base[MEM_BASE_IND]->handle + sizeOfRecord * index, sizeOfRecord);
if (keyCmp(&(rec.key), key))
{
printf("found pk match\n");
return index;
break;
}
}
return 0;
}