-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvsmctl.c
234 lines (233 loc) · 7.44 KB
/
kvsmctl.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
/*#include <limits.h>*/
/*#include <stdio.h>*/
/*#include <string.h>*/
/*#include <unistd.h>*/
/**/
/*#include "finwo/io.h"*/
/*#include "rxi/log.h"*/
/*#include "tidwall/buf.h"*/
/**/
/*#include "kvsm.h"*/
/**/
/*void usage_global(char **argv) {*/
/* printf("\n");*/
/* printf("Usage: %s [global opts] command [command opts]\n", argv[0]);*/
/* printf("\n");*/
/* printf("Global options\n");*/
/* printf(" -h Show this usage\n");*/
/* printf(" -f filename Set database file to operate on\n");*/
/* printf(" -v level Set verbosity level (fatal,error,warn,info,debug,trace)\n");*/
/* printf("\n");*/
/* printf("Commands\n");*/
/* printf(" current-increment Outputs the current transaction increment\n");*/
/* printf(" compact Merge transactions, potentially freeing up disk space\n");*/
/* printf(" serialize <increment> Serialize a transaction into hex\n");*/
/* printf(" ingest Ingest a hex transaction and store it\n");*/
/* printf(" get [key] Outputs the value of the given/stdin key to stdout\n");*/
/* printf(" del [key] Writes a tombstone on the given/stdin key in a new transaction\n");*/
/* printf(" set <key> Sets the value of the given key to stdin data in a new transaction\n");*/
/* printf("\n");*/
/*}*/
/**/
int main(int argc, char **argv) {
/* log_set_level(LOG_INFO);*/
/* char *filename = NULL;*/
/* char *command = NULL;*/
/* int64_t i;*/
/**/
/* // Parse global options*/
/* int c;*/
/* while((c = getopt(argc, argv, "hf:v:")) != -1) {*/
/* switch(c) {*/
/* case 'h':*/
/* usage_global(argv);*/
/* return 0;*/
/* case 'f':*/
/* filename = optarg;*/
/* break;*/
/* case 'v':*/
/* if (0) {*/
/* // Intentionally empty*/
/* } else if (!strcasecmp(optarg, "trace")) {*/
/* log_set_level(LOG_TRACE);*/
/* } else if (!strcasecmp(optarg, "debug")) {*/
/* log_set_level(LOG_DEBUG);*/
/* } else if (!strcasecmp(optarg, "info")) {*/
/* log_set_level(LOG_INFO);*/
/* } else if (!strcasecmp(optarg, "warn")) {*/
/* log_set_level(LOG_WARN);*/
/* } else if (!strcasecmp(optarg, "error")) {*/
/* log_set_level(LOG_ERROR);*/
/* } else if (!strcasecmp(optarg, "fatal")) {*/
/* log_set_level(LOG_FATAL);*/
/* } else {*/
/* log_fatal("Unknown log level: %s", optarg);*/
/* return 1;*/
/* }*/
/* break;*/
/* default:*/
/* log_fatal("illegal option", c);*/
/* return 1;*/
/* }*/
/* }*/
/* if (optind < argc) {*/
/* command = argv[optind++];*/
/* }*/
/* if (!command) {*/
/* log_fatal("No command given");*/
/* return 1;*/
/* }*/
/* if (!filename) {*/
/* log_fatal("No storage file given");*/
/* return 1;*/
/* }*/
/**/
/* struct kvsm *ctx = kvsm_open(filename, 0);*/
/**/
/* if (0) {*/
/* // Intentionally empty*/
/* } else if (!strcasecmp(command, "mini-stat")) {*/
/**/
/* struct kvsm_cursor *current = kvsm_cursor_load(ctx, ctx->current_offset);*/
/* struct kvsm_cursor *fetched = kvsm_cursor_fetch(ctx, ctx->current_increment);*/
/* struct kvsm_cursor *parent = kvsm_cursor_previous(current);*/
/* struct kvsm_cursor *recurrent = kvsm_cursor_next(parent);*/
/**/
/* printf("Current : %lld @ %llx\n", current->increment , current->offset );*/
/* printf("Fetched : %lld @ %llx\n", fetched->increment , fetched->offset );*/
/* printf("Parent : %lld @ %llx\n", parent->increment , parent->offset );*/
/* printf("Recurrent: %lld @ %llx\n", recurrent->increment, recurrent->offset);*/
/**/
/* } else if (!strcasecmp(command, "current-increment")) {*/
/* printf("%lld\n", ctx->current_increment);*/
/* } else if (!strcasecmp(command, "compact")) {*/
/* kvsm_compact(ctx);*/
/* } else if (!strcasecmp(command, "get")) {*/
/* struct buf *key = calloc(1, sizeof(struct buf));*/
/**/
/* if (optind < argc) {*/
/* buf_append(key, argv[optind], strlen(argv[optind]));*/
/* optind++;*/
/* } else {*/
/* log_fatal("Reading key from stdin not implemented");*/
/* return 1;*/
/* }*/
/**/
/* struct buf *response = kvsm_get(ctx, key);*/
/* if (!response) {*/
/* printf("(NULL)\n");*/
/* } else {*/
/* write(STDOUT_FILENO, response->data, response->len);*/
/* buf_clear(response);*/
/* free(response);*/
/* }*/
/**/
/* } else if (!strcasecmp(command, "del")) {*/
/* struct buf *key = calloc(1, sizeof(struct buf));*/
/**/
/* if (optind < argc) {*/
/* buf_append(key, argv[optind], strlen(argv[optind]));*/
/* optind++;*/
/* } else {*/
/* log_fatal("Reading key from stdin not implemented");*/
/* return 1;*/
/* }*/
/**/
/* KVSM_RESPONSE response = kvsm_del(ctx, key);*/
/* if (response != KVSM_OK) {*/
/* fprintf(stderr, "Error during deletion\n");*/
/* }*/
/**/
/* } else if (!strcasecmp(command, "set")) {*/
/* struct buf *key = calloc(1, sizeof(struct buf));*/
/* struct buf *value = calloc(1, sizeof(struct buf));*/
/**/
/* if (optind < argc) {*/
/* buf_append(key, argv[optind], strlen(argv[optind]));*/
/* optind++;*/
/* } else {*/
/* log_fatal("Reading key from stdin not implemented");*/
/* return 1;*/
/* }*/
/**/
/* if (optind < argc) {*/
/* buf_append(value, argv[optind], strlen(argv[optind]));*/
/* optind++;*/
/* } else {*/
/* log_fatal("Reading value from stdin not implemented");*/
/* return 1;*/
/* }*/
/**/
/* KVSM_RESPONSE response = kvsm_set(ctx, key, value);*/
/* if (response != KVSM_OK) {*/
/* fprintf(stderr, "Error during setting of value\n");*/
/* }*/
/**/
/* } else if (!strcasecmp(command, "serialize")) {*/
/* uint64_t increment = ctx->current_increment;*/
/**/
/* if (optind < argc) {*/
/* increment = atoll(argv[optind]);*/
/* optind++;*/
/* } else {*/
/* // Intentionally empty, serialized the current increment;*/
/* }*/
/**/
/* struct kvsm_cursor *cursor = kvsm_cursor_fetch(ctx, increment);*/
/* if (!cursor) {*/
/* printf("(NULL)\n");*/
/* return 0;*/
/* }*/
/**/
/* struct buf *serialized = kvsm_cursor_serialize(cursor);*/
/* if (!serialized) {*/
/* printf("(NULL)\n");*/
/* return 0;*/
/* }*/
/**/
/* // TODO: optimize?*/
/* i = 0;*/
/* while(i < serialized->len) {*/
/* printf("%02x", *(serialized->data + i));*/
/* i++;*/
/* }*/
/* printf("\n");*/
/**/
/* buf_clear(serialized);*/
/* free(serialized);*/
/**/
/* } else {*/
/* const char * serialized_raw = NULL;*/
/**/
/* if (optind < argc) {*/
/* serialized_raw = argv[optind];*/
/* optind++;*/
/* } else {*/
/* log_fatal("Must provide a serialized transaction");*/
/* return 1;*/
/* }*/
/**/
/* struct buf *serialized = calloc(1, sizeof(struct buf));*/
/* serialized->cap = strlen(serialized_raw);*/
/* serialized->data = malloc(serialized->cap);*/
/* if (!serialized->data) {*/
/* log_fatal("Unable to reserve memory for decoded increment");*/
/* return 1;*/
/* }*/
/**/
/* for( i = 0 ; i < serialized->cap ; i += 2 ) {*/
/* sscanf(serialized_raw + i, "%2hhx", &(serialized->data[i/2]));*/
/* serialized->len++;*/
/* }*/
/**/
/* if (kvsm_cursor_ingest(ctx, serialized) != KVSM_OK) {*/
/* log_fatal("Unable to ingest transaction");*/
/* return 1;*/
/* }*/
/**/
/* return 0;*/
/* }*/
/**/
/* kvsm_close(ctx);*/
return 0;
}