Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
lichengyin committed Jul 27, 2018
1 parent 1341236 commit 79fa45d
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 162 deletions.
7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "think",
"globals": {
"SharedArrayBuffer": true,
"Atomics": true
}
}
4 changes: 2 additions & 2 deletions lib/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ const Encrypt = {
if (encoding === null) return Buffer.from(buffer);
return Buffer.from(buffer).toString(encoding);
}
}
};

module.exports = Encrypt;
module.exports = Encrypt;
16 changes: 8 additions & 8 deletions lib/find.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/**
* search buffer in another buffer
* @TODO use KMP
* @param {Buffer} buffer
* @param {Buffer} search
* @param {Int} start
* @param {Int} end
* @param {Int} sep
* @param {Buffer} buffer
* @param {Buffer} search
* @param {Int} start
* @param {Int} end
* @param {Int} sep
*/
module.exports = function find(buffer, search, start = 0, end = 0, sep = 0) {
end = end || buffer.length;
const len = search.length;
for(let i = start; i < end; i++){
for (let i = start; i < end; i++) {
if (buffer[i] === sep) continue;
let j = 0;
for(j = 0; j < len; j++){
for (j = 0; j < len; j++) {
if (buffer[i + j] !== search[j]) break;
}
if (j === len && sep === buffer[i + len]) return i;
}
}
};
75 changes: 31 additions & 44 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Encrypt = require('./encrypt');
const Find = require('./find');
const Lock = require('./lock')
const Lock = require('./lock');

const keyPrefix = '$';
const sep = 0;
Expand All @@ -15,7 +15,7 @@ module.exports = class SharedArrayBufferStore {
} = {}) {
this.keyBuffer = keyBuffer || new SharedArrayBuffer(keyBufferLength * Uint32Array.BYTES_PER_ELEMENT);
this.valueBuffer = valueBuffer || new SharedArrayBuffer(valueBufferLength * Uint32Array.BYTES_PER_ELEMENT);
this.keyArray = new Uint32Array(this.keyBuffer)
this.keyArray = new Uint32Array(this.keyBuffer);
this.valueArray = new Uint32Array(this.valueBuffer);
this.encrypt = encrypt;
this.reservedLength = reservedLength;
Expand All @@ -29,7 +29,7 @@ module.exports = class SharedArrayBufferStore {
}
getKeyStorePos() {
const prefixLength = this.keyPrefixLength;
const prefix = this.keyArray.slice(this.reservedLength, this.reservedLength + prefixLength)
const prefix = this.keyArray.slice(this.reservedLength, this.reservedLength + prefixLength);
const pos = parseInt(this.encrypt.decode(prefix), '10') || 0;
if (!pos) return prefixLength + this.reservedLength + 1;
return pos;
Expand All @@ -38,7 +38,7 @@ module.exports = class SharedArrayBufferStore {
const prefixLength = this.keyPrefixLength;
pos = this.padStart(pos, prefixLength);
const buf = this.encrypt.encode(pos);
for(let i = 0; i < prefixLength; i++){
for (let i = 0; i < prefixLength; i++) {
this.keyArray[i + this.reservedLength] = buf[i];
}
}
Expand All @@ -51,15 +51,15 @@ module.exports = class SharedArrayBufferStore {
}
updateValueStorePos(pos) {
const prefixLength = this.valuePrefixLength;
pos = this.padStart(pos, prefixLength)
pos = this.padStart(pos, prefixLength);
const buf = this.encrypt.encode(pos);
for(let i = 0; i < prefixLength; i++){
for (let i = 0; i < prefixLength; i++) {
this.valueArray[i + this.reservedLength] = buf[i];
}
}
/**
* get key buffer
* @param {String} key
* @param {String} key
*/
getKeyBuf(key) {
if (typeof key !== 'string') return key;
Expand All @@ -70,7 +70,7 @@ module.exports = class SharedArrayBufferStore {
return str.slice(keyPrefix.length);
}
/**
* @param {String} key
* @param {String} key
*/
getKeyStoreProps(key) {
const keyBuf = this.getKeyBuf(key);
Expand All @@ -87,58 +87,51 @@ module.exports = class SharedArrayBufferStore {
return [start, length, pos];
}

/**
* @param {*} key
* @param {*} start
* @param {*} length
*/
addKey(key, start, length) {
key = this.getKeyBuf(key);
start = this.encrypt.encode(this.padStart(start, this.valuePrefixLength));
length = this.encrypt.encode(this.padStart(length, this.valuePrefixLength));
let pos = this.getKeyStorePos();
const newPos = pos + key.length + start.length + length.length + 3;
for(let i = 0, len = key.length; i < len; i++){
for (let i = 0, len = key.length; i < len; i++) {
this.keyArray[pos + i] = key[i];
}
pos += key.length + 1;
for(let i = 0, len = start.length; i < len; i++){
for (let i = 0, len = start.length; i < len; i++) {
this.keyArray[pos + i] = start[i];
}
pos += start.length + 1;
for(let i = 0, len = length.length; i < len; i++){
for (let i = 0, len = length.length; i < len; i++) {
this.keyArray[pos + i] = length[i];
}
if (pos + length.length > this.keyArray.length) {
throw new Error('keyArray size is not enough');
}
this.updateKeyStorePos(newPos);
}
/**
* @param {Buffer} key
* @param {Int} pos
* @param {Int} start
* @param {Int} length
*/

updateKey(key, pos, start, length) {
key = this.getKeyBuf(key);
start = this.encrypt.encode(this.padStart(start, this.valuePrefixLength));
length = this.encrypt.encode(this.padStart(length, this.valuePrefixLength));
pos = pos + key.length + 1;
for(let i = 0, len = start.length; i < len; i++){
for (let i = 0, len = start.length; i < len; i++) {
this.keyArray[pos + i] = start[i];
}
pos += start.length + 1;
for(let i = 0, len = length.length; i < len; i++){
for (let i = 0, len = length.length; i < len; i++) {
this.keyArray[pos + i] = length[i];
}
}
/**
* @param {Buffer} buffer
* @param {Int} pos
*/

updateValueBuffer(buffer, pos) {
buffer = this.encrypt.encode(buffer);
for(let i = 0, len = buffer.length; i < len; i++){
for (let i = 0, len = buffer.length; i < len; i++) {
this.valueArray[pos + i] = buffer[i];
}
if (pos + buffer.length > this.valueArray.length) {
throw new Error(`valueArray size is not enough`);
}
}
lock() {
this.keyLock.lock();
Expand All @@ -148,9 +141,7 @@ module.exports = class SharedArrayBufferStore {
this.keyLock.unlock();
this.valueLock.unlock();
}
/**
* @param {String} key
*/

get(key, encoding) {
this.lock();
const props = this.getKeyStoreProps(key);
Expand All @@ -162,12 +153,7 @@ module.exports = class SharedArrayBufferStore {
this.unlock();
return data;
}
/**
* endPoint:
* @param {String} key
* @param {String|Buffer} value
* @param {String} encoding
*/

set(key, value, encoding) {
this.lock();
key = this.getKeyBuf(key);
Expand Down Expand Up @@ -197,10 +183,11 @@ module.exports = class SharedArrayBufferStore {
this.lock();
key = this.getKeyBuf(key);
const keyProps = this.getKeyStoreProps(key);
if (!keyProps) return;
const end = keyProps[2] + key.length + 2 * this.valuePrefixLength + 3;
for(let i = keyProps[2]; i < end; i++) {
this.keyArray[i] = sep;
if (keyProps) {
const end = keyProps[2] + key.length + 2 * this.valuePrefixLength + 3;
for (let i = keyProps[2]; i < end; i++) {
this.keyArray[i] = sep;
}
}
this.unlock();
}
Expand All @@ -214,7 +201,7 @@ module.exports = class SharedArrayBufferStore {
const keys = [];
const key = [];
const stepLen = this.valuePrefixLength * 2 + 2;
while(startPos < keyPos) {
while (startPos < keyPos) {
const item = this.keyArray[startPos];
if (item === sep) {
if (key.length === 0) {
Expand All @@ -232,4 +219,4 @@ module.exports = class SharedArrayBufferStore {
this.unlock();
return keys;
}
}
};
Loading

0 comments on commit 79fa45d

Please sign in to comment.