Skip to content

Commit

Permalink
PharmaLedger-IMI/epi-workspace#924 load previous version of DSU if l…
Browse files Browse the repository at this point in the history
…oad fails
  • Loading branch information
skutner committed Nov 2, 2022
1 parent 6009a06 commit 1a61e1f
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions lib/DSUFactoryRegistry/DSUFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
* @param {BrickMapStrategyFactory} options.brickMapStrategyFactory
*/
const cache = require('psk-cache').factory();

function DSUFactory(options) {
const barModule = require('bar');
const fsAdapter = require('bar-fs-adapter');
const MAX_BRICK_SIZE = 1000000;
options = options || {};
const MAX_BRICK_SIZE = options.maxBrickSize || 1000000;
this.keySSIFactory = options.keySSIFactory;
this.brickMapStrategyFactory = options.brickMapStrategyFactory;
const openDSU = require("opendsu");
const keySSISpace = openDSU.loadAPI("keyssi");

function castSSI(ssi){
if(typeof ssi !== "undefined"){
if(typeof ssi === "string"){
function castSSI(ssi) {
if (typeof ssi !== "undefined") {
if (typeof ssi === "string") {
ssi = keySSISpace.parse(ssi);
} else {
if(ssi.getTypeName === undefined || ssi.getIdentifier === undefined){
throw Error("Please provide a proper SSI instance ");
}
if (ssi.getTypeName === undefined || ssi.getIdentifier === undefined) {
throw Error("Please provide a proper SSI instance ");
}
}
} else {
throw Error("SSI should not be undefined");
Expand All @@ -43,27 +44,29 @@ function DSUFactory(options) {
*/
const createInstance = (keySSI, options, initializationMethod, callback) => {
const INIT = "init";
const allowedInitMethods = [INIT, "load"];
if(allowedInitMethods.indexOf(initializationMethod) === -1){
throw Error("wrong usage of the createInstace method");
const LOAD = "load";
const LOAD_VERSION = "loadVersion";
const allowedInitMethods = [INIT, LOAD];
if (allowedInitMethods.indexOf(initializationMethod) === -1) {
throw Error("Wrong usage of the createInstance method");
}

let bar;
try{
try {
let identifier = keySSI;
if(typeof identifier == "string"){
if (typeof identifier == "string") {
let bar = forcedArchiveSingletonsCache[identifier];
if(bar) return bar;
if (bar) return bar;
}

const ArchiveConfigurator = barModule.ArchiveConfigurator;
ArchiveConfigurator.prototype.registerFsAdapter("FsAdapter", fsAdapter.createFsAdapter);
const archiveConfigurator = new ArchiveConfigurator();
archiveConfigurator.setCache(cache);
const envTypes = require("overwrite-require").constants;
if($$.environmentType !== envTypes.BROWSER_ENVIRONMENT_TYPE &&
if ($$.environmentType !== envTypes.BROWSER_ENVIRONMENT_TYPE &&
$$.environmentType !== envTypes.SERVICE_WORKER_ENVIRONMENT_TYPE &&
$$.environmentType !== envTypes.WEB_WORKER_ENVIRONMENT_TYPE){
$$.environmentType !== envTypes.WEB_WORKER_ENVIRONMENT_TYPE) {
archiveConfigurator.setFsAdapter("FsAdapter");
}
archiveConfigurator.setBufferSize(MAX_BRICK_SIZE);
Expand All @@ -87,7 +90,7 @@ function DSUFactory(options) {
DSUBase(bar);
forcedArchiveSingletonsCache[identifier] = bar;

}catch(err){
} catch (err) {
return callback(err);
}

Expand All @@ -107,6 +110,15 @@ function DSUFactory(options) {
callback(err, bar);
}

if (initializationMethod === LOAD) {
if (options && options.versionHashlink) {
initializationMethod = LOAD_VERSION;
return bar[initializationMethod](options.versionHashlink, defaultCallback);
}

initializationMethod = LOAD;
}

bar[initializationMethod](initializationMethod === INIT ? initCallback : defaultCallback);
}

Expand Down Expand Up @@ -160,7 +172,7 @@ function DSUFactory(options) {
*/
this.create = (keySSI, options, callback) => {
keySSI = castSSI(keySSI);
if(typeof options === "function"){
if (typeof options === "function") {
callback = options;
options = undefined;
}
Expand Down Expand Up @@ -194,7 +206,7 @@ function DSUFactory(options) {
*/
this.load = (keySSI, options, callback) => {
keySSI = castSSI(keySSI);
if(typeof options === "function"){
if (typeof options === "function") {
callback = options;
options = undefined;
}
Expand Down

0 comments on commit 1a61e1f

Please sign in to comment.