/// <summary>
        ///
        /// </summary>
        internal static void test1()
        {
            //IndexTermDict<string>.test1();
            //Console.ReadLine();
            //return;


            IndexTermDict <string> inMem = new IndexTermDict <string>();
            ulong index = 1;
            ulong term  = 1;

            inMem.Add(index, term, index.ToString() + "_" + term.ToString());

            index = 2;
            term  = 1;
            inMem.Add(index, term, index.ToString() + "_" + term.ToString());

            index = 2;
            term  = 2;
            inMem.Add(index, term, index.ToString() + "_" + term.ToString());

            index = 2;
            term  = 3;
            inMem.Add(index, term, index.ToString() + "_" + term.ToString());


            index = 3;
            term  = 2;
            inMem.Add(index, term, index.ToString() + "_" + term.ToString());
            index = 3;
            term  = 1;
            inMem.Add(index, term, index.ToString() + "_" + term.ToString());

            index = 3;
            term  = 3;
            inMem.Add(index, term, index.ToString() + "_" + term.ToString());


            Console.WriteLine("---------------Tests---------------------");

            if (inMem.Select(13, 1, out var billy))
            {
                Console.WriteLine(billy);
            }
            else
            {
                Console.WriteLine("billy not found");
            }


            Console.WriteLine("------------------------------------");

            foreach (var el in inMem.SelectForwardFromTo(0, 0, true, 1000, 1000))
            {
                Console.WriteLine($"v: {el.Item3}");
            }

            Console.WriteLine("------------------------------------");

            foreach (var el in inMem.SelectForwardFromTo(2, 2, false, 3, 2))
            {
                Console.WriteLine($"v: {el.Item3}");
            }

            //Console.WriteLine("------------------------------------");

            //foreach (var el in inMem.SelectBackwardFromTo(3, 2, false, 2, 2))
            //{
            //    Console.WriteLine($"v: {el.Item3}");
            //}


            Console.WriteLine("-----------After deletion-------------------------");

            inMem.Remove(inMem.SelectForwardFromTo(2, 2, false, 3, 2).ToList());

            foreach (var el in inMem.SelectForwardFromTo(0, 0, true, 1000, 1000))
            {
                Console.WriteLine($"v: {el.Item3}");
            }


            Console.ReadLine();
            return;
        }
Esempio n. 2
0
        /// <summary>
        /// under lock_operations
        /// only for followers
        /// </summary>
        /// <param name="lhb"></param>
        /// <returns>will return false if node needs synchronization from LastCommittedIndex/Term</returns>
        public bool SetLastCommittedIndexFromLeader(LeaderHeartbeat lhb)
        {
            if (this.LastCommittedIndex < lhb.LastStateLogCommittedIndex)
            {
                //Node tries to understand if it contains already this index/term, if not it will need synchronization

                ulong populateFrom = 0;
                Tuple <ulong, StateLogEntry> sleTpl;

                //if (rn.entitySettings.DelayedPersistenceIsActive && rn.entitySettings.InMemoryEntityStartSyncFromLatestEntity)
                //{

                //    populateFrom = this.LastCommittedIndex + 1;

                //    this.LastCommittedIndex = lhb.LastStateLogCommittedIndex;
                //    this.LastCommittedIndexTerm = lhb.LastStateLogCommittedIndexTerm;

                //    sleCacheIndex = lhb.LastStateLogCommittedIndex;
                //    sleCacheTerm = lhb.LastStateLogCommittedIndexTerm;
                //}
                //else
                if (rn.entitySettings.DelayedPersistenceIsActive
                    &&
                    sleCache.TryGetValue(lhb.LastStateLogCommittedIndex, out sleTpl) && sleTpl.Item1 == lhb.LastStateLogCommittedIndexTerm
                    )
                {
                    populateFrom = this.LastCommittedIndex + 1;

                    this.LastCommittedIndex     = lhb.LastStateLogCommittedIndex;
                    this.LastCommittedIndexTerm = lhb.LastStateLogCommittedIndexTerm;

                    sleCacheIndex = lhb.LastStateLogCommittedIndex;
                    sleCacheTerm  = lhb.LastStateLogCommittedIndexTerm;
                }
                else if (rn.entitySettings.InMemoryEntity)
                {
                    lock (inMem.Sync)
                    {
                        if (inMem.Select(lhb.LastStateLogCommittedIndex, lhb.LastStateLogCommittedIndexTerm, out var rSle))
                        {
                            populateFrom = this.LastCommittedIndex + 1;

                            this.LastCommittedIndex     = lhb.LastStateLogCommittedIndex;
                            this.LastCommittedIndexTerm = lhb.LastStateLogCommittedIndexTerm;

                            using (var t = this.rn.db.GetTransaction())
                            {
                                t.Insert <byte[], byte[]>(tblStateLogEntry, new byte[] { 2 }, lhb.LastStateLogCommittedIndex.ToBytes(lhb.LastStateLogCommittedIndexTerm));
                                t.Commit();
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
                else
                {
                    using (var t = this.rn.db.GetTransaction())
                    {
                        t.ValuesLazyLoadingIsOn = false;
                        var row = t.Select <byte[], byte[]>(tblStateLogEntry, (new byte[] { 1 }).ToBytes(lhb.LastStateLogCommittedIndex, lhb.LastStateLogCommittedIndexTerm));
                        if (row.Exists)
                        {
                            populateFrom = this.LastCommittedIndex + 1;

                            this.LastCommittedIndex     = lhb.LastStateLogCommittedIndex;
                            this.LastCommittedIndexTerm = lhb.LastStateLogCommittedIndexTerm;

                            //rn.VerbosePrint($"{rn.NodeAddress.NodeAddressId}> AddToLogFollower (I/T): here");

                            t.Insert <byte[], byte[]>(tblStateLogEntry, new byte[] { 2 }, lhb.LastStateLogCommittedIndex.ToBytes(lhb.LastStateLogCommittedIndexTerm));
                            t.Commit();
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }

                if (populateFrom > 0)
                {
                    this.rn.Commited();
                }
            }

            return(true);
        }