Skip to content

Commit

Permalink
use read lock for read only queries
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensJourney committed Nov 3, 2023
1 parent 961f544 commit fc2eb5d
Show file tree
Hide file tree
Showing 3 changed files with 3,802 additions and 104 deletions.
4 changes: 2 additions & 2 deletions packages/powersync-sdk-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"homepage": "https://docs.powersync.co/",
"peerDependencies": {
"@journeyapps/react-native-quick-sqlite": "0.1.0",
"@journeyapps/react-native-quick-sqlite": "0.0.0-dev-20231103124824",
"base-64": "^1.0.0",
"react": "*",
"react-native": "*",
Expand All @@ -44,7 +44,7 @@
"async-lock": "^1.4.0"
},
"devDependencies": {
"@journeyapps/react-native-quick-sqlite": "0.1.0",
"@journeyapps/react-native-quick-sqlite": "0.0.0-dev-20231103124824",
"@types/async-lock": "^1.4.0",
"react-native": "0.72.4",
"react": "18.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export class RNQSDBAdapter extends BaseObserver<DBAdapterListener> implements DB
this.iterateListeners((cb) => cb.tablesUpdated?.(update));
});

const topLevelUtils = this.generateDBHelpers({ execute: this.readOnlyExecute });
const topLevelUtils = this.generateDBHelpers({
// Arrow function binds `this` for use in readOnlyExecute
execute: (sql: string, params?: any[]) => this.readOnlyExecute(sql, params)
});
// Only assigning get helpers
this.getAll = topLevelUtils.getAll;
this.getOptional = topLevelUtils.getOptional;
this.get = topLevelUtils.get;
Expand Down Expand Up @@ -56,13 +60,13 @@ export class RNQSDBAdapter extends BaseObserver<DBAdapterListener> implements DB
}

/**
* This provides a top-level read only execute method which is executed inside a read lock.
* This provides a top-level read only execute method which is executed inside a read-lock.
* This is necessary since the high level `execute` method uses a write-lock under
* the hood. Helper methods such as `get`, `getAll` and `getOptional` are read only,
* and should use this method.
*/
private readOnlyExecute(sql: string, params?: any[]) {
return this.readLock(ctx => ctx.execute(sql, params));
return this.baseDB.readLock((ctx) => ctx.execute(sql, params));
}

/**
Expand All @@ -78,23 +82,23 @@ export class RNQSDBAdapter extends BaseObserver<DBAdapterListener> implements DB
/**
* Execute a read-only query and return results
*/
async getAll<T>(sql: string, parameters?: any[]): Promise<T[]> {
getAll: async <T>(sql: string, parameters?: any[]): Promise<T[]> => {
const res = await tx.execute(sql, parameters);
return res.rows?._array ?? [];
},

/**
* Execute a read-only query and return the first result, or null if the ResultSet is empty.
*/
async getOptional<T>(sql: string, parameters?: any[]): Promise<T | null> {
getOptional: async <T>(sql: string, parameters?: any[]): Promise<T | null> => {
const res = await tx.execute(sql, parameters);
return res.rows?.item(0) ?? null;
},

/**
* Execute a read-only query and return the first result, error if the ResultSet is empty.
*/
async get<T>(sql: string, parameters?: any[]): Promise<T> {
get: async <T>(sql: string, parameters?: any[]): Promise<T> => {
const res = await tx.execute(sql, parameters);
const first = res.rows?.item(0);
if (!first) {
Expand Down
Loading

0 comments on commit fc2eb5d

Please sign in to comment.