-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
84 lines (76 loc) · 2.38 KB
/
Copy pathstorage.js
File metadata and controls
84 lines (76 loc) · 2.38 KB
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
var mongo = require('mongodb').MongoClient
var args = process.argv.slice(2);
var storage;
var storageMethod;
if (args.length == 4) {
var mongoHost = args[2];
var mongoPort = args[3];
var url = 'mongodb://' + mongoHost + ':' + mongoPort + '/quorumreplicastorage';
storageMethod = 'mongodb';
mongo.connect(url, function(err, db) {
if (err != null) {
console.log('Error connecting to MongoDB');
process.exit(1);
}
storage = db.collection('keyvalues');
});
} else {
storageMethod = 'memory';
storage = {};
}
function readValue(body, onValueRead) {
if (storageMethod == 'mongodb') {
storage.findOne({'key' : body['key']}, function(err, result) {
onValueRead(result['value']);
});
} else if (storageMethod == 'memory') {
var value = body['key'] in storage ? storage[body['key']]['value'] : null;
onValueRead(value);
}
}
function writeValue(storageValue, onValueWrite, onValueWriteFail) {
var key = storageValue['key'];
var value = storageValue['value'];
var timestamp = storageValue['timestamp'];
if (storageMethod == 'mongodb') {
storage.update({'_id' : key, 'timestamp' : {$lt : timestamp}},
{$set : storageValue}, {upsert : true},
function(err, result) {
if (err == null) {
onValueWrite();
} else {
onValueWriteFail();
}
});
} else if (storageMethod == 'memory') {
if (!(key in storage) || key in storage && storage[key]['timestamp'] < timestamp) {
storage[key] = {
'value' : value,
'timestamp' : timestamp
};
}
onValueWrite();
}
}
exports.read = function read(req, res, next) {
var body = req.body;
readValue(body, function(value) {
res.status(200).send({'value' : value});
});
};
exports.write = function write(req, res, next) {
var body = req.body;
var key = body['key'];
var value = body['value'];
var timestamp = body['timestamp'];
var storageValue = {
'key' : key,
'value' : value,
'timestamp' : timestamp
};
writeValue(storageValue, function() {
res.status(200).send({status : 'success'});
}, function() {
res.status(200).send({status : 'fail'});
});
};