Skip to content

Commit d694d82

Browse files
committed
use fopen
1 parent d24c52e commit d694d82

File tree

1 file changed

+136
-49
lines changed

1 file changed

+136
-49
lines changed

src/test_ulog_sqlite.c

Lines changed: 136 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -37,47 +37,92 @@
3737
#include <sys/time.h>
3838
#include <time.h>
3939

40-
int fd;
40+
//int fd;
41+
FILE *fp;
42+
43+
// int32_t read_fn(struct uls_write_context *ctx, void *buf, uint32_t pos, size_t len) {
44+
// if (lseek(fd, pos, SEEK_SET) == -1) {
45+
// ctx->err_no = errno;
46+
// return ULS_RES_SEEK_ERR;
47+
// }
48+
// ssize_t ret = read(fd, buf, len);
49+
// if (ret == -1) {
50+
// ctx->err_no = errno;
51+
// return ULS_RES_READ_ERR;
52+
// }
53+
// return ret;
54+
// }
55+
56+
// int32_t read_fn_rctx(struct uls_read_context *ctx, void *buf, uint32_t pos, size_t len) {
57+
// if (lseek(fd, pos, SEEK_SET) == -1)
58+
// return ULS_RES_SEEK_ERR;
59+
// ssize_t ret = read(fd, buf, len);
60+
// if (ret == -1)
61+
// return ULS_RES_READ_ERR;
62+
// return ret;
63+
// }
64+
65+
// int32_t write_fn(struct uls_write_context *ctx, void *buf, uint32_t pos, size_t len) {
66+
// if (lseek(fd, pos, SEEK_SET) == -1) {
67+
// ctx->err_no = errno;
68+
// return ULS_RES_SEEK_ERR;
69+
// }
70+
// ssize_t ret = write(fd, buf, len);
71+
// if (ret == -1) {
72+
// ctx->err_no = errno;
73+
// return ULS_RES_WRITE_ERR;
74+
// }
75+
// return ret;
76+
// }
77+
78+
// int flush_fn(struct uls_write_context *ctx) {
79+
// int ret = fsync(fd);
80+
// if (ret == -1) {
81+
// ctx->err_no = errno;
82+
// return ULS_RES_FLUSH_ERR;
83+
// }
84+
// return ULS_RES_OK;
85+
// }
4186

4287
int32_t read_fn(struct uls_write_context *ctx, void *buf, uint32_t pos, size_t len) {
43-
if (lseek(fd, pos, SEEK_SET) == -1) {
44-
ctx->err_no = errno;
88+
if (fseek(fp, pos, SEEK_SET)) {
89+
ctx->err_no = ferror(fp);
4590
return ULS_RES_SEEK_ERR;
4691
}
47-
ssize_t ret = read(fd, buf, len);
48-
if (ret == -1) {
49-
ctx->err_no = errno;
92+
size_t ret = fread(buf, 1, len, fp);
93+
if (ret != len) {
94+
ctx->err_no = ferror(fp);
5095
return ULS_RES_READ_ERR;
5196
}
5297
return ret;
5398
}
5499

55100
int32_t read_fn_rctx(struct uls_read_context *ctx, void *buf, uint32_t pos, size_t len) {
56-
if (lseek(fd, pos, SEEK_SET) == -1)
101+
if (fseek(fp, pos, SEEK_SET))
57102
return ULS_RES_SEEK_ERR;
58-
ssize_t ret = read(fd, buf, len);
59-
if (ret == -1)
103+
size_t ret = fread(buf, 1, len, fp);
104+
if (ret != len)
60105
return ULS_RES_READ_ERR;
61106
return ret;
62107
}
63108

64109
int32_t write_fn(struct uls_write_context *ctx, void *buf, uint32_t pos, size_t len) {
65-
if (lseek(fd, pos, SEEK_SET) == -1) {
66-
ctx->err_no = errno;
110+
if (fseek(fp, pos, SEEK_SET)) {
111+
ctx->err_no = ferror(fp);
67112
return ULS_RES_SEEK_ERR;
68113
}
69-
ssize_t ret = write(fd, buf, len);
70-
if (ret == -1) {
71-
ctx->err_no = errno;
114+
size_t ret = fwrite(buf, 1, len, fp);
115+
if (ret != len) {
116+
ctx->err_no = ferror(fp);
72117
return ULS_RES_WRITE_ERR;
73118
}
74119
return ret;
75120
}
76121

77122
int flush_fn(struct uls_write_context *ctx) {
78-
int ret = fsync(fd);
79-
if (ret == -1) {
80-
ctx->err_no = errno;
123+
int ret = fflush(fp);
124+
if (ret) {
125+
ctx->err_no = ferror(fp);
81126
return ULS_RES_FLUSH_ERR;
82127
}
83128
return ULS_RES_OK;
@@ -98,12 +143,25 @@ int test_multilevel(char *filename) {
98143
ctx.write_fn = write_fn;
99144

100145
unlink(filename);
101-
fd = open(filename, O_CREAT | O_EXCL | O_TRUNC | O_RDWR | O_SYNC, S_IRUSR | S_IWUSR);
146+
//fd = open(filename, O_CREAT | O_EXCL | O_TRUNC | O_RDWR | O_SYNC, S_IRUSR | S_IWUSR);
147+
fp = fopen(filename, "w+b");
148+
if (fp == NULL) {
149+
perror ("Open Error:");
150+
return -1;
151+
}
102152

103-
char txt[24];
104153
struct tm *t;
105154
struct timeval tv;
106155
uls_write_init(&ctx);
156+
157+
int32_t ival;
158+
double d1, d2;
159+
char txt[24];
160+
char txt1[11];
161+
uint8_t types[] = {ULS_TYPE_TEXT, ULS_TYPE_INT, ULS_TYPE_REAL, ULS_TYPE_REAL, ULS_TYPE_TEXT};
162+
void *values[] = {txt, &ival, &d1, &d2, txt1};
163+
uint16_t lengths[] = {23, sizeof(ival), sizeof(d1), sizeof(d2), 0};
164+
107165
int32_t max_rows = 1000000;
108166
for (int32_t i = 0; i < max_rows; i++) {
109167
gettimeofday(&tv, NULL);
@@ -133,26 +191,23 @@ int test_multilevel(char *filename) {
133191
txt[20] = '0' + (tv.tv_usec / 100000) % 10;
134192
txt[21] = '0' + (tv.tv_usec / 10000) % 10;
135193
txt[22] = '0' + (tv.tv_usec / 1000) % 10;
136-
int32_t ival = i - max_rows / 2;
137-
double d1 = i;
194+
ival = i - max_rows / 2;
195+
d1 = i;
138196
d1 /= 2;
139-
double d2 = rand();
197+
d2 = rand();
140198
d2 /= 1000;
141199
int txt_len = rand() % 10;
142-
char txt1[11];
143-
for (int j = 0; j < txt_len; j++)
144-
txt1[j] = 'a' + (char)(rand() % 26);
145-
uint8_t types[] = {ULS_TYPE_TEXT, ULS_TYPE_INT, ULS_TYPE_REAL, ULS_TYPE_REAL, ULS_TYPE_TEXT};
146-
void *values[] = {txt, &ival, &d1, &d2, txt1};
147-
uint16_t lengths[] = {23, sizeof(ival), sizeof(d1), sizeof(d2), txt_len};
200+
lengths[4] = txt_len;
201+
while (txt_len--)
202+
txt1[txt_len] = 'a' + (char)(rand() % 26);
148203
uls_append_row_with_values(&ctx, types, (const void **) values, lengths);
149204
}
150205
if (uls_finalize(&ctx)) {
151206
printf("Error during finalize\n");
152207
return -6;
153208
}
154209

155-
close(fd);
210+
fclose(fp);
156211

157212
return 0;
158213

@@ -172,7 +227,12 @@ int test_basic(char *filename) {
172227
ctx.write_fn = write_fn;
173228

174229
unlink(filename);
175-
fd = open(filename, O_CREAT | O_EXCL | O_TRUNC | O_RDWR | O_SYNC, S_IRUSR | S_IWUSR);
230+
//fd = open(filename, O_CREAT | O_EXCL | O_TRUNC | O_RDWR | O_SYNC, S_IRUSR | S_IWUSR);
231+
fp = fopen(filename, "w+b");
232+
if (fp == NULL) {
233+
perror ("Open Error:");
234+
return -1;
235+
}
176236

177237
uls_write_init(&ctx);
178238
uls_set_col_val(&ctx, 0, ULS_TYPE_TEXT, "Hello", 5);
@@ -190,7 +250,8 @@ int test_basic(char *filename) {
190250
printf("Error during finalize\n");
191251
return -6;
192252
}
193-
close(fd);
253+
254+
fclose(fp);
194255

195256
return 0;
196257

@@ -319,16 +380,23 @@ int create_db(int argc, char *argv[]) {
319380
ctx.flush_fn = flush_fn;
320381
ctx.write_fn = write_fn;
321382
unlink(argv[2]);
322-
fd = open(argv[2], O_CREAT | O_EXCL | O_TRUNC | O_RDWR | O_SYNC, S_IRUSR | S_IWUSR);
323-
if (fd == -1) {
324-
perror("Error");
325-
return -2;
383+
//fd = open(argv[2], O_CREAT | O_EXCL | O_TRUNC | O_RDWR | O_SYNC, S_IRUSR | S_IWUSR);
384+
//if (fd == -1) {
385+
// perror("Error");
386+
// return -2;
387+
//}
388+
fp = fopen(argv[2], "w+b");
389+
if (fp == NULL) {
390+
perror ("Open Error:");
391+
return -1;
326392
}
327393
if (uls_write_init(&ctx)) {
328394
printf("Error during init\n");
329395
return -3;
330396
}
331-
return append_records(argc, argv, &ctx);
397+
int ret = append_records(argc, argv, &ctx);
398+
fclose(fp);
399+
return ret;
332400
}
333401

334402
int append_db(int argc, char *argv[]) {
@@ -349,16 +417,23 @@ int append_db(int argc, char *argv[]) {
349417
ctx.read_fn = read_fn;
350418
ctx.flush_fn = flush_fn;
351419
ctx.write_fn = write_fn;
352-
fd = open(argv[2], O_RDWR | O_SYNC, S_IRUSR | S_IWUSR);
353-
if (fd == -1) {
354-
perror("Error");
355-
return -2;
420+
//fd = open(argv[2], O_RDWR | O_SYNC, S_IRUSR | S_IWUSR);
421+
//if (fd == -1) {
422+
// perror("Error");
423+
// return -2;
424+
//}
425+
fp = fopen(argv[2], "w+b");
426+
if (fp == NULL) {
427+
perror ("Open Error:");
428+
return -1;
356429
}
357430
if (uls_init_for_append(&ctx)) {
358431
printf("Error during init\n");
359432
return -3;
360433
}
361-
return append_records(argc, argv, &ctx);
434+
int ret = append_records(argc, argv, &ctx);
435+
fclose(fp);
436+
return ret;
362437
}
363438

364439
int16_t read_int16(const byte *ptr) {
@@ -489,10 +564,15 @@ int bin_srch_db(int argc, char *argv[]) {
489564
struct uls_read_context ctx;
490565
ctx.buf = buf;
491566
ctx.read_fn = read_fn_rctx;
492-
fd = open(argv[2], O_RDWR | O_SYNC, S_IRUSR | S_IWUSR);
493-
if (fd == -1) {
494-
perror("Error");
495-
return -2;
567+
//fd = open(argv[2], O_RDWR | O_SYNC, S_IRUSR | S_IWUSR);
568+
//if (fd == -1) {
569+
// perror("Error");
570+
// return -2;
571+
//}
572+
fp = fopen(argv[2], "r+b");
573+
if (fp == NULL) {
574+
perror ("Open Error:");
575+
return -1;
496576
}
497577
if (uls_read_init(&ctx)) {
498578
printf("Error during init\n");
@@ -511,6 +591,7 @@ int bin_srch_db(int argc, char *argv[]) {
511591
display_row(ctx);
512592
}
513593
putchar('\n');
594+
fclose(fp);
514595
return 0;
515596
}
516597

@@ -519,10 +600,15 @@ int read_db(int argc, char *argv[]) {
519600
struct uls_read_context ctx;
520601
ctx.buf = buf;
521602
ctx.read_fn = read_fn_rctx;
522-
fd = open(argv[2], O_RDWR | O_SYNC, S_IRUSR | S_IWUSR);
523-
if (fd == -1) {
524-
perror("Error");
525-
return -2;
603+
//fd = open(argv[2], O_RDWR | O_SYNC, S_IRUSR | S_IWUSR);
604+
//if (fd == -1) {
605+
// perror("Error");
606+
// return -2;
607+
//}
608+
fp = fopen(argv[2], "r+b");
609+
if (fp == NULL) {
610+
perror ("Open Error:");
611+
return -1;
526612
}
527613
if (uls_read_init(&ctx)) {
528614
printf("Error during init\n");
@@ -537,6 +623,7 @@ int read_db(int argc, char *argv[]) {
537623
display_row(ctx);
538624
}
539625
putchar('\n');
626+
fclose(fp);
540627
return 0;
541628
}
542629

0 commit comments

Comments
 (0)