Skip to content

Commit

Permalink
Add get db path function
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Mar 13, 2024
1 parent 4a013dd commit 1c5a7aa
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 115 deletions.
25 changes: 25 additions & 0 deletions cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,30 @@ void install(jsi::Runtime &rt,
return {};
});

auto get_db_path = HOSTFN("getDbPath", 2) {
std::string db_name = args[0].asString(rt).utf8(rt);
std::string path = std::string(basePath);
if (count > 1 && !args[1].isUndefined() && !args[1].isNull()) {
if (!args[1].isString()) {
throw std::runtime_error(
"[op-sqlite][open] database location must be a string");
}

std::string lastPath = args[1].asString(rt).utf8(rt);

if (lastPath == ":memory:") {
path = ":memory:";
} else if (lastPath.rfind("/", 0) == 0) {
path = lastPath;
} else {
path = path + "/" + lastPath;
}
}

auto result = opsqlite_get_db_path(db_name, path);
return jsi::String::createFromUtf8(rt, result);
});

jsi::Object module = jsi::Object(rt);

module.setProperty(rt, "open", std::move(open));
Expand All @@ -636,6 +660,7 @@ void install(jsi::Runtime &rt,
module.setProperty(rt, "prepareStatement", std::move(prepare_statement));
module.setProperty(rt, "loadExtension", std::move(load_extension));
module.setProperty(rt, "executeRawAsync", std::move(execute_raw_async));
module.setProperty(rt, "getDbPath", std::move(get_db_path));

rt.global().setProperty(rt, "__OPSQLiteProxy", std::move(module));
}
Expand Down
10 changes: 5 additions & 5 deletions cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ inline void check_db_open(std::string const &db_name) {

/// Returns the completely formed db path, but it also creates any sub-folders
/// along the way
std::string get_db_path(std::string const &db_name,
std::string const &location) {
std::string opsqlite_get_db_path(std::string const &db_name,
std::string const &location) {

if (location == ":memory:") {
return location;
Expand All @@ -43,7 +43,7 @@ std::string get_db_path(std::string const &db_name,

BridgeResult opsqlite_open(std::string const &dbName,
std::string const &lastPath) {
std::string dbPath = get_db_path(dbName, lastPath);
std::string dbPath = opsqlite_get_db_path(dbName, lastPath);

int sqlOpenFlags =
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
Expand Down Expand Up @@ -80,7 +80,7 @@ BridgeResult opsqlite_attach(std::string const &mainDBName,
std::string const &docPath,
std::string const &databaseToAttach,
std::string const &alias) {
std::string dbPath = get_db_path(databaseToAttach, docPath);
std::string dbPath = opsqlite_get_db_path(databaseToAttach, docPath);
std::string statement = "ATTACH DATABASE '" + dbPath + "' AS " + alias;

BridgeResult result =
Expand Down Expand Up @@ -124,7 +124,7 @@ BridgeResult opsqlite_remove(std::string const &dbName,
}
}

std::string dbPath = get_db_path(dbName, docPath);
std::string dbPath = opsqlite_get_db_path(dbName, docPath);

if (!file_exists(dbPath)) {
return {.type = SQLiteError,
Expand Down
3 changes: 3 additions & 0 deletions cpp/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ typedef std::function<void(std::string dbName, std::string tableName,
typedef std::function<void(std::string dbName)> CommitCallback;
typedef std::function<void(std::string dbName)> RollbackCallback;

std::string opsqlite_get_db_path(std::string const &db_name,
std::string const &location);

BridgeResult opsqlite_open(std::string const &dbName,
std::string const &dbPath);

Expand Down
Binary file modified example/.yarn/install-state.gz
Binary file not shown.
24 changes: 5 additions & 19 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ PODS:
- hermes-engine/Pre-built (= 0.73.1)
- hermes-engine/Pre-built (0.73.1)
- libevent (2.1.12)
- MMKV (1.3.2):
- MMKVCore (~> 1.3.2)
- MMKVCore (1.3.2)
- op-sqlite (2.0.20):
- op-sqlite (2.0.22):
- React
- React-callinvoker
- React-Core
Expand Down Expand Up @@ -893,9 +890,6 @@ PODS:
- React-Mapbuffer (0.73.1):
- glog
- React-debug
- react-native-mmkv (2.11.0):
- MMKV (>= 1.2.13)
- React-Core
- react-native-performance (5.1.0):
- React-Core
- react-native-restart (0.0.27):
Expand Down Expand Up @@ -1102,7 +1096,6 @@ DEPENDENCIES:
- React-jsinspector (from `../node_modules/react-native/ReactCommon/jsinspector-modern`)
- React-logger (from `../node_modules/react-native/ReactCommon/logger`)
- React-Mapbuffer (from `../node_modules/react-native/ReactCommon`)
- react-native-mmkv (from `../node_modules/react-native-mmkv`)
- react-native-performance (from `../node_modules/react-native-performance`)
- react-native-restart (from `../node_modules/react-native-restart`)
- React-nativeconfig (from `../node_modules/react-native/ReactCommon`)
Expand Down Expand Up @@ -1131,8 +1124,6 @@ SPEC REPOS:
trunk:
- fmt
- libevent
- MMKV
- MMKVCore
- SocketRocket

EXTERNAL SOURCES:
Expand Down Expand Up @@ -1193,8 +1184,6 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon/logger"
React-Mapbuffer:
:path: "../node_modules/react-native/ReactCommon"
react-native-mmkv:
:path: "../node_modules/react-native-mmkv"
react-native-performance:
:path: "../node_modules/react-native-performance"
react-native-restart:
Expand Down Expand Up @@ -1243,17 +1232,15 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon/yoga"

SPEC CHECKSUMS:
boost: d3f49c53809116a5d38da093a8aa78bf551aed09
DoubleConversion: 76ab83afb40bddeeee456813d9c04f67f78771b5
boost: 26fad476bfa736552bbfa698a06cc530475c1505
DoubleConversion: fea03f2699887d960129cc54bba7e52542b6f953
FBLazyVector: 2296bacb2fa157a43991048b0a9d71c1c8b65083
FBReactNativeSpec: df0ebe69acd14ce0be0269cf75b6e338a727259b
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2
hermes-engine: 34df9d5034e90bd9bf1505e1ca198760373935af
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
MMKV: f21593c0af4b3f2a0ceb8f820f28bb639ea22bb7
MMKVCore: 31b4cb83f8266467eef20a35b6d78e409a11060d
op-sqlite: ff96cfc00c55408bdfa9569d0c30961f9109d25e
op-sqlite: 4db95f3c6895f55b4c61eb69223e07322b120e0b
RCT-Folly: 7169b2b1c44399c76a47b5deaaba715eeeb476c0
RCTRequired: 6dda55e483f75d2b43781d8ad5bd7df276a50981
RCTTypeSafety: df0f2632f4e89938b9b9f6152b5e6c66fc6e969e
Expand All @@ -1275,7 +1262,6 @@ SPEC CHECKSUMS:
React-jsinspector: 369048694e39942063c5d08e9580b43e2edd379a
React-logger: e0c1e918d9588a9f39c9bc62d9d6bfe9ca238d9d
React-Mapbuffer: 9731a0a63ebaf8976014623c4d637744d7353a7c
react-native-mmkv: e97c0c79403fb94577e5d902ab1ebd42b0715b43
react-native-performance: cef2b618d47b277fb5c3280b81a3aad1e72f2886
react-native-restart: 7595693413fe3ca15893702f2c8306c62a708162
React-nativeconfig: 37aecd26d64b79327c3f10e43b2e9a6c425e0a60
Expand Down Expand Up @@ -1303,4 +1289,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 0ab74fecad6ac2e35f8eab32fe5772c19d2015b2

COCOAPODS: 1.14.3
COCOAPODS: 1.14.0
1 change: 0 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"nativewind": "^2.0.11",
"react": "18.2.0",
"react-native": "0.73.1",
"react-native-mmkv": "^2.11.0",
"react-native-performance": "^5.1.0",
"react-native-restart": "^0.0.27",
"reflect-metadata": "^0.1.13",
Expand Down
88 changes: 12 additions & 76 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, {useEffect, useState} from 'react';
import {
ActivityIndicator,
Button,
Clipboard,
SafeAreaView,
ScrollView,
Text,
Expand All @@ -22,9 +23,6 @@ import clsx from 'clsx';
import {preparedStatementsTests} from './tests/preparedStatements.spec';
import {constantsTests} from './tests/constants.spec';
import performance from 'react-native-performance';
// import UpdateHookPage from './UpdateHook';
import {MMKV} from 'react-native-mmkv';
export const mmkv = new MMKV();

const StyledScrollView = styled(ScrollView, {
props: {
Expand All @@ -42,10 +40,6 @@ export default function App() {
[],
);
const [singleRecordTime, setSingleRecordTime] = useState<number>(0);
const [sqliteMMSetTime, setSqliteMMSetTime] = useState(0);
const [mmkvSetTime, setMMKVSetTime] = useState(0);
const [sqliteGetTime, setSqliteMMGetTime] = useState(0);
const [mmkvGetTime, setMMKVGetTime] = useState(0);
const [rawExecutionTimes, setRawExecutionTimes] = useState<number[]>([]);
useEffect(() => {
setResults([]);
Expand Down Expand Up @@ -89,89 +83,31 @@ export default function App() {
}
};

const queryAndReload = async () => {
queryLargeDB();
setTimeout(() => {
RNRestart.restart();
}, 200);
const copyDbPathToClipboad = async () => {
const db = await open({name: 'dbPath.sqlite'});
db.execute('CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT)');
const path = db.getDbPath();
await db.close();
console.warn(path);
Clipboard.setString(path);
};

const allTestsPassed = results.reduce((acc: boolean, r: any) => {
return acc && r.type !== 'incorrect';
}, true);

const testAgainstMMKV = () => {
const db = open({
name: 'mmkvTestDb',
});

db.execute('PRAGMA mmap_size=268435456');
// db.execute('PRAGMA journal_mode = OFF;');
db.execute('DROP TABLE IF EXISTS mmkvTest;');
db.execute('CREATE TABLE mmkvTest (text TEXT);');

let insertStatment = db.prepareStatement(
'INSERT INTO "mmkvTest" (text) VALUES (?)',
);
insertStatment.bind(['quack']);

let start = performance.now();
insertStatment.execute();
let end = performance.now();
setSqliteMMSetTime(end - start);

start = performance.now();
mmkv.set('mmkvDef', 'quack');
end = performance.now();
setMMKVSetTime(end - start);

let readStatement = db.prepareStatement('SELECT text from mmkvTest;');
start = performance.now();
readStatement.execute();
end = performance.now();
setSqliteMMGetTime(end - start);

start = performance.now();
mmkv.getString('mmkvDef');
end = performance.now();
setMMKVGetTime(end - start);

db.close();
};

return (
<SafeAreaView className="flex-1 bg-neutral-900">
<StyledScrollView>
<View className="flex-row p-2 bg-neutral-800 items-center">
<Text className={'font-bold flex-1 text-white'}>Tools</Text>
</View>
<Button title="Reload app middle of query" onPress={queryAndReload} />
<Button
title="Copy DB Path to clipboard"
onPress={copyDbPathToClipboad}
/>
<Button title="Create 300k Record DB" onPress={createLargeDb} />
<Button title="Query 300k Records" onPress={queryLargeDb} />
{/* <Button title="Query single record" onPress={querySingleRecord} />
<Button title="Against MMKV" onPress={testAgainstMMKV} /> */}
<View className="gap-2 items-center mt-4">
{!!sqliteMMSetTime && (
<Text className="text-white">
MM SQLite Write: {sqliteMMSetTime.toFixed(3)} ms
</Text>
)}
{!!mmkvSetTime && (
<Text className="text-white">
MMKV Write: {mmkvSetTime.toFixed(3)} ms
</Text>
)}
{!!sqliteGetTime && (
<Text className="text-white">
MM SQLite Get: {sqliteGetTime.toFixed(3)} ms
</Text>
)}
{!!mmkvGetTime && (
<Text className="text-white">
MMKV Get: {mmkvGetTime.toFixed(3)} ms
</Text>
)}
</View>
{isLoading && <ActivityIndicator color={'white'} size="large" />}

{!!singleRecordTime && (
Expand Down
3 changes: 0 additions & 3 deletions example/src/Database.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import performance from 'react-native-performance';
import Chance from 'chance';
import {open} from '@op-engineering/op-sqlite';
import {MMKV} from 'react-native-mmkv';
const mmkv = new MMKV();
import {Buffer} from 'buffer';

const chance = new Chance();

Expand Down
11 changes: 0 additions & 11 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8145,7 +8145,6 @@ __metadata:
prettier: "npm:2.8.8"
react: "npm:18.2.0"
react-native: "npm:0.73.1"
react-native-mmkv: "npm:^2.11.0"
react-native-performance: "npm:^5.1.0"
react-native-restart: "npm:^0.0.27"
react-test-renderer: "npm:18.2.0"
Expand Down Expand Up @@ -8779,16 +8778,6 @@ __metadata:
languageName: node
linkType: hard

"react-native-mmkv@npm:^2.11.0":
version: 2.11.0
resolution: "react-native-mmkv@npm:2.11.0"
peerDependencies:
react: "*"
react-native: ">=0.71.0"
checksum: 10c0/835626215db96509d46a845012e4a2510f126604ba3d4df7b5edff49fdb7af6e5d01ad16b3f760126145099fd647328d6229bbe91f2408768da1964af32afadc
languageName: node
linkType: hard

"react-native-performance@npm:^5.1.0":
version: 5.1.0
resolution: "react-native-performance@npm:5.1.0"
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ interface ISQLite {
query: string,
params?: any[]
) => Promise<any[]>;
getDbPath: (dbName: string, location?: string) => string;
}

const locks: Record<
Expand Down Expand Up @@ -420,6 +421,7 @@ export type OPSQLiteConnection = {
prepareStatement: (query: string) => PreparedStatementObj;
loadExtension: (path: string, entryPoint?: string) => void;
executeRawAsync: (query: string, params?: any[]) => Promise<any[]>;
getDbPath: () => string;
};

export const open = (options: {
Expand Down Expand Up @@ -456,5 +458,6 @@ export const open = (options: {
OPSQLite.loadExtension(options.name, path, entryPoint),
executeRawAsync: (query, params) =>
OPSQLite.executeRawAsync(options.name, query, params),
getDbPath: () => OPSQLite.getDbPath(options.name, options.location),
};
};

0 comments on commit 1c5a7aa

Please sign in to comment.