license /**
 * Classes and functions for model management across multiple storage mediums.
 *
 * Supported client actions:
 * - Listing models on all registered storage mediums.
 * - Remove model by URL from any registered storage mediums, by using URL
 *   string.
 * - Moving or copying model from one path to another in the same medium or from
 *   one medium to another, by using URL strings.
 */ import { assert } from '../util' ; import { IORouterRegistry } from './router_registry' ; import { ModelArtifactsInfo , ModelStoreManager } from './types' ; const URL_SCHEME_SUFFIX = '://' ; export class ModelStoreManagerRegistry { private static instance : ModelStoreManagerRegistry ; private managers : { [ scheme ] : ModelStoreManager ; } ; private constructor ( ) { this . managers = { } ; } private static getInstance ( ) { if ( ModelStoreManagerRegistry . instance == null ) { ModelStoreManagerRegistry . instance = new ModelStoreManagerRegistry ( ) ; } return ModelStoreManagerRegistry . instance ; } param saveRouter static registerManager ( scheme , manager ) { assert ( scheme != null , ( ) => 'scheme must not be undefined or null.' ) ; if ( scheme . endsWith ( URL_SCHEME_SUFFIX ) ) { scheme = scheme . slice ( 0 , scheme . indexOf ( URL_SCHEME_SUFFIX ) ) ; } assert ( scheme . length > 0 , ( ) => 'scheme must not be an empty string.' ) ; const registry = ModelStoreManagerRegistry . getInstance ( ) ; assert ( registry . managers [ scheme ] == null , ( ) => `A model store manager is already registered for scheme '${ scheme }'.` ) ; registry . managers [ scheme ] = manager ; } static getManager ( scheme ) { const manager = this . getInstance ( ) . managers [ scheme ] ; if ( manager == null ) { throw new Error ( `Cannot find model manager for scheme '${ scheme }'` ) ; } return manager ; } static getSchemes ( ) { return Object . keys ( this . getInstance ( ) . managers ) ; } } param url returns function parseURL ( url ) { if ( url . indexOf ( URL_SCHEME_SUFFIX ) === - 1 ) { throw new Error ( `The url string provided does not contain a scheme. ` + `Supported schemes are: ` + `${ ModelStoreManagerRegistry . getSchemes ( ) . join ( ',' ) }` ) ; } return { scheme : url . split ( URL_SCHEME_SUFFIX ) [ 0 ] , path : url . split ( URL_SCHEME_SUFFIX ) [ 1 ] , } ; } async function cloneModelInternal ( sourceURL , destURL , deleteSource = false ) { assert ( sourceURL !== destURL , ( ) => `Old path and new path are the same: '${ sourceURL }'` ) ; const loadHandlers = IORouterRegistry . getLoadHandlers ( sourceURL ) ; assert ( loadHandlers . length > 0 , ( ) => `Copying failed because no load handler is found for source URL ${ sourceURL }.` ) ; assert ( loadHandlers . length < 2 , ( ) => `Copying failed because more than one (${ loadHandlers . length }) ` + `load handlers for source URL ${ sourceURL }.` ) ; const loadHandler = loadHandlers [ 0 ] ; const saveHandlers = IORouterRegistry . getSaveHandlers ( destURL ) ; assert ( saveHandlers . length > 0 , ( ) => `Copying failed because no save handler is found for destination ` + `URL ${ destURL }.` ) ; assert ( saveHandlers . length < 2 , ( ) => `Copying failed because more than one (${ loadHandlers . length }) ` + `save handlers for destination URL ${ destURL }.` ) ; const saveHandler = saveHandlers [ 0 ] ; const sourceScheme = parseURL ( sourceURL ) . scheme ; const sourcePath = parseURL ( sourceURL ) . path ; const sameMedium = sourceScheme === parseURL ( sourceURL ) . scheme ; const modelArtifacts = await loadHandler . load ( ) ; if ( deleteSource && sameMedium ) { await ModelStoreManagerRegistry . getManager ( sourceScheme ) . removeModel ( sourcePath ) ; } const saveResult = await saveHandler . save ( modelArtifacts ) ; if ( deleteSource && ! sameMedium ) { await ModelStoreManagerRegistry . getManager ( sourceScheme ) . removeModel ( sourcePath ) ; } return saveResult . modelArtifactsInfo ; } returns doc async function listModels ( ) { const schemes = ModelStoreManagerRegistry . getSchemes ( ) ; const out = { } ; for ( const scheme of schemes ) { const schemeOut = await ModelStoreManagerRegistry . getManager ( scheme ) . listModels ( ) ; for ( const path in schemeOut ) { const url = scheme + URL_SCHEME_SUFFIX + path ; out [ url ] = schemeOut [ path ] ; } } return out ; } param url returns throws doc async function removeModel ( url ) { const schemeAndPath = parseURL ( url ) ; const manager = ModelStoreManagerRegistry . getManager ( schemeAndPath . scheme ) ; return manager . removeModel ( schemeAndPath . path ) ; } param sourceURL param destURL returns throws doc async function copyModel ( sourceURL , destURL ) { const deleteSource = false ; return cloneModelInternal ( sourceURL , destURL , deleteSource ) ; } param sourceURL param destURL returns throws doc async function moveModel ( sourceURL , destURL ) { const deleteSource = true ; return cloneModelInternal ( sourceURL , destURL , deleteSource ) ; } export { moveModel , copyModel , removeModel , listModels } ;