From 2fe3c88075f475e47d2a525dbc840abb7651d4b1 Mon Sep 17 00:00:00 2001 From: Vitaliy Semenchenko Date: Mon, 28 Jan 2019 12:04:09 +0200 Subject: [PATCH] Update FsProvider PR-URL: https://github.com/metarhia/globalstorage/pull/310 --- lib/fs.provider.js | 147 ++++++++++++++++++++++--------------- package-lock.json | 179 ++++++++++++++++++++++++++++++++++++++++----- package.json | 4 +- test/fs.js | 130 ++++++++++++++++++++------------ 4 files changed, 333 insertions(+), 127 deletions(-) diff --git a/lib/fs.provider.js b/lib/fs.provider.js index 0ca8d4a4..b6f1f1a4 100644 --- a/lib/fs.provider.js +++ b/lib/fs.provider.js @@ -1,31 +1,40 @@ 'use strict'; const fs = require('fs'); -const mkdirp = require('mkdirp'); const common = require('@metarhia/common'); +const { FileStorage } = require('@metarhia/filestorage'); const metasync = require('metasync'); const mdsf = require('mdsf'); const { StorageProvider } = require('./provider'); const { FsCursor } = require('./fs.cursor'); +const readStat = Symbol('readStat'); +const writeStat = Symbol('writeStat'); + const THROTTLE_TIMEOUT = 5000; class FsProvider extends StorageProvider { - constructor( - options // { path } where path is database location - ) { + // Create FsProvider + // options + constructor(options) { super(options); this.stat = null; + this.storage = null; + this.dbOptions = {}; } - readStat(callback) { - fs.readFile(this.path + '/.gs', (err, stat) => { + // Read storage stats + // callback - + // err - + // data - , database stats + [readStat](callback) { + fs.readFile(this.path + '/.gs', 'utf8', (err, stat) => { if (err) { - callback(err); + callback(err, this); return; } - const data = mdsf.parse(stat.toString()); + const data = mdsf.parse(stat); data.count = data.count || 0; data.size = data.size || 0; data.next = data.next || 1; @@ -33,57 +42,83 @@ class FsProvider extends StorageProvider { }); } - open(options, callback) { + // Write storage stats + // callback - + // err - + [writeStat](callback) { callback = common.once(callback); + const save = () => { + const data = mdsf.stringify(this.stat); + fs.writeFile(this.path + '/.gs', data, callback); + }; + this.save = this.save || metasync.throttle(THROTTLE_TIMEOUT, save); + this.save(); + } + + // Open FsProvider + // options - + // path - + // minCompressSize - + // checksum - , checksum type + // dedupHash - , second checksum type + // callback - + // err - + open(options, callback) { this.path = options.path; + this.dbOptions.checksum = options.checksum; + this.dbOptions.dedupHash = options.dedupHash; super.open(options, () => { - this.readStat((err, stat) => { + this[readStat]((err, stat) => { if (err) { callback(new Error('Can not open database: ' + this.path)); - } else { - this.stat = stat; - this.active = true; - callback(); + return; } + this.stat = stat; + this.active = true; + this.storage = new FileStorage( + Object.assign({ dir: options.path }, options) + ); + callback(null, this); }); }); } - writeStat(callback) { - callback = common.once(callback); - const save = () => { - const data = mdsf.stringify(this.stat); - fs.writeFile(this.path + '/.gs', data, callback); - }; - this.save = this.save || metasync.throttle(THROTTLE_TIMEOUT, save); - this.save(); - } - + // Close FsProvider + // callback - close(callback) { - callback = common.once(callback); - this.writeStat(); + this[writeStat](); this.active = false; callback(); } + // Generate unique id + // callback - + // err - + // id - takeId(callback) { - callback = common.once(callback); callback(null, this.stat.next++); this.stat.count++; - this.writeStat(); + this[writeStat](); } + // Get record from GlobalStorage + // id - + // callback - + // err - + // data - get(id, callback) { - callback = common.once(callback); - const path = common.idToPath(id); - fs.readFile(path, (err, data) => { + this.storage.read(id, this.dbOptions, (err, data) => { if (err) callback(err); else callback(null, mdsf.parse(data.toString())); }); } + // Create record in GlobalStorage + // obj - + // callback - + // err - + // id - create(obj, callback) { - callback = common.once(callback); this.takeId((err, id) => { if (err) { callback(err); @@ -91,58 +126,54 @@ class FsProvider extends StorageProvider { } obj.Id = id; const data = mdsf.stringify(obj); - this.stat.size += data.length; - this.writeStat(); - const place = common.idToChunks(obj.Id); - place.path = this.path + place.path; - mkdirp(place.path, err => { + this.storage.write(id, data, this.dbOptions, (err, stats) => { if (err) { callback(err); return; } - const pp = place.path + '/' + place.name; - fs.writeFile(pp, data, err => { - if (err) callback(err); - else callback(null, id); - }); + this.stat.size += stats.size; + this[writeStat](); + callback(null, id); }); }); } + // Update record in GlobalStorage + // obj - + // Id - + // callback - + // err - update(obj, callback) { - callback = common.once(callback); - const path = common.idToPath(obj.Id); - fs.stat(path, (err, stat) => { + const data = mdsf.stringify(obj); + this.storage.update(obj.Id, data, this.dbOptions, (err, stats) => { if (err) { callback(err); return; } - const data = mdsf.stringify(obj); - this.stat.size += data.length - stat.size; - this.writeStat(); - fs.writeFile(path, data, err => { - if (err) callback(err); - else callback(); - }); + this.stat.size += stats.size - stats.originalSize; + this[writeStat](); + callback(); }); } + // Delete record from GlobalStorage + // id - + // callback - + // err - delete(id, callback) { - callback = common.once(callback); - const path = common.idToPath(id); - fs.stat(path, (err, stat) => { + this.storage.stat(id, (err, stats) => { if (err) { callback(err); return; } - fs.unlink(path, err => { + this.storage.rm(id, err => { if (err) { callback(err); return; } this.stat.count--; - this.stat.size -= stat.size; - this.writeStat(); + this.stat.size -= stats.size; + this[writeStat](); callback(); }); }); diff --git a/package-lock.json b/package-lock.json index 5d5a0a77..84d54291 100644 --- a/package-lock.json +++ b/package-lock.json @@ -807,6 +807,16 @@ "resolved": "https://registry.npmjs.org/@metarhia/common/-/common-1.3.1.tgz", "integrity": "sha512-Pb08U7DK3Eo68jTs5SMMw92luWfzpPq3yKuaxUI4f08lMfxYybX0wHecq9cnAzxfOECrAfPCaHy1fh//xkslHw==" }, + "@metarhia/filestorage": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/@metarhia/filestorage/-/filestorage-0.1.0.tgz", + "integrity": "sha512-hizjpsZML6iAtZ5WVhdLqQdMIqaKa/MDbPO6ACf81nXaHGeA+aKbVqAKhIPtkDdIbMWSICDziSQY90o3BuDUWg==", + "requires": { + "@metarhia/common": "^1.2.1", + "compressing": "^1.4.0", + "crc": "^3.8.0" + } + }, "@metarhia/jstp": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/@metarhia/jstp/-/jstp-2.2.1.tgz", @@ -1007,6 +1017,11 @@ } } }, + "base64-js": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.0.tgz", + "integrity": "sha512-ccav/yGvoa80BQDljCxsmmQ3Xvx60/UpBIij5QN21W3wBi/hhIC9OoO+KLpu9IJTS9j4DRVJ3aDDF9cMSoa2lw==" + }, "binary-extensions": { "version": "1.12.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-1.12.0.tgz", @@ -1014,6 +1029,15 @@ "dev": true, "optional": true }, + "bl": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/bl/-/bl-1.2.2.tgz", + "integrity": "sha512-e8tQYnZodmebYDWGH7KMRvtzKXaJHx3BbilrgZCfvyLUYdKpK1t5PSPmpkny/SgiTSCnjfLW7v5rlONXVFkQEA==", + "requires": { + "readable-stream": "^2.3.5", + "safe-buffer": "^5.1.1" + } + }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -1066,6 +1090,39 @@ "node-releases": "^1.1.11" } }, + "buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.2.1.tgz", + "integrity": "sha512-c+Ko0loDaFfuPWiL02ls9Xd3GO3cPVmUobQ6t3rXNUk304u6hGq+8N/kFi+QEIKhzK3uwolVhLzszmfLmMLnqg==", + "requires": { + "base64-js": "^1.0.2", + "ieee754": "^1.1.4" + } + }, + "buffer-alloc": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/buffer-alloc/-/buffer-alloc-1.2.0.tgz", + "integrity": "sha512-CFsHQgjtW1UChdXgbyJGtnm+O/uLQeZdtbDo8mfUgYXCHSM1wgrVxXm6bSyrUuErEb+4sYVGCzASBRot7zyrow==", + "requires": { + "buffer-alloc-unsafe": "^1.1.0", + "buffer-fill": "^1.0.0" + } + }, + "buffer-alloc-unsafe": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/buffer-alloc-unsafe/-/buffer-alloc-unsafe-1.1.0.tgz", + "integrity": "sha512-TEM2iMIEQdJ2yjPJoSIsldnleVaAk1oW3DBVUykyOLsEsFmEc9kn+SFFPz+gl54KQNxlDnAwCXosOS9Okx2xAg==" + }, + "buffer-crc32": { + "version": "0.2.13", + "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", + "integrity": "sha1-DTM+PwDqxQqhRUq9MO+MKl2ackI=" + }, + "buffer-fill": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/buffer-fill/-/buffer-fill-1.0.0.tgz", + "integrity": "sha1-+PeLdniYiO858gXNY39o5wISKyw=" + }, "buffer-writer": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", @@ -1245,6 +1302,21 @@ "dev": true, "optional": true }, + "compressing": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/compressing/-/compressing-1.4.0.tgz", + "integrity": "sha512-7Y6aU45Ud0y7sxr8szQ91dJPHIENKmG0zD9bcLSZSGf67Xap/J8pqFGCklnrFavkl5taulZb7YvSa557mb732A==", + "requires": { + "flushwritable": "^1.0.0", + "get-ready": "^1.0.0", + "mkdirp": "^0.5.1", + "pump": "^3.0.0", + "streamifier": "^0.1.1", + "tar-stream": "^1.5.2", + "yauzl": "^2.7.0", + "yazl": "^2.4.2" + } + }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -1302,9 +1374,15 @@ "core-util-is": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", - "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", - "dev": true, - "optional": true + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, + "crc": { + "version": "3.8.0", + "resolved": "https://registry.npmjs.org/crc/-/crc-3.8.0.tgz", + "integrity": "sha512-iX3mfgcTMIq3ZKLIsVFAbv7+Mc10kxabAGQb8HvjA1o3T1PIYprbakQ65d3I+2HGHt6nSKkM9PYjgoJO2KcFBQ==", + "requires": { + "buffer": "^5.1.0" + } }, "cross-spawn": { "version": "6.0.5", @@ -1842,6 +1920,14 @@ "integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=", "dev": true }, + "fd-slicer": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz", + "integrity": "sha1-JcfInLH5B3+IkbvmHY85Dq4lbx4=", + "requires": { + "pend": "~1.2.0" + } + }, "figures": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/figures/-/figures-2.0.0.tgz", @@ -1921,6 +2007,11 @@ "integrity": "sha512-R+H8IZclI8AAkSBRQJLVOsxwAoHd6WC40b4QTNWIjzAa6BXOBfQcM587MXDTVPeYaopFNWHUFLx7eNmHDSxMWg==", "dev": true }, + "flushwritable": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/flushwritable/-/flushwritable-1.0.0.tgz", + "integrity": "sha1-PjKNj95BKtR+c44751C00pAENJg=" + }, "for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", @@ -1938,6 +2029,11 @@ "map-cache": "^0.2.2" } }, + "fs-constants": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", + "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==" + }, "fs-readdir-recursive": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/fs-readdir-recursive/-/fs-readdir-recursive-1.1.0.tgz", @@ -2515,6 +2611,11 @@ "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.3.tgz", "integrity": "sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w==" }, + "get-ready": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/get-ready/-/get-ready-1.0.0.tgz", + "integrity": "sha1-+RgX8emt7P6hOlYq38jeiDqzR4I=" + }, "get-stdin": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-6.0.0.tgz", @@ -2650,6 +2751,11 @@ "safer-buffer": ">= 2.1.2 < 3" } }, + "ieee754": { + "version": "1.1.13", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz", + "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" + }, "ignore": { "version": "4.0.6", "resolved": "https://registry.npmjs.org/ignore/-/ignore-4.0.6.tgz", @@ -2685,8 +2791,7 @@ "inherits": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", - "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", - "dev": true + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" }, "inquirer": { "version": "6.2.2", @@ -2932,8 +3037,7 @@ "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", - "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", - "dev": true + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" }, "isexe": { "version": "2.0.0", @@ -3535,6 +3639,11 @@ "pify": "^2.0.0" } }, + "pend": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", + "integrity": "sha1-elfrVQpng/kRUzH89GY9XI4AelA=" + }, "pg": { "version": "7.9.0", "resolved": "https://registry.npmjs.org/pg/-/pg-7.9.0.tgz", @@ -3711,9 +3820,7 @@ "process-nextick-args": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz", - "integrity": "sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==", - "dev": true, - "optional": true + "integrity": "sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==" }, "progress": { "version": "2.0.3", @@ -3806,8 +3913,6 @@ "version": "2.3.6", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", - "dev": true, - "optional": true, "requires": { "core-util-is": "~1.0.0", "inherits": "~2.0.3", @@ -4011,8 +4116,7 @@ "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", - "dev": true + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" }, "safe-regex": { "version": "1.1.0", @@ -4323,6 +4427,11 @@ } } }, + "streamifier": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/streamifier/-/streamifier-0.1.1.tgz", + "integrity": "sha1-l+mNj6TRBdYqJpHR3AfoINuN/E8=" + }, "string-width": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", @@ -4336,8 +4445,6 @@ "version": "1.1.1", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", - "dev": true, - "optional": true, "requires": { "safe-buffer": "~5.1.0" } @@ -4475,6 +4582,20 @@ } } }, + "tar-stream": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-1.6.2.tgz", + "integrity": "sha512-rzS0heiNf8Xn7/mpdSVVSMAWAoy9bfb1WOTYC78Z0UQKeKa/CWS8FOq0lKGNa8DWKAn9gxjCvMLYc5PGXYlK2A==", + "requires": { + "bl": "^1.0.0", + "buffer-alloc": "^1.2.0", + "end-of-stream": "^1.0.0", + "fs-constants": "^1.0.0", + "readable-stream": "^2.3.0", + "to-buffer": "^1.1.1", + "xtend": "^4.0.0" + } + }, "text-table": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz", @@ -4495,6 +4616,11 @@ "os-tmpdir": "~1.0.2" } }, + "to-buffer": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/to-buffer/-/to-buffer-1.1.1.tgz", + "integrity": "sha512-lx9B5iv7msuFYE3dytT+KE5tap+rNYw+K4jVkb9R/asAb+pbBSM17jtunHplhBe6RRJdZx3Pn2Jph24O32mOVg==" + }, "to-fast-properties": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-2.0.0.tgz", @@ -4752,9 +4878,7 @@ "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", - "dev": true, - "optional": true + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" }, "uuid": { "version": "3.3.2", @@ -4914,6 +5038,23 @@ "camelcase": "^5.0.0", "decamelize": "^1.2.0" } + }, + "yauzl": { + "version": "2.10.0", + "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", + "integrity": "sha1-x+sXyT4RLLEIb6bY5R+wZnt5pfk=", + "requires": { + "buffer-crc32": "~0.2.3", + "fd-slicer": "~1.1.0" + } + }, + "yazl": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/yazl/-/yazl-2.5.1.tgz", + "integrity": "sha512-phENi2PLiHnHb6QBVot+dJnaAZ0xosj7p3fWl+znIjBDlnMI2PsZCJZ306BPTFOaHf5qdDEI8x5qFrSOBN5vrw==", + "requires": { + "buffer-crc32": "~0.2.3" + } } } } diff --git a/package.json b/package.json index 04d8582a..b6987ccf 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ }, "dependencies": { "@metarhia/common": "^1.3.1", + "@metarhia/filestorage": "^0.1.0", "@metarhia/jstp": "^2.2.1", "@metarhia/sql": "^0.1.0", "mdsf": "^1.1.1", @@ -51,9 +52,6 @@ "metasync": "^0.3.31", "pg": "^7.9.0" }, - "optionalDependencies": { - "mkdirp": "^0.5.1" - }, "devDependencies": { "@babel/cli": "^7.2.3", "@babel/core": "^7.4.0", diff --git a/test/fs.js b/test/fs.js index cef73e7b..52531bb8 100644 --- a/test/fs.js +++ b/test/fs.js @@ -1,65 +1,101 @@ 'use strict'; const gs = require('..'); + +const fs = require('fs'); + const metasync = require('metasync'); const metatests = require('metatests'); +const { Uint64 } = require('@metarhia/common'); -module.exports = (data, done) => { - metatests.test('fs test', test => { - const queue = metasync.queue(100, 100); +const provider = gs('fs', { + serverSuffix: new Uint64(0x4000000), + serverBitmask: new Uint64(0x7ffffff), +}); - const processItem = (item, callback) => { - gs.create(item, () => { - item.Name = item.Id % 2 ? 'Marcus' : 'Aurelius'; - gs.update(item, () => { - if (item.Id % 3) { - gs.delete(item.Id, callback); - } else { - callback(); +const fsTest = metatests.test('FsProvider'); + +fsTest.beforeEach((test, cb) => { + provider.open({ path: './data' }, err => { + if (err) { + test.error(err, 'Cannot open database'); + test.end(); + return; + } + cb(); + }); +}); + +fsTest.afterEach((test, cb) => + provider.close(() => + setTimeout( + () => + fs.writeFile( + './data/.gs', + `{server:'0',count:0,size:0,next:0}`, + err => { + test.error(err); + cb(); } - }); - }); - }; + ), + 6000 + ) + ) +); - queue.process(processItem); +fsTest.test('fs test', test => { + const item = { Num: 123 }; + const nextId = provider.stat.next; + provider.create(item, (err, id) => { + if (err) { + test.error(err); + test.end(); + return; + } + test.strictSame(id, nextId); + test.strictSame(item.Id, nextId); + test.end(); + }); +}); - gs.open( - { - gs, - provider: 'fs', - path: './data', - }, - err => { +fsTest.test('fs test', test => { + const processItem = (item, callback) => { + provider.create(item, (err, id) => { + if (err) { + callback(err); + return; + } + item.Name = id % 2 ? 'Marcus' : 'Aurelius'; + provider.update(item, err => { if (err) { - test.error(err, 'error opening gs'); + callback(err); return; } - console.time('insert'); - for (let i = 0; i < 10; i++) { - queue.add({ Num: i }); + if (id % 3) { + provider.delete(id, callback); + } else { + callback(null); } - gs.select({ category: 'Person', Name: 'Marcus' }) - .limit(10) - .fetch((err, data) => { - console.log(err); - if (err) { - test.throws(err, 'error fetching Marcus'); - return; - } - test.strictSame([err, data], [null, []]); - test.end('select test end'); - }); - } - ); + }); + }); + }; - queue.timeout(1000, () => { + const queue = metasync + .queue(100, 100) + .process(processItem) + .timeout(1000, () => { test.notOk('Insert test error, file writing timeout'); + test.end(); + }) + .failure(err => { + test.error(err, 'insert test failed'); + test.end(); + }) + .drain(() => { + test.end(); }); - queue.drain(() => { - console.timeEnd('insert'); - test.end('insert test done'); - done(); - }); - }); -}; + for (let i = 0; i < 10; i++) { + queue.add({ Num: i }); + } +});