-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdboperations.c
More file actions
214 lines (196 loc) · 5.89 KB
/
dboperations.c
File metadata and controls
214 lines (196 loc) · 5.89 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include "dboperations.h"
#define MAX_KEYS 200 // Number of keys
#define MAX_PATH_LENGTH 100
db_entry_t entries[MAX_KEYS] = {0};
pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER;
// Initialize the database
void db_init() {
for (int i = 0; i < MAX_KEYS; i++) {
pthread_mutex_init(&entries[i].mutex, NULL);
pthread_cond_init(&entries[i].available, NULL);
}
}
// Get the index of the key
int get_index(char *key) {
for (int i = 0; i < MAX_KEYS; i++) {
if (strcmp(entries[i].name, key) == 0) {
return i;
}
}
return -1;
}
// Get the first index which is invalid
int get_free_index() {
// TODO: Can keep a start variable instead of always starting from 0
for (int i = 0; i < MAX_KEYS; i++) {
if (entries[i].state == DB_INVALID) {
return i;
}
}
return -1;
}
// Get the file path for the index
int get_file_path(int index, char *filepath) {
return sprintf(filepath, "%s/data.%d", BASE_FOLDER, index);
}
// Read the value for the key and store it in value
// Returns 0 on success, -1 on failure
int db_read(char *key, char *value) {
pthread_mutex_lock(&db_mutex);
int index = get_index(key);
// Only read valid entries
if (index == -1) {
fprintf(stderr, "[READ] invalid key %s\n", key);
pthread_mutex_unlock(&db_mutex);
return -1;
}
db_entry_t *entry = &entries[index];
// get the lock for this entry
pthread_mutex_lock(&entry->mutex);
// lock on entries can be released now
pthread_mutex_unlock(&db_mutex);
// Wait till entry becomes available
while (entry->state == DB_BUSY) {
pthread_cond_wait(&entry->available, &entry->mutex);
}
// Check for invalid entry
if (entry->state == DB_INVALID) {
fprintf(stderr, "[WRITE] invalid entry, key %s\n", key);
pthread_mutex_unlock(&entry->mutex);
return -1;
}
entry->state = DB_BUSY; // mark the entry as busy
char filepath[MAX_PATH_LENGTH] = {'\0'};
if (get_file_path(index, filepath) < 0) {
fprintf(stderr, "[READ] filepath failed, key %s\n", key);
pthread_mutex_unlock(&entry->mutex);
return -1;
}
int fd = open(filepath, O_RDONLY);
if (fd < 0) {
perror("[READ] file open for read failed");
pthread_mutex_unlock(&entry->mutex);
return -1;
}
ssize_t bytes_read = read(fd, value, DB_VALUE_MAXLENGTH);
if (bytes_read == -1) {
perror("[READ] file read failed");
pthread_mutex_unlock(&entry->mutex);
return -1;
}
close(fd);
entry->state = DB_VALID;
pthread_cond_signal(&entry->available); // signal any threads waiting for this entry
pthread_mutex_unlock(&entry->mutex);
return 0;
}
// Write the value for the key
// Returns 0 on success, -1 on failure
int db_write(char *key, char *value) {
// lock entries here so that delete does not get access to this
pthread_mutex_lock(&db_mutex);
int index = get_index(key); // Check if the key already exists
if (index == -1) {
index = get_free_index(key); // Find an invalid entry
if (index == -1) {
// unlock entries here as well
pthread_mutex_unlock(&db_mutex);
fprintf(stderr, "[WRITE] invalid key %s\n", key);
return -1;
}
}
db_entry_t *entry = &entries[index];
pthread_mutex_lock(&entry->mutex);
// Wait till the entry becomes available (not busy)
while (entry->state == DB_BUSY) {
pthread_cond_wait(&entry->available, &entry->mutex);
}
// If this is rewrite and state is invalid then key is deleted -> invalidate write
if (entry->state == DB_INVALID && strcmp(entry->name, "") != 0) {
fprintf(stderr, "[WRITE] invalid write, key %s\n", key);
pthread_mutex_unlock(&entry->mutex);
pthread_mutex_unlock(&db_mutex);
return -1;
}
entry->state = DB_BUSY;
// unlock entries here
pthread_mutex_unlock(&db_mutex);
usleep(random() % 10000); // add a sleeping delay
// perform write
char filepath[MAX_PATH_LENGTH] = {'\0'};
if (get_file_path(index, filepath) < 0) {
fprintf(stderr, "[WRITE] filepath failed, key %s\n", key);
pthread_mutex_unlock(&entry->mutex);
return -1;
}
int fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0777);
if (fd < 0) {
perror("[WRITE] file open failed");
pthread_mutex_unlock(&entry->mutex);
return -1;
}
ssize_t bytes_written = write(fd, value, strlen(value));
if (bytes_written == -1) {
perror("[WRITE] file write failed");
pthread_mutex_unlock(&entry->mutex);
return -1;
}
close(fd);
// Update db entry
strcpy(entry->name, key);
entry->state = DB_VALID;
pthread_cond_signal(&entry->available); // signal any threads waiting for this entry
pthread_mutex_unlock(&entry->mutex);
return 0;
}
// Delete the key
// Returns 0 on success, -1 on failure
int db_delete(char *key) {
// Get the lock to search for a key
pthread_mutex_lock(&db_mutex);
int index = get_index(key);
if (index == -1) {
fprintf(stderr, "[DELETE] invalid key %s\n", key);
pthread_mutex_unlock(&db_mutex);
return -1;
}
db_entry_t *entry = &entries[index];
// Get the lock for this entry
pthread_mutex_lock(&entry->mutex);
// lock on entries can be release now
pthread_mutex_unlock(&db_mutex);
// wait for the entry to become available
while (entry->state == DB_BUSY) {
pthread_cond_wait(&entry->available, &entry->mutex);
}
// Check if the entry became invalid
if (entry->state == DB_INVALID) {
fprintf(stderr, "[DELETE] invalid delete, key %s \n", key);
pthread_mutex_unlock(&entry->mutex);
return -1;
}
char filepath[MAX_PATH_LENGTH] = {'\0'};
if (get_file_path(index, filepath) < 0) {
fprintf(stderr, "[DELETE] filepath failed, key %s\n", key);
pthread_mutex_unlock(&entry->mutex);
return -1;
}
if (unlink(filepath) < 0) {
perror("[DELETE] unlink failed");
pthread_mutex_unlock(&entry->mutex);
return -1;
}
strcpy(entry->name, ""); // clear the key
entry->state = DB_INVALID; // mark the entry as invalid
pthread_cond_signal(&entry->available); // signal any threads waiting for this entry
pthread_mutex_unlock(&entry->mutex);
return 0;
}