Permalink
Browse files

Basic paged view support.

Change-Id: I9dc2a87ecdc93a71dd4f34502378fe0d33e0a3a0
Reviewed-on: http://review.couchbase.org/9055
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
Tested-by: Bin Cui <bin.cui@gmail.com>
  • Loading branch information...
1 parent 2b7acb9 commit 98ab8ac682ddbb1c5415778eb9bb49a91e915e82 @enyim enyim committed with ingenthr Aug 17, 2011
Showing with 173 additions and 0 deletions.
  1. +2 −0 Couchbase/Couchbase.csproj
  2. +5 −0 Couchbase/CouchbaseView.cs
  3. +7 −0 Couchbase/ICouchbaseView.cs
  4. +37 −0 Couchbase/IPagedView.cs
  5. +122 −0 Couchbase/PagedView.cs
@@ -64,7 +64,9 @@
<Compile Include="ICouchbaseView.cs" />
<Compile Include="IHttpClient.cs" />
<Compile Include="IHttpClientLocator.cs" />
+ <Compile Include="IPagedView.cs" />
<Compile Include="Json.cs" />
+ <Compile Include="PagedView.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
@@ -262,6 +262,11 @@ IView IView.WithInclusiveEnd(bool inclusive)
return new CouchbaseView(this) { inclusive = inclusive };
}
+ IPagedView IView.GetPagedView(int pageSize)
+ {
+ return new PagedView(this, pageSize);
+ }
+
#endregion
#region [ Request helpers ]
@@ -91,6 +91,13 @@ public interface IView : IEnumerable<IViewRow>
/// <param name="inclusive">A value that specifies whether the end of the range is included in the result.</param>
/// <returns>A new <see cref="T:Couchbase.IView"/> that can be used to retrieve the items.</returns>
IView WithInclusiveEnd(bool inclusive);
+
+ /// <summary>
+ /// Returns a view which allows the user to page retrieve all items of an index in pages.
+ /// </summary>
+ /// <param name="pageSize"></param>
+ /// <returns></returns>
+ IPagedView GetPagedView(int pageSize);
}
}
@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Newtonsoft.Json.Linq;
+
+namespace Couchbase
+{
+ public interface IPagedView : IEnumerable<IViewRow>
+ {
+ bool MoveNext();
+
+ int PageSize { get; }
+ int PageIndex { get; }
+ }
+}
+
+#region [ License information ]
+/* ************************************************************
+ *
+ * @author Couchbase <info@couchbase.com>
+ * @copyright 2011 Couchbase, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ************************************************************/
+#endregion
View
@@ -0,0 +1,122 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+using Newtonsoft.Json;
+using Hammock;
+using Hammock.Serialization;
+using System.Diagnostics;
+using Newtonsoft.Json.Linq;
+using Enyim.Caching;
+
+namespace Couchbase
+{
+ internal class PagedView : IPagedView
+ {
+ private IView currentView;
+
+ private int pageIndex;
+ private int pageSize;
+ private List<IViewRow> items;
+
+ private string nextId;
+ private int state;
+
+ public PagedView(IView view, int pageSize)
+ {
+ this.items = new List<IViewRow>();
+ this.pageSize = pageSize;
+ this.currentView = view.EndDocumentId(null);
+
+ this.state = -1;
+ }
+
+ bool IPagedView.MoveNext()
+ {
+ if (this.state == -1)
+ {
+ this.nextId = this.LoadData(this.currentView);
+ this.state = 1;
+
+ return this.nextId != null;
+ }
+
+ // can't go further
+ if (this.nextId == null || this.items.Count == 0)
+ return false;
+
+ // get a reference to the current page
+ var page = this.currentView.StartDocumentId(this.nextId);
+
+ this.nextId = this.LoadData(page);
+ this.pageIndex++;
+
+ // did not load anything, we're at the end
+ return this.items.Count != 0;
+ }
+
+ int IPagedView.PageSize
+ {
+ get { return this.pageSize; }
+ }
+
+ int IPagedView.PageIndex
+ {
+ get { return this.pageIndex; }
+ }
+
+ private string LoadData(IView view)
+ {
+ this.items.Clear();
+
+ var count = this.pageSize;
+ string lastId = null;
+
+ foreach (var row in view.Limit(this.pageSize + 1))
+ {
+ // only store pageSize count of items
+ // the last one will be only stored as a reference to the next page
+ if (count > 0)
+ this.items.Add(row);
+ else
+ lastId = row.ItemId;
+
+ count--;
+ }
+
+ return lastId;
+ }
+
+ IEnumerator<IViewRow> IEnumerable<IViewRow>.GetEnumerator()
+ {
+ return this.items.GetEnumerator();
+ }
+
+ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
+ {
+ return this.items.GetEnumerator();
+ }
+ }
+}
+
+#region [ License information ]
+/* ************************************************************
+ *
+ * @author Couchbase <info@couchbase.com>
+ * @copyright 2011 Couchbase, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ************************************************************/
+#endregion

0 comments on commit 98ab8ac

Please sign in to comment.