3

i want to store this kind of information in memory all the time.

app_id    admin_id    url           status
123       1           xyz.com       1
123       2           xyz.com       0
539       3           exmaple.com   1
876       4           new.com       1

My main concern is that i want the lookup to be easy and fast. Like at any point to query :

  1. get the url and status for a particular admin_id.
  2. get all admin_id for a particular app_id.

SO how do i store this information in memory.

3 Answers 3

3

You 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.com", 1);
store.add(123, 9, "xyz.com", 0);
store.add(539, 3, "exmaple.com", 1);
store.add(876, 4, "new.com", 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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks..i was also thinking on the same lines..major concern was the lockup to be fast.
2

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];
}

Comments

0

see this
http://dojotoolkit.org/documentation/tutorials/1.6/intro_dojo_store/
http://dojotoolkit.org/reference-guide/dojo/data.html#dojo-data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.