PageRenderTime 31ms CodeModel.GetById 25ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Raven.Database/Server/Controllers/DatabasesController.cs

https://github.com/nwendel/ravendb
C# | 124 lines | 98 code | 19 blank | 7 comment | 11 complexity | dd19923ad07d97b6db228315285ae4c9 MD5 | raw file
  1using System.Collections.Generic;
  2using System.Linq;
  3using System.Net;
  4using System.Net.Http;
  5using System.Threading;
  6using System.Web.Http;
  7using Raven.Abstractions.Data;
  8using Raven.Database.Extensions;
  9using Raven.Database.Server.Security;
 10using Raven.Database.Util;
 11using Raven.Json.Linq;
 12
 13namespace Raven.Database.Server.Controllers
 14{
 15	public class DatabasesController : RavenDbApiController
 16	{
 17		[HttpGet]
 18		[Route("databases")]
 19		public HttpResponseMessage Databases(bool getAdditionalData = false)
 20		{
 21			if (EnsureSystemDatabase() == false)
 22				return
 23					GetMessageWithString(
 24						"The request '" + InnerRequest.RequestUri.AbsoluteUri + "' can only be issued on the system database",
 25						HttpStatusCode.BadRequest);
 26
 27			// This method is NOT secured, and anyone can access it.
 28			// Because of that, we need to provide explicit security here.
 29
 30			// Anonymous Access - All / Get / Admin
 31			// Show all dbs
 32
 33			// Anonymous Access - None
 34			// Show only the db that you have access to (read / read-write / admin)
 35
 36			// If admin, show all dbs
 37
 38			var start = GetStart();
 39			var nextPageStart = start; // will trigger rapid pagination
 40			var databases = Database.Documents.GetDocumentsWithIdStartingWith("Raven/Databases/", null, null, start,
 41										GetPageSize(Database.Configuration.MaxPageSize), CancellationToken.None, ref nextPageStart);
 42
 43			var databasesData = databases
 44				.Select(database =>
 45					new
 46					{
 47						Name = database.Value<RavenJObject>("@metadata").Value<string>("@id").Replace("Raven/Databases/", string.Empty),
 48						Disabled = database.Value<bool>("Disabled")
 49					}).ToList();
 50
 51			var databasesNames = databasesData.Select(databaseObject => databaseObject.Name).ToArray();
 52
 53			List<string> approvedDatabases = null;
 54			if (DatabasesLandlord.SystemConfiguration.AnonymousUserAccessMode == AnonymousUserAccessMode.None)
 55			{
 56				var user = User;
 57				if (user == null)
 58					return null;
 59
 60				if (user.IsAdministrator(DatabasesLandlord.SystemConfiguration.AnonymousUserAccessMode) == false)
 61				{
 62					var authorizer = (MixedModeRequestAuthorizer)this.ControllerContext.Configuration.Properties[typeof(MixedModeRequestAuthorizer)];
 63
 64					approvedDatabases = authorizer.GetApprovedResources(user, this, databasesNames);
 65				}
 66			}
 67
 68			var lastDocEtag = Etag.Empty;
 69			Database.TransactionalStorage.Batch(accessor =>
 70			{
 71				lastDocEtag = accessor.Staleness.GetMostRecentDocumentEtag();
 72			});
 73
 74			if (MatchEtag(lastDocEtag))
 75				return GetEmptyMessage(HttpStatusCode.NotModified);
 76
 77			if (approvedDatabases != null)
 78			{
 79				databasesData = databasesData.Where(databaseData => approvedDatabases.Contains(databaseData.Name)).ToList();
 80				databasesNames = databasesNames.Where(databaseName => approvedDatabases.Contains(databaseName)).ToArray();
 81			}
 82
 83			var responseMessage = getAdditionalData ? GetMessageWithObject(databasesData) : GetMessageWithObject(databasesNames);
 84			WriteHeaders(new RavenJObject(), lastDocEtag, responseMessage);
 85			return responseMessage;
 86		}
 87
 88		[HttpGet]
 89		[Route("database/size")]
 90		[Route("databases/{databaseName}/database/size")]
 91		public HttpResponseMessage DatabaseSize()
 92		{
 93			var totalSizeOnDisk = Database.GetTotalSizeOnDisk();
 94			return GetMessageWithObject(new
 95			{
 96				DatabaseSize = totalSizeOnDisk,
 97				DatabaseSizeHumane = SizeHelper.Humane(totalSizeOnDisk)
 98			});
 99		}
100
101		[HttpGet]
102		[Route("database/storage/sizes")]
103		[Route("databases/{databaseName}/database/storage/sizes")]
104		public HttpResponseMessage DatabaseStorageSizes()
105		{
106			var indexStorageSize = Database.GetIndexStorageSizeOnDisk();
107			var transactionalStorageSize = Database.GetTransactionalStorageSizeOnDisk();
108			var totalDatabaseSize = indexStorageSize + transactionalStorageSize.AllocatedSizeInBytes;
109			return GetMessageWithObject(new
110			{
111				TransactionalStorageAllocatedSize = transactionalStorageSize.AllocatedSizeInBytes,
112				TransactionalStorageAllocatedSizeHumaneSize = SizeHelper.Humane(transactionalStorageSize.AllocatedSizeInBytes),
113				TransactionalStorageUsedSize = transactionalStorageSize.UsedSizeInBytes,
114				TransactionalStorageUsedSizeHumaneSize = SizeHelper.Humane(transactionalStorageSize.UsedSizeInBytes),
115				IndexStorageSize = indexStorageSize,
116				IndexStorageSizeHumane = SizeHelper.Humane(indexStorageSize),
117				TotalDatabaseSize = totalDatabaseSize,
118				TotalDatabaseSizeHumane = SizeHelper.Humane(totalDatabaseSize),
119			});
120		}
121
122
123	}
124}