Skip to content

Commit

Permalink
Switched everywhere from IList to IEnumerable (since there is no bene…
Browse files Browse the repository at this point in the history
…fit in having list functionality)

Switched everywhere from arrays to IEnumerable (same reason as the IList switch)
  • Loading branch information
Sebastian Negomireanu committed Dec 10, 2009
1 parent 1e0e519 commit b62ce1d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 89 deletions.
4 changes: 2 additions & 2 deletions samples/Trivial/Program.cs
Expand Up @@ -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);");
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/CouchBulkDeleteDocuments.cs
Expand Up @@ -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)
{
}

Expand Down
7 changes: 4 additions & 3 deletions src/CouchBulkDocuments.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;

namespace Divan
{
Expand All @@ -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)
Expand Down
101 changes: 39 additions & 62 deletions src/CouchDatabase.cs
Expand Up @@ -25,7 +25,8 @@ public CouchDatabase()
Name = "default";
}

public CouchDatabase(CouchServer server) : this()
public CouchDatabase(CouchServer server)
: this()
{
Server = server;
}
Expand All @@ -44,7 +45,8 @@ public string Name
return name;
return Server.DatabasePrefix + name;
}
set {
set
{
name = value;
}
}
Expand Down Expand Up @@ -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>();
}
Expand All @@ -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>();
}
Expand All @@ -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>();

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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>
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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>();
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -723,31 +709,22 @@ public void DeleteDocument(string id, string rev)
Request(id).Query("?rev=" + rev).Delete().Check("Failed to delete document");
}

/// <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));
}
Expand Down
28 changes: 14 additions & 14 deletions src/CouchGenericViewResult.cs
Expand Up @@ -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())
Expand All @@ -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");
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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");
}
Expand All @@ -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())
Expand All @@ -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>();
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand Down

0 comments on commit b62ce1d

Please sign in to comment.