Skip to content
This repository was archived by the owner on Jan 17, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion node-eclib.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,21 @@ ECLib.prototype = {
hd: 0,
ct: 0
};
}
},

/**
* Add header to a fragment
* @param {Buffer} fragment - fragment for adding header
* @param {Number} fragment_index - index of fragment
* @param {Number} object_size - object size (without alignment)
* @param {Number} fragment_size - object size (without header)
* @return {undefined}
*/
addFragmentHeader: function(fragment, fragment_index, object_size,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document the function as the previous ones are.

fragment_size) {
return addon.EclAddFragmentHeader(this.ins_id, fragment,
fragment_index, object_size, fragment_size, this.opt.ct, 1);
},
}

module.exports = ECLib;
Expand Down
28 changes: 28 additions & 0 deletions src/cpp/libmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,31 @@ NAN_METHOD(EclGetFragmentSize) {
Nan::HandleScope scope;
info.GetReturnValue().Set(Nan::New("C++ GetFragmentSize").ToLocalChecked());
}

NAN_METHOD(EclAddFragmentHeader) {
Nan::HandleScope scope;

if (info.Length() != 7) {
Nan::ThrowTypeError("Wrong number of arguments EclAddFragmentHeader");
return ;
}

ec_checksum_type_t ct;
ec_backend_t instance;

int desc = Nan::To<int>(info[0]).FromJust();
char *frag = node::Buffer::Data(info[1]);
int frag_idx = Nan::To<int>(info[2]).FromJust();
int64_t obj_size = Nan::To<int64_t>(info[3]).FromJust();
int frag_size = Nan::To<int>(info[4]).FromJust();
int _ct = Nan::To<int>(info[5]).FromJust();
int add_cs = Nan::To<int>(info[6]).FromJust();

ct = get_ec_checksum_type(_ct);

instance = liberasurecode_backend_instance_get_by_desc(desc);

init_fragment_header(frag);
add_fragment_metadata(instance, frag, frag_idx, obj_size, frag_size, ct,
add_cs);
}
8 changes: 7 additions & 1 deletion src/cpp/libmain.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@
*/

#ifndef ECLIB_LIBMAIN_H
# define ECLIB_LIBMAIN_H
#define ECLIB_LIBMAIN_H

#include <nan.h>
#include <liberasurecode/erasurecode.h>
#include <liberasurecode/erasurecode_backend.h>
#include <liberasurecode/erasurecode_helpers.h>
extern "C" {
#include <liberasurecode/erasurecode_helpers_ext.h>
#include <liberasurecode/erasurecode_postprocessing.h>
}

NAN_METHOD(EclCreate);
NAN_METHOD(EclDestroy);
Expand All @@ -38,5 +43,6 @@ NAN_METHOD(EclVerifyStripeMetadata);
NAN_METHOD(EclGetAlignedDataSize);
NAN_METHOD(EclGetMinimumEncodeSize);
NAN_METHOD(EclGetFragmentSize);
NAN_METHOD(EclAddFragmentHeader);

#endif /* ECLIB_LIBMAIN_H */
1 change: 1 addition & 0 deletions src/cpp/node-eclib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ NAN_MODULE_INIT(Init) {
NAN_EXPORT(target, EclGetAlignedDataSize);
NAN_EXPORT(target, EclGetMinimumEncodeSize);
NAN_EXPORT(target, EclGetFragmentSize);
NAN_EXPORT(target, EclAddFragmentHeader);
}

NODE_MODULE(node_eclib, Init)
67 changes: 67 additions & 0 deletions test/functional/header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';

var eclib = require('../../index');
var enums = eclib.enums;
var ECLibUtil = eclib.util;
var buffertools = require("buffertools");
var crypto = require('crypto');
var assert = require('assert');

var k = 10;
var m = 5;

var ec = new eclib({
"bc_id": enums.BackendId["EC_BACKEND_JERASURE_RS_VAND"],
"k": k,
"m": m,
"w": 8,
"hd": m + 1,
"ct": 2,
});

ec.init();

function genFrags(fragSize) {
var arr = [];
for (var idx = 0; idx < k; idx++) {
arr.push(crypto.randomBytes(fragSize));
}
return arr;
}

var headerSize = 80;
var initHeader = new Buffer(headerSize);
buffertools.fill(initHeader, 0);

var fragSize = 256;
var objSize = k * fragSize;
var frags = genFrags(fragSize);
var data = Buffer.concat(frags, objSize);

describe('create fragment header', function(done) {
before('add init header', function(done) {
frags.forEach(function(frag, idx) {
frags[idx] = Buffer.concat([initHeader, frag],
fragSize + headerSize);
});
done();
});

it('should be OK', function(done) {
frags.forEach(function(frag, fragIdx) {
ec.addFragmentHeader(frag, fragIdx, objSize, fragSize);
});

ec.encode(data, function(status, dataFragments, parityFragments,
fragmentLength) {
assert.equal(status, 0);

// check headered fragments and encoded ones
dataFragments.forEach(function(frag, fragIdx) {
assert.equal(buffertools.compare(frags[fragIdx], frag), 0);
});

done();
});
});
});