public Task <string> GenerateDocumentKeyAsync(IAsyncDatabaseCommands databaseCommands, DocumentConvention conventions, object entity) { var typeTagName = conventions.GetDynamicTagName(entity); if (string.IsNullOrEmpty(typeTagName)) //ignore empty tags { return(CompletedTask.With <string>(null)); } var tag = conventions.TransformTypeTagNameToDocumentKeyPrefix(typeTagName); AsyncHiLoKeyGenerator value; if (keyGeneratorsByTag.TryGetValue(tag, out value)) { return(value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity)); } lock (generatorLock) { if (keyGeneratorsByTag.TryGetValue(tag, out value)) { return(value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity)); } value = new AsyncHiLoKeyGenerator(tag, capacity); keyGeneratorsByTag.TryAdd(tag, value); } return(value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity)); }
public void AsyncSequentialGeneration_NoClashesOrGaps() { using (GetNewServer()) using (var store = new DocumentStore { Url = "http://localhost:8079" }.Initialize()) { var gen = new AsyncHiLoKeyGenerator(store.AsyncDatabaseCommands, "When_async_generating_lots_of_keys_concurrently_there_are_no_clashes", 2); Test(() => gen.NextIdAsync().Result, 1, GeneratedIdCount); } }
public Task<string> GenerateDocumentKeyAsync(IAsyncDatabaseCommands databaseCommands, DocumentConvention conventions, object entity) { var typeTagName = conventions.GetTypeTagName(entity.GetType()); if (string.IsNullOrEmpty(typeTagName)) //ignore empty tags return CompletedTask.With<string>(null); var tag = conventions.TransformTypeTagNameToDocumentKeyPrefix(typeTagName); AsyncHiLoKeyGenerator value; if (keyGeneratorsByTag.TryGetValue(tag, out value)) return value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity); lock(generatorLock) { if (keyGeneratorsByTag.TryGetValue(tag, out value)) return value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity); value = new AsyncHiLoKeyGenerator(tag, capacity); keyGeneratorsByTag.TryAdd(tag, value); } return value.GenerateDocumentKeyAsync(databaseCommands, conventions, entity); }