Mise à jour de GitLab prévue ce dimanche 25 octobre 2020 à partir de 9h00 CEST | GitLab upgrade planned this Sunday 25rd of October 2020 from 9:00 AM CEST

Commit c0d98f1c authored by Éloïs's avatar Éloïs

[enh] add IO file features

parent 4b9dde65
Pipeline #1954 passed with stage
in 2 minutes and 38 seconds
......@@ -5,17 +5,17 @@ const path = require('path');
module.exports = {
/*newFileInstance: (filePath) => {
newFileInstance: (filePath, sigStock) => {
const instance = Object.create(WotB)
if (process.platform == 'win32') {
let bufferedPath = new Buffer(filePath,'ascii');
instance.init(wotb.newFileInstanceWin32(bufferedPath, bufferedPath.length))
instance.init(addon.new_file_instance_win_32(bufferedPath, bufferedPath.length, sigStock))
} else {
instance.init(wotb.new_file_instance(filePath));
instance.init(addon.new_file_instance(filePath, sigStock));
}
instance.setFilePath(filePath)
return instance
},*/
},
newMemoryInstance: (sigStock) => {
const instance = Object.create(WotB)
......@@ -44,6 +44,10 @@ const WotB = {
this.filePath = filePath
},
write: function() {
return addon.write_instance_in_file(this.instanceID, this.filePath);
},
memCopy: function() {
const copy = Object.create(WotB)
copy.instanceID = addon.mem_copy(this.instanceID);
......
......@@ -8,6 +8,7 @@ use duniter_wotb::data::rusty::RustyWebOfTrust;
use duniter_wotb::data::{HasLinkResult, NewLinkResult, NodeId, RemLinkResult, WebOfTrust};
use duniter_wotb::operations::distance::{DistanceCalculator, RustyDistanceCalculator, WotDistance,
WotDistanceParameters};
use duniter_wotb::operations::file::{BinaryFileFormater, FileFormater};
use duniter_wotb::operations::path::{PathFinder, RustyPathFinder};
use neon::js::{JsArray, JsBoolean, JsInteger, JsNumber, JsObject, JsString, Object};
use neon::vm::{Call, JsResult};
......@@ -16,6 +17,7 @@ use std::ops::{Deref, DerefMut};
static mut WOT_INSTANCES: Option<HashMap<usize, Box<RustyWebOfTrust>>> = None;
static DISTANCE_CALCULATOR: RustyDistanceCalculator = RustyDistanceCalculator {};
static FILE_FORMATER: BinaryFileFormater = BinaryFileFormater {};
static PATH_FINDER: RustyPathFinder = RustyPathFinder {};
fn get_wots() -> &'static mut HashMap<usize, Box<RustyWebOfTrust>> {
......@@ -77,6 +79,35 @@ fn threading_hint(call: Call) -> JsResult<JsNumber> {
Ok(JsNumber::new(call.scope, num_cpus::get() as f64))
}
fn new_file_instance(call: Call) -> JsResult<JsInteger> {
let scope = call.scope;
let file_path = try!(try!(call.arguments.require(scope, 0)).check::<JsString>()).value();
let max_links = try!(try!(call.arguments.require(scope, 1)).check::<JsInteger>()).value();
let (wot, _blockstamp) = FILE_FORMATER
.from_file(&file_path, max_links as usize)
.unwrap();
let wots = get_wots();
let mut instance_id = 0;
while wots.contains_key(&instance_id) {
instance_id += 1;
}
wots.insert(instance_id, Box::new(wot));
Ok(JsInteger::new(scope, instance_id as i32))
}
fn write_instance_in_file(call: Call) -> JsResult<JsBoolean> {
let scope = call.scope;
let instance_id =
try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value() as usize;
let file_path = try!(try!(call.arguments.require(scope, 1)).check::<JsString>()).value();
let wot = get_wot(instance_id as usize);
let header: Vec<u8> = Vec::with_capacity(0);
match FILE_FORMATER.to_file(wot, &header, &file_path) {
Ok(_) => Ok(JsBoolean::new(scope, true)),
Err(e) => panic!("Fatal error : fail to write wot in file : {:?}", e),
}
}
fn new_memory_instance(call: Call) -> JsResult<JsInteger> {
let scope = call.scope;
let max_links = try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value();
......@@ -388,6 +419,8 @@ fn remove_wot(call: Call) -> JsResult<JsBoolean> {
register_module!(m, {
m.export("hello", hello)?;
m.export("threadingHint", threading_hint)?;
m.export("new_file_instance", new_file_instance)?;
m.export("write_instance_in_file", write_instance_in_file)?;
m.export("new_memory_instance", new_memory_instance)?;
m.export("mem_copy", mem_copy)?;
m.export("get_max_links", get_max_links)?;
......
......@@ -6,7 +6,7 @@ var fs = require('fs');
var path = require('path');
var should = require('should');
const FILE = path.join(__dirname, 'test.wot');
const FILE = path.join(__dirname, 'g1_genesis.bin');
const X_PERCENT = 1.0;
const _100_PERCENT = 1.0;
const MAX_DISTANCE_1 = 1;
......@@ -19,29 +19,19 @@ const FROM_2_LINKS_SENTRIES = 2;
const FROM_3_LINKS_SENTRIES = 3;
const __OUTDISTANCED__ = true;
const __OK__ = false;
const MEMORY_MODE = true;
const FILE_MODE = false;
testSuite("MEMORY", MEMORY_MODE);
testSuite();
function testSuite(title, mode) {
function testSuite() {
function newInstance(launchTests) {
return () => {
if (mode === FILE_MODE) {
let wot = addon.newFileInstance(FILE);
launchTests(wot, () => {
wot.clear();
wot.setMaxCert(3);
});
} else {
let wot = addon.newMemoryInstance(3);
launchTests(wot, () => {});
}
let wot = addon.newMemoryInstance(3);
launchTests(wot);
}
}
describe(title, () => {
describe('Basic operations', newInstance((wot, cleanInstance) => {
describe("wotb-rs binding tests", () => {
describe('Basic operations', newInstance((wot) => {
it('should have an instanceID zero', function() {
assert.equal(wot.instanceID, 0)
......@@ -264,14 +254,11 @@ function testSuite(title, mode) {
should.equal(wot2.instanceID, 0);
wot2.clear()
});
after(cleanInstance);
}));
describe('Building a larger WoT', newInstance((wot, cleanInstance) => {
describe('Building a larger WoT', newInstance((wot) => {
before(() => {
cleanInstance();
/**
* We build WoT:
*
......@@ -483,6 +470,46 @@ function testSuite(title, mode) {
should.equal(wot.isOutdistanced(2, FROM_3_LINKS_SENTRIES, MAX_DISTANCE_5, 0.01), __OK__);
});
});
describe('testing write in file', () => {
it('should can write in file', function() {
wot.setFilePath("test.bin");
assert.equal(wot.write(), true)
});
});
}));
describe('tests g1 genesis wot', newInstance((wot) => {
before(() => {
wot.clear();
wot = addon.newFileInstance(FILE, 100);
});
it('should have an instanceID zero', function() {
assert.equal(wot.instanceID, 0)
});
it('should have 100 max links', function() {
assert.equal(wot.getMaxCert(), 100)
});
it('should have a wot size of 59', function() {
should.equal(wot.getWoTSize(), 59);
});
it('should have only enabled members', function() {
should.equal(wot.getEnabled().length, 59);
should.equal(wot.getDisabled().length, 0);
});
it('should have 48 sentries', function() {
should.equal(wot.getSentries(FROM_3_LINKS_SENTRIES).length, 48);
});
after(() => {
wot.clear();
});
}));
});
}
\ No newline at end of file
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