summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar grunfink2025-01-15 05:54:48 +0000
committerGravatar grunfink2025-01-15 05:54:48 +0000
commita3995f7efffe559c7c2a7106d7ffe9cb8b1e414a (patch)
tree9aa977880eb79a8506244d9d7fbaa7f5b6a6f780
parentUpdated RELEASE_NOTES. (diff)
parentImplement faster min_id handling (diff)
downloadpenes-snac2-a3995f7efffe559c7c2a7106d7ffe9cb8b1e414a.tar.gz
penes-snac2-a3995f7efffe559c7c2a7106d7ffe9cb8b1e414a.tar.xz
penes-snac2-a3995f7efffe559c7c2a7106d7ffe9cb8b1e414a.zip
Merge pull request 'Implement faster min_id handling' (#277) from nowster/snac2:fast_min_id into master
Reviewed-on: https://codeberg.org/grunfink/snac2/pulls/277
-rw-r--r--data.c31
-rw-r--r--mastoapi.c46
-rw-r--r--snac.h2
3 files changed, 61 insertions, 18 deletions
diff --git a/data.c b/data.c
index ca63084..33947ff 100644
--- a/data.c
+++ b/data.c
@@ -679,6 +679,37 @@ int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip)
679 return 1; 679 return 1;
680} 680}
681 681
682int index_asc_first(FILE *f,char md5[MD5_HEX_SIZE], const char *seek_md5)
683/* reads the first entry of an ascending index, starting from a given md5 */
684{
685 fseek(f, SEEK_SET, 0);
686 while (fread(md5, MD5_HEX_SIZE, 1, f)) {
687 md5[MD5_HEX_SIZE - 1] = '\0';
688 if (strcmp(md5,seek_md5) == 0) {
689 return index_asc_next(f, md5);
690 }
691 }
692 return 0;
693}
694
695int index_asc_next(FILE *f, char md5[MD5_HEX_SIZE])
696/* reads the next entry of an ascending index */
697{
698 for (;;) {
699 /* read an md5 */
700 if (!fread(md5, MD5_HEX_SIZE, 1, f))
701 return 0;
702
703 /* deleted, skip */
704 if (md5[0] != '-')
705 break;
706 }
707
708 md5[MD5_HEX_SIZE - 1] = '\0';
709
710 return 1;
711}
712
682 713
683xs_list *index_list_desc(const char *fn, int skip, int show) 714xs_list *index_list_desc(const char *fn, int skip, int show)
684/* returns an index as a list, in reverse order */ 715/* returns an index as a list, in reverse order */
diff --git a/mastoapi.c b/mastoapi.c
index 3e823ed..3250a20 100644
--- a/mastoapi.c
+++ b/mastoapi.c
@@ -1339,6 +1339,9 @@ xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index_fn
1339 const char *since_id = xs_dict_get(args, "since_id"); 1339 const char *since_id = xs_dict_get(args, "since_id");
1340 const char *min_id = xs_dict_get(args, "min_id"); /* unsupported old-to-new navigation */ 1340 const char *min_id = xs_dict_get(args, "min_id"); /* unsupported old-to-new navigation */
1341 const char *limit_s = xs_dict_get(args, "limit"); 1341 const char *limit_s = xs_dict_get(args, "limit");
1342 int (*iterator)(FILE *, char *);
1343 int initial_status = 0;
1344 int ascending = 0;
1342 int limit = 0; 1345 int limit = 0;
1343 int cnt = 0; 1346 int cnt = 0;
1344 1347
@@ -1348,27 +1351,40 @@ xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index_fn
1348 if (limit == 0) 1351 if (limit == 0)
1349 limit = 20; 1352 limit = 20;
1350 1353
1351 if (index_desc_first(f, md5, 0)) { 1354 if (min_id) {
1355 iterator = &index_asc_next;
1356 initial_status = index_asc_first(f, md5, MID_TO_MD5(min_id));
1357 ascending = 1;
1358 }
1359 else {
1360 iterator = &index_desc_next;
1361 initial_status = index_desc_first(f, md5, 0);
1362 }
1363
1364 if (initial_status) {
1352 do { 1365 do {
1353 xs *msg = NULL; 1366 xs *msg = NULL;
1354 1367
1355 /* only return entries older that max_id */ 1368 /* only return entries older that max_id */
1356 if (max_id) { 1369 if (max_id) {
1357 if (strcmp(md5, MID_TO_MD5(max_id)) == 0) 1370 if (strcmp(md5, MID_TO_MD5(max_id)) == 0) {
1358 max_id = NULL; 1371 max_id = NULL;
1359 1372 if (ascending)
1360 continue; 1373 break;
1374 }
1375 if (!ascending)
1376 continue;
1361 } 1377 }
1362 1378
1363 /* only returns entries newer than since_id */ 1379 /* only returns entries newer than since_id */
1364 if (since_id) { 1380 if (since_id) {
1365 if (strcmp(md5, MID_TO_MD5(since_id)) == 0) 1381 if (strcmp(md5, MID_TO_MD5(since_id)) == 0) {
1366 break; 1382 if (!ascending)
1367 } 1383 break;
1368 1384 since_id = NULL;
1369 if (min_id) { 1385 }
1370 if (strcmp(md5, MID_TO_MD5(min_id)) == 0) 1386 if (ascending)
1371 break; 1387 continue;
1372 } 1388 }
1373 1389
1374 /* get the entry */ 1390 /* get the entry */
@@ -1440,14 +1456,8 @@ xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index_fn
1440 out = xs_list_append(out, st); 1456 out = xs_list_append(out, st);
1441 cnt++; 1457 cnt++;
1442 } 1458 }
1443 if (min_id) {
1444 while (cnt > limit) {
1445 out = xs_list_del(out, 0);
1446 cnt--;
1447 }
1448 }
1449 1459
1450 } while ((min_id || (cnt < limit)) && index_desc_next(f, md5)); 1460 } while ((cnt < limit) && (*iterator)(f, md5));
1451 } 1461 }
1452 1462
1453 int more = index_desc_next(f, md5); 1463 int more = index_desc_next(f, md5);
diff --git a/snac.h b/snac.h
index 5729a22..65ece5d 100644
--- a/snac.h
+++ b/snac.h
@@ -108,6 +108,8 @@ int index_len(const char *fn);
108xs_list *index_list(const char *fn, int max); 108xs_list *index_list(const char *fn, int max);
109int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE]); 109int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE]);
110int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip); 110int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip);
111int index_asc_next(FILE *f, char md5[MD5_HEX_SIZE]);
112int index_asc_first(FILE *f, char md5[MD5_HEX_SIZE], const char *seek_md5);
111xs_list *index_list_desc(const char *fn, int skip, int show); 113xs_list *index_list_desc(const char *fn, int skip, int show);
112 114
113int object_add(const char *id, const xs_dict *obj); 115int object_add(const char *id, const xs_dict *obj);