Skip to content

Commit

Permalink
Implemented GetDatabaseNames and GetDatabaseNamesAsync for non-Silver…
Browse files Browse the repository at this point in the history
…light clients too
  • Loading branch information
synhershko committed Nov 19, 2011
1 parent beae05a commit 44bf9d6
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Raven.Client.Embedded/EmbeddedDatabaseCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ public void DeleteAttachment(string key, Guid? etag)
database.DeleteStatic(key, etag);
}

public string[] GetDatabaseNames()
{
throw new InvalidOperationException("Embedded mode does not support multi-tenancy");
}

/// <summary>
/// Gets the index names from the server
/// </summary>
Expand Down
14 changes: 12 additions & 2 deletions Raven.Client.Lightweight/Connection/Async/AsyncServerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class AsyncServerClient : IAsyncDatabaseCommands
private readonly ICredentials credentials;
private readonly DocumentConvention convention;
private readonly IDictionary<string, string> operationsHeaders = new Dictionary<string, string>();
private readonly HttpJsonRequestFactory jsonRequestFactory;
internal readonly HttpJsonRequestFactory jsonRequestFactory;
private readonly Guid? sessionId;

/// <summary>
Expand Down Expand Up @@ -615,7 +615,17 @@ public Task<DatabaseStatistics> GetStatisticsAsync()
/// </summary>
public Task<string[]> GetDatabaseNamesAsync()
{
throw new NotImplementedException();
return url.Databases()
.NoCache()
.ToJsonRequest(this, credentials, convention)
.ReadResponseStringAsync()
.ContinueWith(task =>
{
var json = (RavenJArray)RavenJToken.Parse(task.Result);
return json
.Select(x => x.Value<RavenJObject>("@metadata").Value<string>("@id").Replace("Raven/Databases/", string.Empty))
.ToArray();
});
}

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions Raven.Client.Lightweight/Connection/IDatabaseCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public interface IDatabaseCommands : IHoldProfilingInformation
/// <param name="etag">The etag.</param>
void DeleteAttachment(string key, Guid? etag);

/// <summary>
/// Returns the names of all tenant databases on the RavenDB server
/// </summary>
/// <returns>List of tenant database names</returns>
string[] GetDatabaseNames();

/// <summary>
/// Returns the names of all indexes that exist on the server
/// </summary>
Expand Down
95 changes: 95 additions & 0 deletions Raven.Client.Lightweight/Connection/RavenUrlExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Net;
using Raven.Client.Connection.Async;
using Raven.Client.Document;

namespace Raven.Client.Connection
{
public static class RavenUrlExtensions
{
public static string Indexes(this string url, string index)
{
return url + "/indexes/" + index;
}

public static string IndexDefinition(this string url, string index)
{
return url + "/indexes/" + index + "?definition=yes";
}

public static string IndexNames(this string url, int start, int pageSize)
{
return url + "/indexes/?namesOnly=true&start=" + start + "&pageSize=" + pageSize;
}

public static string Stats(this string url)
{
return url + "/stats";
}

//public static string Static(this string url, string key)
//{
// return url + "/static/" + HttpUtility.HtmlEncode(key);
//}

public static string Databases(this string url)
{
return url + "/databases/";
}

public static string SilverlightEnsuresStartup(this string url)
{
return url + "/silverlight/ensureStartup";
}

public static string Terms(this string url, string index, string field, string fromValue, int pageSize)
{
return url + "/terms/" + index + "?field=" + field + "&fromValue=" + fromValue + "&pageSize=" + pageSize;
}

//public static string Docs(this string url, string key)
//{
// return url + "/docs/" + HttpUtility.HtmlEncode(key);
//}

public static string Docs(this string url, int start, int pageSize)
{
return url + "/docs/?start=" + start + "&pageSize=" + pageSize;
}

//public static string DocsStartingWith(this string url, string prefix, int start, int pageSize)
//{
// return Docs(url, start, pageSize) + "&startsWith=" + HttpUtility.HtmlEncode(prefix);
//}

public static string Queries(this string url)
{
return url + "/queries/";
}

public static string NoCache(this string url)
{
return (url.Contains("?"))
? url + "&noCache=" + Guid.NewGuid().GetHashCode()
: url + "?noCache=" + Guid.NewGuid().GetHashCode();
}

public static Uri ToUri(this string url)
{
return new Uri(url);
}

public static HttpJsonRequest ToJsonRequest(this string url, AsyncServerClient requestor, ICredentials credentials, Document.DocumentConvention convention)
{
return requestor.jsonRequestFactory.CreateHttpJsonRequest(requestor, url, "GET", credentials, convention);
}

public static HttpJsonRequest ToJsonRequest(this string url, AsyncServerClient requestor, ICredentials credentials, DocumentConvention convention, IDictionary<string, string> operationsHeaders, string method)
{
var httpJsonRequest = requestor.jsonRequestFactory.CreateHttpJsonRequest(requestor, url, method, credentials, convention);
httpJsonRequest.AddOperationHeaders(operationsHeaders);
return httpJsonRequest;
}
}
}
11 changes: 11 additions & 0 deletions Raven.Client.Lightweight/Connection/ServerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,17 @@ public void DeleteAttachment(string key, Guid? etag)
ExecuteWithReplication("DELETE", operationUrl => DirectDeleteAttachment(key, etag, operationUrl));
}

public string[] GetDatabaseNames()
{
var result = ExecuteGetRequest(url.Databases().NoCache());

var json = (RavenJArray) RavenJToken.Parse(result);

return json
.Select(x => x.Value<RavenJObject>("@metadata").Value<string>("@id").Replace("Raven/Databases/", string.Empty))
.ToArray();
}

private void DirectDeleteAttachment(string key, Guid? etag, string operationUrl)
{
var webRequest = WebRequest.Create(operationUrl + "/static/" + key);
Expand Down
1 change: 1 addition & 0 deletions Raven.Client.Lightweight/Raven.Client.Lightweight.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<Compile Include="Connection\Profiling\ProfilingInformation.cs" />
<Compile Include="Connection\RavenQuery.cs" />
<Compile Include="Connection\RavenTransactionAccessor.cs" />
<Compile Include="Connection\RavenUrlExtensions.cs" />
<Compile Include="Connection\ReplicationInformer.cs" />
<Compile Include="Connection\SerializationHelper.cs" />
<Compile Include="Connection\ServerClient.cs" />
Expand Down

0 comments on commit 44bf9d6

Please sign in to comment.