Skip to content

Commit

Permalink
Update FsProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
SemenchenkoVitaliy committed Jan 28, 2019
1 parent 81366e4 commit d03ddbd
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 73 deletions.
106 changes: 72 additions & 34 deletions lib/fs.provider.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'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');

Expand All @@ -12,43 +12,67 @@ const { FsCursor } = require('./fs.cursor');
const THROTTLE_TIMEOUT = 5000;

class FsProvider extends StorageProvider {
constructor(
options // { path } where path is database location
) {

// FsProvider constructor
// options - <Object>
// path - <string>, database location
constructor(options) {
super(options);
this.path = options.path;
this.stat = null;
this.storage = null;
this.dbOptions = {};
}

// Read storage stats
// callback - <Function>
// err - <Error>
// data - <Object>, database stats
readStat(callback) {
fs.readFile(this.path + '/.gs', (err, stat) => {
fs.readFile(this.path + '/.gs', 'utf8', (err, stat) => {
if (err) {
callback(err);
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;
callback(null, data);
});
}

// Open Database
// options - <Object>
// path - <string>
// minCompressSize - <number>
// checksum - <string>, checksum type
// dedupHash - <string>, second checksum type
// callback - <Function>
// err - <Error>
open(options, callback) {
callback = common.once(callback);
this.path = options.path;
this.dbOptions.checksum = options.checksum;
this.dbOptions.dedupHash = options.dedupHash;
super.open(options, () => {
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();
});
});
}

// Write database stats
// callback - <Function>
// err - <Error>
writeStat(callback) {
callback = common.once(callback);
const save = () => {
Expand All @@ -59,29 +83,44 @@ class FsProvider extends StorageProvider {
this.save();
}

// Close database
// callback - <Function>
close(callback) {
callback = common.once(callback);
this.writeStat();
this.active = false;
callback();
}

// Generate new id
// callback - <Function>
// err - <Error>
// id - <number>
takeId(callback) {
callback = common.once(callback);
callback(null, this.stat.next++);
this.stat.count++;
this.writeStat();
}

// Get object from database
// id - <number>
// callback - <Function>
// err - <Error>
// data - <Object>
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 new object in database
// obj - <Object>
// callback - <Function>
// err - <Error>
// id - <number>
create(obj, callback) {
callback = common.once(callback);
this.takeId((err, id) => {
Expand All @@ -91,57 +130,56 @@ 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);
});
});
}

// Create new object in database
// obj - <Object>
// Id - <number>
// callback - <Function>
// err - <Error>
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.stat.size += stats.size - stats.originalSize;
this.writeStat();
fs.writeFile(path, data, err => {
if (err) callback(err);
else callback();
});
callback();
});
}

// Get object from database
// id - <number>
// callback - <Function>
// err - <Error>
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.stat.size -= stats.size;
this.writeStat();
callback();
});
Expand Down
Loading

0 comments on commit d03ddbd

Please sign in to comment.