Skip to content

Commit

Permalink
Converted cache to non-static
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Mar 15, 2023
1 parent c2566d5 commit c76b345
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
19 changes: 11 additions & 8 deletions packages/pouch-storage/src/PouchCommandHistoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,41 @@ const log = Log.module('PouchCommandHistoryCache');
* `PouchCommandHistoryTable` instances.
*/
class PouchCommandHistoryCache {
constructor() {
this.isPruning = new Map();
this.response = new Map();
this.tableRegistry = new Map();
}

/**
* Keep track of pruning status for a database. This helps ensure only 1
* pruning operation gets executed if multiple instances of a PouchCommandHistory
* table load data at the same time.
*/
static isPruning: Map<DatabaseName, boolean> = new Map();
isPruning: Map<DatabaseName, boolean>;

/**
* Cache for command history query results keyed by db name. The cached data
* will be shared across all `PouchCommandHistoryTable` instances that have
* the same db name.
*/
static response: Map<
response: Map<
DatabaseName,
Promise<CommandHistoryStorageItemFindResponse> | null
> = new Map();
>;

/**
* Keeps track of all `PouchCommandHistoryTable` instances.
*/
static tableRegistry: Map<
DatabaseName,
Set<PouchCommandHistoryTable>
> = new Map();
tableRegistry: Map<DatabaseName, Set<PouchCommandHistoryTable>>;

/**
* Pauses PouchDB change listeners for any `PouchCommandHistoryTables` with
* the given database name. This will cancel existing subscriptions and
* return a callback that can be used to re-subscribe them.
* @param dbName
*/
static pauseChangeListeners(dbName: DatabaseName): () => void {
pauseChangeListeners(dbName: DatabaseName): () => void {
const pausedTables: PouchCommandHistoryTable[] = [];

this.tableRegistry.get(dbName)?.forEach(table => {
Expand Down
10 changes: 8 additions & 2 deletions packages/pouch-storage/src/PouchCommandHistoryStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@ import {
StorageListenerRemover,
} from '@deephaven/storage';
import PouchCommandHistoryTable from './PouchCommandHistoryTable';
import PouchCommandHistoryCache from './PouchCommandHistoryCache';

const log = Log.module('PouchCommandHistoryStorage');

export class PouchCommandHistoryStorage implements CommandHistoryStorage {
private cache = new PouchCommandHistoryCache();

private updateTableMap = new Map<string, PouchCommandHistoryTable>();

private getUpdateTable(language: string): PouchCommandHistoryTable {
if (!this.updateTableMap.has(language)) {
this.updateTableMap.set(language, new PouchCommandHistoryTable(language));
this.updateTableMap.set(
language,
new PouchCommandHistoryTable(language, this.cache)
);
}

return this.updateTableMap.get(language) as PouchCommandHistoryTable;
}

async getTable(language: string): Promise<PouchCommandHistoryTable> {
return new PouchCommandHistoryTable(language);
return new PouchCommandHistoryTable(language, this.cache);
}

async addItem(
Expand Down
29 changes: 15 additions & 14 deletions packages/pouch-storage/src/PouchCommandHistoryTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type CommandHistoryDoc = PouchDB.Core.ExistingDocument<
export class PouchCommandHistoryTable
extends PouchStorageTable<CommandHistoryStorageItem>
implements CommandHistoryTable {
constructor(language: string) {
constructor(language: string, private cache: PouchCommandHistoryCache) {
super(`CommandHistoryStorage.${language}`, ({
// Optimizations to cut down on growing table size. These should be safe
// since we don't care about revision history for command history
Expand All @@ -38,12 +38,13 @@ export class PouchCommandHistoryTable
} as unknown) as PouchDB.HttpAdapter.HttpAdapterConfiguration);

// Add this table instance to `allTables`
if (!PouchCommandHistoryCache.tableRegistry.has(this.cacheKey)) {
PouchCommandHistoryCache.tableRegistry.set(this.cacheKey, new Set());
if (!this.cache.tableRegistry.has(this.cacheKey)) {
this.cache.tableRegistry.set(this.cacheKey, new Set());
}

log.debug('Adding table to registry', this.cacheKey)
PouchCommandHistoryCache.tableRegistry.get(this.cacheKey)?.add(this);
this.cache.tableRegistry.get(this.cacheKey)?.add(this);

}

private searchText?: string;
Expand Down Expand Up @@ -87,13 +88,13 @@ export class PouchCommandHistoryTable
): void {
log.debug('Clearing cache and refreshing data', event);

PouchCommandHistoryCache.response.delete(this.cacheKey);
this.cache.response.delete(this.cacheKey);

super.dbUpdate(event);
}

/**
* Fetch command history data from `PouchCommandHistoryCache.cache` or from
* Fetch command history data from `this.cache.response` or from
* PouchDB if data is not found in the cache. If the number of total items in
* the db exceeds COMMAND_HISTORY_ITEMS_MAX, the database will be pruned down
* to COMMAND_HISTORY_ITEMS_PRUNE total items. Note that PouchDB doesn't
Expand All @@ -106,12 +107,12 @@ export class PouchCommandHistoryTable
): Promise<
PouchDB.Find.FindResponse<CommandHistoryStorageItem & PouchStorageItem>
> {
if (PouchCommandHistoryCache.response.has(this.cacheKey)) {
if (this.cache.response.has(this.cacheKey)) {
log.debug('Fetching from cache', this.searchText, this.viewport);
} else {
log.debug('Fetching from PouchDB', this.searchText, this.viewport);

PouchCommandHistoryCache.response.set(
this.cache.response.set(
this.cacheKey,
this.db
.allDocs({
Expand Down Expand Up @@ -145,7 +146,7 @@ export class PouchCommandHistoryTable
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const result = PouchCommandHistoryCache.response.get(this.cacheKey)!;
const result = this.cache.response.get(this.cacheKey)!;

if (this.searchText == null || this.searchText === '') {
return result;
Expand Down Expand Up @@ -210,7 +211,7 @@ export class PouchCommandHistoryTable
* @param items
*/
async pruneItems(items: CommandHistoryDoc[]) {
if (PouchCommandHistoryCache.isPruning.has(this.cacheKey)) {
if (this.cache.isPruning.has(this.cacheKey)) {
return;
}

Expand All @@ -219,13 +220,13 @@ export class PouchCommandHistoryTable

// Disable change notifications while we bulk delete to avoid locking up
// the app
const resumeListeners = PouchCommandHistoryCache.pauseChangeListeners(
const resumeListeners = this.cache.pauseChangeListeners(
this.cacheKey
);

PouchCommandHistoryCache.isPruning.set(this.cacheKey, true);
this.cache.isPruning.set(this.cacheKey, true);
await this.db.bulkDocs(items.map(item => ({ ...item, _deleted: true })));
PouchCommandHistoryCache.isPruning.set(this.cacheKey, false);
this.cache.isPruning.set(this.cacheKey, false);

resumeListeners();

Expand All @@ -236,7 +237,7 @@ export class PouchCommandHistoryTable
}

override close(): void {
PouchCommandHistoryCache.tableRegistry.get(this.cacheKey)?.delete(this);
this.cache.tableRegistry.get(this.cacheKey)?.delete(this);
this.changes?.cancel();
super.close();
}
Expand Down

0 comments on commit c76b345

Please sign in to comment.