@amritk/nish-x86_64-linux 0.45.1 → 0.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -88,6 +88,7 @@ typedef struct nishsqlite_shape {
88
88
  char *columns;
89
89
  u32 cookie;
90
90
  int32_t is_index, ncol, nkey, alias;
91
+ int32_t int_first; /* an index whose first key column has INTEGER or NUMERIC affinity */
91
92
  Pgno root;
92
93
  KeyInfo *ki; /* one reference, the entry's */
93
94
  } nishsqlite_shape;
@@ -186,6 +187,15 @@ static void slot_give(nishsqlite_table *t, int32_t h) {
186
187
 
187
188
  static void cursors_invalidate(nishsqlite_conn *c);
188
189
 
190
+ /* The connection's rollback hook. SQLite calls it at the end of every
191
+ * rollback of the connection's transaction, the ones it makes on its own
192
+ * among them: a statement that fails with SQLITE_NOMEM, SQLITE_IOERR or
193
+ * SQLITE_FULL rolls the whole transaction back, a plain SELECT included. Every
194
+ * other way a read can end (`nishsqlite_exec`, a statement that is not plain
195
+ * stepping, `nishsqlite_end_read`, `nishsqlite_close`) invalidates the cursors
196
+ * before it runs, so a cursor call checks nothing but its handle. */
197
+ static void nishsqlite_rolled_back(void *user) { cursors_invalidate(user); }
198
+
189
199
  /* Empties the connection's checked shapes. Before the close: a KeyInfo is
190
200
  * freed through its connection. */
191
201
  static void shapes_clear(nishsqlite_conn *c) {
@@ -250,6 +260,7 @@ int32_t nishsqlite_open(const char *path, int32_t flags) {
250
260
  c->db = db;
251
261
  c->self = h;
252
262
  sqlite3_set_authorizer(db, nishsqlite_authorize, c);
263
+ sqlite3_rollback_hook(db, nishsqlite_rolled_back, c);
253
264
  return h;
254
265
  }
255
266
  free(c);
@@ -512,11 +523,11 @@ int32_t nishsqlite_column_into(int32_t h, int32_t c, uint8_t *buf, uint64_t n, i
512
523
  * A cursor belongs to its connection (the slot's owner) and lives until the
513
524
  * program closes it, `nishsqlite_end_read`, or anything that could change what
514
525
  * it reads: a statement that is not plain stepping, `nishsqlite_exec`, the
515
- * authorizer seeing an action off its allowlist, or the transaction ending
516
- * under it (`cursor_of`). Invalidating frees the cursor and its slot, so its
517
- * handle answers SQLITE_MISUSE from then on. The moves and reads allocate
518
- * nothing once the first row has been read: the decoded row and the copy
519
- * buffer are the cursor's and are reused. */
526
+ * authorizer seeing an action off its allowlist, or SQLite rolling the
527
+ * transaction back on its own (`nishsqlite_rolled_back`). Invalidating frees
528
+ * the cursor and its slot, so its handle answers SQLITE_MISUSE from then on.
529
+ * The moves and reads allocate nothing once the first row has been read: the
530
+ * decoded row and the copy buffer are the cursor's and are reused. */
520
531
 
521
532
  #define NISH_SQLITE_SEEK_GE 1
522
533
  #define NISH_SQLITE_SEEK_GT 2
@@ -530,18 +541,24 @@ int32_t nishsqlite_column_into(int32_t h, int32_t c, uint8_t *buf, uint64_t n, i
530
541
 
531
542
  typedef struct nishsqlite_cursor {
532
543
  nishsqlite_conn *conn;
544
+ sqlite3_mutex *mutex; /* the connection's `db->mutex`, held around every call */
533
545
  Btree *btree; /* `main`, from sqlite3DbNameToBtree */
534
546
  BtCursor *bt; /* in the same block, after this struct */
535
547
  KeyInfo *ki; /* the record's fields, for the decoder and a seek */
536
548
  UnpackedRecord *row; /* the current row, decoded on the first read of it */
537
549
  Mem *key; /* the seek key's `nkeyfields` values, in the block */
550
+ UnpackedRecord seek; /* the key a seek or count hands SQLite: `key`, under `ki` */
538
551
  int32_t nkeyparts; /* how many of them are set */
539
552
  int32_t nkeyfields; /* an index's key columns; a table's columns */
540
553
  u8 *buf; /* a record that is not all on its page, copied */
541
554
  u32 cap;
555
+ int64_t *counts; /* the last `count_range`'s answer, per key */
556
+ uint64_t ncounts; /* how many of them it answered */
557
+ uint64_t counts_cap;
542
558
  int32_t ncol; /* columns the program reads: a table's, or an index's key and rowid */
543
559
  int32_t alias; /* a table's INTEGER PRIMARY KEY column, -1 when none */
544
560
  int32_t is_index;
561
+ int32_t int_first; /* an index whose first key column stores integers as integers */
545
562
  int32_t has_row; /* the last move answered a row */
546
563
  int32_t unpacked; /* `row` holds the current row */
547
564
  int32_t nfields; /* the fields the current row's record holds */
@@ -554,6 +571,7 @@ static void cursor_free(nishsqlite_cursor *cur) {
554
571
  if (cur->row) sqlite3DbFree(db, cur->row);
555
572
  sqlite3KeyInfoUnref(cur->ki);
556
573
  sqlite3_free(cur->buf);
574
+ sqlite3_free(cur->counts);
557
575
  sqlite3_free(cur);
558
576
  }
559
577
 
@@ -572,18 +590,13 @@ static void cursors_invalidate(nishsqlite_conn *c) {
572
590
  sqlite3_mutex_leave(mx);
573
591
  }
574
592
 
575
- /* The cursor `h` names, or NULL. A cursor whose transaction has ended, by
576
- * whatever route, is invalidated here with every other cursor of its
577
- * connection before anything reads it. */
593
+ /* The cursor `h` names, or NULL: the one check a cursor call makes. A cursor
594
+ * whose read has ended, by whatever route, is already gone from its slot,
595
+ * because every route invalidates the connection's cursors as it ends the read
596
+ * (`nishsqlite_rolled_back`), so a live slot is a cursor inside its read. */
578
597
  static inline nishsqlite_cursor *cursor_of(int32_t h) {
579
598
  nishsqlite_slot *s = slot_of(&nishsqlite_cursors, h);
580
- if (s == NULL) return NULL;
581
- nishsqlite_cursor *cur = s->p;
582
- if (sqlite3_get_autocommit(cur->conn->db) || sqlite3BtreeTxnState(cur->btree) == SQLITE_TXN_NONE) {
583
- cursors_invalidate(cur->conn);
584
- return NULL;
585
- }
586
- return cur;
599
+ return s ? s->p : NULL;
587
600
  }
588
601
 
589
602
  /* Runs the adapter's own SQL, which the authorizer lets through unwatched. */
@@ -821,7 +834,7 @@ static int main_is_utf8(nishsqlite_conn *c) {
821
834
  * has other columns, and SQLITE_NOMEM, all negated. */
822
835
  static int32_t cursor_build(nishsqlite_conn *c, const char *name, const char *columns) {
823
836
  sqlite3 *db = c->db;
824
- int32_t is_index = 0, ncol = 0, alias = -1, nkey = 0;
837
+ int32_t is_index = 0, ncol = 0, alias = -1, nkey = 0, int_first = 0;
825
838
  Pgno root = 0;
826
839
  char *table = NULL;
827
840
  KeyInfo *ki = NULL;
@@ -836,6 +849,7 @@ static int32_t cursor_build(nishsqlite_conn *c, const char *name, const char *co
836
849
  ncol = s->ncol;
837
850
  nkey = s->nkey;
838
851
  alias = s->alias;
852
+ int_first = s->int_first;
839
853
  root = s->root;
840
854
  ki = sqlite3KeyInfoRef(s->ki);
841
855
  goto build;
@@ -856,6 +870,14 @@ static int32_t cursor_build(nishsqlite_conn *c, const char *name, const char *co
856
870
  rc = ki ? index_shape(c, name, want, &nkey, ki) : SQLITE_NOMEM;
857
871
  ncol = nkey + 1;
858
872
  }
873
+ /* A first key column with INTEGER or NUMERIC affinity holds an integer
874
+ * value as an integer, so `count_range`'s integer compare agrees with SQL's
875
+ * `col = k`; TEXT affinity would compare k as text. */
876
+ Index *ix = rc == SQLITE_OK ? sqlite3FindIndex(db, name, "main") : NULL;
877
+ if (ix) {
878
+ char aff = sqlite3TableColumnAffinity(ix->pTable, ix->aiColumn[0]);
879
+ int_first = aff == SQLITE_AFF_INTEGER || aff == SQLITE_AFF_NUMERIC;
880
+ }
859
881
  }
860
882
  sqlite3_free(table);
861
883
  if (rc == SQLITE_OK && sqlite3_str_errcode(want) != SQLITE_OK) rc = SQLITE_NOMEM;
@@ -874,7 +896,7 @@ static int32_t cursor_build(nishsqlite_conn *c, const char *name, const char *co
874
896
  sqlite3_free(s->name);
875
897
  sqlite3_free(s->columns);
876
898
  sqlite3KeyInfoUnref(s->ki);
877
- *s = (nishsqlite_shape){n, cols, cookie, is_index, ncol, nkey, alias, root, sqlite3KeyInfoRef(ki)};
899
+ *s = (nishsqlite_shape){n, cols, cookie, is_index, ncol, nkey, alias, int_first, root, sqlite3KeyInfoRef(ki)};
878
900
  c->shape_next = (c->shape_next + 1) % NISH_SQLITE_SHAPES;
879
901
  } else {
880
902
  sqlite3_free(n);
@@ -893,6 +915,7 @@ build:;
893
915
  }
894
916
  memset(cur, 0, head);
895
917
  cur->conn = c;
918
+ cur->mutex = sqlite3_db_mutex(db);
896
919
  cur->btree = sqlite3DbNameToBtree(db, "main");
897
920
  cur->bt = (BtCursor *)((u8 *)cur + head);
898
921
  sqlite3BtreeCursorZero(cur->bt);
@@ -903,6 +926,9 @@ build:;
903
926
  cur->ncol = ncol;
904
927
  cur->alias = alias;
905
928
  cur->is_index = is_index;
929
+ cur->int_first = int_first;
930
+ cur->seek.pKeyInfo = ki;
931
+ cur->seek.aMem = cur->key;
906
932
  cur->row = sqlite3VdbeAllocUnpackedRecord(ki);
907
933
  rc = cur->row ? SQLITE_OK : SQLITE_NOMEM;
908
934
  if (rc == SQLITE_OK) rc = sqlite3BtreeCursor(cur->btree, root, 0, is_index ? ki : NULL, cur->bt);
@@ -921,8 +947,7 @@ static int32_t unlocked_cursor_open(int32_t h, const char *name, const char *col
921
947
  return cursor_build(c, name, columns);
922
948
  }
923
949
 
924
- static int32_t unlocked_cursor_close(int32_t h) {
925
- nishsqlite_cursor *cur = cursor_of(h);
950
+ static int32_t unlocked_cursor_close(nishsqlite_cursor *cur, int32_t h) {
926
951
  if (cur == NULL) return SQLITE_MISUSE;
927
952
  cur->conn->ncursors--;
928
953
  cursor_free(cur);
@@ -940,16 +965,14 @@ static int32_t moved(nishsqlite_cursor *cur, int rc, int row) {
940
965
  return rc == SQLITE_OK ? cur->has_row : -rc;
941
966
  }
942
967
 
943
- static int32_t unlocked_cursor_first(int32_t h) {
944
- nishsqlite_cursor *cur = cursor_of(h);
968
+ static int32_t unlocked_cursor_first(nishsqlite_cursor *cur) {
945
969
  if (cur == NULL) return -SQLITE_MISUSE;
946
970
  int empty = 1;
947
971
  int rc = sqlite3BtreeFirst(cur->bt, &empty);
948
972
  return moved(cur, rc, !empty);
949
973
  }
950
974
 
951
- static int32_t unlocked_cursor_last(int32_t h) {
952
- nishsqlite_cursor *cur = cursor_of(h);
975
+ static int32_t unlocked_cursor_last(nishsqlite_cursor *cur) {
953
976
  if (cur == NULL) return -SQLITE_MISUSE;
954
977
  int empty = 1;
955
978
  int rc = sqlite3BtreeLast(cur->bt, &empty);
@@ -963,22 +986,19 @@ static int32_t stepped(nishsqlite_cursor *cur, int rc) {
963
986
 
964
987
  /* Next and previous need a row to move from: with none they are
965
988
  * SQLITE_MISUSE, so a loop cannot run past its end and restart. */
966
- static int32_t unlocked_cursor_next(int32_t h) {
967
- nishsqlite_cursor *cur = cursor_of(h);
989
+ static int32_t unlocked_cursor_next(nishsqlite_cursor *cur) {
968
990
  if (cur == NULL || !cur->has_row) return -SQLITE_MISUSE;
969
991
  return stepped(cur, sqlite3BtreeNext(cur->bt, 0));
970
992
  }
971
993
 
972
- static int32_t unlocked_cursor_previous(int32_t h) {
973
- nishsqlite_cursor *cur = cursor_of(h);
994
+ static int32_t unlocked_cursor_previous(nishsqlite_cursor *cur) {
974
995
  if (cur == NULL || !cur->has_row) return -SQLITE_MISUSE;
975
996
  return stepped(cur, sqlite3BtreePrevious(cur->bt, 0));
976
997
  }
977
998
 
978
999
  /* Moves a table cursor to the row `rowid`: 1 when it is there, 0 when not.
979
1000
  * An index cursor is SQLITE_MISUSE. */
980
- static int32_t unlocked_cursor_seek_rowid(int32_t h, int64_t rowid) {
981
- nishsqlite_cursor *cur = cursor_of(h);
1001
+ static int32_t unlocked_cursor_seek_rowid(nishsqlite_cursor *cur, int64_t rowid) {
982
1002
  if (cur == NULL || cur->is_index) return -SQLITE_MISUSE;
983
1003
  int res = -1;
984
1004
  int rc = sqlite3BtreeTableMoveto(cur->bt, rowid, 0, &res);
@@ -987,36 +1007,34 @@ static int32_t unlocked_cursor_seek_rowid(int32_t h, int64_t rowid) {
987
1007
 
988
1008
  /* ---- Seek keys ---------------------------------------------------------- */
989
1009
 
990
- /* The index cursor `h` with room for one more key part, or NULL with `*rc`
991
- * saying why. The caller sets `cur->key[cur->nkeyparts]` and counts it only
992
- * when the set succeeds, so a failed part leaves the key as it was. */
993
- static nishsqlite_cursor *key_next(int32_t h, int32_t *rc) {
994
- nishsqlite_cursor *cur = cursor_of(h);
1010
+ /* `cur` when it is an index cursor with room for one more key part, or NULL
1011
+ * with `*rc` saying why. The caller sets `cur->key[cur->nkeyparts]` and counts
1012
+ * it only when the set succeeds, so a failed part leaves the key as it was. */
1013
+ static nishsqlite_cursor *key_next(nishsqlite_cursor *cur, int32_t *rc) {
995
1014
  *rc = cur == NULL || !cur->is_index ? SQLITE_MISUSE : SQLITE_RANGE;
996
1015
  if (cur == NULL || !cur->is_index || cur->nkeyparts >= cur->nkeyfields) return NULL;
997
1016
  *rc = SQLITE_OK;
998
1017
  return cur;
999
1018
  }
1000
1019
 
1001
- static int32_t unlocked_key_reset(int32_t h) {
1002
- nishsqlite_cursor *cur = cursor_of(h);
1020
+ static int32_t unlocked_key_reset(nishsqlite_cursor *cur) {
1003
1021
  if (cur == NULL || !cur->is_index) return SQLITE_MISUSE;
1004
1022
  for (int32_t i = 0; i < cur->nkeyparts; i++) sqlite3VdbeMemSetNull(&cur->key[i]);
1005
1023
  cur->nkeyparts = 0;
1006
1024
  return SQLITE_OK;
1007
1025
  }
1008
1026
 
1009
- static int32_t unlocked_key_i64(int32_t h, int64_t v) {
1027
+ static int32_t unlocked_key_i64(nishsqlite_cursor *cur, int64_t v) {
1010
1028
  int32_t rc;
1011
- nishsqlite_cursor *cur = key_next(h, &rc);
1029
+ cur = key_next(cur, &rc);
1012
1030
  if (cur) sqlite3VdbeMemSetInt64(&cur->key[cur->nkeyparts++], v);
1013
1031
  return rc;
1014
1032
  }
1015
1033
 
1016
1034
  /* Text values are copied into the key (SQLITE_TRANSIENT), as a bind is. */
1017
- static int32_t unlocked_key_text(int32_t h, const char *text) {
1035
+ static int32_t unlocked_key_text(nishsqlite_cursor *cur, const char *text) {
1018
1036
  int32_t rc;
1019
- nishsqlite_cursor *cur = key_next(h, &rc);
1037
+ cur = key_next(cur, &rc);
1020
1038
  if (cur) rc = sqlite3VdbeMemSetStr(&cur->key[cur->nkeyparts], text, -1, SQLITE_UTF8, SQLITE_TRANSIENT);
1021
1039
  if (cur && rc == SQLITE_OK) cur->nkeyparts++;
1022
1040
  return rc;
@@ -1024,34 +1042,38 @@ static int32_t unlocked_key_text(int32_t h, const char *text) {
1024
1042
 
1025
1043
  /* `buf[off, off + len)` as text, `n` the buffer's length; a window outside
1026
1044
  * the buffer is SQLITE_MISUSE, as `nishsqlite_bind_bytes` answers. */
1027
- static int32_t unlocked_key_bytes(int32_t h, const uint8_t *buf, uint64_t n, int32_t off, int32_t len) {
1045
+ static int32_t unlocked_key_bytes(nishsqlite_cursor *cur, const uint8_t *buf, uint64_t n, int32_t off, int32_t len) {
1028
1046
  if (off < 0 || len < 0 || (uint64_t)off + (uint64_t)len > n) return SQLITE_MISUSE;
1029
1047
  int32_t rc;
1030
- nishsqlite_cursor *cur = key_next(h, &rc);
1048
+ cur = key_next(cur, &rc);
1031
1049
  const char *p = len == 0 ? "" : (const char *)(buf + off);
1032
1050
  if (cur) rc = sqlite3VdbeMemSetStr(&cur->key[cur->nkeyparts], p, len, SQLITE_UTF8, SQLITE_TRANSIENT);
1033
1051
  if (cur && rc == SQLITE_OK) cur->nkeyparts++;
1034
1052
  return rc;
1035
1053
  }
1036
1054
 
1055
+ /* The kept key, made at open, readied for a compare of its first `nfield`
1056
+ * values: an integer seek sets its Mems in place and builds no record. */
1057
+ static UnpackedRecord *seek_key(nishsqlite_cursor *cur, int32_t nfield, int default_rc) {
1058
+ UnpackedRecord *k = &cur->seek;
1059
+ k->nField = (u16)nfield;
1060
+ k->default_rc = (i8)default_rc;
1061
+ k->errCode = 0;
1062
+ k->eqSeen = 0;
1063
+ return k;
1064
+ }
1065
+
1037
1066
  /* Moves an index cursor to the first entry at or past the key (`op` GE or
1038
1067
  * GT), or the last at or before it (LE or LT), comparing only the key's
1039
1068
  * values, so a key shorter than the index is a prefix. This is the VDBE's
1040
1069
  * OP_SeekGE/GT/LE/LT: the record's default answer for an equal prefix decides
1041
1070
  * which side of the equal run the B-tree lands on, and one step corrects it. */
1042
- static int32_t unlocked_cursor_seek(int32_t h, int32_t op) {
1043
- nishsqlite_cursor *cur = cursor_of(h);
1071
+ static int32_t unlocked_cursor_seek(nishsqlite_cursor *cur, int32_t op) {
1044
1072
  if (cur == NULL || !cur->is_index || cur->nkeyparts < 1 || op < NISH_SQLITE_SEEK_GE ||
1045
1073
  op > NISH_SQLITE_SEEK_LT)
1046
1074
  return -SQLITE_MISUSE;
1047
- UnpackedRecord key;
1048
- memset(&key, 0, sizeof key);
1049
- key.pKeyInfo = cur->ki;
1050
- key.aMem = cur->key;
1051
- key.nField = (u16)cur->nkeyparts;
1052
- key.default_rc = op == NISH_SQLITE_SEEK_GT || op == NISH_SQLITE_SEEK_LE ? -1 : 1;
1053
1075
  int res = 0;
1054
- int rc = sqlite3BtreeIndexMoveto(cur->bt, &key, &res);
1076
+ int rc = sqlite3BtreeIndexMoveto(cur->bt, seek_key(cur, cur->nkeyparts, op == NISH_SQLITE_SEEK_GT || op == NISH_SQLITE_SEEK_LE ? -1 : 1), &res);
1055
1077
  if (rc != SQLITE_OK) return moved(cur, rc, 0);
1056
1078
  if (op == NISH_SQLITE_SEEK_GE || op == NISH_SQLITE_SEEK_GT) {
1057
1079
  if (res < 0 || (res == 0 && op == NISH_SQLITE_SEEK_GT)) return stepped(cur, sqlite3BtreeNext(cur->bt, 0));
@@ -1063,35 +1085,42 @@ static int32_t unlocked_cursor_seek(int32_t h, int32_t op) {
1063
1085
 
1064
1086
  /* ---- Cursor columns ----------------------------------------------------- */
1065
1087
 
1066
- /* The current row's record decoded into `row`, once per row. A record that
1067
- * is all on its page is decoded where it lies; one that spills onto overflow
1068
- * pages is copied whole into the cursor's buffer first, with zeroed slack
1069
- * past it. SQLITE_MISUSE with no row, SQLITE_CORRUPT for a record longer than
1070
- * SQLite allows. */
1088
+ /* The current entry's record: where it lies when it is all on its page,
1089
+ * else copied whole into the cursor's buffer with zeroed slack past it.
1090
+ * SQLITE_CORRUPT for a record longer than SQLite allows. */
1091
+ static int record_of(nishsqlite_cursor *cur, u32 *sz, const void **p) {
1092
+ *sz = sqlite3BtreePayloadSize(cur->bt);
1093
+ if (*sz > SQLITE_MAX_LENGTH) return SQLITE_CORRUPT;
1094
+ u32 amt = 0;
1095
+ *p = sqlite3BtreePayloadFetch(cur->bt, &amt);
1096
+ if (amt >= *sz) return SQLITE_OK;
1097
+ if (cur->cap < *sz + NISH_SQLITE_SLACK) {
1098
+ u64 cap = (u64)cur->cap * 2;
1099
+ if (cap < (u64)*sz + NISH_SQLITE_SLACK) cap = (u64)*sz + NISH_SQLITE_SLACK;
1100
+ u8 *buf = sqlite3_realloc64(cur->buf, cap);
1101
+ if (buf == NULL) return SQLITE_NOMEM;
1102
+ cur->buf = buf;
1103
+ cur->cap = (u32)cap;
1104
+ }
1105
+ int rc = sqlite3BtreePayload(cur->bt, 0, *sz, cur->buf);
1106
+ if (rc != SQLITE_OK) return rc;
1107
+ memset(cur->buf + *sz, 0, NISH_SQLITE_SLACK);
1108
+ *p = cur->buf;
1109
+ return SQLITE_OK;
1110
+ }
1111
+
1112
+ /* The current row's record decoded into `row`, once per row. SQLITE_MISUSE
1113
+ * with no row, SQLITE_CORRUPT for a record longer than SQLite allows. */
1071
1114
  static int row_of(nishsqlite_cursor *cur) {
1072
1115
  if (!cur->has_row) return SQLITE_MISUSE;
1073
1116
  if (cur->unpacked) return SQLITE_OK;
1074
1117
  if (sqlite3BtreeCursorHasMoved(cur->bt)) return SQLITE_ABORT;
1075
- u32 sz = sqlite3BtreePayloadSize(cur->bt);
1076
- if (sz > SQLITE_MAX_LENGTH) return SQLITE_CORRUPT;
1118
+ u32 sz = 0;
1119
+ const void *p = NULL;
1120
+ int rc = record_of(cur, &sz, &p);
1121
+ if (rc != SQLITE_OK) return rc;
1077
1122
  cur->nfields = 0;
1078
1123
  if (sz > 0) {
1079
- u32 amt = 0;
1080
- const void *p = sqlite3BtreePayloadFetch(cur->bt, &amt);
1081
- if (amt < sz) {
1082
- if (cur->cap < sz + NISH_SQLITE_SLACK) {
1083
- u64 cap = (u64)cur->cap * 2;
1084
- if (cap < (u64)sz + NISH_SQLITE_SLACK) cap = (u64)sz + NISH_SQLITE_SLACK;
1085
- u8 *buf = sqlite3_realloc64(cur->buf, cap);
1086
- if (buf == NULL) return SQLITE_NOMEM;
1087
- cur->buf = buf;
1088
- cur->cap = (u32)cap;
1089
- }
1090
- int rc = sqlite3BtreePayload(cur->bt, 0, sz, cur->buf);
1091
- if (rc != SQLITE_OK) return rc;
1092
- memset(cur->buf + sz, 0, NISH_SQLITE_SLACK);
1093
- p = cur->buf;
1094
- }
1095
1124
  cur->row->nField = (u16)(cur->nkeyfields + 1);
1096
1125
  sqlite3VdbeRecordUnpack((int)sz, p, cur->row);
1097
1126
  cur->nfields = cur->row->nField;
@@ -1104,9 +1133,7 @@ static int row_of(nishsqlite_cursor *cur) {
1104
1133
  * The INTEGER PRIMARY KEY column is the row's key, not a field of its record,
1105
1134
  * so it answers NULL with `*alias` set. A row whose record holds fewer fields
1106
1135
  * than `n` (one written before ALTER TABLE added the column) is SQLITE_SCHEMA. */
1107
- static Mem *column_mem(int32_t h, int32_t n, nishsqlite_cursor **out, int *alias, int *rc) {
1108
- nishsqlite_cursor *cur = cursor_of(h);
1109
- *out = cur;
1136
+ static Mem *column_mem(nishsqlite_cursor *cur, int32_t n, int *alias, int *rc) {
1110
1137
  *alias = 0;
1111
1138
  if (cur == NULL || !cur->has_row) {
1112
1139
  *rc = SQLITE_MISUSE;
@@ -1130,74 +1157,174 @@ static Mem *column_mem(int32_t h, int32_t n, nishsqlite_cursor **out, int *alias
1130
1157
  return &cur->row->aMem[n];
1131
1158
  }
1132
1159
 
1133
- /* The column's type, SQLITE_INTEGER to SQLITE_NULL, or the negated code. The
1134
- * type is the stored value's: a REAL column holding a whole number stores it
1135
- * as an integer, and answers SQLITE_INTEGER, where a query would convert it. */
1136
- static int32_t unlocked_cursor_column_type(int32_t h, int32_t n) {
1137
- nishsqlite_cursor *cur;
1160
+ /* Column `n`'s type, SQLITE_INTEGER to SQLITE_NULL, or the negated code, with
1161
+ * `*v` its 8 bytes: with `bytes` set, a TEXT or BLOB column's length, else
1162
+ * the integer `sqlite3_column_int64` would convert the column to, the
1163
+ * INTEGER PRIMARY KEY column's being the row's key. With `bytes` set, a TEXT
1164
+ * or BLOB column's bytes are copied to `buf` when all of them fit in its
1165
+ * `room`. The type is the stored value's: a REAL column holding a whole
1166
+ * number stores it as an integer, and answers SQLITE_INTEGER, where a query
1167
+ * would convert it. */
1168
+ static int32_t column_value(nishsqlite_cursor *cur, int32_t n, uint64_t *v, int bytes, uint8_t *buf, uint64_t room) {
1138
1169
  int alias, rc;
1139
- Mem *m = column_mem(h, n, &cur, &alias, &rc);
1140
- if (alias) return sqlite3BtreeCursorHasMoved(cur->bt) ? -SQLITE_ABORT : SQLITE_INTEGER;
1141
- return m ? sqlite3_value_type((sqlite3_value *)m) : -rc;
1170
+ Mem *m = column_mem(cur, n, &alias, &rc);
1171
+ if (alias) {
1172
+ if (sqlite3BtreeCursorHasMoved(cur->bt)) return -SQLITE_ABORT;
1173
+ *v = (uint64_t)sqlite3BtreeIntegerKey(cur->bt);
1174
+ return SQLITE_INTEGER;
1175
+ }
1176
+ if (m == NULL) return -rc;
1177
+ sqlite3_value *val = (sqlite3_value *)m;
1178
+ int32_t type = sqlite3_value_type(val);
1179
+ if (bytes && (type == SQLITE_TEXT || type == SQLITE_BLOB)) {
1180
+ /* NULL for no bytes, so an empty value copies nothing. */
1181
+ const void *p = sqlite3_value_blob(val);
1182
+ *v = (uint32_t)sqlite3_value_bytes(val);
1183
+ if (p != NULL && *v <= room) memcpy(buf, p, (size_t)*v);
1184
+ } else {
1185
+ *v = (uint64_t)sqlite3_value_int64(val);
1186
+ }
1187
+ return type;
1188
+ }
1189
+
1190
+ static int32_t unlocked_cursor_column_type(nishsqlite_cursor *cur, int32_t n) {
1191
+ uint64_t v;
1192
+ return column_value(cur, n, &v, 1, NULL, 0);
1142
1193
  }
1143
1194
 
1144
1195
  /* The column as an integer, converted as `sqlite3_column_int64` converts; 0
1145
1196
  * when it cannot be read. */
1146
- static int64_t unlocked_cursor_column_i64(int32_t h, int32_t n) {
1147
- nishsqlite_cursor *cur;
1148
- int alias, rc;
1149
- Mem *m = column_mem(h, n, &cur, &alias, &rc);
1150
- if (alias) return sqlite3BtreeCursorHasMoved(cur->bt) ? 0 : sqlite3BtreeIntegerKey(cur->bt);
1151
- return m ? sqlite3_value_int64((sqlite3_value *)m) : 0;
1197
+ static int64_t unlocked_cursor_column_i64(nishsqlite_cursor *cur, int32_t n) {
1198
+ uint64_t v;
1199
+ return column_value(cur, n, &v, 0, NULL, 0) < 0 ? 0 : (int64_t)v;
1152
1200
  }
1153
1201
 
1154
1202
  /* Copies a TEXT or BLOB column's bytes into `buf` at `at`, `n` the buffer's
1155
1203
  * length, and answers their length; as `nishsqlite_column_into`, it copies
1156
- * only when all of them fit. NULL answers 0. An INTEGER or FLOAT column,
1157
- * the INTEGER PRIMARY KEY column among them, is SQLITE_MISMATCH: the cursor
1158
- * answers stored bytes and converts no number to text. Codes are negated. */
1159
- static int32_t unlocked_cursor_column_into(int32_t h, int32_t c, uint8_t *buf, uint64_t n, int32_t at) {
1204
+ * only when all of them fit. NULL answers 0. An INTEGER or FLOAT column is
1205
+ * SQLITE_MISMATCH, the INTEGER PRIMARY KEY column among them unless the row
1206
+ * has moved, which is SQLITE_ABORT as columnType answers: the cursor answers
1207
+ * stored bytes and converts no number to text. Codes are negated. */
1208
+ static int32_t unlocked_cursor_column_into(nishsqlite_cursor *cur, int32_t c, uint8_t *buf, uint64_t n, int32_t at) {
1160
1209
  if (at < 0 || (uint64_t)at > n) return -SQLITE_MISUSE;
1161
- nishsqlite_cursor *cur;
1162
- int alias, rc;
1163
- Mem *m = column_mem(h, c, &cur, &alias, &rc);
1164
- if (alias) return -SQLITE_MISMATCH;
1165
- if (m == NULL) return -rc;
1166
- int type = sqlite3_value_type((sqlite3_value *)m);
1210
+ uint64_t len;
1211
+ int32_t type = column_value(cur, c, &len, 1, buf + at, n - (uint64_t)at);
1212
+ if (type < 0) return type;
1167
1213
  if (type == SQLITE_NULL) return 0;
1168
1214
  if (type != SQLITE_TEXT && type != SQLITE_BLOB) return -SQLITE_MISMATCH;
1169
- const void *p = sqlite3_value_blob((sqlite3_value *)m);
1170
- int32_t len = sqlite3_value_bytes((sqlite3_value *)m);
1171
- if (p != NULL && (uint64_t)len <= n - (uint64_t)at) memcpy(buf + at, p, (size_t)len);
1172
- return len;
1215
+ return (int32_t)len;
1173
1216
  }
1174
1217
 
1175
1218
  /* The current row's rowid: a table row's key, or an index entry's last
1176
1219
  * field. 0 when there is no row or it cannot be read. */
1177
- static int64_t unlocked_cursor_rowid(int32_t h) {
1178
- nishsqlite_cursor *cur = cursor_of(h);
1220
+ static int64_t unlocked_cursor_rowid(nishsqlite_cursor *cur) {
1179
1221
  if (cur == NULL || !cur->has_row) return 0;
1180
1222
  if (!cur->is_index) return sqlite3BtreeCursorHasMoved(cur->bt) ? 0 : sqlite3BtreeIntegerKey(cur->bt);
1181
1223
  if (row_of(cur) != SQLITE_OK || cur->nfields < cur->ncol) return 0;
1182
1224
  return sqlite3_value_int64((sqlite3_value *)&cur->row->aMem[cur->ncol - 1]);
1183
1225
  }
1184
1226
 
1227
+ /* ---- Range counts ------------------------------------------------------- */
1228
+
1229
+ /* Counts an index's entries whose first key column is k, for each k in
1230
+ * [lo, hi], into the cursor's `counts[k - lo]` (read back with
1231
+ * `count_at`), in one seek and one forward walk: each entry's record is
1232
+ * compared with k by SQLite's record compare, never unpacked. It uses the
1233
+ * kept key's first value, so it leaves no seek key set, and leaves the cursor
1234
+ * on no row. `n` is the caller's room, which bounds the buffer: it grows to
1235
+ * the largest span asked and is kept. It answers the entries counted, or
1236
+ * negated: SQLITE_MISUSE for a table cursor, SQLITE_MISMATCH for an index
1237
+ * whose first key column does not store integers as integers, SQLITE_RANGE
1238
+ * for lo > hi or a span longer than `n`, SQLITE_NOMEM, SQLITE_TOOBIG past
1239
+ * INT32_MAX entries, and SQLite's code when a page cannot be read. */
1240
+ static int32_t unlocked_cursor_count_range(nishsqlite_cursor *cur, int64_t lo, int64_t hi, uint64_t n) {
1241
+ if (cur == NULL || !cur->is_index) return -SQLITE_MISUSE;
1242
+ if (!cur->int_first) return -SQLITE_MISMATCH;
1243
+ if (lo > hi || (uint64_t)hi - (uint64_t)lo >= n) return -SQLITE_RANGE;
1244
+ uint64_t span = (uint64_t)hi - (uint64_t)lo + 1;
1245
+ cur->ncounts = 0;
1246
+ if (span > cur->counts_cap) {
1247
+ int64_t *counts = sqlite3_realloc64(cur->counts, span * sizeof *counts);
1248
+ if (counts == NULL) return -SQLITE_NOMEM;
1249
+ cur->counts = counts;
1250
+ cur->counts_cap = span;
1251
+ }
1252
+ int64_t *out = cur->counts;
1253
+ memset(out, 0, (size_t)span * sizeof *out);
1254
+ (void)unlocked_key_reset(cur);
1255
+ /* In index order: lo up to hi, or hi down to lo under DESC. */
1256
+ int desc = (cur->ki->aSortFlags[0] & KEYINFO_ORDER_DESC) != 0;
1257
+ uint64_t at = desc ? span - 1 : 0; /* k - lo for the k the walk is on */
1258
+ Mem *k = &cur->key[0];
1259
+ sqlite3VdbeMemSetInt64(k, desc ? hi : lo);
1260
+ UnpackedRecord *key = seek_key(cur, 1, 1);
1261
+ int res = 0;
1262
+ int rc = sqlite3BtreeIndexMoveto(cur->bt, key, &res);
1263
+ if (rc == SQLITE_OK && res < 0) rc = sqlite3BtreeNext(cur->bt, 0);
1264
+ key->default_rc = 0;
1265
+ int32_t total = 0;
1266
+ while (rc == SQLITE_OK) {
1267
+ u32 sz = 0;
1268
+ const void *p = NULL;
1269
+ if ((rc = record_of(cur, &sz, &p)) != SQLITE_OK) break;
1270
+ /* row_of reads an empty record as zero fields, but a compare reads the
1271
+ * record's header size from its first byte, which an empty one does not
1272
+ * have; and an index entry always holds at least its rowid. */
1273
+ if (sz == 0) {
1274
+ rc = SQLITE_CORRUPT;
1275
+ break;
1276
+ }
1277
+ int c = sqlite3VdbeRecordCompare((int)sz, p, key);
1278
+ if (key->errCode) {
1279
+ rc = key->errCode;
1280
+ break;
1281
+ }
1282
+ /* The entry sorts after k: move k on in index order until it does not. */
1283
+ while (c > 0) {
1284
+ if (desc ? at == 0 : at == span - 1) goto done;
1285
+ at = desc ? at - 1 : at + 1;
1286
+ sqlite3VdbeMemSetInt64(k, (int64_t)((uint64_t)lo + at));
1287
+ c = sqlite3VdbeRecordCompare((int)sz, p, key);
1288
+ }
1289
+ if (c == 0) {
1290
+ if (total == INT32_MAX) {
1291
+ rc = SQLITE_TOOBIG;
1292
+ break;
1293
+ }
1294
+ out[at]++;
1295
+ total++;
1296
+ }
1297
+ rc = sqlite3BtreeNext(cur->bt, 0);
1298
+ }
1299
+ done:
1300
+ sqlite3VdbeMemSetNull(k);
1301
+ moved(cur, SQLITE_OK, 0);
1302
+ if (rc != SQLITE_OK && rc != SQLITE_DONE) return -rc;
1303
+ cur->ncounts = span;
1304
+ return total;
1305
+ }
1306
+
1307
+ /* The last successful count's entry `i`: -1 past its span, and for every
1308
+ * `i` after a count that failed past its refusals. */
1309
+ static int64_t unlocked_cursor_count_at(nishsqlite_cursor *cur, uint64_t i) {
1310
+ return cur != NULL && i < cur->ncounts ? cur->counts[i] : -1;
1311
+ }
1312
+
1185
1313
  /* ---- Locking ------------------------------------------------------------ */
1186
1314
 
1187
1315
  /* Every cursor entry point holds its connection's `db->mutex` around the
1188
1316
  * work, as SQLite's own API functions do: the internal functions the cursors
1189
1317
  * call (the allocator, the decoder, the B-tree) expect it held. The mutex is
1190
- * recursive, and NULL (a no-op) when the build or the open asks for none. */
1318
+ * recursive, and NULL (a no-op) when the build or the open asks for none. A
1319
+ * cursor entry point looks its handle up once (`cursor_of`), outside the
1320
+ * mutex: the tables are the thread's own, and the lookup reads nothing of
1321
+ * SQLite's. The work then gets the cursor, NULL for a handle that names none,
1322
+ * which every one of them answers as SQLITE_MISUSE. */
1191
1323
  static sqlite3_mutex *conn_mutex(int32_t h) {
1192
1324
  nishsqlite_conn *c = conn_of(h);
1193
1325
  return c ? sqlite3_db_mutex(c->db) : NULL;
1194
1326
  }
1195
1327
 
1196
- static sqlite3_mutex *cur_mutex(int32_t h) {
1197
- nishsqlite_slot *s = slot_of(&nishsqlite_cursors, h);
1198
- return s ? sqlite3_db_mutex(((nishsqlite_cursor *)s->p)->conn->db) : NULL;
1199
- }
1200
-
1201
1328
  #define NISH_SQLITE_LOCKED(type, mutex, call) \
1202
1329
  sqlite3_mutex *mx_ = (mutex); \
1203
1330
  sqlite3_mutex_enter(mx_); \
@@ -1205,66 +1332,112 @@ static sqlite3_mutex *cur_mutex(int32_t h) {
1205
1332
  sqlite3_mutex_leave(mx_); \
1206
1333
  return r_
1207
1334
 
1335
+ #define NISH_SQLITE_CURSOR(type, call) \
1336
+ nishsqlite_cursor *cur = cursor_of(h); \
1337
+ NISH_SQLITE_LOCKED(type, cur ? cur->mutex : NULL, call)
1338
+
1208
1339
  int32_t nishsqlite_cursor_open(int32_t h, const char *name, const char *columns) {
1209
1340
  NISH_SQLITE_LOCKED(int32_t, conn_mutex(h), unlocked_cursor_open(h, name, columns));
1210
1341
  }
1211
1342
 
1212
1343
  int32_t nishsqlite_cursor_close(int32_t h) {
1213
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_close(h));
1344
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_close(cur, h));
1214
1345
  }
1215
1346
 
1216
1347
  int32_t nishsqlite_cursor_first(int32_t h) {
1217
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_first(h));
1348
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_first(cur));
1218
1349
  }
1219
1350
 
1220
1351
  int32_t nishsqlite_cursor_last(int32_t h) {
1221
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_last(h));
1352
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_last(cur));
1222
1353
  }
1223
1354
 
1224
1355
  int32_t nishsqlite_cursor_next(int32_t h) {
1225
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_next(h));
1356
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_next(cur));
1226
1357
  }
1227
1358
 
1228
1359
  int32_t nishsqlite_cursor_previous(int32_t h) {
1229
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_previous(h));
1360
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_previous(cur));
1230
1361
  }
1231
1362
 
1232
1363
  int32_t nishsqlite_cursor_seek_rowid(int32_t h, int64_t rowid) {
1233
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_seek_rowid(h, rowid));
1364
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_seek_rowid(cur, rowid));
1234
1365
  }
1235
1366
 
1236
1367
  int32_t nishsqlite_key_reset(int32_t h) {
1237
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_key_reset(h));
1368
+ NISH_SQLITE_CURSOR(int32_t, unlocked_key_reset(cur));
1238
1369
  }
1239
1370
 
1240
1371
  int32_t nishsqlite_key_i64(int32_t h, int64_t v) {
1241
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_key_i64(h, v));
1372
+ NISH_SQLITE_CURSOR(int32_t, unlocked_key_i64(cur, v));
1242
1373
  }
1243
1374
 
1244
1375
  int32_t nishsqlite_key_text(int32_t h, const char *text) {
1245
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_key_text(h, text));
1376
+ NISH_SQLITE_CURSOR(int32_t, unlocked_key_text(cur, text));
1246
1377
  }
1247
1378
 
1248
1379
  int32_t nishsqlite_key_bytes(int32_t h, const uint8_t *buf, uint64_t n, int32_t off, int32_t len) {
1249
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_key_bytes(h, buf, n, off, len));
1380
+ NISH_SQLITE_CURSOR(int32_t, unlocked_key_bytes(cur, buf, n, off, len));
1250
1381
  }
1251
1382
 
1383
+ int32_t nishsqlite_cursor_count_range(int32_t h, int64_t lo, int64_t hi, uint64_t n) {
1384
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_count_range(cur, lo, hi, n));
1385
+ }
1386
+ int64_t nishsqlite_cursor_count_at(int32_t h, uint64_t i) {
1387
+ NISH_SQLITE_CURSOR(int64_t, unlocked_cursor_count_at(cur, i));
1388
+ }
1252
1389
  int32_t nishsqlite_cursor_seek(int32_t h, int32_t op) {
1253
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_seek(h, op));
1390
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_seek(cur, op));
1254
1391
  }
1255
1392
 
1256
1393
  int32_t nishsqlite_cursor_column_type(int32_t h, int32_t n) {
1257
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_column_type(h, n));
1394
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_column_type(cur, n));
1258
1395
  }
1259
1396
 
1260
1397
  int64_t nishsqlite_cursor_column_i64(int32_t h, int32_t n) {
1261
- NISH_SQLITE_LOCKED(int64_t, cur_mutex(h), unlocked_cursor_column_i64(h, n));
1398
+ NISH_SQLITE_CURSOR(int64_t, unlocked_cursor_column_i64(cur, n));
1262
1399
  }
1263
1400
 
1264
1401
  int32_t nishsqlite_cursor_column_into(int32_t h, int32_t c, uint8_t *buf, uint64_t n, int32_t at) {
1265
- NISH_SQLITE_LOCKED(int32_t, cur_mutex(h), unlocked_cursor_column_into(h, c, buf, n, at));
1402
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_column_into(cur, c, buf, n, at));
1266
1403
  }
1267
1404
 
1268
1405
  int64_t nishsqlite_cursor_rowid(int32_t h) {
1269
- NISH_SQLITE_LOCKED(int64_t, cur_mutex(h), unlocked_cursor_rowid(h));
1406
+ NISH_SQLITE_CURSOR(int64_t, unlocked_cursor_rowid(cur));
1407
+ }
1408
+
1409
+ /* readRow: reads a list of columns of the current row in one call. `io` holds
1410
+ * `n / 11` columns: first their indices, a little-endian u16 each, which it
1411
+ * leaves as they are, then a slot of 9 bytes per column, in order: the type
1412
+ * unlocked_cursor_column_type answers, then 8 bytes, a TEXT or BLOB
1413
+ * column's length, or the value unlocked_cursor_column_i64 answers for the
1414
+ * rest. No TEXT or BLOB is converted to a number. Each column's Mem is
1415
+ * found once, under the one lock the caller holds. The bytes of TEXT and
1416
+ * BLOB columns go to `text` in column order while they fit. Answers the
1417
+ * length they need, SQLITE_TOOBIG past INT32_MAX, or the code column_type
1418
+ * refuses the first bad column with, MISUSE with no row even for an empty
1419
+ * list; after a refusal, or a length over `ntext`, the caller reads nothing
1420
+ * from `io` or `text`. The slots are stored in the machine's order, which
1421
+ * is little-endian on every target the runtime builds for. Codes are
1422
+ * negated. */
1423
+ #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
1424
+ #error "readRow's slots are little-endian"
1425
+ #endif
1426
+ static int32_t unlocked_cursor_read_row(nishsqlite_cursor *cur, uint8_t *io, uint64_t n, uint8_t *text, uint64_t ntext) {
1427
+ if (cur == NULL || !cur->has_row) return -SQLITE_MISUSE;
1428
+ uint64_t ncols = n / 11, need = 0;
1429
+ uint8_t *out = io + 2 * ncols;
1430
+ for (uint64_t k = 0; k < ncols; k++, out += 9) {
1431
+ uint64_t v;
1432
+ int32_t type = column_value(cur, io[2 * k] | (io[2 * k + 1] << 8), &v, 1, text + need, need <= ntext ? ntext - need : 0);
1433
+ if (type < 0) return type;
1434
+ if (type == SQLITE_TEXT || type == SQLITE_BLOB) need += v;
1435
+ out[0] = (uint8_t)type;
1436
+ memcpy(out + 1, &v, 8);
1437
+ }
1438
+ return need > INT32_MAX ? -SQLITE_TOOBIG : (int32_t)need;
1439
+ }
1440
+
1441
+ int32_t nishsqlite_cursor_read_row(int32_t h, uint8_t *io, uint64_t n, uint8_t *text, uint64_t ntext) {
1442
+ NISH_SQLITE_CURSOR(int32_t, unlocked_cursor_read_row(cur, io, n, text, ntext));
1270
1443
  }