Skip to content

Commit e20f38c

Browse files
authored
Merge pull request #999 from jason-fox/feature/lean
Update MongoDB to use lean()
2 parents a0177db + 39c7874 commit e20f38c

File tree

3 files changed

+83
-97
lines changed

3 files changed

+83
-97
lines changed

CHANGES_NEXT_RELEASE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
- Add: expose getConfigurationSilently to enable retrieve a configuration without raise a mongo alarm.
22
- Add: db uri and options in mongo connection log INFO trace
33
- Set Nodejs 12 as minimum version in packages.json (effectively removing Nodev10 from supported versions)
4+
- Update MongoDB to use lean() for faster retrieval

lib/services/devices/deviceRegistryMongoDB.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -188,26 +188,11 @@ function listDevices(type, service, subservice, limit, offset, callback) {
188188
});
189189
}
190190

191-
/**
192-
* Internal function used to find a device in the DB.
193-
*
194-
* @param {String} id ID of the Device to find.
195-
* @param {String} service Service the device belongs to (optional).
196-
* @param {String} subservice Division inside the service (optional).
197-
*/
198-
function getDeviceById(id, service, subservice, callback) {
199-
const queryParams = {
200-
id,
201-
service,
202-
subservice
203-
};
204-
context = fillService(context, queryParams);
205-
logger.debug(context, 'Looking for device with id [%s].', id);
206-
191+
function findOneInMongoDB(queryParams, id, callback) {
207192
const query = Device.model.findOne(queryParams);
208193
query.select({ __v: 0 });
209194

210-
query.exec(function handleGet(error, data) {
195+
query.lean().exec(function handleGet(error, data) {
211196
if (error) {
212197
logger.debug(context, 'Internal MongoDB Error getting device: %s', error);
213198

@@ -224,6 +209,24 @@ function getDeviceById(id, service, subservice, callback) {
224209
});
225210
}
226211

212+
/**
213+
* Internal function used to find a device in the DB.
214+
*
215+
* @param {String} id ID of the Device to find.
216+
* @param {String} service Service the device belongs to (optional).
217+
* @param {String} subservice Division inside the service (optional).
218+
*/
219+
function getDeviceById(id, service, subservice, callback) {
220+
const queryParams = {
221+
id,
222+
service,
223+
subservice
224+
};
225+
context = fillService(context, queryParams);
226+
logger.debug(context, 'Looking for device with id [%s].', id);
227+
findOneInMongoDB(queryParams, id, callback);
228+
}
229+
227230
/**
228231
* Retrieves a device using it ID, converting it to a plain Object before calling the callback.
229232
*
@@ -236,7 +239,7 @@ function getDevice(id, service, subservice, callback) {
236239
if (error) {
237240
callback(error);
238241
} else {
239-
callback(null, data.toObject());
242+
callback(null, data);
240243
}
241244
});
242245
}
@@ -253,13 +256,13 @@ function getByName(name, service, servicepath, callback) {
253256

254257
query.select({ __v: 0 });
255258

256-
query.exec(function handleGet(error, data) {
259+
query.lean().exec(function handleGet(error, data) {
257260
if (error) {
258261
logger.debug(context, 'Internal MongoDB Error getting device: %s', error);
259262

260263
callback(new errors.InternalDbError(error));
261264
} else if (data) {
262-
callback(null, data.toObject());
265+
callback(null, data);
263266
} else {
264267
logger.debug(context, 'Device [%s] not found.', name);
265268

@@ -293,7 +296,9 @@ function update(device, callback) {
293296
data.explicitAttrs = device.explicitAttrs;
294297
data.ngsiVersion = device.ngsiVersion;
295298

296-
data.save(saveDeviceHandler(callback));
299+
/* eslint-disable-next-line new-cap */
300+
const deviceObj = new Device.model(data);
301+
deviceObj.save(saveDeviceHandler(callback));
297302
}
298303
});
299304
}

lib/services/groups/groupRegistryMongoDB.js

Lines changed: 56 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ let context = {
3636
op: 'IoTAgentNGSI.MongoDBGroupRegister'
3737
};
3838

39+
const attributeList = [
40+
'url',
41+
'resource',
42+
'apikey',
43+
'type',
44+
'service',
45+
'subservice',
46+
'description',
47+
'trust',
48+
'cbHost',
49+
'timezone',
50+
'timestamp',
51+
'commands',
52+
'lazy',
53+
'attributes',
54+
'staticAttributes',
55+
'internalAttributes',
56+
'autoprovision',
57+
'explicitAttrs',
58+
'expressionLanguage',
59+
'defaultEntityNameConjunction',
60+
'ngsiVersion'
61+
];
62+
3963
/**
4064
* Generates a handler for the save device group operations. The handler will take the customary error and the saved
4165
* device group as the parameters (and pass the serialized DAO as the callback value).
@@ -58,33 +82,10 @@ function saveGroupHandler(groupDAO, callback) {
5882
function createGroup(group, callback) {
5983
/* eslint-disable-next-line new-cap */
6084
const groupObj = new Group.model();
61-
const attributeList = [
62-
'url',
63-
'resource',
64-
'apikey',
65-
'type',
66-
'service',
67-
'subservice',
68-
'description',
69-
'trust',
70-
'cbHost',
71-
'timezone',
72-
'timestamp',
73-
'commands',
74-
'lazy',
75-
'attributes',
76-
'staticAttributes',
77-
'internalAttributes',
78-
'autoprovision',
79-
'explicitAttrs',
80-
'expressionLanguage',
81-
'defaultEntityNameConjunction',
82-
'ngsiVersion'
83-
];
84-
85-
for (let i = 0; i < attributeList.length; i++) {
86-
groupObj[attributeList[i]] = group[attributeList[i]];
87-
}
85+
86+
attributeList.forEach((key) => {
87+
groupObj[key] = group[key];
88+
});
8889

8990
logger.debug(
9091
context,
@@ -163,7 +164,7 @@ function getById(id, callback) {
163164
const query = Group.model.findOne({ _id: id });
164165
query.select({ __v: 0 });
165166

166-
query.exec(function handleGet(error, data) {
167+
query.lean().exec(function handleGet(error, data) {
167168
if (error) {
168169
logger.debug(context, 'Internal MongoDB Error getting group: %s', error);
169170

@@ -210,6 +211,24 @@ function find(service, subservice, callback) {
210211
});
211212
}
212213

214+
function findOneInMongoDB(queryObj, fields, callback) {
215+
const query = Group.model.findOne(queryObj);
216+
query.select({ __v: 0 });
217+
query.lean().exec(function handleGet(error, data) {
218+
if (error) {
219+
logger.debug(context, 'Internal MongoDB Error getting group: %s', error);
220+
callback(new errors.InternalDbError(error));
221+
} else if (data) {
222+
context = fillService(context, data);
223+
logger.debug(context, 'Device group data found: %j', data);
224+
callback(null, data);
225+
} else {
226+
logger.debug(context, 'Device group for fields [%j] not found: [%j]', fields, queryObj);
227+
callback(new errors.DeviceGroupNotFound(fields, queryObj));
228+
}
229+
});
230+
}
231+
213232
function findBy(fields) {
214233
return function () {
215234
const queryObj = {};
@@ -228,24 +247,7 @@ function findBy(fields) {
228247

229248
context = fillService(context, { service: 'n/a', subservice: 'n/a' });
230249
logger.debug(context, 'Looking for group params %j with queryObj %j', fields, queryObj);
231-
const query = Group.model.findOne(queryObj);
232-
233-
query.select({ __v: 0 });
234-
235-
query.exec(function handleGet(error, data) {
236-
if (error) {
237-
logger.debug(context, 'Internal MongoDB Error getting group: %s', error);
238-
callback(new errors.InternalDbError(error));
239-
} else if (data) {
240-
context = fillService(context, data);
241-
logger.debug(context, 'Device group data found: %j', data.toObject());
242-
callback(null, data.toObject());
243-
} else {
244-
logger.debug(context, 'Device group for fields [%j] not found: [%j]', fields, queryObj);
245-
246-
callback(new errors.DeviceGroupNotFound(fields, queryObj));
247-
}
248-
});
250+
findOneInMongoDB(queryObj, fields, callback);
249251
};
250252
}
251253

@@ -255,36 +257,15 @@ function update(id, body, callback) {
255257
if (error) {
256258
callback(error);
257259
} else {
258-
const attributes = [
259-
'url',
260-
'apikey',
261-
'type',
262-
'service',
263-
'subservice',
264-
'description',
265-
'trust',
266-
'cbHost',
267-
'timezone',
268-
'timestamp',
269-
'commands',
270-
'lazy',
271-
'attributes',
272-
'staticAttributes',
273-
'internalAttributes',
274-
'explicitAttrs',
275-
'expressionLanguage',
276-
'defaultEntityNameConjunction',
277-
'ngsiVersion'
278-
];
279-
280-
for (let i = 0; i < attributes.length; i++) {
281-
if (body[attributes[i]] !== undefined) {
282-
group[attributes[i]] = body[attributes[i]];
260+
attributeList.forEach((key) => {
261+
if (body[key] !== undefined) {
262+
group[key] = body[key];
283263
}
284-
}
285-
286-
group.isNew = false;
287-
group.save(saveGroupHandler(group, callback));
264+
});
265+
/* eslint-disable-next-line new-cap */
266+
const groupObj = new Group.model(group);
267+
groupObj.isNew = false;
268+
groupObj.save(saveGroupHandler(groupObj, callback));
288269
}
289270
});
290271
}
@@ -303,7 +284,6 @@ function remove(id, callback) {
303284
callback(new errors.InternalDbError(error));
304285
} else {
305286
logger.debug(context, 'Device [%s] successfully removed.', id);
306-
307287
callback(null, deviceGroup);
308288
}
309289
});

0 commit comments

Comments
 (0)