Browse files

Switched everywhere from IList to IEnumerable (since there is no bene…

…fit in having list functionality)

Switched everywhere from arrays to IEnumerable (same reason as the IList switch)
  • Loading branch information...
1 parent 12b0dd8 commit d4e06f3e3edf383df261a77da9df93314eb1da1a Sebastian Negomireanu committed with whenrik Dec 10, 2009
View
4 samples/Trivial/Program.cs
@@ -92,7 +92,7 @@ class Program
// HTTP request to CouchDB and return a CouchGenericViewResult which we in turn can ask to produce objects from JSON,
// in this case we pick out the actual documents and instantiate them as instances of the class Car.
var cars = db.GetAllDocuments<Car>();
- Console.WriteLine("Loaded all Cars: " + cars.Count);
+ Console.WriteLine("Loaded all Cars: " + cars.Count());
// Now try some linq
var tempView = db.NewTempView("test", "test", "if (doc.docType && doc.docType == 'car') emit(doc.Hps, doc);");
@@ -124,7 +124,7 @@ class Program
}
// Get all cars again and see how many are left.
- Console.WriteLine("Cars left: " + db.GetAllDocuments<Car>().Count);
+ Console.WriteLine("Cars left: " + db.GetAllDocuments<Car>().Count());
// We can actually also delete in a single bulk call using DeleteDocuments().
db.DeleteDocuments(cars.ToArray());
View
2 src/CouchBulkDeleteDocuments.cs
@@ -10,7 +10,7 @@ namespace Divan
/// </summary>
public class CouchBulkDeleteDocuments : CouchBulkDocuments
{
- public CouchBulkDeleteDocuments(IList<ICouchDocument> docs) : base(docs)
+ public CouchBulkDeleteDocuments(IEnumerable<ICouchDocument> docs) : base(docs)
{
}
View
7 src/CouchBulkDocuments.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
+using System.Linq;
namespace Divan
{
@@ -10,18 +11,18 @@ namespace Divan
/// </summary>
public class CouchBulkDocuments : ICanJson
{
- public CouchBulkDocuments(IList<ICouchDocument> docs)
+ public CouchBulkDocuments(IEnumerable<ICouchDocument> docs)
{
Docs = docs;
}
- public IList<ICouchDocument> Docs { get; private set; }
+ public IEnumerable<ICouchDocument> Docs { get; private set; }
#region ICouchBulk Members
public int Count()
{
- return Docs.Count;
+ return Docs.Count();
}
public virtual void WriteJson(JsonWriter writer)
View
101 src/CouchDatabase.cs
@@ -25,7 +25,8 @@ public CouchDatabase()
Name = "default";
}
- public CouchDatabase(CouchServer server) : this()
+ public CouchDatabase(CouchServer server)
+ : this()
{
Server = server;
}
@@ -44,7 +45,8 @@ public string Name
return name;
return Server.DatabasePrefix + name;
}
- set {
+ set
+ {
name = value;
}
}
@@ -112,7 +114,7 @@ public CouchRequest RequestAllDocuments()
/// This method is only practical for testing purposes.
/// </summary>
/// <returns>A list of all documents.</returns>
- public IList<CouchJsonDocument> GetAllDocuments()
+ public IEnumerable<CouchJsonDocument> GetAllDocuments()
{
return QueryAllDocuments().IncludeDocuments().GetResult().Documents<CouchJsonDocument>();
}
@@ -124,7 +126,7 @@ public IList<CouchJsonDocument> GetAllDocuments()
/// </summary>
/// <typeparam name="T">The document type to use.</typeparam>
/// <returns>A list of all documents.</returns>
- public IList<T> GetAllDocuments<T>() where T : ICouchDocument, new()
+ public IEnumerable<T> GetAllDocuments<T>() where T : ICouchDocument, new()
{
return QueryAllDocuments().IncludeDocuments().GetResult().Documents<T>();
}
@@ -134,7 +136,7 @@ public IList<T> GetAllDocuments<T>() where T : ICouchDocument, new()
/// CouchDocument does not contain the actual content.
/// </summary>
/// <returns>List of documents</returns>
- public IList<CouchDocument> GetAllDocumentsWithoutContent()
+ public IEnumerable<CouchDocument> GetAllDocumentsWithoutContent()
{
QueryAllDocuments().GetResult().ValueDocuments<CouchDocument>();
@@ -224,8 +226,8 @@ public ICouchDocument SaveDocument(ICouchDocument document)
{
if (document.Id == null)
savedDoc = CreateDocument(document);
- else
- savedDoc = WriteDocument(document);
+ else
+ savedDoc = WriteDocument(document);
}
catch (CouchConflictException)
{
@@ -474,11 +476,9 @@ public ICouchDocument CreateDocument(ICouchDocument document)
}
}
- public void SaveArbitraryDocuments<T>(IList<T> documents, bool allOrNothing)
+ public void SaveArbitraryDocuments<T>(IEnumerable<T> documents, bool allOrNothing)
{
- SaveDocuments(
- (IList<ICouchDocument>)documents.Select(doc => new CouchDocumentWrapper<T>(doc)).ToList(),
- allOrNothing);
+ SaveDocuments(documents.Select(doc => new CouchDocumentWrapper<T>(doc)).Cast<ICouchDocument>(), allOrNothing);
}
/// <summary>
@@ -487,30 +487,35 @@ public void SaveArbitraryDocuments<T>(IList<T> documents, bool allOrNothing)
/// </summary>
/// <param name="documents">List of documents to store.</param>
/// <remarks>POST may be problematic in some environments.</remarks>
- public void SaveDocuments(IList<ICouchDocument> documents, bool allOrNothing)
+ public void SaveDocuments(IEnumerable<ICouchDocument> documents, bool allOrNothing)
{
var bulk = new CouchBulkDocuments(documents);
try
{
- var result =
- Request("_bulk_docs").Data(CouchDocument.WriteJson(bulk)).Query("?all_or_nothing=" + allOrNothing.ToString().ToLower()).PostJson().Parse
- <JArray>();
- for (int i = 0; i < documents.Count; i++)
+ var result = Request("_bulk_docs")
+ .Data(CouchDocument.WriteJson(bulk))
+ .Query("?all_or_nothing=" + allOrNothing.ToString().ToLower())
+ .PostJson()
+ .Parse<JArray>();
+
+ int index = 0;
+ foreach (var document in documents)
{
- documents[i].Id = (result[i])["id"].Value<string>();
- documents[i].Rev = (result[i])["rev"].Value<string>();
- }
+ document.Id = (result[index])["id"].Value<string>();
+ document.Rev = (result[index])["rev"].Value<string>();
+ ++index;
+ }
}
catch (WebException e)
{
throw CouchException.Create("Failed to create bulk documents", e);
}
}
- public void SaveArbitraryDocuments<T>(IList<T> documents, int chunkCount, List<CouchViewDefinition> views, bool allOrNothing)
+ public void SaveArbitraryDocuments<T>(IEnumerable<T> documents, int chunkCount, List<CouchViewDefinition> views, bool allOrNothing)
{
SaveDocuments(
- (IList<ICouchDocument>)documents.Select(doc => new CouchDocumentWrapper<T>(doc)).ToList(),
+ documents.Select(doc => new CouchDocumentWrapper<T>(doc)).Cast<ICouchDocument>(),
chunkCount,
views,
allOrNothing);
@@ -523,7 +528,7 @@ public void SaveArbitraryDocuments<T>(IList<T> documents, int chunkCount, List<C
/// <param name="documents">List of documents to store.</param>
/// <param name="chunkCount">Number of documents to store per "POST"</param>
/// <param name="views">List of views to touch per chunk.</param>
- public void SaveDocuments(IList<ICouchDocument> documents, int chunkCount, List<CouchViewDefinition> views, bool allOrNothing)
+ public void SaveDocuments(IEnumerable<ICouchDocument> documents, int chunkCount, List<CouchViewDefinition> views, bool allOrNothing)
{
var chunk = new List<ICouchDocument>(chunkCount);
int counter = 0;
@@ -577,43 +582,24 @@ public void TouchViews(List<CouchViewDefinition> views)
/// </summary>
/// <param name="documents">List of documents to store.</param>
/// <param name="chunkCount">Number of documents to store per "POST"</param>
- public void SaveDocuments(IList<ICouchDocument> documents, int chunkCount, bool allOrNothing)
+ public void SaveDocuments(IEnumerable<ICouchDocument> documents, int chunkCount, bool allOrNothing)
{
SaveDocuments(documents, chunkCount, null, allOrNothing);
}
- public void SaveArbitraryDocuments<T>(IList<T> documents, int chunkCount, bool allOrNothing)
+ public void SaveArbitraryDocuments<T>(IEnumerable<T> documents, int chunkCount, bool allOrNothing)
{
SaveArbitraryDocuments(documents, chunkCount, null, allOrNothing);
}
-
- /// <summary>
- /// Get multiple documents.
- /// </summary>
- /// <param name="documentIds">List of documents to get.</param>
- public IList<T> GetDocuments<T>(IList<string> documentIds) where T : ICouchDocument, new()
- {
- return GetDocuments<T>(documentIds.ToArray());
- }
-
- public IList<T> GetArbitraryDocuments<T>(IList<string> documentIds, Func<T> ctor)
- {
- return GetArbitraryDocuments<T>(documentIds.ToArray(), ctor);
- }
-
- public IList<CouchJsonDocument> GetDocuments(IList<string> documentIds)
- {
- return GetDocuments<CouchJsonDocument>(documentIds);
- }
-
- public IList<CouchJsonDocument> GetDocuments(string[] documentIds)
+
+ public IEnumerable<CouchJsonDocument> GetDocuments(IEnumerable<string> documentIds)
{
return GetDocuments<CouchJsonDocument>(documentIds);
}
-
- public IList<T> GetDocuments<T>(string[] documentIds) where T : ICouchDocument, new()
+
+ public IEnumerable<T> GetDocuments<T>(IEnumerable<string> documentIds) where T : ICouchDocument, new()
{
- var bulk = new CouchBulkKeys(documentIds);
+ var bulk = new CouchBulkKeys(documentIds.Cast<object>());
return QueryAllDocuments().Data(CouchDocument.WriteJson(bulk)).IncludeDocuments().GetResult().Documents<T>();
}
@@ -646,9 +632,9 @@ public T GetArbitraryDocument<T>(string documentId, Func<T> ctor)
return doc.Instance;
}
- public IList<T> GetArbitraryDocuments<T>(string[] documentIds, Func<T> ctor)
+ public IEnumerable<T> GetArbitraryDocuments<T>(IEnumerable<string> documentIds, Func<T> ctor)
{
- var bulk = new CouchBulkKeys(documentIds);
+ var bulk = new CouchBulkKeys(documentIds.Cast<object>());
return QueryAllDocuments().Data(CouchDocument.WriteJson(bulk)).IncludeDocuments().GetResult().ArbitraryDocuments<T>(ctor);
}
@@ -724,30 +710,21 @@ public void DeleteDocument(string id, string rev)
}
/// <summary>
- /// Delete documents in bulk fashion.
- /// </summary>
- /// <param name="documents">List of documents to delete.</param>
- public void DeleteDocuments(IList<ICouchDocument> documents)
- {
- DeleteDocuments(documents.ToArray());
- }
-
- /// <summary>
/// Delete documents in key range. This method needs to retrieve
/// revisions and then use them to post a bulk delete. Couch can not
/// delete documents without being told about their revisions.
/// </summary>
public void DeleteDocuments(string startKey, string endKey)
{
- IList<CouchQueryDocument> docs = QueryAllDocuments().StartKey(startKey).EndKey(endKey).GetResult().RowDocuments();
- DeleteDocuments(docs.ToArray());
+ var docs = QueryAllDocuments().StartKey(startKey).EndKey(endKey).GetResult().RowDocuments().Cast<ICouchDocument>();
+ DeleteDocuments(docs);
}
/// <summary>
/// Delete documents in bulk fashion.
/// </summary>
/// <param name="documents">Array of documents to delete.</param>
- public void DeleteDocuments(ICouchDocument[] documents)
+ public void DeleteDocuments(IEnumerable<ICouchDocument> documents)
{
DeleteDocuments(new CouchBulkDeleteDocuments(documents));
}
View
28 src/CouchGenericViewResult.cs
@@ -17,7 +17,7 @@ public class CouchGenericViewResult : CouchViewResult
/// </summary>
/// <typeparam name="T">Type of value.</typeparam>
/// <returns>All found values.</returns>
- public IList<T> Values<T>() where T : new()
+ public IEnumerable<T> Values<T>() where T : new()
{
var list = new List<T>();
foreach (JToken row in Rows())
@@ -32,7 +32,7 @@ public IList<T> Values<T>() where T : new()
/// </summary>
/// <typeparam name="T">Type of value.</typeparam>
/// <returns>All found values.</returns>
- public IList<T> ValueDocuments<T>() where T : ICanJson, new()
+ public IEnumerable<T> ValueDocuments<T>() where T : ICanJson, new()
{
return RetrieveDocuments<T>("value");
}
@@ -42,12 +42,12 @@ public IList<T> ValueDocuments<T>() where T : ICanJson, new()
/// </summary>
/// <typeparam name="T">Type of value.</typeparam>
/// <returns>All found documents.</returns>
- public IList<T> ValueDocumentsWithIds<T>() where T : ICouchDocument, new()
+ public IEnumerable<T> ValueDocumentsWithIds<T>() where T : ICouchDocument, new()
{
return RetrieveDocumentsWithIds<T>("value");
}
- public IList<T> ValueDocuments<T>(Func<T> ctor)
+ public IEnumerable<T> ValueDocuments<T>(Func<T> ctor)
{
return RetrieveArbitraryDocuments<T>("value", ctor);
}
@@ -67,7 +67,7 @@ public T ArbitraryValueDocument<T>(Func<T> ctor)
return RetrieveArbitraryDocument<T>("value", ctor);
}
- public IList<T> ArbitraryValueDocuments<T>(Func<T> ctor)
+ public IEnumerable<T> ArbitraryValueDocuments<T>(Func<T> ctor)
{
return RetrieveArbitraryDocuments<T>("value", ctor);
}
@@ -77,12 +77,12 @@ public IList<T> ArbitraryValueDocuments<T>(Func<T> ctor)
/// </summary>
/// <typeparam name="T">Type of documents.</typeparam>
/// <returns>List of documents found.</returns>
- public IList<T> Documents<T>() where T : ICouchDocument, new()
+ public IEnumerable<T> Documents<T>() where T : ICouchDocument, new()
{
return RetrieveDocuments<T>("doc");
}
- public IList<T> ArbitraryDocuments<T>(Func<T> ctor)
+ public IEnumerable<T> ArbitraryDocuments<T>(Func<T> ctor)
{
return RetrieveArbitraryDocuments<T>("doc", ctor);
}
@@ -91,7 +91,7 @@ public IList<T> ArbitraryDocuments<T>(Func<T> ctor)
/// Return all found docs as CouchJsonDocuments.
/// </summary>
/// <returns>List of documents found.</returns>
- public IList<CouchJsonDocument> Documents()
+ public IEnumerable<CouchJsonDocument> Documents()
{
return RetrieveDocuments<CouchJsonDocument>("doc");
}
@@ -111,7 +111,7 @@ public T ArbitraryDocument<T>(Func<T> ctor)
return RetrieveArbitraryDocument<T>("doc", ctor);
}
- protected virtual IList<T> RetrieveDocuments<T>(string docOrValue) where T : ICanJson, new()
+ protected virtual IEnumerable<T> RetrieveDocuments<T>(string docOrValue) where T : ICanJson, new()
{
var list = new List<T>();
foreach (JToken row in Rows())
@@ -126,7 +126,7 @@ protected virtual IList<T> RetrieveDocuments<T>(string docOrValue) where T : ICa
return list;
}
- protected virtual IList<T> RetrieveDocumentsWithIds<T>(string docOrValue) where T : ICouchDocument, new()
+ protected virtual IEnumerable<T> RetrieveDocumentsWithIds<T>(string docOrValue) where T : ICouchDocument, new()
{
var list = new List<T>();
var found = new Dictionary<string, T>();
@@ -156,7 +156,7 @@ protected virtual T RetrieveDocument<T>(string docOrValue) where T : ICanJson, n
}
return default(T);
}
- protected virtual IList<T> RetrieveArbitraryDocuments<T>(string docOrValue, Func<T> ctor)
+ protected virtual IEnumerable<T> RetrieveArbitraryDocuments<T>(string docOrValue, Func<T> ctor)
{
var list = new List<T>();
foreach (JToken row in Rows())
@@ -179,12 +179,12 @@ protected virtual T RetrieveArbitraryDocument<T>(string docOrValue, Func<T> ctor
return default(T);
}
- public IList<CouchQueryDocument> RowDocuments()
+ public IEnumerable<CouchQueryDocument> RowDocuments()
{
return RowDocuments<CouchQueryDocument>();
}
- public IList<T> RowDocuments<T>() where T : ICanJson, new()
+ public IEnumerable<T> RowDocuments<T>() where T : ICanJson, new()
{
var list = new List<T>();
foreach (JObject row in Rows())
@@ -196,7 +196,7 @@ public IList<T> RowDocuments<T>() where T : ICanJson, new()
return list;
}
- public IList<T> ArbitraryRowDocuments<T>(Func<T> ctor)
+ public IEnumerable<T> ArbitraryRowDocuments<T>(Func<T> ctor)
{
var list = new List<T>();
foreach (JObject row in Rows())
View
8 src/CouchViewDefinition.cs
@@ -76,22 +76,22 @@ public void ReadJson(JObject obj)
/// <summary>
/// Utility methods to make queries shorter.
/// </summary>
- public IList<T> Key<T>(object key) where T : ICouchDocument, new()
+ public IEnumerable<T> Key<T>(object key) where T : ICouchDocument, new()
{
return Query().Key(key).IncludeDocuments().GetResult().Documents<T>();
}
- public IList<T> KeyStartEnd<T>(object start, object end) where T : ICouchDocument, new()
+ public IEnumerable<T> KeyStartEnd<T>(object start, object end) where T : ICouchDocument, new()
{
return Query().StartKey(start).EndKey(end).IncludeDocuments().GetResult().Documents<T>();
}
- public IList<T> KeyStartEnd<T>(object[] start, object[] end) where T : ICouchDocument, new()
+ public IEnumerable<T> KeyStartEnd<T>(object[] start, object[] end) where T : ICouchDocument, new()
{
return Query().StartKey(start).EndKey(end).IncludeDocuments().GetResult().Documents<T>();
}
- public IList<T> All<T>() where T : ICouchDocument, new()
+ public IEnumerable<T> All<T>() where T : ICouchDocument, new()
{
return Query().IncludeDocuments().GetResult().Documents<T>();
}
View
7 src/Lucene/CouchLuceneViewResult.cs
@@ -76,7 +76,7 @@ public JEnumerable<JToken> Rows()
/// Return all hits with all meta info. A hit can be told to retrieve its CouchDocument,
/// if you used IncludeDocuments() in the query.
/// </summary>
- public virtual IList<CouchLuceneHit> Hits()
+ public virtual IEnumerable<CouchLuceneHit> Hits()
{
var hits = new List<CouchLuceneHit>();
foreach (JObject row in Rows())
@@ -90,15 +90,16 @@ public virtual IList<CouchLuceneHit> Hits()
/// Extract documents from hits or perform a bulk retrieval of the documents
/// that was returned by this query. Note that this may be a subset of TotalCount().
/// </summary>
- public virtual IList<T> GetDocuments<T>() where T : ICouchDocument, new()
+ public virtual IEnumerable<T> GetDocuments<T>() where T : ICouchDocument, new()
{
var docs = new List<T>();
var hits = Hits();
- if (hits.Count == 0)
+ if (!hits.Any())
{
return docs;
}
+
var firstHit = hits.First();
var db = view.Db();
if (firstHit.HasDocument())

0 comments on commit d4e06f3

Please sign in to comment.