Figaro - The XML Database for the .NET Framework

Looking Up Indexed Documents

You can retrieve all of the values referenced by an index using an XmlIndexLookup object, which is returned by the CreateIndexLookup method. XmlIndexLookup allows you to obtain an XmlResults object that contains all of the nodes or documents for which the identified index has keys. Whether nodes or documents is return depends on several factors:

  • If your container is of type WholeDocContainer, then by default entire documents are always returned in this method's results set.
  • If your container is of type NodeContainer then by default this method returns the nodes to which the index's keys refer.

For example, every container is created with a default index that ensures the uniqueness of the document names in your container.

  • URI is http://www.sleepycat.com/2002/dbxml
  • Node name is name.
  • Indexing strategy is unique-node-metadata-equality-string.

Given this, you can efficiently retrieve every document in the container using XmlIndexLookup as follows:

CopyC#
using System;
using System.Diagnostics;
using System.IO;
using Figaro.BerkeleyDB.Xml;                
namespace Figaro.Documentation.Examples
{
    class IndexLookups
    {                
        static void Main()
        {
            //get our manager
            using (var mgr = new XmlManager(ManagerInitOptions.AllowExternalAccess | ManagerInitOptions.AllowAutoOpen))
            {
                prepareContainer(true);
                //open a container
                using (var container = mgr.OpenContainer(baseUri + testdb))
                {
                    try
                    {
                        // get a query context
                        QueryContext queryContext = mgr.CreateQueryContext();
                        XmlValue val = new XmlValue("Bananas");
                        XmlIndexLookup lookup = mgr.CreateIndexLookup(container, "",
                                                                      "product",
                                                                      "unique-node-element-equality-string", val,
                                                                      IndexLookupOperation.None);
                        //perform lookup with an additional option specified
                        XmlResults res = lookup.Execute(queryContext, IndexLookupOptions.CacheDocuments);
                        while (res.HasNext())
                        {
                            XmlDocument doc = res.NextDocument();
                            Console.WriteLine("document: {0}",doc.Name);
                            Console.WriteLine("{0}", doc.ToString());
                        }
                    }
                    finally
                    {
                        container.Close();
                    }
                }
            }
        }

In the event that you want to look up an edge index, you must provide the lookup method with both the node and the parent node that together comprise the XML edge.

For example, suppose you have the following document in your container:

CopyXML
<?xml version="1.0"?>
<fruits:item xmlns:fruits="http://groceryItem.dbxml/fruits">
  <product>Bananas</product>
  <inventory>
    <sku>Banafruiqud6Kq</sku>
    <price>0.55</price>
    <inventory>220</inventory>
  </inventory>
  <vendor>Simply Fresh</vendor>
</fruits:item>

Further suppose you indexed the presence of the fruits/product edges. In this case, you can look up the values referred to by this index by doing the following:

CopyC#
using System;
using Figaro.BerkeleyDB.Xml;
namespace Figaro.Documentation.Examples
{
    class EdgeLookup
    {
        static void Main()
        {
            //get our manager
            using (var mgr = new XmlManager())
            {
                prepareContainer(false);
                //open a container
                using (var container = mgr.OpenContainer(baseUri + testdb))
                {
                    try
                    {
                        // get a query context
                        QueryContext queryContext = mgr.CreateQueryContext();
                        XmlValue val = new XmlValue("Banafruiqud6Kq");
                        XmlIndexLookup lookup = mgr.CreateIndexLookup(container, "",
                                                                      "sku",
                                                                      "unique-edge-element-equality-string", val,
                                                                      IndexLookupOperation.None);
                        //perform lookup with an additional option specified
                        lookup.SetParent("","inventory");
                        XmlResults res = lookup.Execute(queryContext, IndexLookupOptions.CacheDocuments);
                        while (res.HasNext())
                        {
                            XmlDocument doc = res.NextDocument();
                            Console.WriteLine("document: {0}",doc.Name);
                            Console.WriteLine("{0}", doc.ToString());
                        }
                    }
                    finally
                    {
                        container.Close();
                    }
                }
            }
        }