|
| 1 | +#include "../parse.h" |
| 2 | +#include "../opcodes.h" |
| 3 | +#include "../src/sqliteInt.h" |
| 4 | +#include "../src/vectorIndexInt.h" |
| 5 | +#include "assert.h" |
| 6 | +#include "stdbool.h" |
| 7 | +#include "string.h" |
| 8 | +#include "stdarg.h" |
| 9 | +#include "time.h" |
| 10 | +#include <sqlite3.h> |
| 11 | + |
| 12 | +#define eprintf(...) fprintf(stderr, __VA_ARGS__) |
| 13 | +#define ensure(condition, ...) { if (!(condition)) { eprintf(__VA_ARGS__); exit(1); } } |
| 14 | + |
| 15 | +int searchVectors(sqlite3 *db, sqlite3_stmt *pStmt, void **ppItems, int *pItemSize) { |
| 16 | + ensure(sqlite3_reset(pStmt) == SQLITE_OK, "failed to reset statement: %s\n", sqlite3_errmsg(db)); |
| 17 | + int rows = 0; |
| 18 | + while(1){ |
| 19 | + int rc = sqlite3_step(pStmt); |
| 20 | + if( rc == SQLITE_DONE ){ |
| 21 | + break; |
| 22 | + } else if( rc == SQLITE_ROW ){ |
| 23 | + const void *pBlob = sqlite3_column_blob(pStmt, 0); |
| 24 | + int nBlobSize = sqlite3_column_bytes(pStmt, 0); |
| 25 | + void *pBlobCopy = malloc(nBlobSize); |
| 26 | + memcpy(pBlobCopy, pBlob, nBlobSize); |
| 27 | + ppItems[rows] = pBlobCopy; |
| 28 | + pItemSize[rows] = nBlobSize; |
| 29 | + rows++; |
| 30 | + }else{ |
| 31 | + ensure(false, "unexpected step result: %s\n", sqlite3_errmsg(db)); |
| 32 | + } |
| 33 | + } |
| 34 | + return rows; |
| 35 | +} |
| 36 | + |
| 37 | +int searchRows(sqlite3 *db, sqlite3_stmt *pStmt, unsigned char *pBlob, int nBlobSize, int *result) { |
| 38 | + ensure(sqlite3_reset(pStmt) == SQLITE_OK, "failed to reset statement: %s\n", sqlite3_errmsg(db)); |
| 39 | + ensure(sqlite3_bind_blob(pStmt, 1, pBlob, nBlobSize, SQLITE_TRANSIENT) == SQLITE_OK, "failed to bind blob: %s\n", sqlite3_errmsg(db)); |
| 40 | + int rows = 0; |
| 41 | + while(1){ |
| 42 | + int rc = sqlite3_step(pStmt); |
| 43 | + if( rc == SQLITE_DONE ){ |
| 44 | + break; |
| 45 | + } else if( rc == SQLITE_ROW ){ |
| 46 | + int rowid = sqlite3_column_int(pStmt, 0); |
| 47 | + result[rows++] = rowid; |
| 48 | + }else{ |
| 49 | + ensure(false, "unexpected step result: %s\n", sqlite3_errmsg(db)); |
| 50 | + } |
| 51 | + } |
| 52 | + return rows; |
| 53 | +} |
| 54 | + |
| 55 | +double recall(int *pExact, int nExactSize, int *pAnn, int nAnnSize) { |
| 56 | + int overlap = 0; |
| 57 | + for( int i = 0; i < nExactSize; i++ ){ |
| 58 | + int ok = 0; |
| 59 | + for(int s = 0; !ok && s < nAnnSize; s++ ){ |
| 60 | + ok |= pExact[i] == pAnn[s]; |
| 61 | + } |
| 62 | + if(ok){ |
| 63 | + overlap++; |
| 64 | + } |
| 65 | + } |
| 66 | + return overlap * 1.0 / nExactSize; |
| 67 | +} |
| 68 | + |
| 69 | +int main(int argc, char* argv[]) { |
| 70 | + ensure(argc == 5, "path to the db file, recall type, ann query, exact query"); |
| 71 | + sqlite3* db; |
| 72 | + int rc = sqlite3_open(argv[1], &db); |
| 73 | + ensure(rc == 0, "failed to open db: rc=%d\n", rc); |
| 74 | + printf("open sqlite db at '%s'\n", argv[1]); |
| 75 | + |
| 76 | + char *zType = argv[2]; |
| 77 | + void* vectors[65536]; |
| 78 | + int vectorSize[65536]; |
| 79 | + char *zAnnQuery = argv[3]; |
| 80 | + char *zExactQuery = argv[4]; |
| 81 | + |
| 82 | + sqlite3_stmt *pVectors; |
| 83 | + ensure(sqlite3_prepare_v2(db, "SELECT emb FROM queries", -1, &pVectors, 0) == SQLITE_OK, "failed to prepare vectors statement: %s\n", sqlite3_errmsg(db)); |
| 84 | + sqlite3_stmt *pAnn; |
| 85 | + ensure(sqlite3_prepare_v2(db, zAnnQuery, -1, &pAnn, 0) == SQLITE_OK, "failed to prepare ann statement: %s\n", sqlite3_errmsg(db)); |
| 86 | + sqlite3_stmt *pExact; |
| 87 | + ensure(sqlite3_prepare_v2(db, zExactQuery, -1, &pExact, 0) == SQLITE_OK, "failed to prepare exact statement: %s\n", sqlite3_errmsg(db)); |
| 88 | + |
| 89 | + int nVectors = searchVectors(db, pVectors, vectors, vectorSize); |
| 90 | + |
| 91 | + unsigned char blob[8 * 65536]; |
| 92 | + int annResult[65536]; |
| 93 | + int exactResult[65536]; |
| 94 | + |
| 95 | + printf("ready to perform %d queries with %s ann query and %s exact query\n", nVectors, zAnnQuery, zExactQuery); |
| 96 | + double totalRecall = 0; |
| 97 | + int total = 0; |
| 98 | + for(int i = 0; i < nVectors; i++){ |
| 99 | + if( i % 10 == 9 ){ |
| 100 | + eprintf("progress: %d / %d, %.2f%% %s (avg.)\n", i, nVectors, totalRecall / total * 100, zType); |
| 101 | + } |
| 102 | + int nAnnSize = searchRows(db, pAnn, vectors[i], vectorSize[i], annResult); |
| 103 | + int nExactSize = searchRows(db, pExact, vectors[i], vectorSize[i], exactResult); |
| 104 | + double r = recall(exactResult, nExactSize, annResult, nAnnSize); |
| 105 | + totalRecall += r; |
| 106 | + total++; |
| 107 | + } |
| 108 | + sqlite3_finalize(pAnn); |
| 109 | + sqlite3_finalize(pExact); |
| 110 | + printf("%.2f%% %s (avg.)\n", totalRecall / total * 100, zType); |
| 111 | + sqlite3_close(db); |
| 112 | + return 0; |
| 113 | +} |
0 commit comments