Skip to content

Instantly share code, notes, and snippets.

@imba-tjd
Created January 11, 2023 10:45
Show Gist options
  • Save imba-tjd/c3eacda313956325a6da3b6780eab659 to your computer and use it in GitHub Desktop.
Save imba-tjd/c3eacda313956325a6da3b6780eab659 to your computer and use it in GitHub Desktop.
SQLite Device Characteristics Check
/* SQLite Device Characteristics Check
Goal:
Tell you whether -DSQLITE_ENABLE_ATOMIC_WRITE has benefits on your filesystem.
However I don't konw much about sqlite so the logic may not be right.
Usage:
Download sqlite3 source code.
gcc check.c sqlite3.c -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_ATOMIC_WRITE -DSQLITE_OMIT_LOAD_EXTENSION
Reference:
https://www.sqlite.org/compile.html#enable_atomic_write
https://www.sqlite.org/c3ref/io_methods.html
https://www.sqlite.org/c3ref/c_iocap_atomic.html
https://www.sqlite.org/c3ref/file_control.html
License: MIT
*/
#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"
#define CHECK_ABILITY(ch, ability) do { if(ch & ability) puts(#ability); } while(0)
int main() {
int rc;
sqlite3* db;
rc = sqlite3_open("test.db", &db);
if(rc) { puts("failed to open"); return 1; }
sqlite3_file* f;
rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_FILE_POINTER, &f);
if(rc) { puts("failed on file_control"); return 1; }
int ch = f->pMethods->xDeviceCharacteristics(f);
CHECK_ABILITY(ch, SQLITE_IOCAP_ATOMIC);
CHECK_ABILITY(ch, SQLITE_IOCAP_ATOMIC512);
CHECK_ABILITY(ch, SQLITE_IOCAP_ATOMIC1K);
CHECK_ABILITY(ch, SQLITE_IOCAP_ATOMIC2K);
CHECK_ABILITY(ch, SQLITE_IOCAP_ATOMIC4K);
CHECK_ABILITY(ch, SQLITE_IOCAP_ATOMIC8K);
CHECK_ABILITY(ch, SQLITE_IOCAP_ATOMIC16K);
CHECK_ABILITY(ch, SQLITE_IOCAP_ATOMIC32K);
CHECK_ABILITY(ch, SQLITE_IOCAP_ATOMIC64K);
CHECK_ABILITY(ch, SQLITE_IOCAP_SAFE_APPEND);
CHECK_ABILITY(ch, SQLITE_IOCAP_SEQUENTIAL);
CHECK_ABILITY(ch, SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN);
CHECK_ABILITY(ch, SQLITE_IOCAP_POWERSAFE_OVERWRITE);
CHECK_ABILITY(ch, SQLITE_IOCAP_IMMUTABLE);
CHECK_ABILITY(ch, SQLITE_IOCAP_BATCH_ATOMIC);
sqlite3_close(db);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment