Commit 29801817 authored by Cédric Moreau's avatar Cédric Moreau

[enh] #1037 Migrate File DALs

parent 07505ece
......@@ -8,5 +8,6 @@ app/lib/common.js
app/lib/dal/drivers/*.js
app/lib/dal/sqliteDAL/*.js
app/lib/dal/sqliteDAL/index/*.js
test/blockchain/*.js
test/blockchain/lib/*.js
\ No newline at end of file
app/lib/dal/fileDALs/*.js
test/*.js
test/**/*.js
\ No newline at end of file
......@@ -44,4 +44,5 @@ app/lib/dto/*.js*
app/lib/indexer.js*
app/lib/dal/drivers/*.js*
app/lib/dal/sqliteDAL/*.js*
app/lib/dal/sqliteDAL/index/*.js*
\ No newline at end of file
app/lib/dal/sqliteDAL/index/*.js*
app/lib/dal/fileDALs/*.js*
\ No newline at end of file
......@@ -10,9 +10,8 @@ const Merkle = require('../entity/merkle');
const Transaction = require('../entity/transaction');
const TransactionDTO = require('../dto/TransactionDTO').TransactionDTO
const constants = require('../constants');
const ConfDAL = require('./fileDALs/confDAL');
const StatDAL = require('./fileDALs/statDAL');
const CFSStorage = require('./fileDALs/AbstractCFS');
const ConfDAL = require('./fileDALs/ConfDAL').ConfDAL
const StatDAL = require('./fileDALs/StatDAL').StatDAL
module.exports = (params) => {
return new FileDAL(params);
......@@ -29,12 +28,12 @@ function FileDAL(params) {
this.wotb = params.wotb;
// DALs
this.confDAL = new ConfDAL(rootPath, myFS, null, that, CFSStorage);
this.confDAL = new ConfDAL(rootPath, myFS, null, that)
this.metaDAL = new (require('./sqliteDAL/MetaDAL').MetaDAL)(sqliteDriver);
this.peerDAL = new (require('./sqliteDAL/PeerDAL').PeerDAL)(sqliteDriver);
this.blockDAL = new (require('./sqliteDAL/BlockDAL').BlockDAL)(sqliteDriver);
this.txsDAL = new (require('./sqliteDAL/TxsDAL').TxsDAL)(sqliteDriver);
this.statDAL = new StatDAL(rootPath, myFS, null, that, CFSStorage);
this.statDAL = new StatDAL(rootPath, myFS, null, that)
this.idtyDAL = new (require('./sqliteDAL/IdentityDAL').IdentityDAL)(sqliteDriver);
this.certDAL = new (require('./sqliteDAL/CertDAL').CertDAL)(sqliteDriver);
this.msDAL = new (require('./sqliteDAL/MembershipDAL').MembershipDAL)(sqliteDriver);
......@@ -739,9 +738,9 @@ function FileDAL(params) {
* STATISTICS
**********************/
this.loadStats = that.statDAL.loadStats;
this.getStat = that.statDAL.getStat;
this.pushStats = that.statDAL.pushStats;
this.loadStats = () => that.statDAL.loadStats();
this.getStat = (name) => that.statDAL.getStat(name);
this.pushStats = (stats) => that.statDAL.pushStats(stats);
this.cleanCaches = () => co(function *() {
yield _.values(that.newDals).map((dal) => dal.cleanCache && dal.cleanCache());
......
/**
* Created by cgeek on 22/08/15.
*/
const cfs = require('../../cfs');
module.exports = AbstractCFS;
function AbstractCFS(rootPath, qioFS, parentDAL, localDAL) {
"use strict";
this.coreFS = cfs(rootPath, qioFS, parentDAL);
this.dal = localDAL;
}
import {CFSCore} from "./CFSCore";
export class AbstractCFS {
protected coreFS:CFSCore
dal:any
constructor(rootPath:string, qioFS:any, parentDAL:CFSCore, localDAL:any) {
this.coreFS = new CFSCore(rootPath, qioFS, parentDAL)
this.dal = localDAL;
}
}
/**
* Created by cgeek on 22/08/15.
*/
import {CFSCore} from "./CFSCore";
import {AbstractCFS} from "./AbstractCFS";
import {ConfDTO} from "../../dto/ConfDTO";
const Configuration = require('../../entity/configuration');
const co = require('co');
const _ = require('underscore');
module.exports = ConfDAL;
export class ConfDAL extends AbstractCFS {
function ConfDAL(rootPath, qioFS, parentCore, localDAL, AbstractStorage) {
private logger:any
"use strict";
constructor(rootPath:string, qioFS:any, parentCore:CFSCore|any, localDAL:any) {
super(rootPath, qioFS, parentCore, localDAL)
this.logger = require('../../logger')(this.dal.profile)
}
const that = this;
init() {
return Promise.resolve()
}
AbstractStorage.call(this, rootPath, qioFS, parentCore, localDAL);
const logger = require('../../logger')(this.dal.profile);
this.init = () => Promise.resolve();
this.getParameters = () => co(function *() {
const conf = yield that.loadConf();
async getParameters() {
const conf = await this.loadConf()
return {
"currency": conf.currency,
"c": parseFloat(conf.c),
......@@ -34,7 +32,7 @@ function ConfDAL(rootPath, qioFS, parentCore, localDAL, AbstractStorage) {
"sigQty": parseInt(conf.sigQty,10),
"idtyWindow": parseInt(conf.idtyWindow,10),
"msWindow": parseInt(conf.msWindow,10),
"xpercent": parseFloat(conf.xpercent,10),
"xpercent": parseFloat(conf.xpercent),
"msValidity": parseInt(conf.msValidity,10),
"stepMax": parseInt(conf.stepMax,10),
"medianTimeBlocks": parseInt(conf.medianTimeBlocks,10),
......@@ -44,19 +42,21 @@ function ConfDAL(rootPath, qioFS, parentCore, localDAL, AbstractStorage) {
"udTime0": parseInt(conf.udTime0),
"udReevalTime0": parseInt(conf.udReevalTime0),
"dtReeval": parseInt(conf.dtReeval)
};
});
}
}
this.loadConf = () => co(function *() {
const data = yield that.coreFS.readJSON('conf.json');
async loadConf() {
const data = await this.coreFS.readJSON('conf.json');
if (data) {
return _(Configuration.statics.defaultConf()).extend(data);
} else {
// Silent error
logger.warn('No configuration loaded');
this.logger.warn('No configuration loaded');
return {};
}
});
}
this.saveConf = (confToSave) => that.coreFS.writeJSONDeep('conf.json', confToSave);
async saveConf(confToSave:ConfDTO) {
await this.coreFS.writeJSONDeep('conf.json', confToSave)
}
}
import {AbstractCFS} from "./AbstractCFS";
import {CFSCore} from "./CFSCore";
const _ = require('underscore');
export class StatDAL extends AbstractCFS {
constructor(rootPath:string, qioFS:any, parentDAL:CFSCore, localDAL:any) {
super(rootPath, qioFS, parentDAL, localDAL)
}
init() {
return Promise.resolve()
}
async loadStats() {
try {
return await this.coreFS.readJSON('stats.json')
} catch (e) {
return null;
}
}
getStat(statName:string) {
return this.loadStats().then((stats:any) => (stats && stats[statName]) || { statName: statName, blocks: [], lastParsedBlock: -1 })
}
async pushStats(statsToPush:any) {
const stats = (await this.loadStats()) || {};
_.keys(statsToPush).forEach(function(statName:string){
if (!stats[statName]) {
stats[statName] = { blocks: [] };
}
stats[statName].blocks = stats[statName].blocks.concat(statsToPush[statName].blocks);
});
return this.coreFS.writeJSON('stats.json', stats)
}
}
/**
* Created by cgeek on 22/08/15.
*/
const co = require('co');
const _ = require('underscore');
module.exports = StatDAL;
function StatDAL(rootPath, qioFS, parentCore, localDAL, AbstractStorage) {
"use strict";
const that = this;
AbstractStorage.call(this, rootPath, qioFS, parentCore, localDAL);
this.init = () => Promise.resolve();
this.loadStats = () => co(function*(){
try {
return yield that.coreFS.readJSON('stats.json');
} catch (e) {
return null;
}
});
this.getStat = (statName) => that.loadStats().then((stats) => (stats && stats[statName]) || { statName: statName, blocks: [], lastParsedBlock: -1 });
this.pushStats = (statsToPush) => co(function *() {
const stats = (yield that.loadStats()) || {};
_.keys(statsToPush).forEach(function(statName){
if (!stats[statName]) {
stats[statName] = { blocks: [] };
}
stats[statName].blocks = stats[statName].blocks.concat(statsToPush[statName].blocks);
});
return that.coreFS.writeJSON('stats.json', stats);
});
}
......@@ -3,7 +3,7 @@
const co = require('co');
const opts = require('optimist').argv;
const path = require('path');
const cfs = require('../cfs');
const CFSCore = require('../dal/fileDALs/CFSCore').CFSCore
const Q = require('q');
const qfs = require('q-io/fs');
const fs = require('fs');
......@@ -66,7 +66,7 @@ const dir = module.exports = {
}),
createHomeIfNotExists: (fileSystem, theHome) => co(function *() {
const fsHandler = cfs(theHome, fileSystem);
const fsHandler = new CFSCore(theHome, fileSystem);
return fsHandler.makeTree('');
})
};
......
......@@ -2,7 +2,7 @@
var assert = require('assert');
var co = require('co');
var cfs = require('../../app/lib/cfs');
var CFSCore = require('../../app/lib/dal/fileDALs/CFSCore').CFSCore;
var mockFS = require('q-io/fs-mock')({
'B5_a': {
"A.json": '{ "text": "Content of A from B5_a" }'
......@@ -21,11 +21,11 @@ var mockFS = require('q-io/fs-mock')({
describe("CFS", () => {
var coreB3 = cfs('/B3', mockFS);
var coreB4 = cfs('/B4', mockFS, coreB3);
var coreB5 = cfs('/B5_a', mockFS, coreB4);
var coreB3 = new CFSCore('/B3', mockFS);
var coreB4 = new CFSCore('/B4', mockFS, coreB3);
var coreB5 = new CFSCore('/B5_a', mockFS, coreB4);
var rootCore = cfs('/OTHER', mockFS);
var rootCore = new CFSCore('/OTHER', mockFS);
// ------------ Direct READ ------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment