public Task<string> GenerateDocumentKeyAsync(IAsyncDatabaseCommands databaseCommands, DocumentConvention conventions, object entity)
		{
			var shardId = shardedDocumentStore.ShardStrategy.ShardResolutionStrategy.MetadataShardIdFor(entity);
			if (shardId == null)
				throw new InvalidOperationException(string.Format(
					"ShardResolutionStrategy.MetadataShardIdFor cannot return null. You must specify where to store the metadata documents for the entity type '{0}'.", entity.GetType().FullName));

			AsyncMultiTypeHiLoKeyGenerator value;
			if (generatorsByShard.TryGetValue(shardId, out value))
				return value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity);

			lock (this)
			{
				if (generatorsByShard.TryGetValue(shardId, out value) == false)
				{
					value = new AsyncMultiTypeHiLoKeyGenerator(capacity);
					generatorsByShard = new Dictionary<string, AsyncMultiTypeHiLoKeyGenerator>(generatorsByShard)
					                    	{
					                    		{shardId, value}
					                    	};
				}
			}

			return value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity);
		}
Example #2
0
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        /// <returns></returns>
        public override IDocumentStore Initialize()
        {
            if (initialized)
            {
                return(this);
            }

            AssertValidConfiguration();

#if !SILVERLIGHT
            jsonRequestFactory = new HttpJsonRequestFactory(MaxNumberOfCachedRequests);
#else
            jsonRequestFactory = new HttpJsonRequestFactory();
#endif
            try
            {
                InitializeProfiling();

                InitializeInternal();

                InitializeSecurity();

#if !SILVERLIGHT
                if (Conventions.DocumentKeyGenerator == null)                // don't overwrite what the user is doing
                {
                    var generator = new MultiTypeHiLoKeyGenerator(32);
                    Conventions.DocumentKeyGenerator = (databaseCommands, entity) => generator.GenerateDocumentKey(databaseCommands, Conventions, entity);
                }
#endif

#if !NET35
                if (Conventions.AsyncDocumentKeyGenerator == null && asyncDatabaseCommandsGenerator != null)
                {
#if !SILVERLIGHT
                    var generator = new AsyncMultiTypeHiLoKeyGenerator(32);
                    Conventions.AsyncDocumentKeyGenerator = (commands, entity) => generator.GenerateDocumentKeyAsync(commands, Conventions, entity);
#else
                    Conventions.AsyncDocumentKeyGenerator = (commands, entity) =>
                    {
                        var typeTagName = Conventions.GetTypeTagName(entity.GetType());
                        if (typeTagName == null)
                        {
                            return(CompletedTask.With(Guid.NewGuid().ToString()));
                        }
                        return(CompletedTask.With(typeTagName + "/" + Guid.NewGuid()));
                    };
#endif
                }
#endif

                initialized = true;

#if !SILVERLIGHT
                RecoverPendingTransactions();

                if (string.IsNullOrEmpty(DefaultDatabase) == false)
                {
                    DatabaseCommands.ForDefaultDatabase().EnsureDatabaseExists(DefaultDatabase, ignoreFailures: true);
                }
#endif
            }
            catch (Exception)
            {
                Dispose();
                throw;
            }

            return(this);
        }