Skip to content

Commit

Permalink
PharmaLedger-IMI/epi-workspace#840 update dsu in batch mode in Single…
Browse files Browse the repository at this point in the history
…DSUStorageStrategy.js
  • Loading branch information
skutner committed Jul 18, 2022
1 parent b29cf0c commit 6ab3b49
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions db/storageStrategies/SingleDSUStorageStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,17 @@ function SingleDSUStorageStrategy() {
}

const TaskCounter = require("swarmutils").TaskCounter;
let batchInProgress = false;
if (storageDSU.batchInProgress()) {
batchInProgress = true
} else {
storageDSU.beginBatch();
}
const taskCounter = new TaskCounter(() => {
return callback();
if (batchInProgress) {
return callback();
}
storageDSU.commitBatch(callback);
})

if (primaryKeys.length === 0) {
Expand All @@ -297,7 +306,10 @@ function SingleDSUStorageStrategy() {
return callback(createOpenDSUErrorWrapper(`Failed to create empty index for field ${fieldName} in table ${tableName}`, err));
}

callback();
if (batchInProgress) {
return callback();
}
storageDSU.commitBatch(callback);
});
}

Expand Down Expand Up @@ -496,6 +508,12 @@ function SingleDSUStorageStrategy() {
}

const recordPath = getRecordPath(tableName, key);
let batchInProgress = false;
if (storageDSU.batchInProgress()) {
batchInProgress = true
} else {
storageDSU.beginBatch();
}
storageDSU.writeFile(recordPath, JSON.stringify(record), function (err, res) {
if (err) {
return callback(createOpenDSUErrorWrapper(`Failed to update record in ${recordPath}`, err));
Expand All @@ -512,7 +530,10 @@ function SingleDSUStorageStrategy() {
return callback(createOpenDSUErrorWrapper(`Failed to update indexes for record ${record}`, err));
}

callback(undefined, record);
if (batchInProgress) {
return callback(undefined, record);
}
storageDSU.commitBatch(err => callback(err, record));
});
});
}
Expand All @@ -522,7 +543,10 @@ function SingleDSUStorageStrategy() {
return callback(createOpenDSUErrorWrapper(`Failed to update indexes for record ${record}`, err));
}

callback(undefined, record);
if (batchInProgress) {
return callback(undefined, record);
}
storageDSU.commitBatch(err => callback(err, record));
});
});
};
Expand Down Expand Up @@ -569,6 +593,12 @@ function SingleDSUStorageStrategy() {

const READ_WRITE_KEY_TABLE = "KeyValueTable";
this.writeKey = function (key, value, callback) {
let batchInProgress = false;
if (storageDSU.batchInProgress()) {
batchInProgress = true
} else {
storageDSU.beginBatch();
}
let valueObject = {
type: typeof value,
value: value
Expand All @@ -589,7 +619,15 @@ function SingleDSUStorageStrategy() {
}

const recordPath = getRecordPath(READ_WRITE_KEY_TABLE, key);
storageDSU.writeFile(recordPath, JSON.stringify(valueObject), callback);
storageDSU.writeFile(recordPath, JSON.stringify(valueObject), err => {
if (err) {
return callback(err);
}
if (batchInProgress) {
return callback(undefined);
}
storageDSU.commitBatch(callback);
});
};

this.readKey = function (key, callback) {
Expand Down

0 comments on commit 6ab3b49

Please # to comment.