Skip to content
Snippets Groups Projects
Commit d5f1792c authored by Cédric Moreau's avatar Cédric Moreau
Browse files

[enh] #1037 Refactoring CFS

parent 29801817
No related branches found
No related tags found
No related merge requests found
......@@ -28,12 +28,12 @@ function FileDAL(params) {
this.wotb = params.wotb;
// DALs
this.confDAL = new ConfDAL(rootPath, myFS, null, that)
this.confDAL = new ConfDAL(rootPath, myFS)
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)
this.statDAL = new StatDAL(rootPath, myFS)
this.idtyDAL = new (require('./sqliteDAL/IdentityDAL').IdentityDAL)(sqliteDriver);
this.certDAL = new (require('./sqliteDAL/CertDAL').CertDAL)(sqliteDriver);
this.msDAL = new (require('./sqliteDAL/MembershipDAL').MembershipDAL)(sqliteDriver);
......
......@@ -3,10 +3,9 @@ import {CFSCore} from "./CFSCore";
export class AbstractCFS {
protected coreFS:CFSCore
dal:any
protected dal:any
constructor(rootPath:string, qioFS:any, parentDAL:CFSCore, localDAL:any) {
this.coreFS = new CFSCore(rootPath, qioFS, parentDAL)
this.dal = localDAL;
constructor(rootPath:string, qioFS:any) {
this.coreFS = new CFSCore(rootPath, qioFS)
}
}
......@@ -3,7 +3,6 @@
const _ = require('underscore');
const path = require('path');
const LOCAL_LEVEL = true;
const DEEP_WRITE = true;
export class CFSCore {
......@@ -12,7 +11,7 @@ export class CFSCore {
private deletionFolderPromise: Promise<any> | null
private createDeletionFolder: () => Promise<any> | null
constructor(private rootPath:string, private qfs:any, private parent:CFSCore | null) {
constructor(private rootPath:string, private qfs:any) {
this.deletedFolder = path.join(rootPath, '.deleted')
this.deletionFolderPromise = null
......@@ -23,10 +22,6 @@ export class CFSCore {
this.createDeletionFolder = () => this.deletionFolderPromise || (this.deletionFolderPromise = this.makeTree('.deleted'))
}
changeParent(newParent:CFSCore) {
this.parent = newParent
}
/**
* READ operation of CFS. Reads given file. May lead to tree traversal if file is not found.
* @param filePath Path to the file.
......@@ -41,8 +36,7 @@ export class CFSCore {
}
return await this.qfs.read(path.join(this.rootPath, filePath));
} catch (e) {
if (!this.parent) return null;
return this.parent.read(filePath);
return null
}
}
......@@ -58,14 +52,9 @@ export class CFSCore {
// A deleted file must be considered non-existant
return false;
}
let exists = await this.qfs.exists(path.join(this.rootPath, filePath));
if (!exists && this.parent) {
exists = this.parent.exists(filePath);
}
return exists;
return await this.qfs.exists(path.join(this.rootPath, filePath))
} catch (e) {
if (!this.parent) return null;
return this.parent.exists(filePath);
return null
}
}
......@@ -75,12 +64,9 @@ export class CFSCore {
* @param localLevel Limit listing to local level.
* @returns {*} Promise of file names.
*/
async list(ofPath:string, localLevel = false): Promise<string[]> {
async list(ofPath:string): Promise<string[]> {
const dirPath = path.normalize(ofPath);
let files: string[] = [], folder = path.join(this.rootPath, dirPath);
if (this.parent && !localLevel) {
files = await this.parent.list(dirPath);
}
const hasDir = await this.qfs.exists(folder);
if (hasDir) {
files = files.concat(await this.qfs.list(folder));
......@@ -96,10 +82,6 @@ export class CFSCore {
return _.uniq(files);
};
listLocal(ofPath:string) {
return this.list(ofPath, LOCAL_LEVEL)
}
/**
* WRITE operation of CFS. Writes the file in local Core.
* @param filePath Path to the file to write.
......@@ -107,9 +89,6 @@ export class CFSCore {
* @param deep Wether to make a deep write or not.
*/
async write(filePath:string, content:string, deep:boolean): Promise<void> {
if (deep && this.parent) {
return this.parent.write(filePath, content, deep);
}
return this.qfs.write(path.join(this.rootPath, filePath), content);
};
......@@ -121,14 +100,6 @@ export class CFSCore {
*/
async remove(filePath:string, deep:boolean): Promise<void> {
// Make a deep physical deletion
if (deep && this.parent) {
return this.parent.remove(filePath, deep);
}
// Not the root core, make a logical deletion instead of physical
if (this.parent) {
await this.createDeletionFolder();
return this.qfs.write(path.join(this.rootPath, '.deleted', this.toRemoveFileName(filePath)), '');
}
// Root core: physical deletion
return this.qfs.remove(path.join(this.rootPath, filePath));
}
......@@ -209,8 +180,8 @@ export class CFSCore {
* @param dirPath Path to get the files' contents.
* @param localLevel Wether to read only local level or not.
*/
listJSON(dirPath:string, localLevel:boolean) {
return this.list(dirPath, localLevel).then(async (files) => Promise.all(files.map((f:string) => this.readJSON(path.join(dirPath, f)))))
listJSON(dirPath:string) {
return this.list(dirPath).then(async (files) => Promise.all(files.map((f:string) => this.readJSON(path.join(dirPath, f)))))
}
/**
......@@ -218,7 +189,7 @@ export class CFSCore {
* @param dirPath Path to get the files' contents.
*/
listJSONLocal(dirPath:string) {
return this.listJSON(dirPath, LOCAL_LEVEL)
return this.listJSON(dirPath)
}
/**
......
......@@ -9,9 +9,9 @@ export class ConfDAL extends AbstractCFS {
private logger:any
constructor(rootPath:string, qioFS:any, parentCore:CFSCore|any, localDAL:any) {
super(rootPath, qioFS, parentCore, localDAL)
this.logger = require('../../logger')(this.dal.profile)
constructor(rootPath:string, qioFS:any) {
super(rootPath, qioFS)
this.logger = require('../../logger')()
}
init() {
......
/**
* Created by cgeek on 22/08/15.
*/
const co = require('co');
module.exports = IndicatorsDAL;
function IndicatorsDAL(rootPath, qioFS, parentCore, localDAL, AbstractStorage) {
"use strict";
const that = this;
AbstractStorage.call(this, rootPath, qioFS, parentCore, localDAL);
this.init = () => {
return co(function *() {
yield [
that.coreFS.makeTree('indicators/'),
that.coreFS.makeTree('indicators/issuers')
];
});
};
}
......@@ -4,8 +4,8 @@ const _ = require('underscore');
export class StatDAL extends AbstractCFS {
constructor(rootPath:string, qioFS:any, parentDAL:CFSCore, localDAL:any) {
super(rootPath, qioFS, parentDAL, localDAL)
constructor(rootPath:string, qioFS:any) {
super(rootPath, qioFS)
}
init() {
......
"use strict";
var assert = require('assert');
var should = require('should');
var co = require('co');
var CFSCore = require('../../app/lib/dal/fileDALs/CFSCore').CFSCore;
var mockFS = require('q-io/fs-mock')({
......@@ -22,8 +23,8 @@ var mockFS = require('q-io/fs-mock')({
describe("CFS", () => {
var coreB3 = new CFSCore('/B3', mockFS);
var coreB4 = new CFSCore('/B4', mockFS, coreB3);
var coreB5 = new CFSCore('/B5_a', mockFS, coreB4);
var coreB4 = new CFSCore('/B4', mockFS);
var coreB5 = new CFSCore('/B5_a', mockFS);
var rootCore = new CFSCore('/OTHER', mockFS);
......@@ -36,22 +37,6 @@ describe("CFS", () => {
});
});
// ------------ Traversal READ ------------
it('should have the content of B.json from B5 (traversal read to B4)', () => {
return co(function *() {
var content = yield coreB5.readJSON('B.json');
content.should.have.property('text').equal('Content of B');
});
});
it('should have the content of C.json from B5 (traversal read to B3)', () => {
return co(function *() {
var content = yield coreB5.readJSON('C.json');
content.should.have.property('text').equal('Content of C from B3');
});
});
// WRITE of file /C.json
it('should have the content of C.json modified from B5 (direct read)', () => {
......@@ -64,19 +49,18 @@ describe("CFS", () => {
// WRITE of file /D.json
it('should have the content of C.json modified from B5 (direct read)', () => {
it('should have the content of D.json modified from B4 (direct read/write)', () => {
return co(function *() {
yield coreB4.writeJSON('D.json', { text: 'Content of D'});
var content = yield coreB5.readJSON('D.json');
var content = yield coreB4.readJSON('D.json');
content.should.have.property('text').equal('Content of D');
});
});
// REMOVE file /D.json
it('should have the content of C.json modified from B5 (direct read)', () => {
it('should have the content of D.json modified from B5 (direct read/write)', () => {
return co(function *() {
yield coreB4.remove('D.json');
var exists = yield coreB5.exists('D.json');
var content = yield coreB5.read('D.json');
assert.equal(exists, false);
......@@ -94,26 +78,9 @@ describe("CFS", () => {
yield coreB3.writeJSON('/DIR/G.json', { text: 'Content of DIR/I'});
yield coreB4.writeJSON('/DIR/H.json', { text: 'Content of DIR/H'});
yield coreB5.writeJSON('/DIR/I.json', { text: 'Content of DIR/G'});
var files = yield coreB5.list('/DIR');
files.should.have.length(3);
files.should.deepEqual(['G.json', 'H.json', 'I.json']);
});
});
// WRITE of file /DIR2/I.json in B3
it('should have I as files from /DIR2', () => {
return co(function *() {
yield coreB3.makeTree('/DIR2');
yield coreB3.writeJSON('/DIR2/I.json', { text: 'Content of DIR2/I in B3'});
// Check the list
var files = yield coreB5.list('/DIR2');
files.should.have.length(1);
files.should.deepEqual(['I.json']);
// Check its contents
var contents = yield coreB5.listJSON('/DIR2');
contents.should.have.length(1);
contents.should.deepEqual([{ text: 'Content of DIR2/I in B3' }]);
(yield coreB3.list('/DIR')).should.deepEqual(['G.json']);
(yield coreB4.list('/DIR')).should.deepEqual(['H.json']);
(yield coreB5.list('/DIR')).should.deepEqual(['I.json']);
});
});
......@@ -123,11 +90,11 @@ describe("CFS", () => {
return co(function *() {
yield coreB3.makeTree('/DIR2');
yield coreB3.writeJSON('/DIR2/I.json', { text: 'Content of DIR2/I in B4'});
var files = yield coreB5.list('/DIR2');
var files = yield coreB3.list('/DIR2');
files.should.have.length(1);
files.should.deepEqual(['I.json']);
// Check its contents
var contents = yield coreB5.listJSON('/DIR2');
var contents = yield coreB3.listJSON('/DIR2');
contents.should.have.length(1);
contents.should.deepEqual([{ text: 'Content of DIR2/I in B4' }]);
});
......@@ -137,11 +104,11 @@ describe("CFS", () => {
it('should have no files from /DIR2 after file DELETION', () => {
return co(function *() {
yield coreB5.remove('/DIR2/I.json');
var files = yield coreB5.list('/DIR2');
yield coreB3.remove('/DIR2/I.json');
var files = yield coreB3.list('/DIR2');
files.should.have.length(0);
// Check its contents
var contents = yield coreB5.listJSON('/DIR2');
var contents = yield coreB3.listJSON('/DIR2');
contents.should.have.length(0);
});
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment