-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
203 lines (187 loc) · 5.02 KB
/
index.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var level = require('level');
//var uuid = require("uuid/v4");
var transaction = require('level-transactions');
var dbHandle = null;
var policies = null;
var settings_file = null;
var Promise = require('bluebird');
var console = require('./log');
module.exports = {
init: init,
close: close,
create: create,
update: update,
read: read,
del: del
};
function reInit(){
var settings = settings_file;
return new Promise(function(resolve, reject) {
if(dbHandle){
resolve();
}
else{
console.log("reinitializing policies db! ")
return init(settings);
}
});
}
/**
* Initializes the database
*
* @param settings the settings for the mongodb database.
*/
function init(settings) {
settings_file = settings;
var filename = settings.dbName;
var that = this;
//console.log("attempting to use file "+filename + "_policies");
return new Promise(function(resolve, reject) {
if(!dbHandle){
var options = {
keyEncoding: 'utf8',
valueEncoding: 'json'
};
dbHandle = level(filename + "_policies", options);
}
resolve();
});
}
function close(callback) {
return new Promise(function(resolve, reject) {
console.log("closing database object");
if (dbHandle)
dbHandle.close(function () {
dbHandle = null;
console.log("database cleaned");
return resolve();
});
else {
return reject();
}
});
}
function read(id) {
return new Promise(function(resolve, reject) {
reInit().then(function(){
var tr = transaction(dbHandle);
tr.get(id, function (error, policy) {
if (error && error.notFound){
tr.commit(function(){
resolve(null);
});
}
else if(error){
tr.rollback(function(){
reject(error);
});
}
else if(policy){
if(policy.pO){
tr.commit(function(){
resolve(policy);
});
}
else{
tr.rollback(function(){
reject(new Error("ERROR: Entry for entity '"+id+"' has invalid format."));
});
}
}
else{
tr.rollback(function(){
reject(new Error("unknown error while reading policy. error "+JSON.stringify(error)+" policy "+JSON.stringify(policy)));
});
}
});
});
});
}
function create(id, policy) {
//console.log("creating id "+id+" policy "+JSON.stringify(policy));
return new Promise(function(resolve, reject) {
reInit().then(function(){
var tr = transaction(dbHandle);
var ret= { _id: id, pO : policy, t:1};
tr.put(id,ret,function (error) {
if(error){
tr.rollback(function(){
reject(error);
});
}
else{
tr.commit(function(){
resolve(ret);
})
}
});
});
});
};
function update(id, policy, uid) {
//normally uid should be checked to match t.
return new Promise(function(resolve, reject) {
reInit().then(function(){
var tr = transaction(dbHandle);
//console.log("creating id "+id+" policy "+JSON.stringify(policy));
var ret= { _id: id, pO : policy, t:1};
tr.put(id, ret, function (error) {
if(error){
tr.rollback(function(){
reject(error);
});
}
else{
tr.commit(function(){
console.log("id: "+id+" object "+JSON.stringify(ret.pO)+ " has been stored ");
resolve(ret);
});
}
});
});
});
};
function del(id) {
return new Promise(function(resolve, reject) {
reInit().then(function(){
var tr = transaction(dbHandle);
tr.get(id, function (error, data) {
if (error && error.notFound){
tr.rollback(function(){
resolve(null);
});
}
else if(error){
tr.rollback(function(){
reject(error);
});
}
else if(!data) {
tr.rollback(function(){
resolve(null);
});
}
else{
if(data.pO){
tr.del(id, function (error) {
if(error) {
tr.rollback(function(){
reject(error);
});
} else {
tr.commit(function(){
resolve(data);
});
}
});
}
else{
tr.rollback(function(){
resolve(null);
});
}
}
});
});
});
};