-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.c
536 lines (438 loc) · 16.6 KB
/
storage.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
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
#include "storage.h"
#include "kv_store.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <pthread.h>
#define STORAGE_FILE "storage.db"
#define SNAPSHOT_FILE "snapshot.db"
#define TEMP_SNAPSHOT_FILE "snapshot.db.tmp"
#define BUFFER_SIZE 32768 // Buffer boyutunu 32KB'a çıkarıyorum
#define SNAPSHOT_INTERVAL 300 // 5 dakikalık default snapshot aralığı
static FILE* storage_file = NULL;
static char storage_path[256];
static char write_buffer[BUFFER_SIZE];
static size_t buffer_pos = 0;
static pthread_mutex_t buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_t snapshot_thread;
static int snapshot_interval = SNAPSHOT_INTERVAL;
static bool snapshot_thread_running = false;
static bool shutdown_requested = false;
// Snapshot thread fonksiyonu - belirli aralıklarla snapshot oluşturur
static void* snapshot_thread_func(void* arg) {
if (logging_enabled) printf("DEBUG: Snapshot thread started with interval %d seconds\n", snapshot_interval);
while (!shutdown_requested) {
sleep(snapshot_interval);
if (shutdown_requested) break;
if (logging_enabled) printf("DEBUG: Automatic snapshot triggered\n");
storage_save_snapshot();
}
if (logging_enabled) printf("DEBUG: Snapshot thread exiting\n");
return NULL;
}
// Buffer yönetimi - artık AOF için değil sadece snapshot yazma için kullanılacak
static void flush_buffer() {
pthread_mutex_lock(&buffer_mutex);
if (buffer_pos > 0 && storage_file) {
if (logging_enabled) printf("DEBUG: Flushing buffer with %zu bytes\n", buffer_pos);
size_t written = fwrite(write_buffer, 1, buffer_pos, storage_file);
if (written != buffer_pos) {
if (logging_enabled) printf("DEBUG: Warning - Failed to write all data to storage file\n");
}
fflush(storage_file);
buffer_pos = 0;
if (logging_enabled) printf("DEBUG: Buffer flushed successfully\n");
}
pthread_mutex_unlock(&buffer_mutex);
}
static void append_to_buffer(const char* str) {
if (!str || !storage_file) return;
pthread_mutex_lock(&buffer_mutex);
size_t len = strlen(str);
if (buffer_pos + len >= BUFFER_SIZE) {
// Buffer doldu, hemen flush edelim
size_t written = fwrite(write_buffer, 1, buffer_pos, storage_file);
if (written != buffer_pos && logging_enabled) {
printf("DEBUG: Warning - Partial write to storage file\n");
}
fflush(storage_file);
buffer_pos = 0;
}
if (len < BUFFER_SIZE) {
memcpy(write_buffer + buffer_pos, str, len);
buffer_pos += len;
} else {
// Çok büyük string, doğrudan dosyaya yazalım
if (logging_enabled) printf("DEBUG: String too large for buffer, writing directly\n");
fwrite(str, 1, len, storage_file);
fflush(storage_file);
}
// Eğer buffer 3/4 dolduysa, flush yapalım
if (buffer_pos > (BUFFER_SIZE * 3) / 4) {
size_t written = fwrite(write_buffer, 1, buffer_pos, storage_file);
if (written != buffer_pos && logging_enabled) {
printf("DEBUG: Warning - Partial write to storage file\n");
}
fflush(storage_file);
buffer_pos = 0;
}
pthread_mutex_unlock(&buffer_mutex);
}
// Storage yönetimi
Storage* storage_init(void) {
if (logging_enabled) printf("DEBUG: Initializing storage\n");
Storage* storage = (Storage*)malloc(sizeof(Storage));
if (!storage) {
if (logging_enabled) printf("DEBUG: Failed to allocate storage structure\n");
return NULL;
}
storage->file_path = strdup(SNAPSHOT_FILE);
if (!storage->file_path) {
if (logging_enabled) printf("DEBUG: Failed to duplicate file path\n");
free(storage);
return NULL;
}
// Önce KV store'u başlat
kv_init();
// Snapshot dosyasını okuma için aç
storage->file = fopen(storage->file_path, "r");
if (!storage->file) {
if (logging_enabled) printf("DEBUG: No snapshot file found, starting fresh\n");
} else {
// Snapshot dosyasını oku
if (logging_enabled) printf("DEBUG: Loading data from snapshot file\n");
storage_load_snapshot();
fclose(storage->file);
storage->file = NULL;
}
// Snapshot dosyasını yazma için aç
storage->file = fopen(storage->file_path, "w");
if (!storage->file) {
if (logging_enabled) printf("ERROR: Failed to open snapshot file for writing\n");
free(storage->file_path);
free(storage);
return NULL;
}
// Global storage_file'ı güncelle
storage_file = storage->file;
// Buffer'ı sıfırla
pthread_mutex_lock(&buffer_mutex);
buffer_pos = 0;
memset(write_buffer, 0, BUFFER_SIZE);
pthread_mutex_unlock(&buffer_mutex);
// Snapshot thread'i başlat
storage_schedule_snapshot(SNAPSHOT_INTERVAL);
if (logging_enabled) printf("DEBUG: Storage initialization completed\n");
return storage;
}
void storage_free(Storage* storage) {
if (!storage) return;
if (logging_enabled) printf("DEBUG: Starting storage cleanup\n");
printf("Saving snapshot to disk...\n");
// Snapshot thread'i durdur
shutdown_requested = true;
if (snapshot_thread_running) {
if (logging_enabled) printf("DEBUG: Waiting for snapshot thread to exit\n");
pthread_join(snapshot_thread, NULL);
snapshot_thread_running = false;
}
// Son bir snapshot al
storage_save_snapshot();
// Dosyayı kapat
if (storage->file) {
if (logging_enabled) printf("DEBUG: Closing storage file\n");
if (fclose(storage->file) != 0) {
if (logging_enabled) printf("DEBUG: Warning - Failed to close storage file\n");
}
storage->file = NULL;
}
// Dosya yolunu temizle
if (storage->file_path) {
if (logging_enabled) printf("DEBUG: Freeing file path\n");
free(storage->file_path);
storage->file_path = NULL;
}
// Storage yapısını temizle
if (logging_enabled) printf("DEBUG: Freeing storage structure\n");
free(storage);
if (logging_enabled) printf("DEBUG: Storage cleanup completed\n");
}
// Temel operasyonlar
bool storage_set(Storage* storage, const char* key, const char* value) {
if (!storage || !key || !value) return false;
// Key-value çiftini hafızaya kaydet
kv_set(key, value);
return true;
}
bool storage_set_with_ttl(Storage* storage, const char* key, const char* value, int ttl) {
if (!storage || !key || !value) return false;
// Key-value çiftini hafızaya kaydet
kv_set_with_ttl(key, value, ttl);
return true;
}
char* storage_get(Storage* storage, const char* key) {
if (!storage || !key) return NULL;
// Key'i tabloda ara
const char* value = kv_get(key);
if (!value) return NULL;
// Değeri güvenli bir şekilde kopyala
size_t len = strlen(value);
char* result = malloc(len + 1);
if (!result) return NULL;
memcpy(result, value, len);
result[len] = '\0';
return result;
}
bool storage_delete(Storage* storage, const char* key) {
if (!storage || !key) return false;
// Key'i hafızadan sil
kv_del(key);
return true;
}
// AOF fonksiyonları yerine boş fonksiyonlar (geriye dönük uyumluluk için)
void storage_append_set(const char* key, const char* value, const int ttl) {
// Artık AOF kullanmıyoruz, snapshot'a geçtik
return;
}
void storage_append_del(const char* key) {
// Artık AOF kullanmıyoruz, snapshot'a geçtik
return;
}
void storage_load() {
// Artık AOF kullanmıyoruz, snapshot'a geçtik
return;
}
// Snapshot işlemleri
bool storage_save_snapshot() {
if (logging_enabled) printf("DEBUG: Saving snapshot\n");
// Temporary dosya oluştur
FILE* f = fopen(TEMP_SNAPSHOT_FILE, "w");
if (!f) {
if (logging_enabled) printf("DEBUG: Failed to create temporary file for snapshot\n");
return false;
}
// Daha verimli yazma için tampon boyutunu ayarla
setvbuf(f, NULL, _IOFBF, BUFFER_SIZE);
HashTable* table = kv_get_table();
if (!table) {
fclose(f);
if (logging_enabled) printf("DEBUG: Failed to get hash table for snapshot\n");
return false;
}
time_t now = time(NULL);
size_t total_entries = 0;
size_t live_entries = 0;
// Verileri kilitle
pthread_mutex_lock(&table->mutex);
// Başlık bilgisi yaz (format: AYTDB_SNAPSHOT_V1)
fprintf(f, "AYTDB_SNAPSHOT_V1\n");
fprintf(f, "TIME:%ld\n", now);
// Toplam girdi sayısını hesapla
for (int i = 0; i < table->size; i++) {
if (table->entries[i] != NULL) {
total_entries++;
// Sadece yaşayan girişleri say
if (table->entries[i]->expire_at == 0 || table->entries[i]->expire_at > now) {
live_entries++;
}
}
}
// Toplam giriş sayısını yaz
fprintf(f, "ENTRIES:%zu\n", live_entries);
fprintf(f, "---\n"); // Başlık sonu ayracı
// Girişleri yaz - metin formatında, daha okunaklı
for (int i = 0; i < table->size; i++) {
if (table->entries[i] != NULL) {
// Sadece yaşayan girişleri yaz
if (table->entries[i]->expire_at == 0 || table->entries[i]->expire_at > now) {
time_t ttl = table->entries[i]->expire_at == 0 ? 0 : table->entries[i]->expire_at - now;
// Anahtar değer çiftini ve TTL'i yaz
fprintf(f, "KEY:%s\n", table->entries[i]->key);
fprintf(f, "VALUE:%s\n", table->entries[i]->value);
fprintf(f, "TTL:%ld\n", ttl);
fprintf(f, "---\n"); // Ayraç
}
}
}
pthread_mutex_unlock(&table->mutex);
fclose(f);
// Dosya değişimi
if (remove(SNAPSHOT_FILE) != 0 && errno != ENOENT) {
if (logging_enabled) printf("DEBUG: Failed to remove old snapshot file: %s\n", strerror(errno));
}
if (rename(TEMP_SNAPSHOT_FILE, SNAPSHOT_FILE) != 0) {
if (logging_enabled) printf("DEBUG: Failed to rename temporary file: %s\n", strerror(errno));
return false;
}
if (logging_enabled) printf("DEBUG: Snapshot saved. Total entries: %zu, Live entries: %zu\n",
total_entries, live_entries);
return true;
}
bool storage_load_snapshot() {
if (logging_enabled) printf("DEBUG: Loading snapshot\n");
FILE* f = fopen(SNAPSHOT_FILE, "r");
if (!f) {
if (logging_enabled) printf("DEBUG: No snapshot file found\n");
return false;
}
// Snapshot başlığını oku ve doğrula
char header[50];
if (!fgets(header, sizeof(header), f)) {
if (logging_enabled) printf("DEBUG: Failed to read snapshot header\n");
fclose(f);
return false;
}
// Başlık satırındaki yeni satır karakterini kaldır
header[strcspn(header, "\n")] = 0;
if (strcmp(header, "AYTDB_SNAPSHOT_V1") != 0) {
if (logging_enabled) printf("DEBUG: Invalid snapshot header: %s\n", header);
fclose(f);
return false;
}
// Zaman bilgisini oku
char time_str[50];
if (!fgets(time_str, sizeof(time_str), f)) {
if (logging_enabled) printf("DEBUG: Failed to read snapshot time\n");
fclose(f);
return false;
}
time_str[strcspn(time_str, "\n")] = 0;
time_t snapshot_time = 0;
if (sscanf(time_str, "TIME:%ld", &snapshot_time) != 1) {
if (logging_enabled) printf("DEBUG: Invalid time format: %s\n", time_str);
fclose(f);
return false;
}
// Girdi sayısını oku
char entries_str[50];
if (!fgets(entries_str, sizeof(entries_str), f)) {
if (logging_enabled) printf("DEBUG: Failed to read entry count\n");
fclose(f);
return false;
}
entries_str[strcspn(entries_str, "\n")] = 0;
size_t entry_count = 0;
if (sscanf(entries_str, "ENTRIES:%zu", &entry_count) != 1) {
if (logging_enabled) printf("DEBUG: Invalid entries format: %s\n", entries_str);
fclose(f);
return false;
}
// Ayraç satırını oku
char separator[50];
if (!fgets(separator, sizeof(separator), f)) {
if (logging_enabled) printf("DEBUG: Failed to read separator\n");
fclose(f);
return false;
}
separator[strcspn(separator, "\n")] = 0;
if (strcmp(separator, "---") != 0) {
if (logging_enabled) printf("DEBUG: Invalid separator: %s\n", separator);
fclose(f);
return false;
}
// Girişleri oku
time_t now = time(NULL);
size_t entries_loaded = 0;
char line[MAX_LINE_SIZE];
char key[MAX_KEY_SIZE] = "";
char value[MAX_VALUE_SIZE] = "";
time_t ttl = 0;
bool has_key = false;
bool has_value = false;
bool has_ttl = false;
if (logging_enabled) printf("DEBUG: Loading %zu entries from snapshot created at %s",
entry_count, ctime(&snapshot_time));
// Satır satır dosyayı oku ve key-value çiftlerini işle
while (fgets(line, sizeof(line), f)) {
line[strcspn(line, "\n")] = 0; // Yeni satır karakterini kaldır
if (strcmp(line, "---") == 0) {
// Bir kayıt bitti, tamamlanmışsa kaydet
if (has_key && has_value && has_ttl) {
// TTL'i kontrol et ve girişi ekle
if (ttl == 0 || now + ttl > now) { // overflow kontrolü
if (logging_enabled && entries_loaded < 5) {
printf("DEBUG: Loading key '%s' with value '%s' and TTL %ld\n", key, value, ttl);
}
kv_set_with_ttl(key, value, ttl);
entries_loaded++;
} else if (logging_enabled && entries_loaded < 5) {
printf("DEBUG: Skipping expired key '%s' with TTL %ld\n", key, ttl);
}
}
// Yeni kayıt için bayrakları sıfırla
has_key = false;
has_value = false;
has_ttl = false;
continue;
}
// Anahtar satırı
if (strncmp(line, "KEY:", 4) == 0) {
strncpy(key, line + 4, MAX_KEY_SIZE - 1);
key[MAX_KEY_SIZE - 1] = '\0';
has_key = true;
continue;
}
// Değer satırı
if (strncmp(line, "VALUE:", 6) == 0) {
strncpy(value, line + 6, MAX_VALUE_SIZE - 1);
value[MAX_VALUE_SIZE - 1] = '\0';
has_value = true;
continue;
}
// TTL satırı
if (strncmp(line, "TTL:", 4) == 0) {
ttl = atol(line + 4);
has_ttl = true;
continue;
}
}
// Son kayıt için kontrol
if (has_key && has_value && has_ttl) {
if (ttl == 0 || now + ttl > now) { // overflow kontrolü
if (logging_enabled && entries_loaded < 5) {
printf("DEBUG: Loading key '%s' with value '%s' and TTL %ld\n", key, value, ttl);
}
kv_set_with_ttl(key, value, ttl);
entries_loaded++;
}
}
fclose(f);
if (logging_enabled) printf("DEBUG: Snapshot load completed, loaded %zu/%zu entries\n",
entries_loaded, entry_count);
return entries_loaded > 0;
}
void storage_schedule_snapshot(int interval_seconds) {
// Eğer zaten çalışan bir thread varsa, onu durdur
if (snapshot_thread_running) {
shutdown_requested = true;
pthread_join(snapshot_thread, NULL);
snapshot_thread_running = false;
shutdown_requested = false;
}
// Yeni aralık değerini ayarla
snapshot_interval = interval_seconds > 0 ? interval_seconds : SNAPSHOT_INTERVAL;
// Yeni snapshot thread'i başlat
if (pthread_create(&snapshot_thread, NULL, snapshot_thread_func, NULL) == 0) {
snapshot_thread_running = true;
if (logging_enabled) printf("DEBUG: Snapshot thread scheduled with interval %d seconds\n", snapshot_interval);
} else {
if (logging_enabled) printf("ERROR: Failed to create snapshot thread\n");
}
}
void storage_compact() {
// Artık compact yapmak yerine snapshot alıyoruz
if (logging_enabled) printf("DEBUG: Compaction requested, taking snapshot instead\n");
storage_save_snapshot();
printf("Snapshot saved successfully.\n");
}
long storage_file_size() {
struct stat st;
if (stat(SNAPSHOT_FILE, &st) == 0) {
return st.st_size;
}
return 0;
}