This repository has been archived by the owner on Aug 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmetaparticle-file-storage.js
119 lines (112 loc) · 3.6 KB
/
metaparticle-file-storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* File based storage layer for metaparticle. Not for use in production, test/experiment only!
*/
(function () {
var q = require('q');
var log = require('loglevel');
var file = null;
var fs = function () {
if (file == null) {
file = require('fs');
}
return file;
}
/**
* A note on the 'data' package:
* This library expects to store and load objects of the form:
* {
* 'data': <some-js-object>,
* 'version': <string>
* }
*
* Where 'version' is a string that is used for optimistic concurrency.
*/
/**
* Load a particular data scope and return it
* @param {string} scope The scope to load
* @returns A promise for data for that scope
*/
module.exports.load = function (scope) {
var deferred = q.defer();
var path = scope + ".json";
// TODO: make this async too
var stats = null;
try {
stats = fs().statSync(path);
} catch (err) {
if (err.code != 'ENOENT') {
deferred.reject(err);
return deferred.promise;
}
}
fs().readFile(path, 'utf-8', function (err, data) {
if (err) {
if (err.code == 'ENOENT') {
deferred.resolve({
'data': {},
'version': 'empty'
});
} else {
deferred.reject(err);
}
} else {
try {
var obj = JSON.parse(data);
deferred.resolve({
'data': obj,
'version': stats.mtime,
});
} catch (ex) {
deferred.reject(ex);
}
}
});
return deferred.promise;
}
/**
* Store the data to persistent storage.
* @param {string} scope The scope to store
* @param {data} data The data package
* @returns A promise that resolves to true if the storage succeeded, false otherwise.
*/
module.exports.store = function (scope, data) {
var deferred = q.defer();
// TODO: make this async too
var stats = null;
var empty = false;
var path = scope + '.json';
try {
stats = fs().statSync(path);
} catch (err) {
if (err.code != 'ENOENT') {
deferred.reject(err);
return deferred.promise;
} else {
empty = true;
}
}
if (empty) {
if (data.version != 'empty') {
log.warn('version is not empty but file does not exist');
deferred.resolve(false);
}
} else {
// for some reason straight object compare wasn't working, so stringify then compare
var versionStr = JSON.stringify(data.version);
var statStr = JSON.stringify(stats.mtime);
if (versionStr != statStr) {
log.warn('versions do not match: %' + data.version + '% vs %' + stats.mtime + '%');
deferred.resolve(false);
}
}
var str = JSON.stringify(data.data);
fs().writeFile(scope + ".json", str, function (err) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(true);
}
});
return deferred.promise;
}
} ());