Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Commit

Permalink
fix: Allow multiple independent instances of levelDB.
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Javier Ribó Labrador <[email protected]>
  • Loading branch information
elribonazo committed Dec 19, 2023
1 parent 7b57229 commit 6d04a91
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
17 changes: 3 additions & 14 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,10 @@
"type": "node"
},
{
"cwd": "${workspaceFolder}/packages/database",
"type": "node",
"command": "npm run test",
"name": "Test2",
"request": "launch",
"console": "integratedTerminal",
"name": "TEST",
"program": "${workspaceRoot}/node_modules/.bin/vitest",
"args": [
"--pool",
"threads",
"--poolOptions.threads.singleThread",
],
"skipFiles": [
"${workspaceRoot}/../../node_modules/**/*",
"<node_internals>/**/*"
]
"type": "node-terminal"
}
]
}
2 changes: 2 additions & 0 deletions packages/leveldb/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
db*
prism-db
18 changes: 13 additions & 5 deletions packages/leveldb/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export type * from './leveldb/types';
export type { MangoQuerySelectorAndIndex, CRDTSchemaOptions, RxQueryPlanKey, PrimaryKey, StringKeys, TopLevelProperty, MangoQuerySortDirection } from "rxdb/dist/types/types";
export type { DefaultPreparedQuery, RxJsonSchema, FilledMangoQuery, CompressionMode, RxQueryPlan, MangoQuery, MangoQueryNoLimit, MangoQuerySortPart } from 'rxdb'
export type { Level } from 'level'
let internalInstance: LevelDBInternal<any>
export const RX_STORAGE_NAME_LEVELDB = 'leveldb';

async function preloadData<RxDocType>(constructorProps: LevelDBInternalConstructor<RxDocType>) {
Expand All @@ -46,6 +45,8 @@ async function preloadData<RxDocType>(constructorProps: LevelDBInternalConstruct
}
}

let internalInstance: Map<string, LevelDBInternal<any>> = new Map()

function getRxStorageLevel<RxDocType>(settings: LevelDBSettings): RxStorageLevelDBType<RxDocType> {
const instance: RxStorageLevelDBType<any> = {
name: RX_STORAGE_NAME_LEVELDB,
Expand All @@ -64,18 +65,25 @@ function getRxStorageLevel<RxDocType>(settings: LevelDBSettings): RxStorageLevel
schema: params.schema,
};

if (!internalInstance) {
internalInstance = await preloadData<RxDocType>(levelDBConstructorProps);
const databasePath = "level" in levelDBConstructorProps ?
levelDBConstructorProps.level.path :
levelDBConstructorProps.dbPath;

const existingInstance = internalInstance.get(databasePath);

if (!existingInstance) {
internalInstance.set(databasePath, await preloadData<RxDocType>(levelDBConstructorProps))
} else {
internalInstance.refCount++
existingInstance.refCount++;
internalInstance.set(databasePath, existingInstance)
}

const rxStorageInstance = new RxStorageIntanceLevelDB<RxDocType>(
this,
params.databaseName,
params.collectionName,
params.schema,
internalInstance,
internalInstance.get(databasePath)!,
settings
)

Expand Down
40 changes: 40 additions & 0 deletions packages/leveldb/tests/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,44 @@ describe("LevelDb init", () => {

// await db.close()
})

it('should be able to instanciate multiple databases in the same thread', async ({ expect }) => {

const db = await Database.createEncrypted(
{
name: databaseName,
encryptionKey: defaultPassword,
storage: createLevelDBStorage({
dbPath: "./db"
}),
}
);

await db.storeLinkSecret("first", "first")

const db2 = await Database.createEncrypted(
{
name: databaseName,
encryptionKey: defaultPassword,
storage: createLevelDBStorage({
dbPath: "./db2"
}),
}
);

await db2.storeLinkSecret("second", "second")

const firstLinkSecret = await db.getLinkSecret();

const secondLinkSecret = await db2.getLinkSecret();

expect(firstLinkSecret).toBe("first")
expect(secondLinkSecret).toBe("second")

await db.clear()
await db2.clear()



})
})

0 comments on commit 6d04a91

Please sign in to comment.