mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[data views] Use Map for data view cache instead of custom code (#177904)
## Summary Simple code simplification / refactor. Use Map for data view cache instead of extremely map like custom code.
This commit is contained in:
parent
bcf98321db
commit
9413042274
3 changed files with 21 additions and 54 deletions
|
@ -1,40 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0 and the Server Side Public License, v 1; you may not use this file except
|
||||
* in compliance with, at your election, the Elastic License 2.0 or the Server
|
||||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { DataView } from './data_view';
|
||||
|
||||
export interface DataViewCache {
|
||||
get: (id: string) => Promise<DataView> | undefined;
|
||||
set: (id: string, value: Promise<DataView>) => Promise<DataView>;
|
||||
clear: (id: string) => void;
|
||||
clearAll: () => void;
|
||||
}
|
||||
|
||||
export function createDataViewCache(): DataViewCache {
|
||||
const vals: Record<string, Promise<DataView>> = {};
|
||||
const cache: DataViewCache = {
|
||||
get: (id: string) => {
|
||||
return vals[id];
|
||||
},
|
||||
set: (id: string, prom: Promise<DataView>) => {
|
||||
vals[id] = prom;
|
||||
return prom;
|
||||
},
|
||||
clear: (id: string) => {
|
||||
delete vals[id];
|
||||
},
|
||||
clearAll: () => {
|
||||
for (const id in vals) {
|
||||
if (vals.hasOwnProperty(id)) {
|
||||
delete vals[id];
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
return cache;
|
||||
}
|
|
@ -13,7 +13,6 @@ import { FieldFormatsStartCommon, FORMATS_UI_SETTINGS } from '@kbn/field-formats
|
|||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { PersistenceAPI } from '../types';
|
||||
|
||||
import { createDataViewCache } from '.';
|
||||
import type { RuntimeField, RuntimeFieldSpec, RuntimeType } from '../types';
|
||||
import { DataView } from './data_view';
|
||||
import {
|
||||
|
@ -319,7 +318,7 @@ export class DataViewsService {
|
|||
* @param key used to indicate uniqueness of the error
|
||||
*/
|
||||
private onError: OnError;
|
||||
private dataViewCache: ReturnType<typeof createDataViewCache>;
|
||||
private dataViewCache: Map<string, Promise<DataView>>;
|
||||
/**
|
||||
* Can the user save advanced settings?
|
||||
*/
|
||||
|
@ -355,7 +354,7 @@ export class DataViewsService {
|
|||
this.getCanSave = getCanSave;
|
||||
this.getCanSaveAdvancedSettings = getCanSaveAdvancedSettings;
|
||||
|
||||
this.dataViewCache = createDataViewCache();
|
||||
this.dataViewCache = new Map();
|
||||
this.scriptedFieldsEnabled = scriptedFieldsEnabled;
|
||||
}
|
||||
|
||||
|
@ -450,9 +449,9 @@ export class DataViewsService {
|
|||
*/
|
||||
clearInstanceCache = (id?: string) => {
|
||||
if (id) {
|
||||
this.dataViewCache.clear(id);
|
||||
this.dataViewCache.delete(id);
|
||||
} else {
|
||||
this.dataViewCache.clearAll();
|
||||
this.dataViewCache.clear();
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -930,13 +929,17 @@ export class DataViewsService {
|
|||
return dataView;
|
||||
});
|
||||
|
||||
const indexPatternPromise =
|
||||
dataViewFromCache ||
|
||||
this.dataViewCache.set(id, this.getSavedObjectAndInit(id, displayErrors));
|
||||
let indexPatternPromise: Promise<DataView>;
|
||||
if (dataViewFromCache) {
|
||||
indexPatternPromise = dataViewFromCache;
|
||||
} else {
|
||||
indexPatternPromise = this.getSavedObjectAndInit(id, displayErrors);
|
||||
this.dataViewCache.set(id, indexPatternPromise);
|
||||
}
|
||||
|
||||
// don't cache failed requests
|
||||
indexPatternPromise.catch(() => {
|
||||
this.dataViewCache.clear(id);
|
||||
this.dataViewCache.delete(id);
|
||||
});
|
||||
|
||||
return indexPatternPromise;
|
||||
|
@ -999,11 +1002,16 @@ export class DataViewsService {
|
|||
return cachedDataView;
|
||||
}
|
||||
|
||||
return this.dataViewCache.set(spec.id, doCreate());
|
||||
const dataViewPromise = doCreate();
|
||||
|
||||
this.dataViewCache.set(spec.id, dataViewPromise);
|
||||
|
||||
return dataViewPromise;
|
||||
}
|
||||
|
||||
const dataView = await doCreate();
|
||||
return this.dataViewCache.set(dataView.id!, Promise.resolve(dataView));
|
||||
this.dataViewCache.set(dataView.id!, Promise.resolve(dataView));
|
||||
return dataView;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1162,7 +1170,7 @@ export class DataViewsService {
|
|||
indexPattern.version = samePattern.version;
|
||||
|
||||
// Clear cache
|
||||
this.dataViewCache.clear(indexPattern.id!);
|
||||
this.dataViewCache.delete(indexPattern.id!);
|
||||
|
||||
// Try the save again
|
||||
return this.updateSavedObject(indexPattern, saveAttempts, ignoreErrors, displayErrors);
|
||||
|
@ -1179,7 +1187,7 @@ export class DataViewsService {
|
|||
if (!(await this.getCanSave())) {
|
||||
throw new DataViewInsufficientAccessError(indexPatternId);
|
||||
}
|
||||
this.dataViewCache.clear(indexPatternId);
|
||||
this.dataViewCache.delete(indexPatternId);
|
||||
return this.savedObjectsClient.delete(indexPatternId);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
export * from './_pattern_cache';
|
||||
export * from './flatten_hit';
|
||||
export * from './data_view';
|
||||
export * from './data_views';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue