Skip to content
This repository
Newer
Older
100644 108 lines (86 sloc) 3.024 kb
d7c12362 »
2009-11-02 added streaming functionality, first pass.
1 using System.Collections.Generic;
2 using Newtonsoft.Json.Linq;
3 using System;
4 using Newtonsoft.Json;
5 using System.Collections;
6
7 namespace Divan
8 {
9 /// <summary>
10 /// This is a view result from a CouchQuery that can return CouchDocuments for
11 /// resulting documents (include_docs) and/or ICanJson documents for the
12 /// result values. A value returned from a CouchDB view does not need to be
13 /// a CouchDocument.
14 /// </summary>
15 public class CouchViewResultStream<T> : CouchViewResult, IEnumerable<CouchRecord<T>> , IDisposable where T: ICanJson, new()
16 {
17 class RecordEnumerator : IEnumerator<CouchRecord<T>>
18 {
510a86da »
2010-04-06 Merge
19 readonly JsonReader reader;
d7c12362 »
2009-11-02 added streaming functionality, first pass.
20 CouchRecord<T> current;
21 bool hasMore = true;
510a86da »
2010-04-06 Merge
22 readonly CouchViewResultStream<T> owner;
d7c12362 »
2009-11-02 added streaming functionality, first pass.
23
82a3381a »
2009-11-04 added unit tests for Linq, Arbitrary docs and Result streaming
24 public RecordEnumerator(JsonReader reader, CouchViewResultStream<T> owner)
d7c12362 »
2009-11-02 added streaming functionality, first pass.
25 {
26 this.reader = reader;
82a3381a »
2009-11-04 added unit tests for Linq, Arbitrary docs and Result streaming
27 this.owner = owner;
d7c12362 »
2009-11-02 added streaming functionality, first pass.
28 }
29
30 public CouchRecord<T> Current
31 {
32 get { return current; }
33 }
34
35 public void Dispose() { }
36
37 object IEnumerator.Current
38 {
39 get { return current; }
40 }
41
42 public bool MoveNext()
43 {
44 if (!hasMore)
45 return false;
46
47 var token = JToken.ReadFrom(reader);
48 hasMore = reader.Read() && reader.TokenType == JsonToken.StartObject;
82a3381a »
2009-11-04 added unit tests for Linq, Arbitrary docs and Result streaming
49 owner.hasMore = hasMore;
d7c12362 »
2009-11-02 added streaming functionality, first pass.
50
51 current = new CouchRecord<T>(token as JObject);
52
53 return true;
54 }
55
56 public void Reset()
57 {
58 throw new NotSupportedException();
59 }
60 }
61
510a86da »
2010-04-06 Merge
62 readonly JsonReader reader;
82a3381a »
2009-11-04 added unit tests for Linq, Arbitrary docs and Result streaming
63 bool hasMore = true;
d7c12362 »
2009-11-02 added streaming functionality, first pass.
64
65 public CouchViewResultStream(JsonReader reader)
66 {
67 this.reader = reader;
68
69 var header = new JObject();
70
71 // start object
72 reader.Read();
73
74 while (reader.Read() && reader.TokenType != JsonToken.StartArray)
75 {
76 var name = reader.Value.ToString();
77 if (name == "rows")
78 continue;
79
80 reader.Read();
81 header[name] = new JValue(reader.Value);
82 }
83
84 reader.Read();
85 }
86
87 public void Dispose()
88 {
89 reader.Close();
90 }
91
92 public IEnumerator<CouchRecord<T>> GetEnumerator()
93 {
82a3381a »
2009-11-04 added unit tests for Linq, Arbitrary docs and Result streaming
94 if (!hasMore)
95 throw new InvalidOperationException("Result stream cannot be re-enumerated");
96
97 return new RecordEnumerator(reader, this);
d7c12362 »
2009-11-02 added streaming functionality, first pass.
98 }
99
100 IEnumerator System.Collections.IEnumerable.GetEnumerator()
101 {
82a3381a »
2009-11-04 added unit tests for Linq, Arbitrary docs and Result streaming
102 if (!hasMore)
103 throw new InvalidOperationException("Result stream cannot be re-enumerated");
104
105 return new RecordEnumerator(reader, this);
d7c12362 »
2009-11-02 added streaming functionality, first pass.
106 }
107 }
108 }
Something went wrong with that request. Please try again.