Skip to content

Commit

Permalink
Feature/remote modes (pubkey#4611)
Browse files Browse the repository at this point in the history
* ADD `mode` option to remote storage

* ADD default mode is storage

* FIX tests

* FIX typo

* FIX(remote-storage) one mode
  • Loading branch information
pubkey authored Apr 6, 2023
1 parent 211752f commit b17c230
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# RxDB Changelog

<!-- CHANGELOG NEWEST -->

- FIX(remote-storage) `mode: 'one'` option must reuse the RemoteMessageChannel
<!-- ADD new changes here! -->

<!-- /CHANGELOG NEWEST -->
Expand Down
30 changes: 21 additions & 9 deletions orga/performance-trackings.md
Original file line number Diff line number Diff line change
Expand Up @@ -1259,15 +1259,27 @@ BEFORE:
LOG LOG: 'performanceResult: 34.1'


bulkWrite(20000) 1 - 290.4000000022352
file-system-access.worker.js:9220 bulkWrite(20000) 2 - 352.5
file-system-access.worker.js:9230 bulkWrite(20000) 3 - 352.6000000014901
context.js:265 .
file-system-access.worker.js:9236 bulkWrite(20000) 4 - 461.6000000014901
file-system-access.worker.js:9252 bulkWrite(20000) 5 - 1091.6000000014901
context.js:265 .
file-system-access.worker.js:9257 bulkWrite(20000) 6 - 1428.9000000022352
context.js:265
AFTER:
'performanceResult: 28.6



FIXED TEST!!

BEFORE:
performanceResult: 87.94'

AFTER:
performanceResult: 83.39






AFTER fixing merging sorted array

AFTER2:


### Query
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/storage-remote/message-channel-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type RemoteMessageChannelCacheItem = {
cacheKey: string;
messageChannel: Promise<RemoteMessageChannel>;
refCount: number;
keepAlive: boolean;
};

export const MESSAGE_CHANNEL_CACHE_BY_IDENTIFIER = new Map<string, Map<string, RemoteMessageChannelCacheItem>>();
Expand All @@ -30,7 +31,8 @@ function getMessageChannelCache(

export function getMessageChannel(
settings: RxStorageRemoteSettings,
cacheKeys: string[]
cacheKeys: string[],
keepAlive: boolean = false
): Promise<RemoteMessageChannel> {
const cacheKey = getCacheKey(settings, cacheKeys);
const cacheItem = getFromMapOrCreate(
Expand All @@ -40,6 +42,7 @@ export function getMessageChannel(
const newCacheItem: RemoteMessageChannelCacheItem = {
identifier: settings.identifier,
cacheKey,
keepAlive,
refCount: 1,
messageChannel: settings.messageChannelCreator()
.then((messageChannel) => {
Expand All @@ -62,7 +65,7 @@ export function closeMessageChannel(
): Promise<void> {
const cacheItem = getFromMapOrThrow(CACHE_ITEM_BY_MESSAGE_CHANNEL, messageChannel);
cacheItem.refCount = cacheItem.refCount - 1;
if (cacheItem.refCount === 0) {
if (cacheItem.refCount === 0 && !cacheItem.keepAlive) {
getMessageChannelCache(cacheItem.identifier).delete(cacheItem.cacheKey);
return messageChannel.close();
} else {
Expand Down
18 changes: 15 additions & 3 deletions src/plugins/storage-remote/rx-storage-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import type {
MessageFromRemote,
MessageToRemote,
RemoteMessageChannel,
RxStorageRemoteInternals,
RxStorageRemoteSettings
} from './storage-remote-types';
Expand All @@ -39,10 +40,18 @@ export class RxStorageRemote implements RxStorage<RxStorageRemoteInternals, any>
public readonly name: string = 'remote';
private seed: string = randomCouchString(10);
private lastRequestId: number = 0;
public messageChannelIfOneMode?: Promise<RemoteMessageChannel>;
constructor(
public readonly settings: RxStorageRemoteSettings
) {
this.statics = settings.statics;
if (settings.mode === 'one') {
this.messageChannelIfOneMode = getMessageChannel(
settings,
[],
true
);
}
}

public getRequestId() {
Expand All @@ -68,9 +77,12 @@ export class RxStorageRemote implements RxStorage<RxStorageRemoteInternals, any>
case 'storage':
cacheKeys.push('seed-' + this.seed);
}
const messageChannel = await getMessageChannel(
this.settings,
cacheKeys
const messageChannel = await (this.messageChannelIfOneMode ?
this.messageChannelIfOneMode :
getMessageChannel(
this.settings,
cacheKeys
)
);

const requestId = this.getRequestId();
Expand Down
18 changes: 18 additions & 0 deletions test/unit/rx-storage-remote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,26 @@ config.parallel('rx-storage-remote.test.ts', () => {
storageInstanceB.internals.messageChannel
);


await storageInstanceA.close();
await storageInstanceB.close();

// even after closing all and reopnening a new one, it must be the same instance.
const storageInstanceC = await getStorage(port).createStorageInstance({
databaseInstanceToken: randomCouchString(10),
databaseName: randomCouchString(10),
collectionName: 'human',
devMode: true,
multiInstance: false,
options: {},
schema: fillWithDefaultSettings(schemas.human)
});
assert.strictEqual(
storageInstanceA.internals.messageChannel,
storageInstanceC.internals.messageChannel
);

await storageInstanceC.close();
await colServer.database.destroy();
});
it('mode: storage', async () => {
Expand Down

0 comments on commit b17c230

Please sign in to comment.