Skip to content

Commit

Permalink
[Ingest Manager] Move cache functions to from registry to archive (#8…
Browse files Browse the repository at this point in the history
…2871)

## Summary

Moving the memory store functions to `archive/cache.ts` to better express their role. 

They are archive-related functions. The registry is just one possible source of an archive/assets. Also considered moving to `assets`, but can always come back to that.

`deletePackageCache` undoes side-effects from `unpackArchiveToCache` so put them in the same file
  • Loading branch information
John Schulz authored Nov 6, 2020
1 parent 98b46c9 commit 715d43b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { pkgToPkgKey } from './index';
import { pkgToPkgKey } from '../registry/index';

const cache: Map<string, Buffer> = new Map();
export const cacheGet = (key: string) => cache.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@

import { ArchivePackage } from '../../../../common/types';
import { PackageInvalidArchiveError, PackageUnsupportedMediaTypeError } from '../../../errors';
import { cacheSet, setArchiveFilelist } from '../registry/cache';
import {
cacheSet,
cacheDelete,
getArchiveFilelist,
setArchiveFilelist,
deleteArchiveFilelist,
} from './cache';
import { ArchiveEntry, getBufferExtractor } from '../registry/extract';
import { parseAndVerifyArchive } from './validation';

export * from './cache';

export async function loadArchivePackage({
archiveBuffer,
contentType,
Expand Down Expand Up @@ -64,3 +72,15 @@ export async function unpackArchiveToCache(
}
return paths;
}

export const deletePackageCache = (name: string, version: string) => {
// get cached archive filelist
const paths = getArchiveFilelist(name, version);

// delete cached archive filelist
deleteArchiveFilelist(name, version);

// delete cached archive files
// this has been populated in unpackArchiveToCache()
paths?.forEach((path) => cacheDelete(path));
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '../../../../common/types';
import { PackageInvalidArchiveError } from '../../../errors';
import { pkgToPkgKey } from '../registry';
import { cacheGet } from '../registry/cache';
import { cacheGet } from './cache';

// TODO: everything below performs verification of manifest.yml files, and hence duplicates functionality already implemented in the
// package registry. At some point this should probably be replaced (or enhanced) with verification based on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import { InstallablePackage } from '../../../types';
import { getAssets } from './assets';
import { getArchiveFilelist } from '../registry/cache';
import { getArchiveFilelist } from '../archive/cache';

jest.mock('../registry/cache', () => {
jest.mock('../archive/cache', () => {
return {
getArchiveFilelist: jest.fn(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { InstallablePackage } from '../../../types';
import * as Registry from '../registry';
import { getArchiveFilelist } from '../registry/cache';
import { getArchiveFilelist } from '../archive/cache';

// paths from RegistryPackage are routes to the assets on EPR
// e.g. `/package/nginx/1.2.0/data_stream/access/fields/fields.yml`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import { deletePipeline } from '../elasticsearch/ingest_pipeline/';
import { installIndexPatterns } from '../kibana/index_pattern/install';
import { deleteTransforms } from '../elasticsearch/transform/remove';
import { packagePolicyService, appContextService } from '../..';
import { splitPkgKey, deletePackageCache } from '../registry';
import { splitPkgKey } from '../registry';
import { deletePackageCache } from '../archive';

export async function removeInstallation(options: {
savedObjectsClient: SavedObjectsClientContract;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ import {
RegistrySearchResult,
} from '../../../types';
import { unpackArchiveToCache } from '../archive';
import {
cacheGet,
cacheDelete,
getArchiveFilelist,
setArchiveFilelist,
deleteArchiveFilelist,
} from './cache';
import { cacheGet, getArchiveFilelist, setArchiveFilelist } from '../archive';
import { ArchiveEntry } from './extract';
import { fetchUrl, getResponse, getResponseStream } from './requests';
import { streamToBuffer } from './streams';
Expand Down Expand Up @@ -247,15 +241,3 @@ export function groupPathsByService(paths: string[]): AssetsGroupedByServiceByTy
// elasticsearch: assets.elasticsearch,
};
}

export const deletePackageCache = (name: string, version: string) => {
// get cached archive filelist
const paths = getArchiveFilelist(name, version);

// delete cached archive filelist
deleteArchiveFilelist(name, version);

// delete cached archive files
// this has been populated in unpackRegistryPackageToCache()
paths?.forEach((path) => cacheDelete(path));
};

0 comments on commit 715d43b

Please sign in to comment.