New cache function for bulk-inserts in single txn to avoid excessive disk writes and blocking by worker.

Adding new getBlockByID function for future tool functionality.
This commit is contained in:
Alexander Blair
2017-05-19 14:51:48 -07:00
parent db1a3263e4
commit cce2a67c70
2 changed files with 32 additions and 5 deletions
+25
View File
@@ -218,6 +218,21 @@ function Database(){
return highestBlock; return highestBlock;
}; };
this.getBlockByID = function(blockID){
this.refreshEnv();
debug("Getting the data for blockID: " + blockID);
let txn = this.env.beginTxn({readOnly: true});
let data = txn.getBinary(blockID);
if (data === null){
debug("Unable to get block at height: "+ blockID);
return false;
}
let blockData = global.protos.Block.decode(data);
txn.commit();
debug("Done getting the last block for: "+ blockData.poolType + " height of: "+ blockID);
return blockData;
};
this.calculateShares = function(blockData, blockHeight){ this.calculateShares = function(blockData, blockHeight){
debug("Calculating shares for "+ blockData.hash); debug("Calculating shares for "+ blockData.hash);
this.refreshEnv(); this.refreshEnv();
@@ -376,6 +391,16 @@ function Database(){
txn.commit(); txn.commit();
}; };
this.bulkSetCache = function(cacheUpdates){
let txn = this.env.beginTxn();
for (let key in cacheUpdates){
if (cacheUpdates.hasOwnProperty(key)){
txn.putString(this.cacheDB, key, JSON.stringify(cacheUpdates[key]));
}
}
txn.commit();
};
this.cleanShareDB = function() { this.cleanShareDB = function() {
/* /*
This function takes the difficulty of the current block, and the last PPS block. If it's 0, save everything, This function takes the difficulty of the current block, and the last PPS block. If it's 0, save everything,
+7 -5
View File
@@ -137,6 +137,7 @@ function updateShareStats() {
if (globalMinerList === false) { if (globalMinerList === false) {
globalMinerList = []; globalMinerList = [];
} }
let cache_updates = {};
// pplns: 0, pps: 0, solo: 0, prop: 0, global: 0 // pplns: 0, pps: 0, solo: 0, prop: 0, global: 0
['pplns', 'pps', 'solo', 'prop', 'global'].forEach(function (key) { ['pplns', 'pps', 'solo', 'prop', 'global'].forEach(function (key) {
let cachedData = global.database.getCache(key + "_stats"); let cachedData = global.database.getCache(key + "_stats");
@@ -172,7 +173,7 @@ function updateShareStats() {
minerHistory: [{ts: currentTime, cn: cachedData.hash}] minerHistory: [{ts: currentTime, cn: cachedData.hash}]
}; };
} }
global.database.setCache(key + "_stats", cachedData); cache_updates[key + "_stats"] = cachedData;
}); });
minerList.forEach(function (miner) { minerList.forEach(function (miner) {
if (globalMinerList.indexOf(miner) === -1) { if (globalMinerList.indexOf(miner) === -1) {
@@ -206,7 +207,7 @@ function updateShareStats() {
badShares: 0 badShares: 0
}; };
} }
global.database.setCache(miner, cachedData); cache_updates[miner] = cachedData;
}); });
globalMinerList.forEach(function (miner) { globalMinerList.forEach(function (miner) {
if (minerList.indexOf(miner) === -1) { if (minerList.indexOf(miner) === -1) {
@@ -234,14 +235,15 @@ function updateShareStats() {
}); });
} }
minerStats.hash = 0; minerStats.hash = 0;
global.database.setCache(miner, minerStats); cache_updates[miner] = minerStats;
} }
} }
}); });
Object.keys(identifiers).forEach(function (key) { Object.keys(identifiers).forEach(function (key) {
global.database.setCache(key + '_identifiers', identifiers[key]); cache_updates[key + '_identifiers'] = identifiers[key];
}); });
global.database.setCache('minerList', globalMinerList); cache_updates['minerList'] = globalMinerList;
global.database.bulkSetCache(cache_updates);
callback(null); callback(null);
}); });
} }