Paging with nested property indexes

37 views
Skip to first unread message

spokeypokey

unread,
Dec 12, 2011, 5:30:50 PM12/12/11
to rav...@googlegroups.com
When looking through the RavenDB samples, I spotted what I think
may be an error in one of your test cases having to do with paging
(Raven.Tests.MailingList.Vlad.WillOnlyGetPost2Once).

It has the following line:
    recordsToSkip = pageSize * pageNumber + statistics.SkippedResults;

which I think should read:
    skippedResults += statistics.SkippedResults;
    recordsToSkip = pageSize * pageNumber + skippedResults;


Please take a look at my sample test below and see if I am
doing this correctly:




public void Can_page_when_using_nested_property_index()
{
    using (var session = DocStore.OpenSession())
    {
        for (int i = 1; i < 11; i++)
        {
            session.Store(CreateProvider(i));
        }
        session.SaveChanges();
    }

    new NestedPropertyIndex1().Execute(DocStore);
   
    // Retrieves all 10 providers in a single result set.
    using (var session = DocStore.OpenSession())
    {
        var result = (from p in session.Query<Provider, NestedPropertyIndex1>()
                      where p.Zip == "97520"
                      select p).ToArray();
        Assert.AreEqual(10, result.Count());
    }

    // Retrieves all 10 providers, 2 at a time.
    using (var session = DocStore.OpenSession())
    {
        const int pageSize = 2;
        int pageNumber = 0;
        int skippedResults = 0;
        int recordsToSkip = 0;

        var providers = new List<Provider>();

        RavenQueryStatistics statistics;
        while (true)
        {
            10var result = (from p in session.Query<Provider, NestedPropertyIndex1>()
                              .Customize(x => x.WaitForNonStaleResults())
                              .Statistics(out statistics)
                          where p.Zip == "97520"
                          select p)
                .Take(pageSize)
                .Skip(recordsToSkip)
                .ToList();

            providers.AddRange(result);

            if (result.Count < pageSize)
                break;

            pageNumber++;

            skippedResults += statistics.SkippedResults;
            recordsToSkip = pageSize * pageNumber + skippedResults;

            // I found this in the Raven.Tests.MailingList.Vlad.WillOnlyGetPost2Once() method
            //recordsToSkip = pageSize * pageNumber + statistics.SkippedResults;
        }

        Assert.AreEqual(10, providers.Count);
        Assert.AreEqual(5, pageNumber);
    }
}


public class Category
{
    public string Name { get; set; }
    public DateTime EffectiveFrom { get; set; }
    public DateTime EffectiveThrough { get; set; }
}


public class Provider
{
    public string Name { get; set; }
    public string Zip { get; set; }
    public IList<Category> Categories { get; set; }
}


// Indexing nested properties
// Creates multiple index entries, one for each nested property combination
public class NestedPropertyIndex1 : AbstractIndexCreationTask<Provider>
{
    public NestedPropertyIndex1()
    {
        Map = providers =>
              from p in providers
              from c in p.Categories
              select new
              {
                  p.Name,
                  p.Zip,
                  Categories_Name = c.Name,
                  Categories_EffectiveFrom = c.EffectiveFrom,
                  Categories_EffectiveThrough = c.EffectiveThrough,
              };
    }

}


Oren Eini (Ayende Rahien)

unread,
Dec 13, 2011, 6:25:13 AM12/13/11
to rav...@googlegroups.com
Yes, you are correct, you need to keep track of where you actually stopped previously.
Reply all
Reply to author
Forward
0 new messages