private void FindDependencies(IProgressTask progressTask) { var dependencies = new ConcurrentDictionary<MemberInfo, ICollection<AssemblyInfo>>(); _inputAssemblies.AsParallel().ForAll(filename => { foreach (var dep in GetDependencies(filename)) { MemberInfo m = new MemberInfo() { MemberDocId = dep.MemberDocId, TypeDocId = dep.TypeDocId, DefinedInAssemblyIdentity = dep.DefinedInAssemblyIdentity }; // Add this memberinfo HashSet<AssemblyInfo> newassembly = new HashSet<AssemblyInfo>(); newassembly.Add(dep.CallingAssembly); ICollection<AssemblyInfo> assemblies = dependencies.AddOrUpdate(m, newassembly, (key, existingSet) => { lock (existingSet) { existingSet.Add(dep.CallingAssembly); } return existingSet; }); } progressTask.ReportUnitComplete(); }); _cachedDependencies = dependencies; }
public void MainMethod() { Console.WriteLine("Concurrent Dictionary Run implementation"); ConcurrentDictionary<string, int> dictionary = new ConcurrentDictionary<string, int>(); if(dictionary.TryAdd("k1",10)) { Console.WriteLine("Added Key k1"); } if(dictionary.TryUpdate("k1",19,10)) { Console.WriteLine("Updated Key k1"); } if (dictionary.TryUpdate("k1", 19, 10)) { Console.WriteLine("Updated Key k1"); } else Console.WriteLine("Failed to Update"); dictionary["k1"] = 16; int r1 = dictionary.AddOrUpdate("k1", 2, (s, i) => i * 2); Console.WriteLine(r1); int r3 = dictionary.AddOrUpdate("k3", 2, (s, i) => i * 2); Console.WriteLine(r3); int r2 = dictionary.GetOrAdd("k2", 4); Console.WriteLine(r2); }
static Logger() { Container = new ConcurrentDictionary<string, List<Message>>(); Container.AddOrUpdate("error", new List<Message>(), (s, list) => new List<Message>()); Container.AddOrUpdate("info", new List<Message>(), (s, list) => new List<Message>()); Container.AddOrUpdate("warning", new List<Message>(), (s, list) => new List<Message>()); Container.AddOrUpdate("debug", new List<Message>(), (s, list) => new List<Message>()); Container.AddOrUpdate("fatal", new List<Message>(), (s, list) => new List<Message>()); }
public void SetValue() { var Test = new ConcurrentDictionary<string, int>(); Test.AddOrUpdate("Q", 4, (x, y) => 4); Test.AddOrUpdate("Z", 2, (x, y) => 4); Test.AddOrUpdate("C", 3, (x, y) => 4); Test.AddOrUpdate("A", 1, (x, y) => 4); Assert.Equal(4, Test.GetValue("Q")); Test.SetValue("Q", 40); Assert.Equal(40, Test.GetValue("Q")); }
public static void Test_ConcurrentDictionary_02() { ConcurrentDictionary<int, string> dictionary = new ConcurrentDictionary<int, string>(); // Func<TKey, TValue, TValue> updateValueFactory : Fonction utilisée pour générer une nouvelle valeur pour une clé existante en fonction de la valeur existante de la clé dictionary.AddOrUpdate(1, "toto1", (id, text) => "toto1"); dictionary.AddOrUpdate(1, "toto2", (id, text) => "toto2"); dictionary.AddOrUpdate(2, "tata", (id, text) => "tata"); dictionary.AddOrUpdate(3, "tutu", (id, text) => "tutu"); dictionary.AddOrUpdate(4, "titi", (id, text) => "titi"); foreach (KeyValuePair<int, string> value in dictionary) { Trace.WriteLine("{0} - \"{1}\"", value.Key, value.Value); } }
//buffer private void DataReceived(IList<DataRecord> dataRecords) { ConcurrentDictionary<EntityKey, Entity> buffer = new ConcurrentDictionary<EntityKey, Entity>(); for (int i = 0; i < dataRecords.Count; i++) { var key = String.Intern(dataRecords[i].DataRecordKey); var name = String.Intern(dataRecords[i].PropertyName); var value = dataRecords[i].PropertyValue; Entity entity; bool isNew = !_entityIndex.TryGetValue(EntityBuilder.Fnv1Hash(key), out entity); // add if (isNew) { entity = EntityBuilder.Build<Entity>(key, KeyType.Isin); _entityIndex.TryAdd(entity.Key.HashedValue, entity); } // update entity.Values.AddOrUpdate(name, new EntityItem(value), (n, oldValue) => new EntityItem(value, oldValue.Value)); buffer.AddOrUpdate(entity.Key, entity, (n, oldValue) => entity); } _eventExploder(buffer.Values); }
public static void Main() { var stock = new ConcurrentDictionary<string, int>(); stock.TryAdd("jDays", 4); stock.TryAdd("technologyhour", 3); Console.WriteLine("No. of shirts in stock = {0}", stock.Count); var success = stock.TryAdd("pluralsight", 6); Console.WriteLine("Added succeeded? " + success); success = stock.TryAdd("pluralsight", 6); Console.WriteLine("Added succeeded? " + success); stock["buddhistgeeks"] = 5; //stock["pluralsight"]++; <-- not thread safe two instructions var psStock = stock.AddOrUpdate("pluralsight", 1, (key, oldValue) => oldValue + 1); Console.WriteLine("pluralsight new value = {0}", psStock); Console.WriteLine("stock[pluralsight] = {0}", stock.GetOrAdd("pluralsight", 0)); int jDaysValue; success= stock.TryRemove("jDays", out jDaysValue); if (success) Console.WriteLine("Value removed was: " + jDaysValue); Console.WriteLine("\r\nEnumerating:"); foreach (var keyValuePair in stock) { Console.WriteLine("{0}: {1}", keyValuePair.Key, keyValuePair.Value); } }
public static ConcurrentDictionary<int, ILinie> ReadFile(String filePath) { FileHelperEngine<LinieRecord> engine = new FileHelperEngine<LinieRecord>(); ConcurrentDictionary<int, ILinie> linien = new ConcurrentDictionary<int, ILinie>(); try { engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; engine.Encoding = Encoding.UTF8; LinieRecord[] res = engine.ReadFile(filePath); foreach (LinieRecord linie in res) { //Übernehmen der eingelesenen Daten in das Programm-Model: ILinie transport = new Linie(); transport.Bezeichnung = linie.Bezeichnung; transport.Echtzeit = linie.Echtzeit; transport.Id = linie.Id; transport.Reihenfolge = linie.Reihenfolge; transport.Verkehrsmittel = Linie.VerkehrsmittelConverter(linie.Verkehrsmittel); //Schreiben des Models in Collection für den Rückgabewert: linien.AddOrUpdate(transport.Id, transport, (key, oldValue) => transport); } } catch (Exception ex) { //Dokument konnte nicht geparst werden (Nicht vorhanden/geöffnet) throw new VtmParsingException("Beim Versuch die Linien zu parsen ist ein Fehler aufgetreten!", filePath, ex); } return (linien); }
public static ConcurrentDictionary<int, IHaltestelle> ReadFile(String filePath) { FileHelperEngine<HaltestelleRecord> engine = new FileHelperEngine<HaltestelleRecord>(); ConcurrentDictionary<int, IHaltestelle> haltestellen = new ConcurrentDictionary<int, IHaltestelle>(); try { engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; engine.Encoding = Encoding.UTF8; HaltestelleRecord[] res = engine.ReadFile(filePath); foreach (HaltestelleRecord haltestelle in res) { //Übernehmen der eingelesenen Daten in das Programm-Model: IHaltestelle transport = new Haltestelle(); Point HsLoc = new Point(haltestelle.XKoord, haltestelle.YKoord); transport.Diva = haltestelle.Diva; transport.Id = haltestelle.Id; transport.Location = HsLoc; transport.Name = haltestelle.Name; //Schreiben des Models in Collection für den Rückgabewert: haltestellen.AddOrUpdate(transport.Id, transport, (key, oldValue) => transport); } } catch (Exception ex) { //Dokument konnte nicht geparst werden (Nicht vorhanden/geöffnet) throw new VtmParsingException("Beim Versuch die Haltestellen zu parsen ist ein Fehler aufgetreten!", filePath, ex); } return (haltestellen); }
static void Main(string[] args) { string[] urls = File.ReadAllLines("urls.txt"); long totalWords = 0; ConcurrentDictionary<string, int> wordIndex = new ConcurrentDictionary<string, int>(8, 100000); Parallel.ForEach<string>(urls, (url) => { Console.WriteLine("Fetching contents from: " + url); WebClient client = new WebClient(); Task<string> fetchTask = client.DownloadStringTaskAsync(url); fetchTask.ContinueWith((text) => { string[] words = text.Result.ToLower().Split(null); Parallel.ForEach<string>(words, (aWord) => { aWord = aWord.Trim(); if (!String.IsNullOrWhiteSpace(aWord)) { Interlocked.Increment(ref totalWords); wordIndex.AddOrUpdate(aWord, 1, (key, oldValue) => oldValue++); } }); }).ContinueWith((countTask) => { countTask.Wait(); Console.WriteLine("After {0}, Unique word count = {1}, totalWordCount = {2}", url, wordIndex.Keys.Count, Interlocked.Read(ref totalWords)); }); } ); Console.WriteLine("Done... press enter"); Console.ReadLine(); }
public void Queue_WithLowThreadCount_WorksCorrectly(int threads, int enqueues, int dequeues) { //The elements we insert are unique and sentinel value is -1 int initialValue = -1; int currentValue = initialValue; ConcurrentDictionary<int, int> table = new ConcurrentDictionary<int, int>(); Core.Queue.Queue<int> queue = new Core.Queue.Queue<int>(initialValue); ThreadBuilder .Empty() .AddThreads(() => { for (int i = 0; i < enqueues; i++) { int value = Interlocked.Increment(ref currentValue); queue.Enqueue(value); } }, threads) .AddThreads(() => { for (int i = 0; i < dequeues; i++) { int value = queue.Dequeue(); table.AddOrUpdate(value, x => 1, (k, v) => v + 1); } }, threads) .Start(); //The sentinel value can be returned more than once if queue is empty at the time of a dequeue int expectedDequeues = table.Keys.Count + (table.ContainsKey(initialValue) ? -1 : 0); int actualDequeues = table.Count(x => x.Key != initialValue && x.Value == 1); Assert.AreEqual(expectedDequeues, actualDequeues); }
public void VirtualNetwork_MultiThreadIntegrityTest_Ok() { var rnd = new Random(); var network = new VirtualNetwork(); var checksums = new ConcurrentDictionary<int, uint>(); Parallel.For(0, 100000, async (int i) => { int bufferSize = rnd.Next(5, 2048); byte[] writeFrame = new byte[bufferSize]; rnd.NextBytes(writeFrame); // First 4 bytes represent the sequence number. byte [] sequenceNo = BitConverter.GetBytes(i); sequenceNo.CopyTo(writeFrame, 0); uint writeChecksum = Fletcher32.Checksum(writeFrame, 0, writeFrame.Length); checksums.AddOrUpdate(i, writeChecksum, (seq, checkSum) => { Debug.Fail("Attempt to update checksum."); return 0; }); network.WriteFrame(i % 2 == 0, writeFrame); int delayMilliseconds = rnd.Next(0, 1000); await Task.Delay(delayMilliseconds); byte[] readFrame; network.ReadFrame(i % 2 == 1, out readFrame); uint readChecksum = Fletcher32.Checksum(readFrame, 0, readFrame.Length); int idx = BitConverter.ToInt32(readFrame, 0); Assert.Equal(checksums[idx], readChecksum); }); }
public void Stack_WithLowThreadCount_WorksCorrectly(int threads, int pushes, int pops) { //The elements we insert are unique and sentinel value is -1 int initialValue = -1; int currentValue = initialValue; ConcurrentDictionary<int, int> table = new ConcurrentDictionary<int, int>(); Core.Stack.Stack<int> stack = new Core.Stack.Stack<int>(initialValue); ThreadBuilder .Empty() .AddThreads(() => { for (int i = 0; i < pushes; i++) { int value = Interlocked.Increment(ref currentValue); stack.Push(value); } }, threads) .AddThreads(() => { for (int i = 0; i < pops; i++) { int value = stack.Pop(); table.AddOrUpdate(value, x => 1, (k, v) => v + 1); } }, threads) .Start(); //The sentinel value can be returned more than once if stack is empty at the time of a pop int expectedPops = table.Keys.Count + (table.ContainsKey(initialValue) ? -1 : 0); int actualPops = table.Count(x => x.Key != initialValue && x.Value == 1); Assert.AreEqual(expectedPops, actualPops); }
public static void Main(string[] args) { // When working with a ConcurrentDictionary you have methods that can atomically add, get, and update items. // An atomic operation means that it will be started and finished as a single step without other threads interfering. // TryUpdate checks to see whether the current value is equal to the existing value before updating it. // AddOrUpdate makes sure an item is added if it’s not there, and updated to a new value if it is. // GetOrAdd gets the current value of an item if it’s available; if not, it adds the new value by using a factory method. var dictionary = new ConcurrentDictionary<string, int>(); if (dictionary.TryAdd("k1", 42)) { Console.WriteLine("Added"); } if (dictionary.TryUpdate("k1", 21, 42)) { Console.WriteLine("42 updated to 21"); } dictionary["k1"] = 42; // Overwrite unconditionally int r1 = dictionary.AddOrUpdate("k1", 3, (s, i) => i * 2); int r2 = dictionary.GetOrAdd("k2", 3); Console.ReadLine(); }
// Demonstrates: // ConcurrentDictionary<TKey, TValue> ctor(concurrencyLevel, initialCapacity) // ConcurrentDictionary<TKey, TValue>[TKey] public static void Run() { // We know how many items we want to insert into the ConcurrentDictionary. // So set the initial capacity to some prime number above that, to ensure that // the ConcurrentDictionary does not need to be resized while initializing it. int HIGHNUMBER = 64; int initialCapacity = 101; // The higher the concurrencyLevel, the higher the theoretical number of operations // that could be performed concurrently on the ConcurrentDictionary. However, global // operations like resizing the dictionary take longer as the concurrencyLevel rises. // For the purposes of this example, we'll compromise at numCores * 2. int numProcs = Environment.ProcessorCount; int concurrencyLevel = numProcs * 2; // Construct the dictionary with the desired concurrencyLevel and initialCapacity ConcurrentDictionary<int, int> cd = new ConcurrentDictionary<int, int>(concurrencyLevel, initialCapacity); // Initialize the dictionary for (int i = 1; i <= HIGHNUMBER; i++) cd[i] = i * i; Console.WriteLine("The square of 23 is {0} (should be {1})", cd[23], 23 * 23); // Now iterate through, adding one to the end of the list. Existing items should be updated to be divided by their // key and a new item will be added that is the square of its key. for (int i = 1; i <= HIGHNUMBER + 1; i++) cd.AddOrUpdate(i, i * i, (k, v) => v / i); Console.WriteLine("The square root of 529 is {0} (should be {1})", cd[23], 529 / 23); Console.WriteLine("The square of 65 is {0} (should be {1})", cd[HIGHNUMBER + 1], ((HIGHNUMBER + 1) * (HIGHNUMBER + 1))); }
public static IDelayDecorator Get(string username, CommandHandler command) { var userDecorator = UserDecorator.Get(username, command, true); var globalDecorator = GlobalDecorator.Get(command); if (HybridInstances.ContainsKey(command)) { if (HybridInstances[command].ContainsKey(username)) return HybridInstances[command][username]; var hybridDecorator = new HybridDecorator(userDecorator, globalDecorator); HybridInstances[command].AddOrUpdate(username, hybridDecorator, (k, v) => hybridDecorator); return hybridDecorator; } var decorator = new HybridDecorator(userDecorator, globalDecorator); var hybrid = new ConcurrentDictionary<string, HybridDecorator>(); hybrid.AddOrUpdate(username, decorator, (k, v) => decorator); HybridInstances.AddOrUpdate(command, hybrid, (k, v) => hybrid); return decorator; }
public async Task CanHandleMultipleWorkItemInstances() { var metrics = new InMemoryMetricsClient(); var queue = new InMemoryQueue<WorkItemData>(retryDelay: TimeSpan.Zero, retries: 0); queue.AttachBehavior(new MetricsQueueBehavior<WorkItemData>(metrics)); var messageBus = new InMemoryMessageBus(); var handlerRegistry = new WorkItemHandlers(); var j1 = new WorkItemJob(queue, messageBus, handlerRegistry); var j2 = new WorkItemJob(queue, messageBus, handlerRegistry); var j3 = new WorkItemJob(queue, messageBus, handlerRegistry); int errors = 0; var jobIds = new ConcurrentDictionary<string, int>(); handlerRegistry.Register<MyWorkItem>(ctx => { var jobData = ctx.GetData<MyWorkItem>(); Assert.Equal("Test", jobData.SomeData); jobIds.AddOrUpdate(ctx.JobId, 1, (key, value) => value + 1); for (int i = 0; i < 10; i++) ctx.ReportProgress(10 * i); if (RandomData.GetBool(1)) { Interlocked.Increment(ref errors); throw new ApplicationException("Boom!"); } return TaskHelper.Completed(); }); for (int i = 0; i < 100; i++) queue.Enqueue(new MyWorkItem { SomeData = "Test", Index = i }, true); var completedItems = new List<string>(); object completedItemsLock = new object(); messageBus.Subscribe<WorkItemStatus>(status => { if (status.Progress < 100) return; lock (completedItemsLock) completedItems.Add(status.WorkItemId); }); var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10)); var token = cancellationTokenSource.Token; var tasks = new List<Task>(); tasks.AddRange(new[] { Task.Run(async () => await j1.RunUntilEmptyAsync(token), token), Task.Run(async () => await j2.RunUntilEmptyAsync(token), token), Task.Run(async () => await j3.RunUntilEmptyAsync(token), token), }); await Task.WhenAll(tasks); Thread.Sleep(10); Assert.Equal(100, completedItems.Count + errors); Assert.Equal(3, jobIds.Count); Assert.Equal(100, jobIds.Sum(kvp => kvp.Value)); }
public void TestRandomness(Func<List<int>> getRandomList, int rangeLength, int count) { var combinations = rangeLength.Factorial(rangeLength - count + 1); var iterations = combinations * 100; var ocurrences = new ConcurrentDictionary<long, int>(Environment.ProcessorCount, (int)combinations); var partitioner = Partitioner.Create(0, (long)iterations); Parallel.ForEach(partitioner, new ParallelOptions {MaxDegreeOfParallelism = Environment.ProcessorCount}, range => { //hopefully having a private dictionary will help concurrency var privateOcurrences = new Dictionary<long, int>(); for (long i = range.Item1; i < range.Item2; ++i) { var list = getRandomList(); //this will only work when numbers are between 0 and 88 long acc = list.Aggregate<int, long>(0, (current, value) => (value + 11) + current*100); privateOcurrences.AddOrUpdate(acc, 1, v => v + 1); } foreach (var privateOcurrence in privateOcurrences) { ocurrences.AddOrUpdate(privateOcurrence.Key, privateOcurrence.Value, (k, v) => v + privateOcurrence.Value); } }); var averageOcurrences = iterations / (combinations * 1.0m); var currentAverage = ocurrences.Values.Average(); Debug.WriteLine("The average ocurrences of this implementation is {0} comparing to {1} in the 'perfect' scenario", currentAverage, averageOcurrences); Assert.Less(currentAverage, averageOcurrences * 1.05m); Assert.Greater(currentAverage, averageOcurrences * 0.95m); }
private void Refresh() { if (_isRefreshing) return; _isRefreshing = true; try { var tempDictionary = new ConcurrentDictionary<Seed, SearchState>(); { foreach (var seed in _amoebaManager.CacheSeeds) { tempDictionary[seed] = SearchState.Cache; } foreach (var information in _amoebaManager.UploadingInformation) { if (information.Contains("Seed") && ((UploadState)information["State"]) != UploadState.Completed) { var seed = (Seed)information["Seed"]; tempDictionary.AddOrUpdate(seed, SearchState.Uploading, (_, orignalState) => orignalState | SearchState.Uploading); } } foreach (var information in _amoebaManager.DownloadingInformation) { if (information.Contains("Seed") && ((DownloadState)information["State"]) != DownloadState.Completed) { var seed = (Seed)information["Seed"]; tempDictionary.AddOrUpdate(seed, SearchState.Downloading, (_, orignalState) => orignalState | SearchState.Downloading); } } foreach (var seed in _amoebaManager.UploadedSeeds) { tempDictionary.AddOrUpdate(seed, SearchState.Uploaded, (_, orignalState) => orignalState | SearchState.Uploaded); } foreach (var seed in _amoebaManager.DownloadedSeeds) { tempDictionary.AddOrUpdate(seed, SearchState.Downloaded, (_, orignalState) => orignalState | SearchState.Downloaded); } } lock (_thisLock) { _seedsDictionary = tempDictionary; } } catch (Exception) { } finally { _isRefreshing = false; } }
public IEnumerable<SearchResult> Search(string text) { // find the unique tokens in the input var tokens = Tokenizer.Tokenize(text).Distinct().ToArray(); if (0 == tokens.Length) return new SearchResult[] { }; var results = new ConcurrentDictionary<string, IndexEntry<Metadata>[]>(); Parallel.ForEach(tokens, x => results.TryAdd(x, this.indexStore.Query(x).ToArray())); var documents = new ConcurrentDictionary<string, List<string>>(); // count the results Parallel.ForEach(results.Keys, token => { foreach (var doc in results[token]) { documents.AddOrUpdate(doc.Value, new List<string>(new[] {token}), (t, x) => { x.Add(token); return x; }); } }); // resturn the results return documents.OrderByDescending(x => x.Value.Count()).Select(x => new SearchResult(x.Key, x.Value.ToArray())); }
private IProperty GetProperty(PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute) { var property = _visitor.Visit(propertyInfo, attribute); if (property != null) return property; if (propertyInfo.GetGetMethod().IsStatic) return null; if (attribute != null) property = attribute; else property = InferProperty(propertyInfo.PropertyType); var objectProperty = property as IObjectProperty; if (objectProperty != null) { var type = GetUnderlyingType(propertyInfo.PropertyType); var seenTypes = new ConcurrentDictionary<Type, int>(_seenTypes); seenTypes.AddOrUpdate(type, 0, (t, i) => ++i); var walker = new PropertyWalker(type, _visitor, _maxRecursion, seenTypes); objectProperty.Properties = walker.GetProperties(seenTypes, _maxRecursion); } _visitor.Visit(property, propertyInfo, attribute); return property; }
private void CacheTemplateHelper(ICompiledTemplate template, ITemplateKey templateKey, Type modelTypeKey) { var uniqueKey = templateKey.GetUniqueKeyString(); _cache.AddOrUpdate(uniqueKey, key => { // new item added _assemblies.Add(template.TemplateAssembly); var dict = new ConcurrentDictionary<Type, ICompiledTemplate>(); dict.AddOrUpdate(modelTypeKey, template, (t, old) => { throw new Exception("Expected the dictionary to be empty."); }); return dict; }, (key, dict) => { dict.AddOrUpdate(modelTypeKey, t => { // new item added (template was not compiled with the given type). _assemblies.Add(template.TemplateAssembly); return template; }, (t, old) => { // item was already added before return template; }); return dict; }); }
public static ConcurrentDictionary<string, int> Occurences(Array words) { ConcurrentDictionary<string, int> occurences = new ConcurrentDictionary<string, int>(); foreach (string word in words){ occurences.AddOrUpdate (UpdateWord(word), 1, (s, i) => i + 1); } return occurences; }
public static unsafe bool HasDuplicateBranchReferences(Transaction tx, Page start,out long pageNumberWithDuplicates) { var stack = new Stack<Page>(); var existingTreeReferences = new ConcurrentDictionary<long, List<long>>(); stack.Push(start); while (stack.Count > 0) { var currentPage = stack.Pop(); if (currentPage.IsBranch) { for (int nodeIndex = 0; nodeIndex < currentPage.NumberOfEntries; nodeIndex++) { var node = currentPage.GetNode(nodeIndex); existingTreeReferences.AddOrUpdate(currentPage.PageNumber, new List<long> { node->PageNumber }, (branchPageNumber, pageNumberReferences) => { pageNumberReferences.Add(node->PageNumber); return pageNumberReferences; }); } for (int nodeIndex = 0; nodeIndex < currentPage.NumberOfEntries; nodeIndex++) { var node = currentPage.GetNode(nodeIndex); if (node->PageNumber < 0 || node->PageNumber > tx.State.NextPageNumber) { throw new InvalidDataException("found invalid reference on branch - tree is corrupted"); } var child = tx.GetReadOnlyPage(node->PageNumber); stack.Push(child); } } } Func<long, HashSet<long>> relevantPageReferences = branchPageNumber => new HashSet<long>(existingTreeReferences .Where(kvp => kvp.Key != branchPageNumber) .SelectMany(kvp => kvp.Value)); // ReSharper disable once LoopCanBeConvertedToQuery foreach (var branchReferences in existingTreeReferences) { if ( branchReferences.Value.Any( referencePageNumber => relevantPageReferences(branchReferences.Key).Contains(referencePageNumber))) { pageNumberWithDuplicates = branchReferences.Key; return true; } } pageNumberWithDuplicates = -1; return false; }
static DalBase() { //初始化数据库连接 connectionList = new System.Collections.Concurrent.ConcurrentDictionary<string, ConnectionStringSettings>(); foreach (ConnectionStringSettings cs in ConfigurationManager.ConnectionStrings) { if (cs.Name.CompareTo("LocalSqlServer") == 0) continue; connectionList.AddOrUpdate(cs.Name, cs, (key, val) => { return val; }); } }
public void CopyToTest() { var Test = new ConcurrentDictionary<string, int>(); var Test2 = new ConcurrentDictionary<string, int>(); Test.AddOrUpdate("Q", 4, (x, y) => 4); Test.AddOrUpdate("Z", 2, (x, y) => 2); Test.AddOrUpdate("C", 3, (x, y) => 3); Test.AddOrUpdate("A", 1, (x, y) => 1); Test.CopyTo(Test2); string Value = ""; int Value2 = 0; foreach (string Key in Test2.Keys.OrderBy(x => x)) { Value += Key; Value2 += Test2[Key]; } Assert.Equal("ACQZ", Value); Assert.Equal(10, Value2); }
public static ActionBlock<StatsdMessage> CreateBlock(ITargetBlock<Bucket> target, string rootNamespace, bool removeZeroGauges, IIntervalService intervalService, ILog log) { var gauges = new ConcurrentDictionary<string, double>(); var root = rootNamespace; var ns = String.IsNullOrEmpty(rootNamespace) ? "" : rootNamespace + "."; var incoming = new ActionBlock<StatsdMessage>(p => { var gauge = p as Gauge; gauges.AddOrUpdate(gauge.Name, gauge.Value, (key, oldValue) => gauge.Value); }, Utility.UnboundedExecution()); intervalService.Elapsed += (sender, e) => { if (gauges.Count == 0) { return; } var items = gauges.ToArray(); var bucket = new GaugesBucket(items, e.Epoch, ns); if (removeZeroGauges) { // Get all zero-value gauges double placeholder; var zeroGauges = 0; for (int index = 0; index < items.Length; index++) { if (items[index].Value == 0) { gauges.TryRemove(items[index].Key, out placeholder); zeroGauges += 1; } } if (zeroGauges > 0) { log.InfoFormat("Removed {0} empty gauges.", zeroGauges); } } gauges.Clear(); target.Post(bucket); }; incoming.Completion.ContinueWith(p => { // Tell the upstream block that we're done target.Complete(); }); return incoming; }
private static ConcurrentDictionary<CardValue, int> CreatePairs(IEnumerable<Card> cards) { var pairs = new ConcurrentDictionary<CardValue, int>(); foreach (var card in cards) { pairs.AddOrUpdate(card.Value, 1, (cardValue, quantity) => quantity + 1 ); } return pairs; }
private static Generator GetCurrentGenerator(ConcurrentDictionary<int, Generator> dict, int managedThreadId) { Generator result; if (!dict.TryGetValue(managedThreadId, out result)) { result = new Generator(new QrEncoder()); dict.AddOrUpdate(managedThreadId, result, (_, __) => result); } return result; }
public void Increment(int hash) { if (hash >= RowCount) { throw new ArgumentOutOfRangeException(nameof(hash), "Hash value is out of range for bucket count"); } if (table != null) { var old = table[hash]; if (old < UInt16.MaxValue) { old++; } table[hash] = old; } else { dict?.AddOrUpdate(hash, 1, (_, oldValue) => { if (oldValue < UInt16.MaxValue) { oldValue++; } return(oldValue); }); if (dict == null || dict.Count <= MaxDictionarySize) { return; } lock (upgradeToTableLock) { if (dict == null) { return; } var oldDict = dict; dict = null; // We stop listening to increments here. var newTable = new ushort[RowCount]; foreach (var pair in oldDict) { newTable[pair.Key] = pair.Value; } // We start listening to increments again here table = newTable; dict = null; } } }
public static void AddOrUpdateLocked <TKey, TValue>(this ConcurrentDictionary <TKey, HashSet <TValue> > dict, TKey key, TValue value) { dict?.AddOrUpdate(key, k => new HashSet <TValue> { value }, (k, set) => { lock (set) { set.Add(value); return(set); } }); }
public void AddOrUpdate(List <RedirectObject> redirects) { if (RedirectObjects == null) { return; } foreach (var redirectObject in redirects) { var AddOrUpdateValue = new CachedRedirectObject(redirectObject, NormalizePath(redirectObject.Path)); RedirectObjects?.AddOrUpdate(redirectObject.Id, AddOrUpdateValue, (key, item) => AddOrUpdateValue); } }
private AzureRegion(string name) { Name = name.ToLowerInvariant(); regions.AddOrUpdate(Name, this, (k, v) => v); }
public static void StateTypeToStoreName(string storeName, Type stateType) => StateStoreNames.AddOrUpdate(stateType.FullName, storeName, (key, value) => storeName);
private void RunWriteQueueAction() { //Dequeue all items until threshold is passed long totalLength = 0; RecyclableMemoryStream stream = null; var timestamp = GetTimestamp(); while (totalLength < Connection.CoalescingThreshold) { OperationState state = null; while (_writeQueue.TryDequeue(out var tempState)) { if (tempState.CanBeWritten()) { state = tempState; break; } DecrementInFlight(); } if (state == null) { //No more items in the write queue break; } if (!_freeOperations.TryPop(out short streamId)) { //Queue it up for later. _writeQueue.Enqueue(state); //When receiving the next complete message, we can process it. Connection.Logger.Info("Enqueued, no streamIds available. If this message is recurrent consider configuring more connections per host or lower the pressure"); break; } Connection.Logger.Verbose("Sending #{0} for {1} to {2}", streamId, state.Request.GetType().Name, EndPoint.EndpointFriendlyName); if (_isCanceled) { DecrementInFlight(); state.InvokeCallback(RequestError.CreateClientError(new SocketException((int)SocketError.NotConnected), true), timestamp); break; } _pendingOperations.AddOrUpdate(streamId, state, (k, oldValue) => state); var startLength = stream?.Length ?? 0; try { //lazy initialize the stream stream = stream ?? (RecyclableMemoryStream)Configuration.BufferPool.GetStream(Connection.StreamWriteTag); var frameLength = state.WriteFrame(streamId, stream, Serializer, timestamp); _connectionObserver.OnBytesSent(frameLength); totalLength += frameLength; } catch (Exception ex) { //There was an error while serializing or begin sending Connection.Logger.Error(ex); //The request was not written, clear it from pending operations RemoveFromPending(streamId); //Callback with the Exception state.InvokeCallback(RequestError.CreateClientError(ex, true), timestamp); //Reset the stream to before we started writing this frame stream?.SetLength(startLength); break; } } if (totalLength == 0L) { // Nothing to write, set the queue as not running Interlocked.CompareExchange(ref _writeState, Connection.WriteStateInit, Connection.WriteStateRunning); // Until now, we were preventing other threads to running the queue. // Check if we can now write: // a read could have finished (freeing streamIds) or new request could have been added to the queue if (!_freeOperations.IsEmpty && !_writeQueue.IsEmpty) { //The write queue is not empty //An item was added to the queue but we were running: try to launch a new queue RunWriteQueue(); } if (stream != null) { //The stream instance could be created if there was an exception while generating the frame stream.Dispose(); } return; } //Write and close the stream when flushed // ReSharper disable once PossibleNullReferenceException : if totalLength > 0 the stream is initialized _tcpSocket.Write(stream, () => stream.Dispose()); }
private void AddSubtype(Declaration subtype) { InvalidateCachedIsGlobal(); _subtypes.AddOrUpdate(subtype, 1, (key, value) => value); }
public static void SetObjectMapping(XLObjectMapping mapping) { _types.AddOrUpdate(mapping.MappedType, mapping, (t, m) => mapping); }
public void ObserveEvent(object sender, LoggingEventArgs e) { Lines.AddOrUpdate(e.Message, 1, (key, oldValue) => oldValue + 1); }
/// <summary> /// TBD /// </summary> /// <param name="actorRef">TBD</param> /// <param name="s">TBD</param> /// <returns>TBD</returns> public void Set(IActorRef actorRef, State s) => _state.AddOrUpdate(actorRef, s, (@ref, oldState) => s);
/// <summary> /// For tests. /// </summary> internal static void AddInstance(string url, WopiDiscovery discovery) { Instances.AddOrUpdate(url.TrimEnd('/'), oosUrl => new Lazy <WopiDiscovery>(() => discovery), (oosUrl, current) => new Lazy <WopiDiscovery>(() => discovery)); }
/// <summary> /// Tuple of app authentication result and principal are added to the cache after the token is acquired. /// </summary> /// <param name="cacheKey"></param> /// <param name="resultTuple"></param> public static void AddOrUpdate(string cacheKey, Tuple <AppAuthenticationResult, Principal> resultTuple) { CacheDictionary.AddOrUpdate(cacheKey, resultTuple, (s, tuple) => resultTuple); }
public void Set(DownstreamReRoute key, IHttpClient client, TimeSpan expirationTime) { _httpClientsCache.AddOrUpdate(key, client, (k, oldValue) => client); }
//public static readonly Random GLOBAL_RANDOM = new Random((int)DateTime.Now.Ticks & 0x0000FFFF); //private const double NUMERIC_ERROR = 0.001; //public const string TRUE_STR = "true"; //public const string FALSE_STR = "false"; //public static bool? EmptyNullableBool => new bool?(); //public static bool IsSameAs(this double mainD, double otherD) //{ // return Math.Abs(mainD - otherD) < NUMERIC_ERROR; //} //public static bool IsDifferentThan(this double mainD, double otherD) //{ // return !IsSameAs(mainD, otherD); //} //public static bool IsEmpty(this double mainD) //{ // return IsSameAs(mainD, 0); //} //public static bool IsNotEmpty(this double mainD) //{ // return !IsEmpty(mainD); //} //public static bool IsSameAs(this float mainF, float otherF) //{ // return Math.Abs(mainF - otherF) < NUMERIC_ERROR; //} //public static bool IsDifferentThan(this float mainF, float otherF) //{ // return !IsSameAs(mainF, otherF); //} //public static bool IsEmpty(this string str) //{ // return String.IsNullOrWhiteSpace(str); //} //public static bool IsNotEmpty(this string str) //{ // return !IsEmpty(str); //} //public static bool IsEmpty<T>(this IEnumerable<T> list) //{ // return list == null || !list.Any(); //} //public static bool IsNotEmpty<T>(this IEnumerable<T> list) //{ // return !IsEmpty(list); //} //public static bool IsSameAs(this string mainStr, string otherStr) //{ // return String.Compare(mainStr, otherStr, StringComparison.CurrentCultureIgnoreCase) == 0; //} //public static bool IsDifferentThan(this string mainStr, string otherStr) //{ // return !IsSameAs(mainStr, otherStr); //} //public static int RoundToInt(this double floatVal) //{ // return (int) Math.Round(floatVal, 0); //} //public static bool IsLessThanOrEqualTo(this double thisD, double otherD) //{ // return IsSameAs(thisD, otherD) || thisD < otherD; //} //public static bool IsGreaterThanOrEqualTo(this double thisD, double otherD) //{ // return IsSameAs(thisD, otherD) || thisD > otherD; //} //public static bool IsTrue(this bool? b) //{ // return b.HasValue && b.Value; //} //public static bool IsNotTheSame(this bool? first, bool? second) //{ // return first == null != (second == null) // || // IsNotAnEqualObjectTo(first, second); //} //public static bool IsNonNullRegexMatch(this string s, string regex) //{ // return s != null && Regex.IsMatch(s, regex, RegexOptions.IgnoreCase); //} #region Public Methods public static void AddOrUpdate <T, U>(this ConcurrentDictionary <T, U> retDict, T key, U value) { retDict.AddOrUpdate(key, value, (k, v) => v); }
public void AddChallengeResponse(string token, string response) => _values.AddOrUpdate(token, response, (_, __) => response);
private void OnProjectFinished(object sender, ProjectFinishedEventArgs e) => _resultsByProject.AddOrUpdate(e.ProjectFile, e.Succeeded, (projectFile, succeeded) => succeeded && e.Succeeded);
/// <summary> /// Extension method to automatically set the update value to same as "add" value for TryAddUpdate /// </summary> public static void AddOrUpdate <K, V>(this ConcurrentDictionary <K, V> dictionary, K key, V value) { dictionary.AddOrUpdate(key, value, (oldkey, oldvalue) => value); }
public void ProcessServiceJson(string result) { var newService = result.ToObj <ServiceInfo>(); _serviceInfoMap.TryGetValue(newService.GetKey(), out var oldService); if (newService.Hosts == null || !newService.Validate()) { return; } var changed = false; if (oldService != null) { if (oldService.lastRefTime > newService.lastRefTime) { _logger.LogWarning("out of date data received, old-t: {0}, new-t: {1}", oldService.lastRefTime, newService.lastRefTime); } _serviceInfoMap.AddOrUpdate(newService.GetKey(), newService, (k, v) => newService); var oldMap = oldService.Hosts.ToDictionary(x => x.ToInetAddr()); var newMap = newService.Hosts.ToDictionary(x => x.ToInetAddr()); var modHosts = newMap.Where(x => oldMap.ContainsKey(x.Key) && !x.Value.ToString().Equals(oldMap[x.Key].ToString())) .Select(x => x.Value).ToList(); var newHosts = newMap.Where(x => !oldMap.ContainsKey(x.Key)) .Select(x => x.Value).ToList(); var removeHosts = oldMap.Where(x => !newMap.ContainsKey(x.Key)) .Select(x => x.Value).ToList(); if (newHosts.Count > 0) { changed = true; _logger.LogInformation( "new ips ({0}) service: {1} -> {2}", newHosts.Count(), newService.GetKey(), newHosts.ToJsonString()); } if (removeHosts.Count() > 0) { changed = true; _logger.LogInformation( "removed ips ({0}) service: {1} -> {2}", removeHosts.Count(), newService.GetKey(), removeHosts.ToJsonString()); } if (modHosts.Count() > 0) { changed = true; _logger.LogInformation( "modified ips ({0}) service: {1} -> {2}", modHosts.Count(), newService.GetKey(), modHosts.ToJsonString()); } if (newHosts.Count() > 0 || removeHosts.Count() > 0 || modHosts.Count() > 0) { _eventDispatcher.ServiceChanged(newService); } } else { changed = true; _logger.LogInformation( "init new ips({0}) service: {1} -> {2}", newService.IpCount(), newService.GetKey(), newService.Hosts.ToJsonString()); _serviceInfoMap.TryAdd(newService.GetKey(), newService); _eventDispatcher.ServiceChanged(newService); } if (changed) { _logger.LogInformation( "current ips({0}) service: {1} -> {2}", newService.IpCount(), newService.GetKey(), newService.Hosts.ToJsonString()); } }
public async Task ProcessEvents() { #region Snippet:EventHubs_Processor_Sample01_ProcessEvents var storageConnectionString = "<< CONNECTION STRING FOR THE STORAGE ACCOUNT >>"; var blobContainerName = "<< NAME OF THE BLOB CONTAINER >>"; /*@@*/ /*@@*/ storageConnectionString = StorageTestEnvironment.Instance.StorageConnectionString; /*@@*/ blobContainerName = _storageScope.ContainerName; var eventHubsConnectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; var consumerGroup = "<< NAME OF THE EVENT HUB CONSUMER GROUP >>"; /*@@*/ /*@@*/ eventHubsConnectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; /*@@*/ eventHubName = _eventHubScope.EventHubName; /*@@*/ consumerGroup = _eventHubScope.ConsumerGroups.First(); var storageClient = new BlobContainerClient( storageConnectionString, blobContainerName); var processor = new EventProcessorClient( storageClient, consumerGroup, eventHubsConnectionString, eventHubName); var partitionEventCount = new ConcurrentDictionary <string, int>(); async Task processEventHandler(ProcessEventArgs args) { try { // If the cancellation token is signaled, then the // processor has been asked to stop. It will invoke // this handler with any events that were in flight; // these will not be lost if not processed. // // It is up to the handler to decide whether to take // action to process the event or to cancel immediately. if (args.CancellationToken.IsCancellationRequested) { return; } string partition = args.Partition.PartitionId; byte[] eventBody = args.Data.EventBody.ToArray(); Debug.WriteLine($"Event from partition { partition } with length { eventBody.Length }."); int eventsSinceLastCheckpoint = partitionEventCount.AddOrUpdate( key: partition, addValue: 1, updateValueFactory: (_, currentCount) => currentCount + 1); if (eventsSinceLastCheckpoint >= 50) { await args.UpdateCheckpointAsync(); partitionEventCount[partition] = 0; } } catch { // It is very important that you always guard against // exceptions in your handler code; the processor does // not have enough understanding of your code to // determine the correct action to take. Any // exceptions from your handlers go uncaught by // the processor and will NOT be redirected to // the error handler. } } Task processErrorHandler(ProcessErrorEventArgs args) { try { Debug.WriteLine("Error in the EventProcessorClient"); Debug.WriteLine($"\tOperation: { args.Operation }"); Debug.WriteLine($"\tException: { args.Exception }"); Debug.WriteLine(""); } catch { // It is very important that you always guard against // exceptions in your handler code; the processor does // not have enough understanding of your code to // determine the correct action to take. Any // exceptions from your handlers go uncaught by // the processor and will NOT be handled in any // way. } return(Task.CompletedTask); } try { using var cancellationSource = new CancellationTokenSource(); cancellationSource.CancelAfter(TimeSpan.FromSeconds(30)); processor.ProcessEventAsync += processEventHandler; processor.ProcessErrorAsync += processErrorHandler; try { await processor.StartProcessingAsync(cancellationSource.Token); await Task.Delay(Timeout.Infinite, cancellationSource.Token); } catch (TaskCanceledException) { // This is expected if the cancellation token is // signaled. } finally { // This may take up to the length of time defined // as part of the configured TryTimeout of the processor; // by default, this is 60 seconds. await processor.StopProcessingAsync(); } } catch { // The processor will automatically attempt to recover from any // failures, either transient or fatal, and continue processing. // Errors in the processor's operation will be surfaced through // its error handler. // // If this block is invoked, then something external to the // processor was the source of the exception. } finally { // It is encouraged that you unregister your handlers when you have // finished using the Event Processor to ensure proper cleanup. This // is especially important when using lambda expressions or handlers // in any form that may contain closure scopes or hold other references. processor.ProcessEventAsync -= processEventHandler; processor.ProcessErrorAsync -= processErrorHandler; } #endregion }
private void AddConversationReference(Activity activity) { var conversationReference = activity.GetConversationReference(); _conversationReferences.AddOrUpdate(conversationReference.User.Id, conversationReference, (key, newValue) => conversationReference); }
public static void IncrementReportCount(string ruleId) { counters.AddOrUpdate(ruleId, addValueFactory: key => 1, updateValueFactory: (key, count) => count + 1); }
public static void AddOrUpdate <TK, TV>(this ConcurrentDictionary <TK, TV> dictionary, TK key, TV value) { dictionary?.AddOrUpdate(key, value, (oldkey, oldvalue) => value); }
public void CacheProposalToApprove(Hash proposalId, long height) { // keep the higher block index _proposalsToApprove.AddOrUpdate(proposalId, height, (hash, h) => h >= height ? h : height); }
public void SetGuildLocale(Snowflake guildId, string locale) { _guildLocales.AddOrUpdate(guildId, locale, (id, old) => locale); }
public void Register(string operationName, ScheduleParams scheduleParams) { paramsIndex.AddOrUpdate(operationName, scheduleParams, (opName, sParams) => scheduleParams); }
public override bool Process(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session) { HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString; var auth_token = FindAuthCookie(request); var xsrf_token = FindXSRFToken(request); if (!HasXSRFCookie(request)) { var cookieAdded = AddXSRFTokenToRespone(response); if (!cookieAdded) { response.Status = System.Net.HttpStatusCode.ServiceUnavailable; response.Reason = "Too Many Concurrent Request, try again later"; return(true); } } Tuple <DateTime, string> tmpTuple; DateTime tmpDateTime; if (LOGOUT_SCRIPT_URI.Equals(request.Uri.AbsolutePath, StringComparison.OrdinalIgnoreCase)) { if (!string.IsNullOrWhiteSpace(auth_token)) { // Remove the active auth token m_activeTokens.TryRemove(auth_token, out tmpDateTime); } response.Status = System.Net.HttpStatusCode.NoContent; response.Reason = "OK"; return(true); } else if (LOGIN_SCRIPT_URI.Equals(request.Uri.AbsolutePath, StringComparison.OrdinalIgnoreCase)) { // Remove expired nonces foreach (var k in (from n in m_activeNonces where DateTime.UtcNow > n.Value.Item1 select n.Key)) { m_activeNonces.TryRemove(k, out tmpTuple); } if (input["get-nonce"] != null && !string.IsNullOrWhiteSpace(input["get-nonce"].Value)) { if (m_activeNonces.Count > 50) { response.Status = System.Net.HttpStatusCode.ServiceUnavailable; response.Reason = "Too many active login attempts"; return(true); } var password = Program.DataConnection.ApplicationSettings.WebserverPassword; if (request.Headers[TRAYICONPASSWORDSOURCE_HEADER] == "database") { password = Program.DataConnection.ApplicationSettings.WebserverPasswordTrayIconHash; } var buf = new byte[32]; var expires = DateTime.UtcNow.AddMinutes(AUTH_TIMEOUT_MINUTES); m_prng.GetBytes(buf); var nonce = Convert.ToBase64String(buf); var sha256 = System.Security.Cryptography.SHA256.Create(); sha256.TransformBlock(buf, 0, buf.Length, buf, 0); buf = Convert.FromBase64String(password); sha256.TransformFinalBlock(buf, 0, buf.Length); var pwd = Convert.ToBase64String(sha256.Hash); m_activeNonces.AddOrUpdate(nonce, key => new Tuple <DateTime, string>(expires, pwd), (key, existingValue) => { // Simulate the original behavior => if the nonce, against all odds, is already used // we throw an ArgumentException throw new ArgumentException("An element with the same key already exists in the dictionary."); }); response.Cookies.Add(new HttpServer.ResponseCookie(NONCE_COOKIE_NAME, nonce, expires)); using (var bw = new BodyWriter(response, request)) { bw.OutputOK(new { Status = "OK", Nonce = nonce, Salt = Program.DataConnection.ApplicationSettings.WebserverPasswordSalt }); } return(true); } else { if (input["password"] != null && !string.IsNullOrWhiteSpace(input["password"].Value)) { var nonce_el = request.Cookies[NONCE_COOKIE_NAME] ?? request.Cookies[Library.Utility.Uri.UrlEncode(NONCE_COOKIE_NAME)]; var nonce = nonce_el == null || string.IsNullOrWhiteSpace(nonce_el.Value) ? "" : nonce_el.Value; var urldecoded = nonce == null ? "" : Duplicati.Library.Utility.Uri.UrlDecode(nonce); if (m_activeNonces.ContainsKey(urldecoded)) { nonce = urldecoded; } if (!m_activeNonces.ContainsKey(nonce)) { response.Status = System.Net.HttpStatusCode.Unauthorized; response.Reason = "Unauthorized"; response.ContentType = "application/json"; return(true); } var pwd = m_activeNonces[nonce].Item2; // Remove the nonce m_activeNonces.TryRemove(nonce, out tmpTuple); if (pwd != input["password"].Value) { response.Status = System.Net.HttpStatusCode.Unauthorized; response.Reason = "Unauthorized"; response.ContentType = "application/json"; return(true); } var buf = new byte[32]; var expires = DateTime.UtcNow.AddHours(1); m_prng.GetBytes(buf); var token = Duplicati.Library.Utility.Utility.Base64UrlEncode(buf); while (token.Length > 0 && token.EndsWith("=", StringComparison.Ordinal)) { token = token.Substring(0, token.Length - 1); } m_activeTokens.AddOrUpdate(token, key => expires, (key, existingValue) => { // Simulate the original behavior => if the token, against all odds, is already used // we throw an ArgumentException throw new ArgumentException("An element with the same key already exists in the dictionary."); }); response.Cookies.Add(new HttpServer.ResponseCookie(AUTH_COOKIE_NAME, token, expires)); using (var bw = new BodyWriter(response, request)) bw.OutputOK(); return(true); } } } var limitedAccess = request.Uri.AbsolutePath.StartsWith(RESTHandler.API_URI_PATH, StringComparison.OrdinalIgnoreCase) ; // Override to allow the CAPTCHA call to go through if (request.Uri.AbsolutePath.StartsWith(CAPTCHA_IMAGE_URI, StringComparison.OrdinalIgnoreCase) && request.Method == "GET") { limitedAccess = false; } if (limitedAccess) { if (xsrf_token != null && m_activexsrf.ContainsKey(xsrf_token)) { var expires = DateTime.UtcNow.AddMinutes(XSRF_TIMEOUT_MINUTES); m_activexsrf[xsrf_token] = expires; response.Cookies.Add(new ResponseCookie(XSRF_COOKIE_NAME, xsrf_token, expires)); } else { response.Status = System.Net.HttpStatusCode.BadRequest; response.Reason = "Missing XSRF Token"; return(true); } } if (string.IsNullOrWhiteSpace(Program.DataConnection.ApplicationSettings.WebserverPassword)) { return(false); } foreach (var k in (from n in m_activeTokens where DateTime.UtcNow > n.Value select n.Key)) { m_activeTokens.TryRemove(k, out tmpDateTime); } // If we have a valid token, proceed if (!string.IsNullOrWhiteSpace(auth_token)) { DateTime expires; var found = m_activeTokens.TryGetValue(auth_token, out expires); if (!found) { auth_token = Duplicati.Library.Utility.Uri.UrlDecode(auth_token); found = m_activeTokens.TryGetValue(auth_token, out expires); } if (found && DateTime.UtcNow < expires) { expires = DateTime.UtcNow.AddHours(1); m_activeTokens[auth_token] = expires; response.Cookies.Add(new ResponseCookie(AUTH_COOKIE_NAME, auth_token, expires)); return(false); } } if ("/".Equals(request.Uri.AbsolutePath, StringComparison.OrdinalIgnoreCase) || "/index.html".Equals(request.Uri.AbsolutePath, StringComparison.OrdinalIgnoreCase)) { response.Redirect("/login.html"); return(true); } if (limitedAccess) { response.Status = System.Net.HttpStatusCode.Unauthorized; response.Reason = "Not logged in"; response.AddHeader("Location", "login.html"); return(true); } return(false); }
public override async Task <IViewProviderResult> BuildUpdateAsync(SpamSettings settings, IViewProviderContext context) { // All possible spam operations var spamOperations = _spamOperationManager.GetSpamOperations(); // Build operations to add var operationsToAdd = new ConcurrentDictionary <string, SpamOperation>(); foreach (var operation in spamOperations) { operation.FlagAsSpam = false; operation.NotifyAdmin = false; operation.NotifyStaff = false; operation.CustomMessage = false; foreach (var key in _request.Form.Keys) { if (key.EndsWith(operation.Name)) { var values = _request.Form[key]; foreach (var value in values) { switch (value) { case "flagAsSpam": operation.FlagAsSpam = true; break; case "notifyAdmin": operation.NotifyAdmin = true; break; case "notifyStaff": operation.NotifyStaff = true; break; case "allowAlter": operation.CustomMessage = true; break; } } } if (key.StartsWith("customMessage") && key.EndsWith(operation.Name)) { var values = _request.Form[key]; foreach (var value in values) { operation.Message = value; } } // Ensure unique entries operationsToAdd.AddOrUpdate(operation.Name, operation, (k, v) => operation); } } // Build model var model = new SpamSettingsViewModel(); // Validate model if (!await context.Updater.TryUpdateModelAsync(model)) { return(await BuildEditAsync(settings, context)); } // Update settings if (context.Updater.ModelState.IsValid) { settings = new SpamSettings() { ApiKey = settings.ApiKey, SpamLevelId = settings.SpamLevelId, SpamOperations = operationsToAdd.Values }; // Persist settings await _recaptchaSettingsStore.SaveAsync(settings); } // Redirect back to edit return(await BuildEditAsync(settings, context)); }
public T AddService <T>(T service) { _services.AddOrUpdate(typeof(T), service, (key, oldValue) => service); return(service); }
/// <summary> /// Returns metadata of specified table in this keyspace. /// </summary> /// <param name="tableName"> the name of table to retrieve </param> /// <returns>the metadata for table <c>tableName</c> in this keyspace if it /// exists, <c>null</c> otherwise.</returns> public TableMetadata GetTableMetadata(string tableName) { TableMetadata table; if (_tables.TryGetValue(tableName, out table)) { //The table metadata is available in local cache return(table); } var keyspaceName = Name; var cols = new Dictionary <string, TableColumn>(); TableOptions options = null; var tableMetadataRow = _cc.Query(String.Format(SelectSingleTable, tableName, keyspaceName), true).FirstOrDefault(); if (tableMetadataRow == null) { return(null); } var columnsMetadata = _cc.Query(String.Format(SelectColumns, tableName, keyspaceName), true); foreach (var row in columnsMetadata) { var dataType = TypeCodec.ParseDataType(row.GetValue <string>("validator")); var dsc = new TableColumn { Name = row.GetValue <string>("column_name"), Keyspace = row.GetValue <string>("keyspace_name"), Table = row.GetValue <string>("columnfamily_name"), TypeCode = dataType.TypeCode, TypeInfo = dataType.TypeInfo, SecondaryIndexName = row.GetValue <string>("index_name"), SecondaryIndexType = row.GetValue <string>("index_type"), KeyType = row.GetValue <string>("index_name") != null ? KeyType.SecondaryIndex : KeyType.None, }; cols.Add(dsc.Name, dsc); } var colNames = tableMetadataRow.GetValue <string>("column_aliases"); var rowKeys = colNames.Substring(1, colNames.Length - 2).Split(','); for (var i = 0; i < rowKeys.Length; i++) { if (rowKeys[i].StartsWith("\"")) { rowKeys[i] = rowKeys[i].Substring(1, rowKeys[i].Length - 2).Replace("\"\"", "\""); } } if (rowKeys.Length > 0 && rowKeys[0] != string.Empty) { bool isCompact = true; var comparator = tableMetadataRow.GetValue <string>("comparator"); //Remove reversed type comparator = comparator.Replace(TypeCodec.ReversedTypeName, ""); if (comparator.StartsWith(TypeCodec.CompositeTypeName)) { comparator = comparator.Replace(TypeCodec.CompositeTypeName, ""); isCompact = false; } var rg = new Regex(@"org\.apache\.cassandra\.db\.marshal\.\w+"); var rowKeysTypes = rg.Matches(comparator); for (var i = 0; i < rowKeys.Length; i++) { var keyName = rowKeys[i]; var dataType = TypeCodec.ParseDataType(rowKeysTypes[i].ToString()); var dsc = new TableColumn { Name = keyName, Keyspace = tableMetadataRow.GetValue <string>("keyspace_name"), Table = tableMetadataRow.GetValue <string>("columnfamily_name"), TypeCode = dataType.TypeCode, TypeInfo = dataType.TypeInfo, KeyType = KeyType.Clustering, }; cols[dsc.Name] = dsc; } options = new TableOptions { isCompactStorage = isCompact, bfFpChance = tableMetadataRow.GetValue <double>("bloom_filter_fp_chance"), caching = tableMetadataRow.GetValue <string>("caching"), comment = tableMetadataRow.GetValue <string>("comment"), gcGrace = tableMetadataRow.GetValue <int>("gc_grace_seconds"), localReadRepair = tableMetadataRow.GetValue <double>("local_read_repair_chance"), readRepair = tableMetadataRow.GetValue <double>("read_repair_chance"), compactionOptions = GetCompactionStrategyOptions(tableMetadataRow), compressionParams = (SortedDictionary <string, string>)Utils.ConvertStringToMap(tableMetadataRow.GetValue <string>("compression_parameters")) }; //replicate_on_write column not present in C* >= 2.1 if (tableMetadataRow.GetColumn("replicate_on_write") != null) { options.replicateOnWrite = tableMetadataRow.GetValue <bool>("replicate_on_write"); } } //In Cassandra 1.2, the keys are not stored in the system.schema_columns table //But you can get it from system.schema_columnfamilies var keys = tableMetadataRow.GetValue <string>("key_aliases") .Replace("[", "") .Replace("]", "") .Split(','); var keyTypes = tableMetadataRow.GetValue <string>("key_validator") .Replace("org.apache.cassandra.db.marshal.CompositeType", "") .Replace("(", "") .Replace(")", "") .Split(','); for (var i = 0; i < keys.Length; i++) { var name = keys[i].Replace("\"", "").Trim(); var dataType = TypeCodec.ParseDataType(keyTypes[i].Trim()); cols[name] = new TableColumn() { Name = name, Keyspace = tableMetadataRow.GetValue <string>("keyspace_name"), Table = tableMetadataRow.GetValue <string>("columnfamily_name"), TypeCode = dataType.TypeCode, TypeInfo = dataType.TypeInfo, KeyType = KeyType.Partition }; } table = new TableMetadata(tableName, cols.Values.ToArray(), options); //Cache it _tables.AddOrUpdate(tableName, table, (k, o) => table); return(table); }
/// <summary> /// Adds a key value pair to the dictionary /// </summary> /// <param name="key"></param> /// <param name="mapper"></param> private static void CacheMapper(Type key, Type mapper) { MapperCache.AddOrUpdate(key, mapper, (x, y) => mapper); }
public static void Main(string[] args) { Console.WriteLine("Hello World!"); // Создание объекта ConcurrentDictionary var dict = new ConcurrentDictionary<String, int>(); // Попытка добавить элемент [k1;42] if (dict.TryAdd("k1", 42)) Console.WriteLine("Added"); // Обновление значения элемента [k1;42] на [k1;21] if (dict.TryUpdate("k1", 21, 42)) Console.WriteLine("42 updated to 21"); dict["k1"] = 42; // безоговорочная перезапись значения k1 // Добавление или обновление элемента (если существует) int r1 = dict.AddOrUpdate("k1", 3, (s, i) => i * 2); // Получение значения k2 (если его не существует - добавление в коллекцию int r2 = dict.GetOrAdd("k2", 3); Console.WriteLine(r1); Console.WriteLine(r2); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }