-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgc.c
More file actions
559 lines (487 loc) · 14.2 KB
/
Copy pathgc.c
File metadata and controls
559 lines (487 loc) · 14.2 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
* EmbeddingBridge - Garbage Collection Implementation
* Copyright (C) 2024 ProgramComputer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
#include <dirent.h>
#include <limits.h>
#include <fcntl.h>
#include "gc.h"
#include "types.h"
#include "error.h"
#include "path_utils.h"
#include "debug.h"
/* Define PATH_MAX if not available */
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
/* Default grace period for unreferenced objects (2 weeks) */
#define DEFAULT_PRUNE_EXPIRE_SECONDS (14 * 24 * 60 * 60)
/* GC lock file name */
#define GC_LOCK_FILE "gc.lock"
/* Forward declarations for helper functions */
static int remove_unreferenced_embeddings(const char* objects_dir, time_t expire_time);
static bool is_referenced(const char* object_id);
static time_t parse_expire_time(const char* expire_str);
/**
* Run garbage collection on the repository
*
* @param prune_expire Expiration string for pruning (e.g., "2.weeks.ago", "now", or "never")
* @param aggressive Whether to do more aggressive optimization
* @param result Pointer to store operation result
* @return Status code (0 = success)
*/
eb_status_t gc_run(const char* prune_expire, bool aggressive, eb_gc_result_t* result)
{
/* Initialize result struct */
if (result) {
result->status = EB_SUCCESS;
result->message[0] = '\0';
result->objects_removed = 0;
result->bytes_freed = 0;
}
/* Check if another GC is running */
if (gc_is_running()) {
if (result) {
result->status = EB_ERROR_LOCK_FAILED;
strcpy(result->message, "Another garbage collection process is running");
}
return EB_ERROR_LOCK_FAILED;
}
/* Get repository path */
char* repo_path = get_repository_path();
if (!repo_path) {
if (result) {
result->status = EB_ERROR_NOT_INITIALIZED;
strcpy(result->message, "Repository not initialized");
}
return EB_ERROR_NOT_INITIALIZED;
}
/* Create GC lock file */
char lock_path[PATH_MAX];
int lock_fd;
snprintf(lock_path, sizeof(lock_path), "%s/%s", repo_path, GC_LOCK_FILE);
lock_fd = open(lock_path, O_CREAT | O_EXCL | O_WRONLY, 0644);
if (lock_fd < 0) {
if (result) {
result->status = EB_ERROR_LOCK_FAILED;
strcpy(result->message, "Failed to create GC lock file");
}
free(repo_path);
return EB_ERROR_LOCK_FAILED;
}
/* Write PID to lock file */
char pid_str[16];
snprintf(pid_str, sizeof(pid_str), "%d\n", (int)getpid());
write(lock_fd, pid_str, strlen(pid_str));
close(lock_fd);
/* Determine expiration time */
time_t expire_time;
if (!prune_expire) {
/* Default: 2 weeks ago */
expire_time = time(NULL) - DEFAULT_PRUNE_EXPIRE_SECONDS;
} else if (strcmp(prune_expire, "now") == 0) {
/* No grace period */
expire_time = time(NULL);
} else if (strcmp(prune_expire, "never") == 0) {
/* Don't prune */
if (result) {
sprintf(result->message, "Pruning disabled, no objects removed");
}
unlink(lock_path); /* Remove lock file */
free(repo_path);
return EB_SUCCESS;
} else {
expire_time = parse_expire_time(prune_expire);
if (expire_time == 0) {
if (result) {
result->status = EB_ERROR_INVALID_PARAMETER;
sprintf(result->message, "Invalid expiration format: %s", prune_expire);
}
unlink(lock_path); /* Remove lock file */
free(repo_path);
return EB_ERROR_INVALID_PARAMETER;
}
}
/* Get objects directory path */
char objects_dir[PATH_MAX];
snprintf(objects_dir, sizeof(objects_dir), "%s/.embr/objects", repo_path);
/* Debug output for diagnosis */
struct stat st;
DEBUG_PRINT("gc_run: Checking objects_dir: %s", objects_dir);
int stat_result = stat(objects_dir, &st);
DEBUG_PRINT("gc_run: stat() result: %d, errno: %d (%s)", stat_result, errno, strerror(errno));
/* Check if objects directory exists */
if (stat_result != 0 || !S_ISDIR(st.st_mode)) {
if (result) {
result->status = EB_ERROR_NOT_INITIALIZED;
sprintf(result->message, "Objects directory not found");
}
unlink(lock_path); /* Remove lock file */
free(repo_path);
return EB_ERROR_NOT_INITIALIZED;
}
/* Remove unreferenced objects */
int removed = remove_unreferenced_embeddings(objects_dir, expire_time);
if (result) {
result->objects_removed = removed;
}
/* Additional aggressive optimization if requested */
if (aggressive) {
/* TODO: Implement additional optimizations like:
* - Repack embeddings
* - Optimize indices
* - Compress data
*/
if (result) {
strcat(result->message, " (aggressive mode)");
}
}
/* Remove lock file */
unlink(lock_path);
free(repo_path);
return EB_SUCCESS;
}
/**
* Check if another garbage collection process is running
*
* @return true if another gc is running, false otherwise
*/
bool gc_is_running(void)
{
char* repo_path = get_repository_path();
if (!repo_path)
return false;
char lock_path[PATH_MAX];
snprintf(lock_path, sizeof(lock_path), "%s/%s", repo_path, GC_LOCK_FILE);
/* Check if lock file exists */
struct stat st;
if (stat(lock_path, &st) == 0) {
/* Lock file exists, read PID from it */
FILE* lock_file = fopen(lock_path, "r");
if (lock_file) {
char pid_str[16];
if (fgets(pid_str, sizeof(pid_str), lock_file)) {
int pid = atoi(pid_str);
/* Check if process with this PID exists */
if (pid > 0) {
char proc_path[32];
snprintf(proc_path, sizeof(proc_path), "/proc/%d", pid);
if (stat(proc_path, &st) == 0) {
/* Process exists, gc is running */
fclose(lock_file);
free(repo_path);
return true;
}
}
}
fclose(lock_file);
/* PID doesn't exist, lock file is stale, remove it */
unlink(lock_path);
}
}
free(repo_path);
return false;
}
/**
* Find unreferenced embedding objects
*
* @param unreferenced_out Array to store unreferenced object hashes
* @param max_unreferenced Maximum number of objects to store
* @param count_out Pointer to store actual count of unreferenced objects
* @param expire_time Expiration time for objects
* @return Status code (0 = success)
*/
eb_status_t gc_find_unreferenced(char** unreferenced_out,
size_t max_unreferenced,
size_t* count_out,
time_t expire_time)
{
if (!unreferenced_out || !count_out)
return EB_ERROR_INVALID_PARAMETER;
/* Initialize count */
*count_out = 0;
/* Get repository path */
char* repo_path = get_repository_path();
if (!repo_path)
return EB_ERROR_NOT_INITIALIZED;
/* Get objects directory path */
char objects_dir[PATH_MAX];
snprintf(objects_dir, sizeof(objects_dir), "%s/.embr/objects", repo_path);
/* Check if objects directory exists */
struct stat st;
if (stat(objects_dir, &st) != 0 || !S_ISDIR(st.st_mode)) {
free(repo_path);
return EB_ERROR_NOT_INITIALIZED;
}
/* Scan objects directory */
DIR* dir = opendir(objects_dir);
if (!dir) {
free(repo_path);
return EB_ERROR_IO;
}
struct dirent* entry;
while ((entry = readdir(dir)) != NULL && *count_out < max_unreferenced) {
if (entry->d_name[0] == '.')
continue;
char object_path[PATH_MAX];
snprintf(object_path, sizeof(object_path), "%s/%s", objects_dir, entry->d_name);
/* Check if it's a file */
if (stat(object_path, &st) == 0 && S_ISREG(st.st_mode)) {
/* Check if it's unreferenced */
if (!is_referenced(entry->d_name)) {
/* Check if it's older than expire_time */
if (st.st_mtime < expire_time) {
/* Add to unreferenced list */
unreferenced_out[*count_out] = strdup(entry->d_name);
(*count_out)++;
}
}
}
}
closedir(dir);
free(repo_path);
return EB_SUCCESS;
}
/**
* Remove a specific object from the repository
*
* @param object_hash Hash of the object to remove
* @param size_removed_out Pointer to store the size of removed object
* @return Status code (0 = success)
*/
eb_status_t gc_remove_object(const char* object_hash, size_t* size_removed_out)
{
if (!object_hash)
return EB_ERROR_INVALID_PARAMETER;
/* Initialize size if pointer provided */
if (size_removed_out)
*size_removed_out = 0;
/* Get repository path */
char* repo_path = get_repository_path();
if (!repo_path)
return EB_ERROR_NOT_INITIALIZED;
/* Get object path */
char object_path[PATH_MAX];
snprintf(object_path, sizeof(object_path), "%s/objects/%s", repo_path, object_hash);
/* Check if object exists */
struct stat st;
if (stat(object_path, &st) != 0) {
free(repo_path);
return EB_ERROR_NOT_FOUND;
}
/* Check if it's referenced */
if (is_referenced(object_hash)) {
free(repo_path);
return EB_ERROR_REFERENCED;
}
/* Store size if requested */
if (size_removed_out)
*size_removed_out = st.st_size;
/* Remove the file */
if (unlink(object_path) != 0) {
free(repo_path);
return EB_ERROR_IO;
}
free(repo_path);
return EB_SUCCESS;
}
/**
* Parse an expiration time string (e.g., "2.weeks.ago") into a timestamp
*/
static time_t parse_expire_time(const char* expire_str)
{
time_t now = time(NULL);
time_t result = 0;
char* str_copy = strdup(expire_str);
if (!str_copy)
return 0;
/* Parse format like "2.weeks.ago" */
char* token = strtok(str_copy, ".");
if (!token) {
free(str_copy);
return 0;
}
/* Get the number value */
char* endptr;
long value = strtol(token, &endptr, 10);
if (*endptr != '\0' || value < 0) {
free(str_copy);
return 0;
}
/* Get the unit */
token = strtok(NULL, ".");
if (!token) {
free(str_copy);
return 0;
}
/* Convert to seconds based on unit */
long seconds = 0;
if (strcmp(token, "seconds") == 0 || strcmp(token, "second") == 0) {
seconds = value;
} else if (strcmp(token, "minutes") == 0 || strcmp(token, "minute") == 0) {
seconds = value * 60;
} else if (strcmp(token, "hours") == 0 || strcmp(token, "hour") == 0) {
seconds = value * 60 * 60;
} else if (strcmp(token, "days") == 0 || strcmp(token, "day") == 0) {
seconds = value * 24 * 60 * 60;
} else if (strcmp(token, "weeks") == 0 || strcmp(token, "week") == 0) {
seconds = value * 7 * 24 * 60 * 60;
} else if (strcmp(token, "months") == 0 || strcmp(token, "month") == 0) {
seconds = value * 30 * 24 * 60 * 60; /* Approximate */
} else if (strcmp(token, "years") == 0 || strcmp(token, "year") == 0) {
seconds = value * 365 * 24 * 60 * 60; /* Approximate */
} else {
free(str_copy);
return 0;
}
/* Verify the "ago" part */
token = strtok(NULL, ".");
if (!token || strcmp(token, "ago") != 0) {
free(str_copy);
return 0;
}
result = now - seconds;
free(str_copy);
return result;
}
/**
* Check if an object is referenced by any set
*/
static bool is_referenced(const char* object_id)
{
/* Get repository path */
char* repo_path = get_repository_path();
if (!repo_path)
return false;
/* Get sets directory path */
char sets_dir[PATH_MAX];
snprintf(sets_dir, sizeof(sets_dir), "%s/sets", repo_path);
/* Check if sets directory exists */
struct stat st;
if (stat(sets_dir, &st) != 0 || !S_ISDIR(st.st_mode)) {
free(repo_path);
return false;
}
/* Iterate through all sets */
DIR* dir = opendir(sets_dir);
if (!dir) {
free(repo_path);
return false;
}
struct dirent* entry;
bool referenced = false;
while ((entry = readdir(dir)) != NULL && !referenced) {
if (entry->d_name[0] == '.')
continue;
char set_dir[PATH_MAX];
snprintf(set_dir, sizeof(set_dir), "%s/sets/%s", repo_path, entry->d_name);
/* Check if it's a directory */
if (stat(set_dir, &st) == 0 && S_ISDIR(st.st_mode)) {
/* 1. Check log file for object_id as the second field */
char log_path[PATH_MAX];
snprintf(log_path, sizeof(log_path), "%s/log", set_dir);
FILE* log_fp = fopen(log_path, "r");
if (log_fp) {
char line[2048];
while (fgets(line, sizeof(line), log_fp)) {
char ts[32], hash[65];
if (sscanf(line, "%31s %64s", ts, hash) == 2) {
if (strcmp(hash, object_id) == 0) {
referenced = true;
break;
}
}
}
fclose(log_fp);
}
}
}
closedir(dir);
if (referenced) {
free(repo_path);
return true;
}
/* Fallback: check refs as before */
dir = opendir(sets_dir);
if (!dir) {
free(repo_path);
return false;
}
while ((entry = readdir(dir)) != NULL && !referenced) {
if (entry->d_name[0] == '.')
continue;
char set_dir[PATH_MAX];
snprintf(set_dir, sizeof(set_dir), "%s/sets/%s", repo_path, entry->d_name);
if (stat(set_dir, &st) == 0 && S_ISDIR(st.st_mode)) {
char reference_path[PATH_MAX];
snprintf(reference_path, sizeof(reference_path), "%s/refs/%s", set_dir, object_id);
if (stat(reference_path, &st) == 0) {
referenced = true;
}
}
}
closedir(dir);
free(repo_path);
return referenced;
}
/**
* Remove unreferenced embedding objects older than the expire time
*
* @param objects_dir Path to the objects directory
* @param expire_time Timestamp before which unreferenced objects will be removed
* @return Number of objects removed
*/
static int remove_unreferenced_embeddings(const char* objects_dir, time_t expire_time)
{
int removed_count = 0;
size_t bytes_freed = 0;
/* In a real implementation, this would scan the objects directory for
* embedding objects, check if they are referenced by any set, and
* remove them if they are unreferenced and older than expire_time.
*
* For this simulation, we'll create a mock implementation that:
* 1. Looks for files in the "objects" directory
* 2. Checks if they're referenced
* 3. Checks their modification time
* 4. Removes them if unreferenced and old enough
*/
DIR* dir = opendir(objects_dir);
if (!dir)
return 0;
struct dirent* entry;
struct stat st;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] == '.')
continue;
char object_path[PATH_MAX];
snprintf(object_path, sizeof(object_path), "%s/%s", objects_dir, entry->d_name);
/* Check if it's a file */
if (stat(object_path, &st) == 0 && S_ISREG(st.st_mode)) {
/* Check if it's unreferenced */
if (!is_referenced(entry->d_name)) {
/* Check if it's older than expire_time */
if (st.st_mtime < expire_time) {
/* Remove the file */
bytes_freed += st.st_size;
if (unlink(object_path) == 0)
removed_count++;
}
}
}
}
closedir(dir);
return removed_count;
}