Copied to clipboard

Flag this post as spam?

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


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    May 27, 2012 @ 15:01
    Bo Damgaard Mortensen
    0

    Best way (performance wise) to get a reference to the topmost node

    Hi all,

    I'm in a situation where I need to get a reference to the top-most (root) node from one of it childnodes (or maybe even grandchild nodes)

    My structure is a as follows:

    - da (site node with the property)
          - Childnode
                 - Childnode

    - en (site node)
          - Childnode
                 - Childnode

    On each of the site nodes there's a property that I need to read the value of on one/some of the child and/or childrens childnode(s) It's important that it reads the value of the correct site-node :-)

    What I'm doing so far is using uComponents GetNodesByXPath() uQuery method. But there's two problems:

    1) It takes 2  ~ 5 seconds to give me the result which is not ideal.

    2) It returns the first site node in the tree

    My code is as follows:

    var siteRootNode = uQuery.GetNodesByXPath("//Site[@isDoc]").FirstOrDefault();

    So, my question is: is there any other way to do this with performance in mind? :-) It's probably just me that don't know my XPath'ing too well ;-)

    Thanks a lot in advance,

    Bo

  • Stefan Kip 1614 posts 4131 karma points c-trib
    May 27, 2012 @ 15:37
    Stefan Kip
    1

    Well, in C# I use this piece of code:

    /// <summary>
    /// Retrieve the root node
    /// </summary>
    /// <param name="node">The current node</param>
    /// <returns>The root node</returns>
    public static Node GetRoot(this INode node)
    {
        INode parent = node;
    
        while (parent != null)
        {
            node = parent;
            parent = node.Parent;
        }
    
        return (Node)node;
    }
    
  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    May 27, 2012 @ 15:58
    Hendy Racher
    0

    Hi Bo,

    I'm suprised the GetNodesByXPath() method is slow ! the first hit will create a compiled XPath expression, so subsequent ones should be faster. (also if you have multiple 'Site' nodes, I the above XPath expression might not be returning the correct one)

    An alternative approach could be to walk up the tree (just like Stefan suggests)

    using System.Linq;
    using uComponents.Core.uQueryExtensions;

    Node siteNode = uQuery.GetCurrentNode().GetAncestorNodes().Where(x => x.NodeTypeAlias == 'Site').Single();

    HTH,

    Hendy

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    May 28, 2012 @ 21:16
    Bo Damgaard Mortensen
    0

    Thanks kipusoep and Hendy, greatly appreciated! :-)

    Tried both of your methods and it turns out that it basically has nothing to do with the code I had (at least not the performance part of it) Also, I'd like to point out that it was not the uQuery Extension from uComponent that is slow! Very important to note that.

    Still the problem persists. It feels like it's when the app pool has gone to sleep (for the lack of better sentence right now) and I hit one of the filter checkboxes, then the first search takes up to 5 seconds to give back the results. This search is done with Examine.

    Does anyone know any way to increase the performance of the first examine search? It feels like *something* has to "wake up" before actually executing the search (?)

    My examine (Razor) code is as follows and is called when a user checks/unchecks one or more checkboxes on the frontend:

    @{
        @* Get theme node ids from querystring *@
        var themeIds = HttpContext.Current.Request.QueryString["themes"];
    
        ISearchCriteria sc;
        IBooleanOperation query;
        IOrderedEnumerable<SearchResult> result = null;
    
        @* Get current sites language *@
        SiteLanguage siteLanguage = new SiteLanguage();
        string language = siteLanguage.GetSiteLanguageByXPath(Model.Id);
        if (!string.IsNullOrEmpty(language))
        {
            sc = ExamineManager.Instance.SearchProviderCollection["RecipeSearcher"].CreateSearchCriteria();
    
            @* If no theme is chosen, render all recipes *@
            if (!string.IsNullOrEmpty(themeIds))
            {    
                @* Perform examine search *@            
                query = sc.NodeTypeAlias("Recipe").And().GroupedAnd(new[] { "selectedThemes" }, new[] { themeIds.TrimEnd(',') }).And().Field("_siteLanguage", language);            
            }
            else
            {
                @* Perform examine search *@            
                result = ExamineManager.Instance.SearchProviderCollection["RecipeSearcher"].Search(query.Compile()).OrderByDescending(x => x.Id);
            }
    
            result = ExamineManager.Instance.SearchProviderCollection["RecipeSearcher"].Search(query.Compile()).OrderByDescending(x => x.Id);
    
            @* Check if the search contains any elements *@
            if (result != null && result.Count() > 0)
            {
            <div class="recipe-container">
            @foreach (var recipe in result)
            {
                @* Output markup *@
                <div class="recipe">
                    @if (recipe.Fields.ContainsKey("image"))
                    {
                        dynamic image = Library.MediaById(int.Parse(recipe.Fields["image"]));
                        <div><a href="@umbraco.library.NiceUrl(recipe.Id)"><img src="/[email protected]&amp;width=213&amp;height=169&amp;compression=100" alt="@recipe.Fields["header"]" /></a></div>   
                    }    
    
                    <p><a href="#">@recipe.Fields["header"]</a></p>
                </div>                
            }
            </div>
            <span style="display:none;" class="filter-count">@result.Count()</span>
    
            @* TODO: Paging *@
            }
            else
            {
            <span style="display:none;" class="filter-count">0</span>
            }
        }
    }

    Thanks a lot in advance.

    All the best,

    Bo

  • Nigel Wilson 945 posts 2077 karma points
    May 29, 2012 @ 02:56
    Nigel Wilson
    0

    Hi Bo

    Just wondering - have you tried changing the Idel Time Out setting to 0 (zero) within the application pool (located under advanced settings) to see if this prevents the slow first response ?

    Cheers

    Nigel

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    May 29, 2012 @ 07:57
    Bo Damgaard Mortensen
    0

    Good point there, Nigel. However, I don't have access to the IIS on the live environment :/

    Will have to check if there's some kind of Umbraco setting that could do the trick :) Thanks again!

Please Sign in or register to post replies

Write your reply to:

Draft