-
Notifications
You must be signed in to change notification settings - Fork 4
/
peerservice.js
181 lines (141 loc) · 4.37 KB
/
peerservice.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// AppNet Peer-Service store pure JS implementation
// Copyright (c) 2012-present Tom Zhou<[email protected]>
// Notes: a peer-service MUST be described by domain, usrkey and category, and identified by vURL.
'use strict';
var debug = require('debug')('peerservice');
var eventEmitter = require('events').EventEmitter,
util = require('util');
// Debug level
var Debug = 0;
// pass key-value db instance
var peerService = module.exports = exports = function(db){
var self = this;
// super constructor
eventEmitter.call(self);
self.db = db || {};
}
util.inherits(peerService, eventEmitter);
// instance method: _genIndex, _parseKeys
// index like: domain:usrkey:cate:
peerService.prototype._genIndex = function(keys){
var indx = '';
if (keys && keys.domain)
indx += keys.domain + ':';
else
indx += 'any:';
if (keys && keys.usrkey)
indx += keys.usrkey + ':';
else
indx += 'any:';
if (keys && keys.cate)
indx += keys.cate + ':';
else
indx += 'any:';
return indx;
};
peerService.prototype._parseKeys = function(index){
var strs = index.split(':');
var keys = {};
if (strs[0] === 'any') {
keys.domain = null;
} else {
keys.domain = strs[0];
}
if (strs[1] === 'any') {
keys.usrkey = null;
} else {
keys.usrkey = strs[1];
}
if (strs[2] === 'any') {
keys.cate = null;
} else {
keys.cate = strs[2];
}
return keys;
};
// instance method: get/put/del
peerService.prototype.get = function(entry, fn){
var self = this;
var indx = self._genIndex(entry);
// TBD... wildcards query
if (indx.match('any:')) {
fn('invalid get service keys');
return;
}
function matchEntry(obj, select){
var yes = true;
// TBD... select full fields
['cate', 'domain', 'usrkey', 'live'].forEach(function(k){
if (!(obj[k] === select[k])) yes = false;
});
return yes;
}
if ((indx in self.db) && self.db[indx]) {
var rslt = {};
// filter selected fields
Object.keys(self.db[indx]).forEach(function(k){
if (matchEntry((self.db[indx])[k], entry)) rslt[k] = (self.db[indx])[k];
});
fn(null, rslt);
if (Debug) console.log('find service:'+JSON.stringify(rslt));
} else {
fn('invalid service keys');
}
};
peerService.prototype.put = function(entry, fn){
var self = this;
var indx = self._genIndex(entry);
// TBD... wildcards query
if (indx.match('any:') || !entry.vurl) {
if (fn) fn('invalid put service keys');
return;
}
self.db[indx] = self.db[indx] || {};
(self.db[indx])[entry.vurl] = (self.db[indx])[entry.vurl] || {};
// filter selected fields
Object.keys(entry).forEach(function(k){
((self.db[indx])[entry.vurl])[k] = entry[k];
});
if (fn) fn(null, (self.db[indx])[entry.vurl]);
if (Debug) console.log('add peer service:' + JSON.stringify(entry));
// emit add for cluster
self.emit('add', entry);
}
peerService.prototype.del = function(entry, fn){
var self = this;
var indx = self._genIndex(entry);
// TBD... wildcards query
if (indx.match('any:')) {
if (fn) fn('invalid del service keys');
return;
}
if (indx in self.db) {
if (entry.vurl) {
if (entry.vurl in self.db[indx])
(self.db[indx])[entry.vurl] = null;
} else {
self.db[indx] = null;
}
if (fn) fn(null);
if (Debug) console.log('delete peer service:' + entry);
// emit delete for cluster
self.emit('delete', entry);
} else {
if (fn) fn(null);
}
}
// clear by vURL
peerService.prototype.clrByvURL = function(vurl, fn){
var self = this;
Object.keys(self.db).forEach(function(k){
if (self.db[k] && (typeof self.db[k] === 'object'))
Object.keys(self.db[k]).forEach(function(kk){
// set live flag as false
if (kk.match(vurl))
((self.db[k])[kk])['live'] = false;
});
});
if (fn) fn(null);
// emit clear for cluster
self.emit('clear', vurl);
};