public string GenerateDocumentKey(DocumentConvention conventions, object entity) { var tag = conventions.GetTypeTagName(entity.GetType()).ToLowerInvariant(); HiLoKeyGenerator value; if (keyGeneratorsByTag.TryGetValue(tag, out value)) { return(value.GenerateDocumentKey(entity)); } lock (generatorLock) { if (keyGeneratorsByTag.TryGetValue(tag, out value)) { return(value.GenerateDocumentKey(entity)); } value = new HiLoKeyGenerator(databaseCommands, tag, capacity); // doing it this way for thread safety keyGeneratorsByTag = new Dictionary <string, HiLoKeyGenerator>(keyGeneratorsByTag) { { tag, value } }; } return(value.GenerateDocumentKey(entity)); }
public AbsoluteOrderingRavenPersistenceEngine(IDocumentStore store, ISerialize serializer, bool consistentQueries) : base(store, serializer, consistentQueries) { _store = store; _serializer = serializer; _hiLoGen = new HiLoKeyGenerator(store, "", 1000); }
/// <summary> /// Generates the document key. /// </summary> /// <param name="conventions">The conventions.</param> /// <param name="entity">The entity.</param> /// <param name="databaseCommands">Low level database commands.</param> /// <returns></returns> public string GenerateDocumentKey(IDatabaseCommands databaseCommands, DocumentConvention conventions, object entity) { var typeTagName = conventions.GetDynamicTagName(entity); if (string.IsNullOrEmpty(typeTagName)) //ignore empty tags { return(null); } var tag = conventions.TransformTypeTagNameToDocumentKeyPrefix(typeTagName); HiLoKeyGenerator value; if (keyGeneratorsByTag.TryGetValue(tag, out value)) { return(value.GenerateDocumentKey(databaseCommands, conventions, entity)); } lock (generatorLock) { if (keyGeneratorsByTag.TryGetValue(tag, out value)) { return(value.GenerateDocumentKey(databaseCommands, conventions, entity)); } value = new HiLoKeyGenerator(tag, capacity); // doing it this way for thread safety keyGeneratorsByTag = new Dictionary <string, HiLoKeyGenerator>(keyGeneratorsByTag) { { tag, value } }; } return(value.GenerateDocumentKey(databaseCommands, conventions, entity)); }
/// <summary> /// Generates the document key. /// </summary> /// <param name="conventions">The conventions.</param> /// <param name="entity">The entity.</param> /// <returns></returns> public string GenerateDocumentKey(DocumentConvention conventions, object entity) { var typeTagName = conventions.GetTypeTagName(entity.GetType()); if (string.IsNullOrEmpty(typeTagName)) //ignore empty tags { return(null); } var tag = typeTagName.ToLowerInvariant(); HiLoKeyGenerator value; if (keyGeneratorsByTag.TryGetValue(tag, out value)) { return(value.GenerateDocumentKey(conventions, entity)); } lock (generatorLock) { if (keyGeneratorsByTag.TryGetValue(tag, out value)) { return(value.GenerateDocumentKey(conventions, entity)); } value = new HiLoKeyGenerator(documentStore, tag, capacity); // doing it this way for thread safety keyGeneratorsByTag = new Dictionary <string, HiLoKeyGenerator>(keyGeneratorsByTag) { { tag, value } }; } return(value.GenerateDocumentKey(conventions, entity)); }
public void CanUseServerPrefix() { using (var store = NewDocumentStore()) { store.DatabaseCommands.Put( "Raven/Hilo/Users", null, new RavenJObject { {"Max", 32} }, new RavenJObject()); store.DatabaseCommands.Put( "Raven/ServerPrefixForHilo", null, new RavenJObject { {"ServerPrefix", "2,"} }, new RavenJObject()); var hiLoKeyGenerator = new HiLoKeyGenerator("Users", 32); var generateDocumentKey = hiLoKeyGenerator.GenerateDocumentKey(store.DatabaseCommands, new DocumentConvention(), new User()); Assert.Equal("Users/2,33", generateDocumentKey); } }
public void HiLo_Modified_InReplicated_Scenario() { const string key = "Raven/Hilo/managers"; var store1 = CreateStore(); var store2 = CreateStore(); TellFirstInstanceToReplicateToSecondInstance(); TellSecondInstanceToReplicateToFirstInstance(); var hiLoKeyGenerator = new HiLoKeyGenerator("managers", 2) { DisableCapacityChanges = true }; for (long i = 0; i < 4; i++) { Assert.Equal(i + 1, hiLoKeyGenerator.NextId(store1.DatabaseCommands)); } WaitForReplication(store2, key); var jsonDocument = store2.DatabaseCommands.Get(key); store2.DatabaseCommands.Put(key, null, new RavenJObject { {"Max", 49} }, jsonDocument.Metadata); WaitForReplication(store1, key); for (long i = 0; i < 4; i++) { Assert.Equal(i + 50, hiLoKeyGenerator.NextId(store1.DatabaseCommands)); } }
/// <summary> /// Generates the document key. /// </summary> /// <param name="conventions">The conventions.</param> /// <param name="entity">The entity.</param> /// <returns></returns> public string GenerateDocumentKey(IDatabaseCommands databaseCommands, DocumentConvention conventions, object entity) { var typeTagName = conventions.GetDynamicTagName(entity); if (string.IsNullOrEmpty(typeTagName)) //ignore empty tags return null; var tag = conventions.TransformTypeTagNameToDocumentKeyPrefix(typeTagName); HiLoKeyGenerator value; if (keyGeneratorsByTag.TryGetValue(tag, out value)) return value.GenerateDocumentKey(databaseCommands, conventions, entity); lock(generatorLock) { if (keyGeneratorsByTag.TryGetValue(tag, out value)) return value.GenerateDocumentKey(databaseCommands, conventions, entity); value = new HiLoKeyGenerator(tag, capacity); // doing it this way for thread safety keyGeneratorsByTag = new Dictionary<string, HiLoKeyGenerator>(keyGeneratorsByTag) { {tag, value} }; } return value.GenerateDocumentKey(databaseCommands, conventions, entity); }
/// <summary> /// Generates the document key. /// </summary> /// <param name="conventions">The conventions.</param> /// <param name="entity">The entity.</param> /// <returns></returns> public string GenerateDocumentKey(DocumentConvention conventions, object entity) { var typeTagName = conventions.GetTypeTagName(entity.GetType()); if (string.IsNullOrEmpty(typeTagName)) //ignore empty tags return null; var tag = typeTagName.ToLowerInvariant(); HiLoKeyGenerator value; if (keyGeneratorsByTag.TryGetValue(tag, out value)) return value.GenerateDocumentKey(conventions, entity); lock(generatorLock) { if (keyGeneratorsByTag.TryGetValue(tag, out value)) return value.GenerateDocumentKey(conventions, entity); value = new HiLoKeyGenerator(documentStore, tag, capacity); // doing it this way for thread safety keyGeneratorsByTag = new Dictionary<string, HiLoKeyGenerator>(keyGeneratorsByTag) { {tag, value} }; } return value.GenerateDocumentKey(conventions, entity); }
public void FromScratch() { using(var store = NewDocumentStore()) { var generator = new HiLoKeyGenerator("users", 3); for (long i = 0; i < 50; i++) { Assert.Equal(i + 1, generator.NextId(store.DatabaseCommands)); } } }
public void SequentialGeneration_NoClashesOrGaps() { using (GetNewServer()) using (var store = new DocumentStore { Url = "http://localhost:8079" }.Initialize()) { var gen = new HiLoKeyGenerator(store.DatabaseCommands, "When_generating_lots_of_keys_concurrently_there_are_no_clashes", 2); Test(gen.NextId, 1, GeneratedIdCount); } }
public void ConcurrentWillNoGenerateConflicts() { using (var store = NewDocumentStore()) { var generator1 = new HiLoKeyGenerator("users", 3); var generator2 = new HiLoKeyGenerator("users", 3); var dic = new Dictionary<long, int>(); for (long i = 0; i < 50; i++) { dic.Add(generator1.NextId(store.DatabaseCommands), 1); dic.Add(generator2.NextId(store.DatabaseCommands), 1); } } }
public void CanUpgradeFromOldHiLo() { using (var store = NewDocumentStore()) { store.DatabaseCommands.Put("Raven/Hilo/users", null, RavenJObject.FromObject(new{ ServerHi = 2}), new RavenJObject()); var generator = new HiLoKeyGenerator("users", 1024); for (long i = 0; i < 50; i++) { Assert.Equal((i + 1) + 1024, generator.NextId(store.DatabaseCommands)); } } }
public void HiLo_Modified_InReplicated_Scenario() { const string key = "Raven/Hilo/managers"; var store1 = CreateStore(configureStore: store => store.Conventions.FailoverBehavior = FailoverBehavior.ReadFromAllServers); var store2 = CreateStore(); ((DocumentStore) store1).GetReplicationInformerForDatabase() .UpdateReplicationInformationIfNeeded((ServerClient) store1.DatabaseCommands) .Wait(); ((DocumentStore) store2).GetReplicationInformerForDatabase() .UpdateReplicationInformationIfNeeded((ServerClient) store2.DatabaseCommands) .Wait(); TellFirstInstanceToReplicateToSecondInstance(); TellSecondInstanceToReplicateToFirstInstance(); var hiLoKeyGenerator = new HiLoKeyGenerator("managers", 2) { DisableCapacityChanges = true }; for (long i = 0; i < 4; i++) { Assert.Equal(i + 1, hiLoKeyGenerator.NextId(store1.DatabaseCommands)); } WaitForReplication(store2, session => { var load = session.Load<dynamic>(key); return load != null && load.Max == 4; }); var jsonDocument = store2.DatabaseCommands.Get(key); store2.DatabaseCommands.Put(key, null, new RavenJObject { {"Max", 49} }, jsonDocument.Metadata); WaitForReplication(store1, session => session.Load<dynamic>(key).Max == 49); for (long i = 0; i < 4; i++) { var nextId = hiLoKeyGenerator.NextId(store1.DatabaseCommands); Assert.Equal(i + 50, nextId); } }
public string GenerateDocumentKey(DocumentConvention conventions, object entity) { var tag = conventions.GetTypeTagName(entity.GetType()).ToLowerInvariant(); HiLoKeyGenerator value; if (keyGeneratorsByTag.TryGetValue(tag, out value)) return value.GenerateDocumentKey(conventions, entity); lock(generatorLock) { if (keyGeneratorsByTag.TryGetValue(tag, out value)) return value.GenerateDocumentKey(conventions, entity); value = new HiLoKeyGenerator(databaseCommands, tag, capacity); // doing it this way for thread safety keyGeneratorsByTag = new Dictionary<string, HiLoKeyGenerator>(keyGeneratorsByTag) { {tag, value} }; } return value.GenerateDocumentKey(conventions, entity); }
public void HiloCannotGoDown() { using (var store = NewDocumentStore()) { store.DatabaseCommands.Put( "Raven/Hilo/Users", null, new RavenJObject { {"Max", 32} }, new RavenJObject()); var hiLoKeyGenerator = new HiLoKeyGenerator("Users", 32); var ids = new HashSet<long> { hiLoKeyGenerator.NextId(store.DatabaseCommands) }; store.DatabaseCommands.Put( "Raven/Hilo/Users", null, new RavenJObject { {"Max", 12} }, new RavenJObject()); for (int i = 0; i < 128; i++) { Assert.True(ids.Add(hiLoKeyGenerator.NextId(store.DatabaseCommands)), "Failed at " + i); } var list = ids.GroupBy(x => x).Select(g => new { g.Key, Count = g.Count() }).Where(x => x.Count > 1).ToList(); Assert.Empty(list); } }
public CheckpointNumberIncrementListener(IDocumentStore store) { _store = store; // http://stackoverflow.com/a/12687849/1010 _generator = new HiLoKeyGenerator("CheckpointNumber", 1); }
public void ShouldResolveConflictWithHighestNumber() { using (var server = GetNewServer()) using (var store = new DocumentStore { Url = "http://*****:*****@Http-Status-Code" , 409 }, { "@Http-Status-Description" , "Conflicted doc" } }); }); var hiLoKeyGenerator = new HiLoKeyGenerator("Users", 32); var nextId = hiLoKeyGenerator.NextId(store.DatabaseCommands); Assert.Equal(65, nextId); } }