i want to store this kind of information in memory all the time.
app_id admin_id url status
123 1 xyz 1
123 2 xyz 0
539 3 exmaple 1
876 4 new 1
My main concern is that i want the lookup to be easy and fast. Like at any point to query :
- get the
url
andstatus
for a particularadmin_id
. - get all
admin_id
for a particularapp_id
.
SO how do i store this information in memory.
i want to store this kind of information in memory all the time.
app_id admin_id url status
123 1 xyz. 1
123 2 xyz. 0
539 3 exmaple. 1
876 4 new. 1
My main concern is that i want the lookup to be easy and fast. Like at any point to query :
- get the
url
andstatus
for a particularadmin_id
. - get all
admin_id
for a particularapp_id
.
SO how do i store this information in memory.
Share Improve this question edited Dec 26, 2011 at 18:38 Bhesh Gurung 51k23 gold badges95 silver badges143 bronze badges asked Dec 26, 2011 at 18:19 chiragchirag 431 silver badge5 bronze badges3 Answers
Reset to default 3You could do something as follows. Create an object for each row. And keep the references in and array byAdminId
for admin-id, and an object byAppId
for app-id, the add
function does that in following. getByAppId
and getByAdminId
functions for retrieval of those objects.
var store = (function() {
var byAdminId = [];
var byAppId = {};
return {
add: function(appId, adminId, url, status) {
var d = {
appId: appId,
adminId: adminId,
url: url,
status: status
};
if (appId in byAppId) { // You could also use byAppId.hasOwnProperty(appId) here
byAppId[appId].push(d);
} else {
byAppId[appId] = [];
byAppId[appId].push(d);
}
byAdminId[adminId-1] = d;
},
getByAppId: function(appId) {
if (appId in byAppId) {
return byAppId[appId];
}
return null;
},
getByAdminId: function(adminId) {
return byAdminId[adminId-1];
}
};
})();
store.add(123, 1, "abc.", 1);
store.add(123, 9, "xyz.", 0);
store.add(539, 3, "exmaple.", 1);
store.add(876, 4, "new.", 1);
var objs = store.getByAppId(123);
for (var i = 0; i < objs.length; i++) {
// access admin id as follows
// objs[i].adminId
}
var obj = store.getByAdminId(1);
// access the url and status as follows
// obj.url
// obj.status
If you have your items in an array called a
, you could use a couple of dictionaries:
var i;
var app_id_index = {};
var admin_id_index = {};
for (i = 0; i < a.length; i++) {
app_id_index[a[i].app_id] = a[i];
admin_id_index[a[i].admin_id] = a[i];
}
see this
http://dojotoolkit/documentation/tutorials/1.6/intro_dojo_store/
http://dojotoolkit/reference-guide/dojo/data.html#dojo-data