Skip to content
This repository has been archived by the owner on Sep 17, 2023. It is now read-only.

Commit

Permalink
Resurrect search (#195)
Browse files Browse the repository at this point in the history
* obsolete disk_size and data_size

* ibmcom/couchdb3 docker image

* search comments removed
  • Loading branch information
iojancode authored Aug 20, 2020
1 parent 6367571 commit d02c495
Show file tree
Hide file tree
Showing 25 changed files with 1,345 additions and 1,320 deletions.
2 changes: 1 addition & 1 deletion azure-pipelines-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pr: none
resources:
containers:
- container: couchdb
image: couchdb:2.3.1
image: ibmcom/couchdb3:3.1.0
ports:
- 5984:5984
env:
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pr:
resources:
containers:
- container: couchdb
image: couchdb:2.3.1
image: ibmcom/couchdb3:3.1.0
ports:
- 5984:5984
env:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3'
services:
couchdb:
image: "couchdb:2.3.1"
image: "ibmcom/couchdb3:3.1.0"
environment:
COUCHDB_USER: ${MyCouch_User}
COUCHDB_PASSWORD: ${MyCouch_Pass}
Expand Down
102 changes: 51 additions & 51 deletions source/projects/MyCouch/Contexts/Searches.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
//using System.Threading.Tasks;
//using EnsureThat;
//using MyCouch.Extensions;
//using MyCouch.HttpRequestFactories;
//using MyCouch.Requests;
//using MyCouch.Responses;
//using MyCouch.Responses.Factories;
//using MyCouch.Serialization;

//namespace MyCouch.Contexts
//{
// public class Searches : ApiContextBase<IDbConnection>, ISearches
// {
// protected SearchIndexHttpRequestFactory SearchIndexHttpRequestFactory { get; set; }
// protected SearchIndexResponseFactory SearchIndexResponseFactory { get; set; }

// public Searches(IDbConnection connection, ISerializer documentSerializer, ISerializer serializer)
// : base(connection)
// {
// Ensure.That(documentSerializer, "documentSerializer").IsNotNull();
// Ensure.Any.IsNotNull(serializer, nameof(serializer));

// SearchIndexHttpRequestFactory = new SearchIndexHttpRequestFactory(serializer);
// SearchIndexResponseFactory = new SearchIndexResponseFactory(documentSerializer);
// }

// public virtual async Task<SearchIndexResponse> SearchAsync(SearchIndexRequest request)
// {
// Ensure.Any.IsNotNull(request, nameof(request));

// var httpRequest = SearchIndexHttpRequestFactory.Create(request);

// using (var res = await SendAsync(httpRequest).ForAwait())
// {
// return await SearchIndexResponseFactory.CreateAsync(res).ForAwait();
// }
// }

// public virtual async Task<SearchIndexResponse<TIncludedDoc>> SearchAsync<TIncludedDoc>(SearchIndexRequest request)
// {
// Ensure.Any.IsNotNull(request, nameof(request));

// var httpRequest = SearchIndexHttpRequestFactory.Create(request);

// using (var res = await SendAsync(httpRequest).ForAwait())
// {
// return await SearchIndexResponseFactory.CreateAsync<TIncludedDoc>(res).ForAwait();
// }
// }
// }
//}
using System.Threading.Tasks;
using EnsureThat;
using MyCouch.Extensions;
using MyCouch.HttpRequestFactories;
using MyCouch.Requests;
using MyCouch.Responses;
using MyCouch.Responses.Factories;
using MyCouch.Serialization;

namespace MyCouch.Contexts
{
public class Searches : ApiContextBase<IDbConnection>, ISearches
{
protected SearchIndexHttpRequestFactory SearchIndexHttpRequestFactory { get; set; }
protected SearchIndexResponseFactory SearchIndexResponseFactory { get; set; }

public Searches(IDbConnection connection, ISerializer documentSerializer, ISerializer serializer)
: base(connection)
{
Ensure.That(documentSerializer, "documentSerializer").IsNotNull();
Ensure.Any.IsNotNull(serializer, nameof(serializer));

SearchIndexHttpRequestFactory = new SearchIndexHttpRequestFactory(serializer);
SearchIndexResponseFactory = new SearchIndexResponseFactory(documentSerializer);
}

public virtual async Task<SearchIndexResponse> SearchAsync(SearchIndexRequest request)
{
Ensure.Any.IsNotNull(request, nameof(request));

var httpRequest = SearchIndexHttpRequestFactory.Create(request);

using (var res = await SendAsync(httpRequest).ForAwait())
{
return await SearchIndexResponseFactory.CreateAsync(res).ForAwait();
}
}

public virtual async Task<SearchIndexResponse<TIncludedDoc>> SearchAsync<TIncludedDoc>(SearchIndexRequest request)
{
Ensure.Any.IsNotNull(request, nameof(request));

var httpRequest = SearchIndexHttpRequestFactory.Create(request);

using (var res = await SendAsync(httpRequest).ForAwait())
{
return await SearchIndexResponseFactory.CreateAsync<TIncludedDoc>(res).ForAwait();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,110 +1,110 @@
//using System.Collections.Generic;
//using System.Linq;
//using System.Net.Http;
//using EnsureThat;
//using MyCouch.Net;
//using MyCouch.Requests;
//using MyCouch.Serialization;

//namespace MyCouch.HttpRequestFactories
//{
// public class SearchIndexHttpRequestFactory
// {
// protected ISerializer Serializer { get; private set; }

// public SearchIndexHttpRequestFactory(ISerializer serializer)
// {
// Ensure.Any.IsNotNull(serializer, nameof(serializer));

// Serializer = serializer;
// }

// public virtual HttpRequest Create(SearchIndexRequest request)
// {
// Ensure.Any.IsNotNull(request, nameof(request));

// return new HttpRequest(HttpMethod.Get, GenerateRelativeUrl(request))
// .SetRequestTypeHeader(request.GetType());
// }

// protected virtual string GenerateRelativeUrl(SearchIndexRequest request)
// {
// return string.Format("/_design/{0}/_search/{1}{2}",
// new UrlSegment(request.IndexIdentity.DesignDocument),
// new UrlSegment(request.IndexIdentity.Name),
// GenerateRequestUrlQueryString(request));
// }

// protected virtual string GenerateRequestUrlQueryString(SearchIndexRequest request)
// {
// var p = GenerateQueryStringParams(request);

// return string.IsNullOrEmpty(p) ? string.Empty : string.Concat("?", p);
// }

// protected virtual string GenerateQueryStringParams(SearchIndexRequest request)
// {
// return string.Join("&", GenerateJsonCompatibleKeyValues(request)
// .Select(kv => string.Format("{0}={1}", kv.Key, UrlParam.Encode(kv.Value))));
// }

// protected virtual IDictionary<string, string> GenerateJsonCompatibleKeyValues(SearchIndexRequest request)
// {
// var kvs = new Dictionary<string, string>();

// if (!string.IsNullOrWhiteSpace(request.Expression))
// kvs.Add(KeyNames.Expression, request.Expression);

// if (request.HasSortings())
// kvs.Add(KeyNames.Sort, Serializer.ToJsonArray(request.Sort.ToArray()));

// if (!string.IsNullOrWhiteSpace(request.Bookmark))
// kvs.Add(KeyNames.Bookmark, request.Bookmark);

// if (request.Stale.HasValue)
// kvs.Add(KeyNames.Stale, request.Stale.Value.AsString());

// if (request.Limit.HasValue)
// kvs.Add(KeyNames.Limit, Serializer.ToJson(request.Limit.Value));

// if (request.IncludeDocs.HasValue)
// kvs.Add(KeyNames.IncludeDocs, Serializer.ToJson(request.IncludeDocs.Value));
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using EnsureThat;
using MyCouch.Net;
using MyCouch.Requests;
using MyCouch.Serialization;

namespace MyCouch.HttpRequestFactories
{
public class SearchIndexHttpRequestFactory
{
protected ISerializer Serializer { get; private set; }

public SearchIndexHttpRequestFactory(ISerializer serializer)
{
Ensure.Any.IsNotNull(serializer, nameof(serializer));

Serializer = serializer;
}

public virtual HttpRequest Create(SearchIndexRequest request)
{
Ensure.Any.IsNotNull(request, nameof(request));

return new HttpRequest(HttpMethod.Get, GenerateRelativeUrl(request))
.SetRequestTypeHeader(request.GetType());
}

protected virtual string GenerateRelativeUrl(SearchIndexRequest request)
{
return string.Format("/_design/{0}/_search/{1}{2}",
new UrlSegment(request.IndexIdentity.DesignDocument),
new UrlSegment(request.IndexIdentity.Name),
GenerateRequestUrlQueryString(request));
}

protected virtual string GenerateRequestUrlQueryString(SearchIndexRequest request)
{
var p = GenerateQueryStringParams(request);

return string.IsNullOrEmpty(p) ? string.Empty : string.Concat("?", p);
}

protected virtual string GenerateQueryStringParams(SearchIndexRequest request)
{
return string.Join("&", GenerateJsonCompatibleKeyValues(request)
.Select(kv => string.Format("{0}={1}", kv.Key, UrlParam.Encode(kv.Value))));
}

protected virtual IDictionary<string, string> GenerateJsonCompatibleKeyValues(SearchIndexRequest request)
{
var kvs = new Dictionary<string, string>();

if (!string.IsNullOrWhiteSpace(request.Expression))
kvs.Add(KeyNames.Expression, request.Expression);

if (request.HasSortings())
kvs.Add(KeyNames.Sort, Serializer.ToJsonArray(request.Sort.ToArray()));

if (!string.IsNullOrWhiteSpace(request.Bookmark))
kvs.Add(KeyNames.Bookmark, request.Bookmark);

if (request.Stale.HasValue)
kvs.Add(KeyNames.Stale, request.Stale.Value.AsString());

if (request.Limit.HasValue)
kvs.Add(KeyNames.Limit, Serializer.ToJson(request.Limit.Value));

if (request.IncludeDocs.HasValue)
kvs.Add(KeyNames.IncludeDocs, Serializer.ToJson(request.IncludeDocs.Value));

// if (request.Ranges != null)
// kvs.Add(KeyNames.Ranges, Serializer.Serialize(request.Ranges));

// if (request.HasCounts())
// kvs.Add(KeyNames.Counts, Serializer.ToJsonArray(request.Counts.ToArray()));

// if (!string.IsNullOrWhiteSpace(request.GroupField))
// kvs.Add(KeyNames.GroupField, request.GroupField);

// if (request.GroupLimit.HasValue)
// kvs.Add(KeyNames.GroupLimit, Serializer.ToJson(request.GroupLimit.Value));

// if (request.HasGroupSortings())
// kvs.Add(KeyNames.GroupSort, Serializer.ToJsonArray(request.GroupSort.ToArray()));

// if (request.DrillDown.HasValue)
// kvs.Add(KeyNames.DrillDown, Serializer.ToJsonArray(new[] { request.DrillDown.Value.Key, request.DrillDown.Value.Value }));

// return kvs;
// }

// protected static class KeyNames
// {
// public const string Expression = "q";
// public const string Sort = "sort";
// public const string Bookmark = "bookmark";
// public const string Stale = "stale";
// public const string Limit = "limit";
// public const string IncludeDocs = "include_docs";
// public const string Ranges = "ranges";
// public const string Counts = "counts";
// public const string GroupField = "group_field";
// public const string GroupLimit = "group_limit";
// public const string GroupSort = "group_sort";
// public const string DrillDown = "drilldown";
// }
// }
//}
if (request.Ranges != null)
kvs.Add(KeyNames.Ranges, Serializer.Serialize(request.Ranges));

if (request.HasCounts())
kvs.Add(KeyNames.Counts, Serializer.ToJsonArray(request.Counts.ToArray()));

if (!string.IsNullOrWhiteSpace(request.GroupField))
kvs.Add(KeyNames.GroupField, request.GroupField);

if (request.GroupLimit.HasValue)
kvs.Add(KeyNames.GroupLimit, Serializer.ToJson(request.GroupLimit.Value));

if (request.HasGroupSortings())
kvs.Add(KeyNames.GroupSort, Serializer.ToJsonArray(request.GroupSort.ToArray()));

if (request.DrillDown.HasValue)
kvs.Add(KeyNames.DrillDown, Serializer.ToJsonArray(new[] { request.DrillDown.Value.Key, request.DrillDown.Value.Value }));

return kvs;
}

protected static class KeyNames
{
public const string Expression = "q";
public const string Sort = "sort";
public const string Bookmark = "bookmark";
public const string Stale = "stale";
public const string Limit = "limit";
public const string IncludeDocs = "include_docs";
public const string Ranges = "ranges";
public const string Counts = "counts";
public const string GroupField = "group_field";
public const string GroupLimit = "group_limit";
public const string GroupSort = "group_sort";
public const string DrillDown = "drilldown";
}
}
}
2 changes: 1 addition & 1 deletion source/projects/MyCouch/IMyCouchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public interface IMyCouchClient : IDisposable
///// <summary>
///// Used to access Search Indexes.
///// </summary>
//ISearches Searches { get; }
ISearches Searches { get; }

/// <summary>
/// View oriented API operations, for accessing and managing views.
Expand Down
Loading

0 comments on commit d02c495

Please sign in to comment.