Skip to content

Commit

Permalink
Release 9.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Oct 2, 2024
1 parent cdf0d82 commit 6d1523b
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 18 deletions.
9 changes: 6 additions & 3 deletions cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,11 @@ BridgeResult opsqlite_execute(std::string const &name, std::string const &query,
int status, current_column, column_count, column_type;
std::string column_name, column_declared_type;
std::vector<std::string> column_names;
column_names.reserve(20);
std::vector<std::vector<JSVariant>> rows;
rows.reserve(20);
std::vector<JSVariant> row;
row.reserve(10);

do {
const char *query_str =
Expand Down Expand Up @@ -436,7 +439,7 @@ BridgeResult opsqlite_execute(std::string const &name, std::string const &query,
// Do a first pass to get the column names
for (int i = 0; i < column_count; i++) {
column_name = sqlite3_column_name(statement, i);
column_names.push_back(column_name);
column_names.emplace_back(column_name);
}

while (is_consuming_rows) {
Expand Down Expand Up @@ -464,9 +467,9 @@ BridgeResult opsqlite_execute(std::string const &name, std::string const &query,
case SQLITE_TEXT: {
string_value = reinterpret_cast<const char *>(
sqlite3_column_text(statement, current_column));
int byteLen = sqlite3_column_bytes(statement, current_column);
int len = sqlite3_column_bytes(statement, current_column);
// Specify length too; in case string contains NULL in the middle
row.emplace_back(std::string(string_value, byteLen));
row.emplace_back(std::string(string_value, len));
break;
}

Expand Down
8 changes: 4 additions & 4 deletions cpp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace opsqlite {

namespace jsi = facebook::jsi;

jsi::Value toJSI(jsi::Runtime &rt, JSVariant value) {
jsi::Value toJSI(jsi::Runtime &rt, const JSVariant &value) {
if (std::holds_alternative<bool>(value)) {
return std::get<bool>(value);
} else if (std::holds_alternative<int>(value)) {
Expand Down Expand Up @@ -158,7 +158,7 @@ std::vector<JSVariant> to_variant_vec(jsi::Runtime &rt, jsi::Value const &xs) {
return res;
}

jsi::Value create_js_rows(jsi::Runtime &rt, BridgeResult status) {
jsi::Value create_js_rows(jsi::Runtime &rt, const BridgeResult &status) {
if (status.type == SQLiteError) {
throw std::invalid_argument(status.message);
}
Expand All @@ -181,10 +181,10 @@ jsi::Value create_js_rows(jsi::Runtime &rt, BridgeResult status) {
auto value = toJSI(rt, native_row[j]);
row.setValueAtIndex(rt, j, value);
}
rows.setValueAtIndex(rt, i, std::move(row));
rows.setValueAtIndex(rt, i, row);
}
}
res.setProperty(rt, "rawRows", std::move(rows));
res.setProperty(rt, "rawRows", rows);

size_t column_count = status.column_names.size();
auto column_array = jsi::Array(rt, column_count);
Expand Down
4 changes: 2 additions & 2 deletions cpp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ namespace opsqlite {

namespace jsi = facebook::jsi;

jsi::Value toJSI(jsi::Runtime &rt, JSVariant value);
jsi::Value toJSI(jsi::Runtime &rt, const JSVariant &value);
JSVariant toVariant(jsi::Runtime &rt, jsi::Value const &value);
std::vector<std::string> to_string_vec(jsi::Runtime &rt, jsi::Value const &xs);
std::vector<JSVariant> to_variant_vec(jsi::Runtime &rt, jsi::Value const &xs);
std::vector<int> to_int_vec(jsi::Runtime &rt, jsi::Value const &xs);
jsi::Value createResult(jsi::Runtime &rt, BridgeResult status,
std::vector<DumbHostObject> *results,
std::shared_ptr<std::vector<SmartHostObject>> metadata);
jsi::Value create_js_rows(jsi::Runtime &rt, BridgeResult status);
jsi::Value create_js_rows(jsi::Runtime &rt, const BridgeResult &status);
jsi::Value
create_raw_result(jsi::Runtime &rt, BridgeResult status,
const std::vector<std::vector<JSVariant>> *results);
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PODS:
- hermes-engine (0.74.0):
- hermes-engine/Pre-built (= 0.74.0)
- hermes-engine/Pre-built (0.74.0)
- op-sqlite (8.0.3):
- op-sqlite (9.0.0):
- React
- React-callinvoker
- React-Core
Expand Down Expand Up @@ -1393,7 +1393,7 @@ SPEC CHECKSUMS:
GCDWebServer: 2c156a56c8226e2d5c0c3f208a3621ccffbe3ce4
glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2
hermes-engine: 6eae7edb2f563ee41d7c1f91f4f2e57c26d8a5c3
op-sqlite: d567632567b87bedda1f718e4ce6c081457c2f42
op-sqlite: 4a145846fefc46b14350344450809144775d2ec3
RCT-Folly: 045d6ecaa59d826c5736dfba0b2f4083ff8d79df
RCTDeprecation: 3ca8b6c36bfb302e1895b72cfe7db0de0c92cd47
RCTRequired: 9fc183af555fd0c89a366c34c1ae70b7e03b1dc5
Expand Down
82 changes: 82 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@ import {reactiveTests} from './tests/reactive.spec';
import {setServerResults, startServer, stopServer} from './server';
import {open} from '@op-engineering/op-sqlite';
import Share from 'react-native-share';
import {createLargeDB, queryLargeDB} from './Database';

export default function App() {
const [isLoading, setIsLoading] = useState(false);
const [times, setTimes] = useState<number[]>([]);
const [accessingTimes, setAccessingTimes] = useState<number[]>([]);
const [prepareTimes, setPrepareTimes] = useState<number[]>([]);
const [prepareExecutionTimes, setPrepareExecutionTimes] = useState<number[]>(
[],
);
const [rawExecutionTimes, setRawExecutionTimes] = useState<number[]>([]);
const [results, setResults] = useState<any>([]);
useEffect(() => {
runTests(
Expand Down Expand Up @@ -78,6 +87,28 @@ export default function App() {
}
};

const createLargeDb = async () => {
setIsLoading(true);
await createLargeDB();
setIsLoading(false);
};

const queryLargeDb = async () => {
try {
setIsLoading(true);
const times = await queryLargeDB();
setTimes(times.loadFromDb);
setAccessingTimes(times.access);
setPrepareTimes(times.prepare);
setPrepareExecutionTimes(times.preparedExecution);
setRawExecutionTimes(times.rawExecution);
} catch (e) {
console.error(e);
} finally {
setIsLoading(false);
}
};

const copyDbPathToClipboad = async () => {
const db = await open({name: 'shareableDb.sqlite'});
const path = db.getDbPath();
Expand All @@ -91,6 +122,57 @@ export default function App() {
<ScrollView>
<Button title="Share DB" onPress={shareDb} />
<Button title="Copy DB Path" onPress={copyDbPathToClipboad} />
<Button title="Create 300k Record DB" onPress={createLargeDb} />
<Button title="Query 300k Records" onPress={queryLargeDb} />
{!!times.length && (
<Text className="text-lg text-white self-center">
Normal query{' '}
{(times.reduce((acc, t) => (acc += t), 0) / times.length).toFixed(
0,
)}{' '}
ms
</Text>
)}
{!!accessingTimes.length && (
<Text className="text-lg text-white self-center">
Read property{' '}
{(
accessingTimes.reduce((acc, t) => (acc += t), 0) /
accessingTimes.length
).toFixed(0)}{' '}
ms
</Text>
)}
{!!prepareTimes.length && (
<Text className="text-lg text-white self-center">
Prepare statement{' '}
{(
prepareTimes.reduce((acc, t) => (acc += t), 0) /
prepareTimes.length
).toFixed(0)}{' '}
ms
</Text>
)}
{!!prepareExecutionTimes.length && (
<Text className="text-lg text-white self-center">
Execute prepared query{' '}
{(
prepareExecutionTimes.reduce((acc, t) => (acc += t), 0) /
prepareExecutionTimes.length
).toFixed(0)}{' '}
ms
</Text>
)}
{!!rawExecutionTimes.length && (
<Text className="text-lg text-white self-center">
Raw execution:{' '}
{(
rawExecutionTimes.reduce((acc, t) => (acc += t), 0) /
rawExecutionTimes.length
).toFixed(0)}{' '}
ms
</Text>
)}
<Text
className={clsx('font-bold flex-1 text-white p-2 mt-4', {
'bg-green-500': allTestsPassed,
Expand Down
18 changes: 12 additions & 6 deletions example/src/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export async function createLargeDB() {
await largeDb.executeBatch(insertions);

largeDb.close();

console.log('DB created');
}

export async function querySingleRecordOnLargeDB() {
Expand Down Expand Up @@ -74,14 +76,16 @@ export async function queryLargeDB() {
rawExecution: [],
};

console.log('Querying DB');

for (let i = 0; i < 10; i++) {
// @ts-ignore
global.gc();

// let start = performance.now();
let start = performance.now();
await largeDb.execute('SELECT * FROM Test;');
// let end = performance.now();
// times.loadFromDb.push(end - start);
let end = performance.now();
times.loadFromDb.push(end - start);

// mmkv.set('largeDB', JSON.stringify(results));
// @ts-ignore
Expand All @@ -92,10 +96,10 @@ export async function queryLargeDB() {
// JSON.parse(rawStr!);
// end = performance.now();

// start = performance.now();
start = performance.now();
await largeDb.executeRaw('SELECT * FROM Test;');
// end = performance.now();
// times.rawExecution.push(end - start);
end = performance.now();
times.rawExecution.push(end - start);

// console.log('MMKV time', (end - start).toFixed(2));

Expand Down Expand Up @@ -130,5 +134,7 @@ export async function queryLargeDB() {
// times.preparedExecution.push(end - start);
}

console.log('Querying DB done');

return times;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@op-engineering/op-sqlite",
"version": "9.0.0",
"version": "9.1.0",
"description": "Next generation SQLite for React Native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down

0 comments on commit 6d1523b

Please sign in to comment.