aboutsummaryrefslogtreecommitdiff
path: root/src/storage.c
blob: 4ebbfdc507d9aa08767281ef1d73fba6ed8aa564 (plain) (blame)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "db.h"


ltable *newLtable(uint64_t size){
	ltable *lt = malloc(sizeof(ltable));
	size = (((uint64_t)size) < 0) ? 0 : size;
	lt->size = size;
	lt->table = malloc(size*sizeof(char*));
	return lt;
}

ltable *loadLtable(FILE *fp){
	char header;
	fread(&header, sizeof(char), 1, fp);
	if(header != 'L'){
		fprintf(stderr, "Header is '%c' not 'L'\n", header);
	}
	uint64_t size;
	fread(&size, sizeof(uint64_t), 1, fp);
	ltable *lt = newLtable(size);
	for(uint64_t i = 0; i < lt->size; ++i){
		uint32_t sl;
		fread(&sl, sizeof(uint32_t), 1, fp);
		lt->table[i] = malloc(sl*sizeof(char));
		fread(lt->table[i], sizeof(char), sl, fp);
	}
	char end;
	fread(&end, sizeof(char), 1, fp);
	if(end != 'E'){
		fprintf(stderr, "End is '%c' not 'E'\n", end);
	}
	return lt;
}

int storeLtable(const ltable *lt, FILE *fp){
	char header = 'L';
	fwrite(&header, sizeof(char), 1, fp);
	fwrite(&lt->size, sizeof(uint64_t), 1, fp);
	for(uint64_t i = 0; i < lt->size; ++i){
		uint32_t l = len(lt->table[i]) + 1;
		fwrite(&l, sizeof(uint32_t), 1, fp);
		fwrite(lt->table[i], sizeof(char), l, fp);
	}
	char end = 'E';
	fwrite(&end, sizeof(char), 1, fp);
	return 0;
}

int ltableAdd(ltable *lt, char *str){
	uint32_t ls;
	str = normalizeStrLimit(str, &ls, MAXPATH-1);
	
	char **nlt = malloc((lt->size+1)*sizeof(char*));
	for(uint64_t i = 0; i < lt->size; ++i){
		if(sameStr(str, lt->table[i])){
			return -1;
		}
		uint32_t l = len(lt->table[i]);
		nlt[i] = malloc((l+1)*sizeof(char));
		memcpy(nlt[i], lt->table[i], l+1);
	}
	nlt[lt->size] = malloc((ls+1)*sizeof(char));
	memcpy(nlt[lt->size], str, ls+1);
	
	lt->size++;
	lt->table = malloc(lt->size*sizeof(char*));
	for(uint64_t i = 0; i < lt->size; ++i){
		uint32_t l = len(nlt[i]);
		lt->table[i] = malloc((l+1)*sizeof(char));
		memcpy(lt->table[i], nlt[i], l+1);
	}
	return 0;
}

uint64_t ltableSearch(ltable *lt, char *str){
	uint32_t l;
	str = normalizeStrLimit(str, &l, MAXPATH-1);
	
	for(uint64_t i = 0; i < lt->size; ++i){
		if(sameStr(str, lt->table[i])){
			return i;
		}
	}
	return -1;
}

htable *newHtable(uint64_t size){
	htable *ht = malloc(sizeof(htable));
	size = (((uint64_t)size) < 0) ? 0 : size;
	ht->size = size;
	ht->table = malloc(size*sizeof(uint64_t));
	return ht;
}

htable *loadHtable(FILE *fp){
	char header;
	fread(&header, sizeof(char), 1, fp);
	if(header != 'H'){
		fprintf(stderr, "Header is '%c' not 'H'\n", header);
	}
	uint64_t size;
	fread(&size, sizeof(uint64_t), 1, fp);
	htable *ht = newHtable(size);
	for(uint64_t i = 0; i < ht->size; ++i){
		fread(&ht->table[i], sizeof(uint64_t), 1, fp);
	}
	char end;
	fread(&end, sizeof(char), 1, fp);
	if(end != 'E'){
		fprintf(stderr, "End is '%c' not 'E'\n", end);
	}
	return ht;
}

int storeHtable(const htable *ht, FILE *fp){
	char header = 'H';
	fwrite(&header, sizeof(char), 1, fp);
	fwrite(&ht->size, sizeof(uint64_t), 1, fp);
	for(uint64_t i = 0; i < ht->size; ++i){
		fwrite(&ht->table[i], sizeof(uint64_t), 1, fp);
	}
	char end = 'E';
	fwrite(&end, sizeof(char), 1, fp);
	return 0;
}

int htableAdd(htable *ht, uint64_t h){
	uint64_t *nht = malloc((ht->size+1)*sizeof(uint64_t));
	for(uint64_t i = 0; i < ht->size; ++i){
		if(h == ht->table[i]){
			return -1;
		}
		nht[i] = ht->table[i];
	}
	nht[ht->size] = h;
	
	ht->size++;
	ht->table = malloc(ht->size*sizeof(uint64_t));
	for(uint64_t i = 0; i < ht->size; ++i){
		ht->table[i] = nht[i];
	}
	return 0;
}

uint64_t htableSearch(htable *ht, uint64_t h){
	for(uint64_t i = 0; i < ht->size; ++i){
		if(h == ht->table[i]){
			return i;
		}
	}
	return -1;
}

mtable *newMtable(uint64_t size){
	mtable *mt = malloc(sizeof(mtable));
	size = (((uint64_t)size) < 0) ? 0 : size;
	mt->size = size;
	mt->table = malloc(size*sizeof(relation));
	return mt;
}

mtable *loadMtable(FILE *fp){
	char header;
	fread(&header, sizeof(char), 1, fp);
	if(header != 'M'){
		fprintf(stderr, "Header is '%c' not 'M'\n", header);
	}
	uint64_t size;
	fread(&size, sizeof(uint64_t), 1, fp);
	mtable *mt = newMtable(size);
	for(uint64_t i = 0; i < mt->size; ++i){
		fread(&mt->table[i].file, sizeof(uint64_t), 1, fp);
		fread(&mt->table[i].tag, sizeof(uint64_t), 1, fp);
	}
	char end;
	fread(&end, sizeof(char), 1, fp);
	if(end != 'E'){
		fprintf(stderr, "End is '%c' not 'E'\n", end);
	}
	return mt;
}

int storeMtable(const mtable *mt, FILE *fp){
	char header = 'M';
	fwrite(&header, sizeof(char), 1, fp);
	fwrite(&mt->size, sizeof(uint64_t), 1, fp);
	for(uint64_t i = 0; i < mt->size; ++i){
		fwrite(&mt->table[i].file, sizeof(uint64_t), 1, fp);
		fwrite(&mt->table[i].tag, sizeof(uint64_t), 1, fp);
	}
	char end = 'E';
	fwrite(&end, sizeof(char), 1, fp);
	return 0;
}

int mtableAdd(mtable *mt, relation r){
	relation *nmt = malloc((mt->size+1)*sizeof(relation));
	for(uint64_t i = 0; i < mt->size; ++i){
		if(r.file == mt->table[i].file && r.tag == mt->table[i].tag){
			return -1;
		}
		nmt[i] = mt->table[i];
	}
	nmt[mt->size] = r;
	
	mt->size++;
	mt->table = malloc(mt->size*sizeof(relation));
	for(uint64_t i = 0; i < mt->size; ++i){
		mt->table[i] = nmt[i];
	}
	return 0;
}

uint64_t mtableSearch(mtable *mt, relation r){
	for(uint64_t i = 0; i < mt->size; ++i){
		if(r.file == mt->table[i].file && r.tag == mt->table[i].tag){
			return i;
		}
	}
	return -1;
}




/*
// TODO: remove old impl

// Splits src into words based on a separator character (sep) and stores them in arr,
// and the length in len. Inspired by https://github.com/joshdk/tag/blob/master/src/dsv.c's split
static void split(const char *src, char sep, char ***arr, uint32_t *len){
	int slen = 0, ai = 0, wnum = 0, wlen = 0;

	while(src[slen] != '\0'){
		if(src[slen] == sep){
			++wnum;
		}
		++slen;
	}
	if(slen != 0 && src[slen-1] != sep){
		++wnum;
	}
	++slen;

	*arr = calloc((wnum+1), sizeof(char*));
	for(int i = 0; i < slen; ++i){
		if(src[i] == sep || src[i] == '\0'){
			(*arr)[ai] = calloc(wlen+1, sizeof(char));
			for(int j = i-wlen, k = 0; j < i; ++j, ++k){
				(*arr)[ai][k] = src[j];
			}
			++ai;
			wlen = 0;
		}else{
			++wlen;
		}
	}
	*len = wnum;
}

static void swapWords(char ***arr, int a, int b){
	char *tmp = (*arr)[a];
	(*arr)[a] = (*arr)[b];
	(*arr)[b] = tmp;
}

// Adds a tag in the tags array in the row r, sorted by natural string
// comparison with strnatcmp. We assume that when adding a tag all other
// tags are already sorted. Nothing is done if the tag is already in the tags
void insertTag(row *r, char *tag){
	uint32_t l, ltag;
	tag = normalizeStr(tag, &ltag);

	if(ltag == 0){
		return;
	}
	if((r->lenTags+ltag) > MAXTAGS-1){
		fprintf(stderr, "Can't insert tag '%s': too big (%d + %d > %d - 1) (insertTag)",
			tag, r->lenTags, ltag, MAXTAGS);
		exit(EXIT_FAILURE);
	}

	// Dump tags into array of strings and add tag
	char **arr, **tmp;
	split(r->tags, ';', &arr, &l);
	
	if((tmp = realloc(arr, (l+1)*sizeof(char*))) != NULL){
		arr = tmp;
		tmp = NULL;
	}else{
		fprintf(stderr, "Error reallocating array (insertTag)");
		exit(EXIT_FAILURE);
	}
	arr[l] = malloc((len(tag)+1)*sizeof(char));
	strcpy(arr[l], tag);
	
	// Sort tag by natural string comparison, starting from the last element (the new tag)
	for(int i = l; i > 0; --i){
		switch(strnatcmp(arr[i-1], arr[i])){
			case 1:
				// arr[i-1] is higher than arr[i]; swap them
				swapWords(&arr, i, i-1);
				break;
			case -1:
				// arr[i-1] is lower than arr[i]; exit loop
				i = 0;
				break;
			case 0:
				// The tag already exists, no need to alter anything
				free(arr);
				return;
		}
	}
	++l; // Succesfully added new tag

	// Insert tags back into tags member of row structure with the ';' separator in between them
	int tagnum = 0;
	for(int i = 0; i < l; ++i){
		int j = 0;
		while(arr[i][j] != '\0'){
			r->tags[tagnum] = arr[i][j];
			++j;
			++tagnum;
		}
		r->tags[tagnum++] = ';';
	}
	r->tags[tagnum] = '\0';
	r->numTags = l;
	r->lenTags = tagnum;
}

// Remove a tag from the tags array in the row r
// Nothing is done if the tag isnt in the tags
void removeTag(row *r, char *tag){
	uint32_t l, ltag;
	tag = normalizeStr(tag, &ltag);

	if(ltag == 0){
		return;
	}

	// Dump tags into array of strings
	char **arr;
	split(r->tags, ';', &arr, &l);

	// Search for tag and remove it
	for(int i = 0; i <= l; ++i){
		if(sameStr(arr[i], tag)){
			for(int j = i; j < l; ++j){
				arr[j] = arr[j+1];
			}
			--l;
			break;
		}
	}

	// Insert tags back into tags member of row structure with the ';' separator in between them
	int tagnum = 0;
	for(int i = 0; i < l; ++i){
		int j = 0;
		while(arr[i][j] != '\0'){
			r->tags[tagnum] = arr[i][j];
			++j;
			++tagnum;
		}
		r->tags[tagnum++] = ';';
	}
	r->tags[tagnum] = '\0';
	r->numTags = l;
	r->lenTags = tagnum;
}
*/