这是《White Paper: Working with large lists in Office SharePoint Server 2007》译文的第二部分,介绍测试中使用的不同数据访问方法。

Data access methods

Each test consisted of retrieving a subset of data from the list using one of a number of different data access methods. This section shows the different methods that were tested.

每个测试都使用这些不同数据访问方法中的一种从列表从检索出数据的一个子集。本节介绍测试中使用的不同数据访问方法。

Note: The code samples included in the following sections are intended to show the process used to conduct tests. The code may not comply with coding best practices, and should not be used in a production environment without careful review and testing.

注意:下节中的示例代码主要是为了展示测试中的过程。因此这些代码可能并不是最佳的,如果未经仔细检查和测试请不要在实际生产环境中使用。

Browser

The list was viewed using a browser and the predefined Office SharePoint Server 2007 interface. A special tool, which is described in the Test Harness section later in this white paper, was developed to accurately capture how long it takes to view that information and browse through pages of data.

列表使用浏览器和MOSS2007预定义接口展示。测试中使用的特殊工具Test Harness,是被用作准确获取查看信息和浏览页面数据时花费时的,这将在白皮书的后面详细介绍,

SPList with For/Each

The Office SharePoint Server 2007 object model (OM) was used to retrieve the list into an SPList object. Each item in the list was then enumerated with a For/Each loop until items were found that matched the search criteria.

MOSS2007对象模型被用作把列表检索到一个SPList对象。每个列表中的列表项通过For/Each循环,并匹配相应的检索标准后被列举出来。

The following sample code was used for this method.

下面的代码使用本方法。

'get the site

Dim curSite As SPSite = New SPSite("http://myPortal")

 

'get the web

Dim curWeb As SPWeb = curSite.OpenWeb()

 

'get our list

Dim curList As SPList = curWeb.Lists(New Guid("myListGUID"))

 

'get the collection of items in the list

Dim curItems As SPListItemCollection = curList.Items

 

'enumerate the items in the list

For Each curItem As SPListItem In curItems

'do some comparison in here to see if it's an item we need

'这里需要进行条件筛选,以获得我们需要的列表项(比如判断ID是否在某范围内)

Next

SPList with SPQuery

The OM was used to create an SPQuery object that contained the query criteria. That object was then used to against an instance of the list in a SPList object. The results of the query were returned by calling the GetItems method on the SPList object.

这里的对象模型是创建一个包含查询条件的SPQuery对象。该对象被用在SPList对象的实例,也就是一个列表上。通过调用SPList对象的GetItems方法获取查询的结果集。

The following sample code was used for this method.

下面的代码使用本方法。

'get the site

Dim curSite As SPSite = New SPSite("http://myPortal")

 

'get the web

Dim curWeb As SPWeb = curSite.OpenWeb()

 

'create our query

Dim curQry As SPQuery = New SPQuery()

 

'configure the query

curQry.Query = "<Where><Eq><FieldRef Name='Expense_x0020_Category'/><Value Type='Text'>

Hotel</Value></Eq></Where>"

curQry.RowLimit = 100

 

'get our list

Dim curList As SPList = curWeb.Lists(New Guid("myListGUID"))

 

'get the collection of items in the list

Dim curItems As SPListItemCollection = curList.GetItems(curQry)

 

'enumerate the items in the list

For Each curItem As SPListItem In curItems

'do something with each match

   Next

SPList with DataTable

This is one of two methods that test using a Microsoft ADO.NET DataTable to work with the data. In this case an instance of the list is obtained with an SPList object. The data from it is then retrieved into a DataTable by calling the GetDataTable() method on the Items property —for example, SPList.Items.GetDataTable(). The DataTable’s DefaultView has a property called RowFilter that was then set to find the items. To keep the methodology between data access methods consistent, the DataTable was not cached between tests —it was filled each time by calling the GetDataTable() method. In a real-world scenario this test would have performed better had the DataTable been cached after the data was first retrieved, but it serves as a valuable point in comparison testing about the cost of this approach versus retrieving a DataTable from a selection of data that’s already filtered.

这是测试中使用微软ADO.NETDataTable处理数据的方法中的一种。本例中,使用SPList对象获取一个列表对象的实例。然后通过调用其属性Items上的GetDataTable()方法检索数据到DataTable中,即:SPList.Items.GetDataTable()DataTable的缺省视图有一个属性叫做RowFilter,可以利用其筛选我们需要的列表项。为了保证各次数据访问方法时的一致性,DataTable在测试中并不被缓存,每次都通过调用GetDataTable()方法对其进行填充。

The following sample code was used for this method.

下面的代码使用本方法。

'get the site

Dim curSite As SPSite = New SPSite("http://myPortal")

 

'get the web

Dim curWeb As SPWeb = curSite.OpenWeb()

 

'get our list

Dim curList As SPList = curWeb.Lists(New Guid("myListGUID"))

 

'get the item in a datatable

Dim dt As DataTable = curList.Items.GetDataTable()

 

'get a dataview for filtering

Dim dv As DataView = dt.DefaultView

dv.RowFilter = "Expense_x0020_Category='Hotel'"

 

'enumerate matches

For rowNum As Integer = 0 To dv.Count - 1

'do something with each match

   Next

SPListItems with DataTable

This method is similar to the SPList with DataTable method, but with a twist. An instance of the list is retrieved through an SPList object. An SPQuery object is created to build a query, and that query is executed against the SPList object, which returns an SPListItems collection. The data from that collection is then retrieved into a DataTable by using the GetDataTable() method on the SPListItems collection.

本方法与上一个方法相似,只是作了一些变化。列表的实例被检索到一个SPList对象。创建一个SPQuery对象以构建查询,该查询在SPList对象上执行,并返回一个SPListItems集合。通过GetDataTable()方法将结果集填充到DataTable

The following sample code was used for this method.

下面的代码使用本方法。

'get the site

Dim curSite As SPSite = New SPSite("http://myPortal")

 

'get the web

Dim curWeb As SPWeb = curSite.OpenWeb()

 

'create our query

Dim curQry As SPQuery = New SPQuery()

 

'configure the query

curQry.Query = "<Where><Eq><FieldRef Name='Expense_x0020_Category'/><Value Type='Text'>Hotel</Value></Eq></Where>"

curQry.RowLimit = 100

 

'get our list

Dim curList As SPList = curWeb.Lists(New Guid("myListGUID"))

 

'get the collection of items in the list

Dim curItems As SPListItemCollection = curList.GetItems(curQry)

 

'get the item in a datatable

Dim dt As DataTable = curItems.GetDataTable()

 

'enumerate matches

For Each dr As DataRow In dt.Rows

'do something with each match

   Next

Lists Web service

The Lists Web service, which comes with Windows SharePoint Services 3.0 and Office SharePoint Server 2007, was used to retrieve the data. A Collaborative Application Markup Language (CAML) query was created and submitted along with the list identifier, and an XML result set was returned from the Lists Web service.

列表的Web服务是同WSS3.0MOSS2007一起发布的,其被用作检索数据。协作应用程序标记语言(CAML)查询连同列表标识符一起创建提交,列表的Web服务将返一个XML数据集。

The following sample code was used for this method.

下面的代码使用本方法。

'create a new xml doc we can use to create query nodes

Dim xDoc As New XmlDocument

 

'create our query node

Dim xQry As XmlNode = xDoc.CreateNode(XmlNodeType.Element, "Query", "")

 

'set the query constraints

xQry.InnerXml = "<Where><Eq><FieldRef Name='Expense_x0020_Category'/><Value Type='Text'>Hotel</Value></Eq></Where>"

 

'create the Web service proxy that is mapped to Lists.asmx

Using ws As New wsLists.Lists()

 

      'configure it

      ws.Credentials = System.Net.CredentialCache.DefaultCredentials

      ws.Url = "http://myPortal/_vti_bin/lists.asmx"

 

      'create the optional elements

      Dim xView As XmlNode = xDoc.CreateNode(XmlNodeType.Element, "ViewFields", "")

      Dim xQryOpt As XmlNode = xDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "")

 

      'query the server

      Dim xNode As XmlNode = ws.GetListItems("myListID", "", xQry, xView, "", xQryOpt, "")

 

      'enumerate returned items

      For nodeCount As Integer = 0 To xNode.ChildNodes.Count - 1

        'do something with each match

      Next

End Using

Search

The OM was used to execute a query against the Office SharePoint Server 2007 search engine and return the results as a ResultTableCollection. That was then further distilled down into an ADO.NET DataTable via the ResultTable of ResultType.RelevantResults from the ResultTableCollection.

这里对象模型被用作在MOSS2007搜索引擎上执行一个查询,并返回一个被称作ResultTableCollection的结果集。接下来再通过ResultType.RelevantResultsResultTable将结果集提取到ADO.NETDataTable

The following sample code was used for this method.

下面的代码使用本方法。

'get the site

Dim curSite As SPSite = New SPSite("http://myPortal")

 

'get the web

Dim curWeb As SPWeb = curSite.OpenWeb()

 

'get our list

Dim curList As SPList = curWeb.Lists(New Guid("myListGUID"))

 

Dim qry As New FullTextSqlQuery(curSite)

Dim SQL As String = "SELECT Title, Rank, Size, Description, Write, Path, Deductible, ExpenseCategory, ID, Vendor, Amount FROM portal..scope() WHERE CONTAINS (""URL"",'""#SITEURL#Lists/#LISTURL#*""') #DEFAULT#  ORDER BY ""Rank"""

 

'do token replacement

SQL = SQL.Replace("#SITEURL#", "http://myPortal/")

SQL = SQL.Replace("#LISTURL#", curList.Title)

 

SQL = SQL.Replace("#DEFAULT#", "AND FREETEXT (""ExpenseCategory"",'""Hotel""')")

 

qry.QueryText = SQL

qry.RowLimit = 100

qry.ResultTypes = ResultType.RelevantResults

 

'execute the query

Dim rtc As ResultTableCollection = qry.Execute()

Dim rt As ResultTable = rtc(ResultType.RelevantResults)

Dim dt As New DataTable()

dt.Load(rt, LoadOption.OverwriteChanges)

 

'enumerate matches

For Each dr As DataRow In dt.Rows

'do something with each match

   Next

PortalSiteMapProvider

One approach to retrieving list data in Office SharePoint Server 2007 that’s not very well known is the use of the PortalSiteMapProvider class. It was originally created to help cache content for navigation. However, it also provides a nice automatic caching infrastructure for retrieving list data. The class includes a method called GetCachedListItemsByQuery that was used in this test. This method first retrieves data from a list based on an SPQuery object that is provided as a parameter to the method call. The method then looks in its cache to see if the items already exist. If they do, the method returns the cached results, and if not, it queries the list, stores the results in cache and returns them from the method call.

MOSS2007中有一种检索列表数据的方法可能鲜为人所熟知,这就是PortalSiteMapProvider类。它被创建的最初目的是用来缓存导航内容。然而,它也提供了一种非常好的动态缓存从列表中检索的数据的基础构造。本测试中使用了该类中一个叫作GetCachedListItemsByQuery()的方法。这个方法首先是基于SPQuery对象,并把其作为该方法的一个参数从一个列表中检索出数据。该方法显示看缓存中列表项是否已经存在。如果存在,那么方法就直接返回缓存中的结果集;如果不存在,那么就查询列表,将结果集存储在缓存中并返回结果集给调用。

The following sample code was used for this method. Note that it is different from all of the previous examples in that you cannot use the PortalSiteMapProvider class in Windows forms applications.

下面的代码使用本方法。请注意,这里和之前所有的代码不同的是,PortalSiteMapProvider类不能在Win Form应用中使用。

'get the current web

Dim curWeb As SPWeb = SPControl.GetContextWeb(HttpContext.Current)

 

'create the query

Dim curQry As New SPQuery()

curQry.Query = "<Where><Eq><FieldRef Name='Expense_x0020_Category'/><Value Type='Text'>Hotel</Value></Eq></Where>"

 

'get the portal map provider stuff

Dim ps As PortalSiteMapProvider = PortalSiteMapProvider.WebSiteMapProvider

 

Dim pNode As PortalWebSiteMapNode = TryCast(ps.FindSiteMapNode(curWeb.ServerRelativeUrl), PortalWebSiteMapNode)

 

'get the items

pItems = ps.GetCachedListItemsByQuery(pNode, "myListName_NotID", curQry, curWeb)

 

'enumerate all matches

For Each pItem As PortalListItemSiteMapNode In pItems

'do something with each match

   Next

Posted on 2007-11-07 20:33  dotnba  阅读(790)  评论(0编辑  收藏  举报