Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • alimac 177 posts 345 karma points
    Nov 07, 2012 @ 18:26
    alimac
    0

    Fastest way to iterate through nodes?

    Hi guys,

    I've written a search for a website that iterates through all nodes under a certain folder and then filters out nodes based on certain criteria. The site itself is used to search apartments. Here is the function I have for building the collection:

    private static List<Apartment> GetApartmentsCollection()
            {
                List<Apartment> apartmentsCollection = new List<Apartment>();
    
                Document apartmentsContainer = new Document(Convert.ToInt32(ConfigurationManager.AppSettings["apartmentsContainerNodeId"]));
    
                if (apartmentsContainer.HasChildren)
                {
                    Document[] buildings = apartmentsContainer.Children;
                    foreach (Document building in buildings)
                    {
                        if (building.Published)
                        {
                            // Store building id for apartment
                            int buildingId = building.Id;
                            if (building.HasChildren)
                            {
                                Document[] apartments = building.Children;
                                foreach (Document apartment in apartments)
                                {
                                    if (apartment.Published)
                                    {
                                        apartmentsCollection.Add(new Apartment(apartment.Id,
                                                                               buildingId,
                                                                               Convert.ToInt32(apartment.Text),
                                                                               (int)apartment.getProperty("apartmentType").Value,
                                                                               (int)apartment.getProperty("floor").Value,
                                                                               (int)apartment.getProperty("status").Value,
                                                                               (int)apartment.getProperty("bedrooms").Value,
                                                                               Convert.ToInt32(apartment.getProperty("price").Value),
                                                                               string.IsNullOrEmpty(apartment.getProperty("room1").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room1").Value.ToString()),
                                                                               apartment.getProperty("room1Dimensions").Value.ToString(),
                                                                               string.IsNullOrEmpty(apartment.getProperty("room2").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room2").Value.ToString()),
                                                                               apartment.getProperty("room2Dimensions").Value.ToString(),
                                                                               string.IsNullOrEmpty(apartment.getProperty("room3").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room3").Value.ToString()),
                                                                               apartment.getProperty("room3Dimensions").Value.ToString(),
                                                                               string.IsNullOrEmpty(apartment.getProperty("room4").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room4").Value.ToString()),
                                                                               apartment.getProperty("room4Dimensions").Value.ToString(),
                                                                               string.IsNullOrEmpty(apartment.getProperty("room5").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room5").Value.ToString()),
                                                                               apartment.getProperty("room5Dimensions").Value.ToString(),
                                                                               string.IsNullOrEmpty(apartment.getProperty("room6").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room6").Value.ToString()),
                                                                               apartment.getProperty("room6Dimensions").Value.ToString(),
                                                                               string.IsNullOrEmpty(apartment.getProperty("room7").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room7").Value.ToString()),
                                                                               apartment.getProperty("room7Dimensions").Value.ToString(),
                                                                               apartment.getProperty("totalArea").Value.ToString(),
                                                                               apartment.getProperty("projectArea").Value.ToString()));
                                    }
                                }
                            }
                        }
                    }
                }
    
                return apartmentsCollection;
            }

    The only problem is that this takes a good 8-10 seconds or so (even though there are only ~150 nodes tops) which is too long.

    Bear in mind this is just for retrieving the raw collection. I then filter out out results using a handful of linq statements which takes a further few seconds. Ultimately this leads to a search of ~150 nodes taking around 15 seconds which is far too long.

    How can I speed this up?

    Thanks

     

     

  • Nathan Woulfe 422 posts 1580 karma points MVP 3x c-trib
    Nov 08, 2012 @ 01:21
    Nathan Woulfe
    1

    Hi Alimac

    Best bet is to avoid using Document - you're querying the database and as you're not updating/changing data, there's no need - hit the XML cache instead. Creating the apartment object as a Node will be much quicker, or getting the data via Examine will be quicker still. Using Examine you can do all your filtering as part of the search query, so you get the exact data you're chasing without any extra processing. Iterate the results and push it out into the List<Apartments> object and you're good to go. Guarantee it will be quicker than 8-10 seconds.

    N

  • alimac 177 posts 345 karma points
    Nov 08, 2012 @ 10:35
    alimac
    0

    Perfect! Thanks so much

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies