Skip to content

Commit d24c52e

Browse files
committed
append row with values working
1 parent d80ccd4 commit d24c52e

File tree

5 files changed

+33
-38
lines changed

5 files changed

+33
-38
lines changed

examples/Uno_and_above/Uno_and_above.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ File myFile;
2929

3030
#define SD_CS_PIN 8
3131

32-
int32_t read_fn(struct uls_write_context *ctx, void *buf, long pos, size_t len) {
32+
int32_t read_fn(struct uls_write_context *ctx, void *buf, uint32_t pos, size_t len) {
3333
myFile.seek(pos);
3434
size_t ret = myFile.read((byte *)buf, len);
3535
if (ret != len)
3636
return ULS_RES_READ_ERR;
3737
return ret;
3838
}
3939

40-
int32_t write_fn(struct uls_write_context *ctx, void *buf, long pos, size_t len) {
40+
int32_t write_fn(struct uls_write_context *ctx, void *buf, uint32_t pos, size_t len) {
4141
myFile.seek(pos);
4242
size_t ret = myFile.write((byte *)buf, len);
4343
if (ret != len)
@@ -147,7 +147,7 @@ void loop() {
147147
break;
148148
}
149149
if (num_entries) {
150-
res = uls_append_row(&ctx);
150+
res = uls_append_empty_row(&ctx);
151151
if (res)
152152
break;
153153
delay(dly);
@@ -169,4 +169,4 @@ void loop() {
169169
Serial.print(F("IO Err\n"));
170170
}
171171

172-
}
172+
}

examples/Uno_and_above_Binary_Search/Uno_and_above_Binary_Search.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ File myFile;
2929

3030
#define SD_CS_PIN 8
3131

32-
int32_t read_fn(struct uls_write_context *ctx, void *buf, long pos, size_t len) {
32+
int32_t read_fn(struct uls_write_context *ctx, void *buf, uint32_t pos, size_t len) {
3333
myFile.seek(pos);
3434
size_t ret = myFile.read((byte *)buf, len);
3535
if (ret != len)
3636
return ULS_RES_READ_ERR;
3737
return ret;
3838
}
3939

40-
int32_t write_fn(struct uls_write_context *ctx, void *buf, long pos, size_t len) {
40+
int32_t write_fn(struct uls_write_context *ctx, void *buf, uint32_t pos, size_t len) {
4141
myFile.seek(pos);
4242
size_t ret = myFile.write((byte *)buf, len);
4343
if (ret != len)
@@ -147,7 +147,7 @@ void loop() {
147147
break;
148148
}
149149
if (num_entries) {
150-
res = uls_append_row(&ctx);
150+
res = uls_append_empty_row(&ctx);
151151
if (res)
152152
break;
153153
delay(dly);
@@ -169,4 +169,4 @@ void loop() {
169169
Serial.print(F("IO Err\n"));
170170
}
171171

172-
}
172+
}

src/test_ulog_sqlite.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,10 @@ int test_multilevel(char *filename) {
142142
char txt1[11];
143143
for (int j = 0; j < txt_len; j++)
144144
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};
145+
uint8_t types[] = {ULS_TYPE_TEXT, ULS_TYPE_INT, ULS_TYPE_REAL, ULS_TYPE_REAL, ULS_TYPE_TEXT};
146146
void *values[] = {txt, &ival, &d1, &d2, txt1};
147147
uint16_t lengths[] = {23, sizeof(ival), sizeof(d1), sizeof(d2), txt_len};
148-
uls_append_row_with_values(&ctx, types, (const void **) values, lengths);*/
149-
uls_set_col_val(&ctx, 0, ULS_TYPE_TEXT, txt, 23);
150-
uls_set_col_val(&ctx, 1, ULS_TYPE_INT, &ival, sizeof(ival));
151-
uls_set_col_val(&ctx, 2, ULS_TYPE_REAL, &d1, sizeof(d1));
152-
uls_set_col_val(&ctx, 3, ULS_TYPE_REAL, &d2, sizeof(d2));
153-
uls_set_col_val(&ctx, 4, ULS_TYPE_TEXT, txt, txt_len);
154-
if (i < max_rows - 1)
155-
uls_append_row(&ctx);
148+
uls_append_row_with_values(&ctx, types, (const void **) values, lengths);
156149
}
157150
if (uls_finalize(&ctx)) {
158151
printf("Error during finalize\n");
@@ -187,7 +180,7 @@ int test_basic(char *filename) {
187180
uls_set_col_val(&ctx, 2, ULS_TYPE_TEXT, "How", 3);
188181
uls_set_col_val(&ctx, 3, ULS_TYPE_TEXT, "Are", 3);
189182
uls_set_col_val(&ctx, 4, ULS_TYPE_TEXT, "You", 3);
190-
uls_append_row(&ctx);
183+
uls_append_empty_row(&ctx);
191184
uls_set_col_val(&ctx, 0, ULS_TYPE_TEXT, "I", 1);
192185
uls_set_col_val(&ctx, 1, ULS_TYPE_TEXT, "am", 2);
193186
uls_set_col_val(&ctx, 2, ULS_TYPE_TEXT, "fine", 4);
@@ -294,7 +287,7 @@ int append_records(int argc, char *argv[], struct uls_write_context *ctx) {
294287
return -4;
295288
}
296289
if (i < argc - 1) {
297-
if (uls_append_row(ctx)) {
290+
if (uls_append_empty_row(ctx)) {
298291
printf("Error during add col\n");
299292
return -5;
300293
}

src/ulog_sqlite.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ int32_t get_pagesize(byte page_size_exp) {
187187
uint16_t acquire_last_pos(struct uls_write_context *wctx, byte *ptr) {
188188
uint16_t last_pos = read_uint16(ptr + 5);
189189
if (last_pos == 0) {
190-
uls_append_row(wctx);
190+
uls_append_empty_row(wctx);
191191
last_pos = read_uint16(ptr + 5);
192192
}
193193
return last_pos;
@@ -482,7 +482,7 @@ int form_page1(struct uls_write_context *wctx, char *table_name, char *table_scr
482482
int orig_col_count = wctx->col_count;
483483
wctx->cur_write_page = 0;
484484
wctx->col_count = 5;
485-
uls_append_row(wctx);
485+
uls_append_empty_row(wctx);
486486
uls_set_col_val(wctx, 0, ULS_TYPE_TEXT, "table", 5);
487487
if (table_name == NULL)
488488
table_name = default_table_name;
@@ -596,23 +596,23 @@ int uls_write_init(struct uls_write_context *wctx) {
596596
// Checks space for appending new row
597597
// If space not available, writes current buffer to disk and
598598
// initializes buffer as new page
599-
uint16_t check_space_for_new_row(struct uls_write_context *wctx, int32_t page_size,
600-
int rec_count, uint16_t len_of_rec_len_rowid, uint16_t new_rec_len) {
599+
uint16_t make_space_for_new_row(struct uls_write_context *wctx, int32_t page_size,
600+
uint16_t len_of_rec_len_rowid, uint16_t new_rec_len) {
601601
byte *ptr = wctx->buf + (wctx->buf[0] == 13 ? 0 : 100);
602602
uint16_t last_pos = read_uint16(ptr + 5);
603+
int rec_count = read_uint16(ptr + 3) + 1;
603604
if (last_pos == 0)
604605
last_pos = page_size - wctx->page_resv_bytes - new_rec_len - len_of_rec_len_rowid;
605606
else {
606607
last_pos -= new_rec_len;
607608
last_pos -= len_of_rec_len_rowid;
608-
if (last_pos < (ptr - wctx->buf) + 9 + CHKSUM_LEN + rec_count * 2) {
609+
if (last_pos < ((ptr - wctx->buf) + 9 + CHKSUM_LEN + (rec_count * 2))) {
609610
int res = write_page(wctx, wctx->cur_write_page, page_size);
610611
if (res)
611612
return res;
612613
wctx->cur_write_page++;
613614
init_bt_tbl_leaf(wctx->buf);
614615
last_pos = page_size - wctx->page_resv_bytes - new_rec_len - len_of_rec_len_rowid;
615-
rec_count = 1;
616616
}
617617
}
618618
return last_pos;
@@ -657,21 +657,23 @@ int uls_append_row_with_values(struct uls_write_context *wctx,
657657

658658
wctx->cur_write_rowid++;
659659
byte *ptr = wctx->buf + (wctx->buf[0] == 13 ? 0 : 100);
660-
int rec_count = read_uint16(ptr + 3) + 1;
661660
int32_t page_size = get_pagesize(wctx->page_size_exp);
662661
uint16_t len_of_rec_len_rowid = LEN_OF_REC_LEN + get_vlen_of_uint32(wctx->cur_write_rowid);
663-
uint16_t new_rec_len = LEN_OF_HDR_LEN;
662+
uint16_t new_rec_len = 0;
663+
uint16_t hdr_len = LEN_OF_HDR_LEN;
664664
for (int i = 0; i < wctx->col_count; i++) {
665665
if (values[i] != NULL)
666-
new_rec_len += lengths[i];
666+
new_rec_len += (types[i] == ULS_TYPE_REAL ? 8 : lengths[i]);
667667
uint32_t col_type = derive_col_type_or_len(types[i], values[i], lengths[i]);
668-
new_rec_len += get_vlen_of_uint32(col_type);
668+
hdr_len += get_vlen_of_uint32(col_type);
669669
}
670-
uint16_t last_pos = check_space_for_new_row(wctx, page_size,
671-
rec_count, len_of_rec_len_rowid, new_rec_len);
670+
new_rec_len += hdr_len;
671+
uint16_t last_pos = make_space_for_new_row(wctx, page_size,
672+
len_of_rec_len_rowid, new_rec_len);
673+
int rec_count = read_uint16(ptr + 3) + 1;
672674

673675
write_rec_len_rowid_hdr_len(wctx->buf + last_pos, new_rec_len,
674-
wctx->cur_write_rowid, wctx->col_count + LEN_OF_HDR_LEN);
676+
wctx->cur_write_rowid, hdr_len);
675677
byte *rec_ptr = wctx->buf + last_pos + len_of_rec_len_rowid + LEN_OF_HDR_LEN;
676678
for (int i = 0; i < wctx->col_count; i++) {
677679
uint32_t col_type = derive_col_type_or_len(types[i], values[i], lengths[i]);
@@ -691,17 +693,17 @@ int uls_append_row_with_values(struct uls_write_context *wctx,
691693
}
692694

693695
// See .h file for API description
694-
int uls_append_row(struct uls_write_context *wctx) {
696+
int uls_append_empty_row(struct uls_write_context *wctx) {
695697

696698
wctx->cur_write_rowid++;
697699
byte *ptr = wctx->buf + (wctx->buf[0] == 13 ? 0 : 100);
698-
int rec_count = read_uint16(ptr + 3) + 1;
699700
int32_t page_size = get_pagesize(wctx->page_size_exp);
700701
uint16_t len_of_rec_len_rowid = LEN_OF_REC_LEN + get_vlen_of_uint32(wctx->cur_write_rowid);
701702
uint16_t new_rec_len = wctx->col_count;
702703
new_rec_len += LEN_OF_HDR_LEN;
703-
uint16_t last_pos = check_space_for_new_row(wctx, page_size,
704-
rec_count, len_of_rec_len_rowid, new_rec_len);
704+
uint16_t last_pos = make_space_for_new_row(wctx, page_size,
705+
len_of_rec_len_rowid, new_rec_len);
706+
int rec_count = read_uint16(ptr + 3) + 1;
705707

706708
memset(wctx->buf + last_pos, '\0', new_rec_len + len_of_rec_len_rowid);
707709
write_rec_len_rowid_hdr_len(wctx->buf + last_pos, new_rec_len,
@@ -931,7 +933,7 @@ int uls_init_for_append(struct uls_write_context *wctx) {
931933
res = read_bytes_wctx(wctx, wctx->buf, wctx->cur_write_page * page_size, page_size);
932934
if (res)
933935
return res;
934-
res = uls_append_row(wctx);
936+
res = uls_append_empty_row(wctx);
935937
if (res)
936938
return res;
937939
return ULS_RES_OK;

src/ulog_sqlite.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ int uls_init_for_append(struct uls_write_context *wctx);
8888
// Creates new record with all columns null
8989
// If no more space in page, writes it to disk
9090
// creates new page, and creates a new record
91-
int uls_append_row(struct uls_write_context *wctx);
91+
int uls_append_empty_row(struct uls_write_context *wctx);
9292

9393
// Creates new record with given column values
9494
// If no more space in page, writes it to disk

0 commit comments

Comments
 (0)