private static void InitUTF16(Links links) { if (UTF16Initialized) return; UTF16Initialized = true; UTF16FirstCharLink = 1; UTF16LastCharLink = UTF16FirstCharLink + char.MaxValue; var firstLink = links.Create(0, 0); if (firstLink != UTF16FirstCharLink) { links.Delete(firstLink); Console.WriteLine("Assume UTF16 table already created."); } else { for (var i = UTF16FirstCharLink + 1; i <= UTF16LastCharLink; i++) { // From NIL to It (NIL -> Character) transformation meaning, (or infinite amount of NIL characters before actual Character) var createdLink = links.Create(firstLink, 0); if (createdLink != i) throw new Exception("Unable to initialize UTF 16 table."); } Console.WriteLine("UTF16 table created and initialized."); } Console.WriteLine("Total links count: {0}.", links.Total); }
public void AllPartialVariantsSearchTest() { string tempFilename = Path.GetTempFileName(); const long sequenceLength = 8; const ulong itself = Links.Itself; using (var links = new Links(tempFilename, LinksSizeStep)) { var sequence = new ulong[sequenceLength]; for (int i = 0; i < sequenceLength; i++) sequence[i] = links.Create(itself, itself); var sequences = new Sequences(links); var createResults = sequences.CreateAllVariants2(sequence); //var createResultsStrings = createResults.Select(x => x + ": " + sequences.FormatSequence(x)).ToList(); //Global.Trash = createResultsStrings; var partialSequence = new ulong[sequenceLength - 2]; Array.Copy(sequence, 1, partialSequence, 0, sequenceLength - 2); var sw1 = Stopwatch.StartNew(); var searchResults1 = sequences.GetAllPartiallyMatchingSequences0(partialSequence); sw1.Stop(); var sw2 = Stopwatch.StartNew(); var searchResults2 = sequences.GetAllPartiallyMatchingSequences1(partialSequence); sw2.Stop(); //var sw3 = Stopwatch.StartNew(); //var searchResults3 = sequences.GetAllPartiallyMatchingSequences2(partialSequence); sw3.Stop(); //Global.Trash = searchResults3; //var searchResults1Strings = searchResults1.Select(x => x + ": " + sequences.FormatSequence(x)).ToList(); //Global.Trash = searchResults1Strings; var intersection1 = createResults.Intersect(searchResults1).ToList(); Assert.IsTrue(intersection1.Count == createResults.Length); var intersection2 = createResults.Intersect(searchResults2).ToList(); Assert.IsTrue(intersection2.Count == createResults.Length); for (int i = 0; i < sequenceLength; i++) links.Delete(sequence[i]); } File.Delete(tempFilename); }
public void TestGetTargetInParallel() { string tempFilename = Path.GetTempFileName(); using (var links = new Links(tempFilename, DefaultLinksSize)) { Console.WriteLine("Testing GetTarget function with {0} Iterations in parallel.", Iterations); long counter = 0; //var firstLink = links.First(); ulong firstLink = links.Create(0, 0); Stopwatch sw = Stopwatch.StartNew(); Parallel.For(0, Iterations, x => { Interlocked.Add(ref counter, (long)links.GetTarget(firstLink)); //Interlocked.Increment(ref counter); }); TimeSpan elapsedTime = sw.Elapsed; double iterationsPerSecond = Iterations / elapsedTime.TotalSeconds; links.Delete(firstLink); Console.WriteLine( "{0} Iterations of GetTarget function done in {1} ({2} Iterations per second), counter result: {3}", Iterations, elapsedTime, (long)iterationsPerSecond, counter); } File.Delete(tempFilename); }
public void TestGetTarget() { string tempFilename = Path.GetTempFileName(); using (var links = new Links(tempFilename, DefaultLinksSize)) { Console.WriteLine("Testing GetTarget function with {0} Iterations.", Iterations); ulong counter = 0; //var firstLink = links.First(); ulong firstLink = links.Create(0, 0); Stopwatch sw = Stopwatch.StartNew(); for (ulong i = 0; i < Iterations; i++) counter += links.GetTarget(firstLink); TimeSpan elapsedTime = sw.Elapsed; double iterationsPerSecond = Iterations / elapsedTime.TotalSeconds; links.Delete(firstLink); Console.WriteLine( "{0} Iterations of GetTarget function done in {1} ({2} Iterations per second), counter result: {3}", Iterations, elapsedTime, (long)iterationsPerSecond, counter); } File.Delete(tempFilename); }
public void PathsTest() { var tempDatabaseFilename = Path.GetTempFileName(); var tempTransactionLogFilename = Path.GetTempFileName(); const ulong itself = Links.Itself; const Links.PathElement source = Links.PathElement.Source; const Links.PathElement target = Links.PathElement.Target; using (var links = new Links(tempDatabaseFilename, tempTransactionLogFilename, 1024 * 1024)) { var l1 = links.Create(itself, itself); var l2 = links.Create(itself, itself); var r1 = links.Get(l1, source, target, source); var r2 = links.Get(l2, l2, l2, l2); } File.Delete(tempDatabaseFilename); File.Delete(tempTransactionLogFilename); }
public void GetSourceTest() { string tempFilename = Path.GetTempFileName(); using (var links = new Links(tempFilename, DefaultLinksSize)) { Console.WriteLine("Testing GetSource function with {0} Iterations.", Iterations); ulong counter = 0; //var firstLink = links.First(); // Создаём одну связь, из которой будет производить считывание ulong firstLink = links.Create(0, 0); Stopwatch sw = Stopwatch.StartNew(); // Тестируем саму функцию for (ulong i = 0; i < Iterations; i++) counter += links.GetSource(firstLink); TimeSpan elapsedTime = sw.Elapsed; double iterationsPerSecond = Iterations / elapsedTime.TotalSeconds; // Удаляем связь, из которой производилось считывание links.Delete(firstLink); Console.WriteLine( "{0} Iterations of GetSource function done in {1} ({2} Iterations per second), counter result: {3}", Iterations, elapsedTime, (long)iterationsPerSecond, counter); } File.Delete(tempFilename); }
public void CascadeUpdateTest() { throw new StackOverflowException(); var tempDatabaseFilename = Path.GetTempFileName(); var tempTransactionLogFilename = Path.GetTempFileName(); const ulong itself = Links.Itself; using (var links = new Links(tempDatabaseFilename, tempTransactionLogFilename, 1024 * 1024)) { var l1 = links.Create(itself, itself); var l2 = links.Create(itself, itself); l2 = links.Update(l2, l2, l1, l2); links.Create(l2, itself); links.Create(l2, itself); l2 = links.Update(l2, l1); links.Delete(l2); Global.Trash = links.Total; } Global.Trash = FileHelpers .ReadAll<Links.Transition>(tempTransactionLogFilename); File.Delete(tempDatabaseFilename); File.Delete(tempTransactionLogFilename); }
public void BasicTransactionLogTest() { var tempDatabaseFilename = Path.GetTempFileName(); var tempTransactionLogFilename = Path.GetTempFileName(); const ulong itself = Links.Itself; using (var links = new Links(tempDatabaseFilename, tempTransactionLogFilename, 1024 * 1024)) { var l1 = links.Create(itself, itself); var l2 = links.Create(itself, itself); Global.Trash = links.Update(l2, l2, l1, l2); links.Delete(l1); } Global.Trash = FileHelpers .ReadAll<Links.Transition>(tempTransactionLogFilename); File.Delete(tempDatabaseFilename); File.Delete(tempTransactionLogFilename); }
public void AllVariantsSearchTest() { string tempFilename = Path.GetTempFileName(); const long sequenceLength = 8; const ulong itself = Links.Itself; using (var links = new Links(tempFilename, LinksSizeStep)) { var sequence = new ulong[sequenceLength]; for (int i = 0; i < sequenceLength; i++) sequence[i] = links.Create(itself, itself); var sequences = new Sequences(links); var createResults = sequences.CreateAllVariants2(sequence).Distinct().ToArray(); var sw0 = Stopwatch.StartNew(); var searchResults0 = sequences.GetAllMatchingSequences0(sequence); sw0.Stop(); var sw1 = Stopwatch.StartNew(); var searchResults1 = sequences.GetAllMatchingSequences1(sequence); sw1.Stop(); var sw2 = Stopwatch.StartNew(); var searchResults2 = sequences.Each(sequence); sw2.Stop(); var intersection0 = createResults.Intersect(searchResults0).ToList(); Assert.IsTrue(intersection0.Count == searchResults0.Count); Assert.IsTrue(intersection0.Count == createResults.Length); var intersection1 = createResults.Intersect(searchResults1).ToList(); Assert.IsTrue(intersection1.Count == searchResults1.Count); Assert.IsTrue(intersection1.Count == createResults.Length); var intersection2 = createResults.Intersect(searchResults2).ToList(); Assert.IsTrue(intersection2.Count == searchResults2.Count); Assert.IsTrue(intersection2.Count == createResults.Length); //Assert.IsTrue(sw1.Elapsed < sw2.Elapsed); for (int i = 0; i < sequenceLength; i++) links.Delete(sequence[i]); } File.Delete(tempFilename); }
public void AllPossibleConnectionsTest() { InitBitString(); string tempFilename = Path.GetTempFileName(); const long sequenceLength = 5; const ulong itself = Links.Itself; using (var links = new Links(tempFilename, LinksSizeStep)) { var sequence = new ulong[sequenceLength]; for (int i = 0; i < sequenceLength; i++) sequence[i] = links.Create(itself, itself); var sequences = new Sequences(links); var createResults = sequences.CreateAllVariants2(sequence); var reverseResults = sequences.CreateAllVariants2(sequence.Reverse().ToArray()); for (var i = 0; i < 1; i++) { var sw1 = Stopwatch.StartNew(); var searchResults1 = sequences.GetAllConnections(sequence); sw1.Stop(); var sw2 = Stopwatch.StartNew(); var searchResults2 = sequences.GetAllConnections1(sequence); sw2.Stop(); var sw3 = Stopwatch.StartNew(); var searchResults3 = sequences.GetAllConnections2(sequence); sw3.Stop(); var sw4 = Stopwatch.StartNew(); var searchResults4 = sequences.GetAllConnections3(sequence); sw4.Stop(); Global.Trash = searchResults3; Global.Trash = searchResults4; var intersection1 = createResults.Intersect(searchResults1).ToList(); Assert.IsTrue(intersection1.Count == createResults.Length); var intersection2 = reverseResults.Intersect(searchResults1).ToList(); Assert.IsTrue(intersection2.Count == reverseResults.Length); var intersection0 = searchResults1.Intersect(searchResults2).ToList(); Assert.IsTrue(intersection0.Count == searchResults2.Count); var intersection3 = searchResults2.Intersect(searchResults3).ToList(); Assert.IsTrue(intersection3.Count == searchResults3.Count); var intersection4 = searchResults3.Intersect(searchResults4).ToList(); Assert.IsTrue(intersection4.Count == searchResults4.Count); } for (int i = 0; i < sequenceLength; i++) links.Delete(sequence[i]); } File.Delete(tempFilename); }
public void PatternMatchTest() { string tempFilename = Path.GetTempFileName(); const ulong itself = Links.Itself; const ulong one = Sequences.Any; const ulong zeroOrMany = Sequences.ZeroOrMany; using (var links = new Links(tempFilename, LinksSizeStep)) { var e1 = links.Create(itself, itself); var e2 = links.Create(itself, itself); var sequence = new[] { e1, e2, e1, e2 // mama / papa }; var sequences = new Sequences(links); var balancedVariant = sequences.CreateBalancedVariant(sequence); // 1: [1] // 2: [2] // 3: [1,2] // 4: [1,2,1,2] var pair = links.GetSource(balancedVariant); var matchedSequences1 = sequences.MatchPattern(e2, e1, zeroOrMany); Assert.IsTrue(matchedSequences1.Count == 0); var matchedSequences2 = sequences.MatchPattern(zeroOrMany, e2, e1); Assert.IsTrue(matchedSequences2.Count == 0); var matchedSequences3 = sequences.MatchPattern(e1, zeroOrMany, e1); Assert.IsTrue(matchedSequences3.Count == 0); var matchedSequences4 = sequences.MatchPattern(e1, zeroOrMany, e2); Assert.IsTrue(matchedSequences4.Contains(pair)); Assert.IsTrue(matchedSequences4.Contains(balancedVariant)); for (int i = 0; i < sequence.Length; i++) links.Delete(sequence[i]); } File.Delete(tempFilename); }
public void CreateAllVariantsTest() { string tempFilename = Path.GetTempFileName(); const long sequenceLength = 8; const ulong itself = Links.Itself; using (var links = new Links(tempFilename, LinksSizeStep)) { var sequence = new ulong[sequenceLength]; for (int i = 0; i < sequenceLength; i++) sequence[i] = links.Create(itself, itself); var sequences = new Sequences(links); var sw1 = Stopwatch.StartNew(); var results1 = sequences.CreateAllVariants1(sequence); sw1.Stop(); var sw2 = Stopwatch.StartNew(); var results2 = sequences.CreateAllVariants2(sequence); sw2.Stop(); Assert.IsTrue(results1.Count > results2.Length); Assert.IsTrue(sw1.Elapsed > sw2.Elapsed); for (int i = 0; i < sequenceLength; i++) links.Delete(sequence[i]); } File.Delete(tempFilename); }
public void CalculateAllUsagesTest() { InitBitString(); string tempFilename = Path.GetTempFileName(); const long sequenceLength = 3; const ulong itself = Links.Itself; using (var links = new Links(tempFilename, LinksSizeStep)) { var sequence = new ulong[sequenceLength]; for (int i = 0; i < sequenceLength; i++) sequence[i] = links.Create(itself, itself); var sequences = new Sequences(links); var createResults = sequences.CreateAllVariants2(sequence); //var reverseResults = sequences.CreateAllVariants2(sequence.Reverse().ToArray()); for (var i = 0; i < 1; i++) { var linksTotalUsages1 = new ulong[links.Total + 1]; sequences.CalculateAllUsages(linksTotalUsages1); var linksTotalUsages2 = new ulong[links.Total + 1]; sequences.CalculateAllUsages2(linksTotalUsages2); var intersection1 = linksTotalUsages1.Intersect(linksTotalUsages2).ToList(); Assert.IsTrue(intersection1.Count == linksTotalUsages2.Length); } for (int i = 0; i < sequenceLength; i++) links.Delete(sequence[i]); } File.Delete(tempFilename); }
public void BalancedVariantSearchTest() { string tempFilename = Path.GetTempFileName(); const long sequenceLength = 200; const ulong itself = Links.Itself; using (var links = new Links(tempFilename, LinksSizeStep)) { var sequence = new ulong[sequenceLength]; for (int i = 0; i < sequenceLength; i++) sequence[i] = links.Create(itself, itself); var sequences = new Sequences(links); var sw1 = Stopwatch.StartNew(); var balancedVariant = sequences.CreateBalancedVariant(sequence); sw1.Stop(); var sw2 = Stopwatch.StartNew(); var searchResults2 = sequences.GetAllMatchingSequences0(sequence); sw2.Stop(); var sw3 = Stopwatch.StartNew(); var searchResults3 = sequences.GetAllMatchingSequences1(sequence); sw3.Stop(); // На количестве в 200 элементов это будет занимать вечность //var sw4 = Stopwatch.StartNew(); //var searchResults4 = sequences.Each(sequence); sw4.Stop(); Assert.IsTrue(searchResults2.Count == 1 && balancedVariant == searchResults2[0]); Assert.IsTrue(searchResults3.Count == 1 && balancedVariant == searchResults3.First()); //Assert.IsTrue(sw1.Elapsed < sw2.Elapsed); for (int i = 0; i < sequenceLength; i++) links.Delete(sequence[i]); } File.Delete(tempFilename); }
public void BalancedPartialVariantsSearchTest() { string tempFilename = Path.GetTempFileName(); const long sequenceLength = 200; const ulong itself = Links.Itself; using (var links = new Links(tempFilename, LinksSizeStep)) { var sequence = new ulong[sequenceLength]; for (int i = 0; i < sequenceLength; i++) sequence[i] = links.Create(itself, itself); var sequences = new Sequences(links); var balancedVariant = sequences.CreateBalancedVariant(sequence); var partialSequence = new ulong[sequenceLength - 2]; Array.Copy(sequence, 1, partialSequence, 0, sequenceLength - 2); var sw1 = Stopwatch.StartNew(); var searchResults1 = sequences.GetAllPartiallyMatchingSequences0(partialSequence); sw1.Stop(); var sw2 = Stopwatch.StartNew(); var searchResults2 = sequences.GetAllPartiallyMatchingSequences1(partialSequence); sw2.Stop(); Assert.IsTrue(searchResults1.Count == 1 && balancedVariant == searchResults1[0]); Assert.IsTrue(searchResults2.Count == 1 && balancedVariant == searchResults2.First()); for (int i = 0; i < sequenceLength; i++) links.Delete(sequence[i]); } File.Delete(tempFilename); }
public void TransactionsTest() { var tempDatabaseFilename = Path.GetTempFileName(); var tempTransactionLogFilename = Path.GetTempFileName(); const ulong itself = Links.Itself; // Auto Reverted (Because no commit at transaction) using (var links = new Links(tempDatabaseFilename, tempTransactionLogFilename, 1024 * 1024)) { using (var transaction = links.BeginTransaction()) { var l1 = links.Create(itself, itself); var l2 = links.Create(itself, itself); Global.Trash = links.Update(l2, l2, l1, l2); links.Delete(l1); Global.Trash = transaction; } Global.Trash = links.Total; } Global.Trash = FileHelpers .ReadAll<Links.Transition>(tempTransactionLogFilename); // User Code Error (Autoreverted) try { using (var links = new Links(tempDatabaseFilename, tempTransactionLogFilename, 1024 * 1024)) { using (var transaction = links.BeginTransaction()) { var l1 = links.Create(itself, itself); var l2 = links.Create(itself, itself); l2 = links.Update(l2, l2, l1, l2); links.Create(l2, itself); links.Create(l2, itself); ExceptionThrower(); l2 = links.Update(l2, l1); // TODO: Fix CascadeUpdateTest and move ExceptionThrower() before transaction.Commit() links.Delete(l2); transaction.Commit(); } Global.Trash = links.Total; } } catch { Global.Trash = FileHelpers .ReadAll<Links.Transition>(tempTransactionLogFilename); } // Commit using (var links = new Links(tempDatabaseFilename, tempTransactionLogFilename, 1024 * 1024)) { using (var transaction = links.BeginTransaction()) { var l1 = links.Create(itself, itself); var l2 = links.Create(itself, itself); Global.Trash = links.Update(l2, l2, l1, l2); links.Delete(l1); transaction.Commit(); } Global.Trash = links.Total; } Global.Trash = FileHelpers .ReadAll<Links.Transition>(tempTransactionLogFilename); // Damage database FileHelpers .WriteFirst(tempTransactionLogFilename, new Links.Transition { TransactionId = 555 }); // Try load damaged database try { // TODO: Fix using (var links = new Links(tempDatabaseFilename, tempTransactionLogFilename, 1024 * 1024)) { Global.Trash = links.Total; } } catch (NotSupportedException ex) { Assert.IsTrue(ex.Message == "Database is damaged, autorecovery is not supported yet."); } Global.Trash = FileHelpers .ReadAll<Links.Transition>(tempTransactionLogFilename); File.Delete(tempDatabaseFilename); File.Delete(tempTransactionLogFilename); }
public static void Create64BillionLinks() { string tempFilename = Path.GetTempFileName(); using (var links = new Links(tempFilename, DefaultLinksSize)) { ulong linksBeforeTest = links.Total; long linksToCreate = 64 * 1024 * 1024 / Links.LinkSizeInBytes; Console.WriteLine("Creating {0} links.", linksToCreate); TimeSpan elapsedTime = PerformanceHelpers.Measure(() => { for (long i = 0; i < linksToCreate; i++) { links.Create(0, 0); } }); ulong linksCreated = links.Total - linksBeforeTest; double linksPerSecond = linksCreated / elapsedTime.TotalSeconds; Console.WriteLine("Current links count: {0}.", links.Total); Console.WriteLine("{0} links created in {1} ({2} links per second)", linksCreated, elapsedTime, (long)linksPerSecond); } File.Delete(tempFilename); }
public static void Create64BillionLinksInParallel() { string tempFilename = Path.GetTempFileName(); using (var links = new Links(tempFilename, DefaultLinksSize)) { ulong linksBeforeTest = links.Total; Stopwatch sw = Stopwatch.StartNew(); long linksToCreate = 64 * 1024 * 1024 / Links.LinkSizeInBytes; Console.WriteLine("Creating {0} links in parallel.", linksToCreate); Parallel.For(0, linksToCreate, x => links.Create(0, 0)); TimeSpan elapsedTime = sw.Elapsed; ulong linksCreated = links.Total - linksBeforeTest; double linksPerSecond = linksCreated / elapsedTime.TotalSeconds; Console.WriteLine("{0} links created in {1} ({2} links per second)", linksCreated, elapsedTime, (long)linksPerSecond); } File.Delete(tempFilename); }