void Master(int port, bool async, bool ack, int nIterations)
        {
            Console.WriteLine("Starting a replication master");
            ReplicationMasterDatabase db =
                DatabaseFactory.CreateReplicationMasterDatabase(new string[] { "localhost:" + port },
                                                                async ? asyncBufSize : 0);
            string dbName = "replicmaster.dbs";

            Tests.TryDeleteFile(dbName);
            db.ReplicationAck = ack;
            var dbFile = new OsFile(dbName);

            dbFile.NoFlush = true;
            db.Open(dbFile, cacheSizeInBytes);

            IFieldIndex <int, Record> root = (IFieldIndex <int, Record>)db.Root;

            if (root == null)
            {
                root    = db.CreateFieldIndex <int, Record>("key", IndexType.Unique);
                db.Root = root;
            }
            DateTime start = DateTime.Now;
            int      i;
            int      lastKey = 0;

            for (i = 0; i < nIterations; i++)
            {
                if (i >= count)
                {
                    root.Remove(new Key(i - count));
                }

                Record rec = new Record();
                rec.key = i;
                lastKey = rec.key;
                root.Put(rec);
                if (i >= count && i % transSize == 0)
                {
                    db.Commit();
                }
                if (log && i % 1000 == 0)
                {
                    Console.WriteLine("Master processed {0} rounds", i);
                }
            }
            db.Commit();
            while (true)
            {
                long slaveKey = Interlocked.Read(ref slaveCurrKey);
                if (slaveKey == lastKey)
                {
                    break;
                }
                Thread.Sleep(100); // 1/10th sec
            }
            db.Close();

            Console.WriteLine("Replication master finished", i);
        }
        /// <summary>
        /// Searches for a unique value. The field1 and field2 should be unique
        /// </summary>
        /// <typeparam name="TField1"></typeparam>
        /// <typeparam name="TField2"></typeparam>
        /// <param name="field1"></param>
        /// <param name="field2"></param>
        /// <param name="value1"></param>
        /// <param name="value2"></param>
        /// <returns></returns>
        public RowId Find <TField1, TField2>(IField <TRecord, TField1> field1, IField <TRecord, TField2> field2, TField1 value1, TField2 value2)
        {
            IFieldIndex <Composite <TField1, TField2> > index = (IFieldIndex <Composite <TField1, TField2> >) this.FindIndex(field1, field2, true);

            if (index != null)
            {
                return(((IUniqueIndex <Composite <TField1, TField2> >)index).FindUnique(new Composite <TField1, TField2>()
                {
                    t1 = value1, t2 = value2
                }, this.StoreSnapshot.Version));
            }
            throw new InvalidOperationException(Properties.Resources.ErrorNoIndex2(this.Name, field1.Name, field2.Name));
        }
        void Slave(int port, bool async, bool ack)
        {
            Console.WriteLine("Starting a replication slave");
            int i;
            ReplicationSlaveDatabase db = DatabaseFactory.CreateReplicationSlaveDatabase(port);

            db.ReplicationAck = ack;
            string dbName = "replicslave.dbs";

            Tests.TryDeleteFile(dbName);
            var dbFile = new OsFile(dbName);

            dbFile.NoFlush = true;
            db.Open(dbFile, cacheSizeInBytes);

            DateTime total   = new DateTime(0);
            int      n       = 0;
            long     lastKey = 0;

            while (db.IsConnected())
            {
                db.WaitForModification();
                db.BeginThreadTransaction(TransactionMode.ReplicationSlave);
                IFieldIndex <int, Record> root = (IFieldIndex <int, Record>)db.Root;
                if (root != null && root.Count == count)
                {
                    DateTime start   = DateTime.Now;
                    int      prevKey = -1;
                    i = 0;
                    foreach (Record rec in root)
                    {
                        int key = rec.key;
                        lastKey = rec.key;
                        Debug.Assert(prevKey < 0 || key == prevKey + 1);
                        prevKey = key;
                        i      += 1;
                    }
                    Debug.Assert(i == count);
                    n     += i;
                    total += (DateTime.Now - start);
                }
                db.EndThreadTransaction();
                Interlocked.Exchange(ref slaveCurrKey, lastKey);
                if (log && n % 1000 == 0)
                {
                    Console.WriteLine("Slave processed {0} transactions", n);
                }
            }
            db.Close();
            Console.WriteLine("Replication slave finished", n);
        }
        /// <summary>
        /// Selects rows by the provided criteria
        /// </summary>
        /// <typeparam name="TField1"></typeparam>
        /// <typeparam name="TField2"></typeparam>
        /// <param name="field1"></param>
        /// <param name="field2"></param>
        /// <param name="value1"></param>
        /// <param name="value2"></param>
        /// <returns></returns>
        public IEnumerable <RowId> Select <TField1, TField2>(IField <TRecord, TField1> field1, IField <TRecord, TField2> field2, TField1 value1, TField2 value2)
        {
            IFieldIndex <Composite <TField1, TField2> > index = (IFieldIndex <Composite <TField1, TField2> >) this.FindIndex(field1, field2, false);

            if (index != null)
            {
                return(index.Find(new Composite <TField1, TField2>()
                {
                    t1 = value1, t2 = value2
                }, this.StoreSnapshot.Version));
            }
            else
            {
                return(this.SelectDirect(new CompositeField <TField1, TField2>(field1, field2), new Composite <TField1, TField2>()
                {
                    t1 = value1, t2 = value2
                }));
            }
        }
        public void Run(TestConfig config)
        {
            Stock stock;
            int   i;
            int   count = config.Count;
            var   res   = new TestTimeSeriesResult();

            config.Result = res;

            var       start = DateTime.Now;
            IDatabase db    = config.GetDatabase();

            IFieldIndex <string, Stock> stocks = (IFieldIndex <string, Stock>)db.Root;

            Tests.Assert(stocks == null);
            stocks       = db.CreateFieldIndex <string, Stock>("name", IndexType.Unique);
            stock        = new Stock();
            stock.name   = "BORL";
            stock.quotes = db.CreateTimeSeries <Quote>(N_ELEMS_PER_BLOCK, N_ELEMS_PER_BLOCK * TICKS_PER_SECOND * 2);
            stocks.Put(stock);
            db.Root = stocks;

            Tests.Assert(!stock.quotes.IsReadOnly);
            rand = new Random(2004);
            int startTimeInSecs = getSeconds(start);
            int currTime        = startTimeInSecs;

            for (i = 0; i < count; i++)
            {
                Quote quote = NewQuote(currTime++);
                stock.quotes.Add(quote);
            }
            Tests.Assert(stock.quotes.Count == count);
            db.Commit();
            Tests.Assert(stock.quotes.Count == count);
            res.InsertTime = DateTime.Now - start;

            start = DateTime.Now;
            rand  = new Random(2004);
            start = DateTime.Now;
            i     = 0;
            foreach (Quote quote in stock.quotes)
            {
                Tests.Assert(quote.timestamp == startTimeInSecs + i);
                float open = (float)rand.Next(10000) / 100;
                Tests.Assert(quote.open == open);
                float close = (float)rand.Next(10000) / 100;
                Tests.Assert(quote.close == close);
                Tests.Assert(quote.high == Math.Max(quote.open, quote.close));
                Tests.Assert(quote.low == Math.Min(quote.open, quote.close));
                Tests.Assert(quote.volume == rand.Next(1000));
                i += 1;
            }
            Tests.Assert(i == count);

            res.SearchTime1 = DateTime.Now - start;

            start = DateTime.Now;
            long from = getTicks(startTimeInSecs + count / 2);
            long till = getTicks(startTimeInSecs + count);

            i = 0;
            foreach (Quote quote in stock.quotes.Range(new DateTime(from), new DateTime(till), IterationOrder.DescentOrder))
            {
                int expectedtimestamp = startTimeInSecs + count - i - 1;
                Tests.Assert(quote.timestamp == expectedtimestamp);
                i += 1;
            }
            res.SearchTime2 = DateTime.Now - start;
            start           = DateTime.Now;

            // insert in the middle
            stock.quotes.Add(NewQuote(startTimeInSecs - count / 2));

            long n = stock.quotes.Remove(stock.quotes.FirstTime, stock.quotes.LastTime);

            Tests.Assert(n == count + 1);
            Tests.Assert(stock.quotes.Count == 0);
            res.RemoveTime = DateTime.Now - start;

            Quote q;
            Quote qFirst  = NewQuote(0);
            Quote qMiddle = NewQuote(0);
            Quote qEnd    = NewQuote(0);

            for (i = 0; i < 10; i++)
            {
                q = NewQuote(startTimeInSecs + i);
                stock.quotes.Add(q);
                if (i == 0)
                {
                    qFirst = q;
                }
                else if (i == 5)
                {
                    qMiddle = q;
                }
                else if (i == 9)
                {
                    qEnd = q;
                }
            }
            Tests.Assert(stock.quotes.Contains(qFirst));
            Tests.Assert(stock.quotes.Contains(qEnd));
            Tests.Assert(stock.quotes.Contains(qMiddle));
            Tests.Assert(stock.quotes.Remove(qFirst));
            Tests.Assert(!stock.quotes.Contains(qFirst));
            Tests.Assert(stock.quotes.Remove(qEnd));
            Tests.Assert(!stock.quotes.Contains(qEnd));
            Tests.Assert(stock.quotes.Remove(qMiddle));
            Tests.Assert(!stock.quotes.Contains(qMiddle));

            Quote[] quotes = new Quote[10];
            stock.quotes.CopyTo(quotes, 0);
            stock.quotes.Clear();

            Tests.AssertDatabaseException(
                () => { long tmp = stock.quotes.FirstTime.Ticks; }, DatabaseException.ErrorCode.KEY_NOT_FOUND);
            Tests.AssertDatabaseException(
                () => { long tmp = stock.quotes.LastTime.Ticks; }, DatabaseException.ErrorCode.KEY_NOT_FOUND);

            for (i = 0; i < 10; i++)
            {
                q = NewQuote(startTimeInSecs + i);
                stock.quotes.Add(q);
            }

            IEnumerator e = stock.quotes.GetEnumerator();

            i = 0;
            while (e.MoveNext())
            {
                i++;
            }
            Tests.Assert(i == 10);
            Tests.Assert(!e.MoveNext());
            Tests.AssertException <InvalidOperationException>(
                () => { object o = e.Current; });
            e.Reset();
            Tests.Assert(e.MoveNext());

            e = stock.quotes.Reverse().GetEnumerator();
            i = 0;
            while (e.MoveNext())
            {
                i++;
            }
            Tests.Assert(i == 10);
            Tests.Assert(!e.MoveNext());
            Tests.AssertException <InvalidOperationException>(
                () => { object o = e.Current; });
            e.Reset();
            Tests.Assert(e.MoveNext());
            DateTime tStart  = new DateTime(getTicks(startTimeInSecs));
            DateTime tMiddle = new DateTime(getTicks(startTimeInSecs + 5));
            DateTime tEnd    = new DateTime(getTicks(startTimeInSecs + 9));

            IEnumerator <Quote> e2 = stock.quotes.GetEnumerator(tStart, tMiddle);

            VerifyEnumerator(e2, tStart.Ticks, tMiddle.Ticks);
            e2 = stock.quotes.GetEnumerator(tStart, tMiddle, IterationOrder.DescentOrder);
            VerifyEnumerator(e2, tStart.Ticks, tMiddle.Ticks, IterationOrder.DescentOrder);

            e2 = stock.quotes.GetEnumerator(IterationOrder.DescentOrder);
            VerifyEnumerator(e2, tStart.Ticks, tEnd.Ticks, IterationOrder.DescentOrder);

            e2 = stock.quotes.Range(tMiddle, tEnd, IterationOrder.AscentOrder).GetEnumerator();
            VerifyEnumerator(e2, tMiddle.Ticks, tEnd.Ticks, IterationOrder.AscentOrder);

            e2 = stock.quotes.Range(IterationOrder.DescentOrder).GetEnumerator();
            VerifyEnumerator(e2, tStart.Ticks, tEnd.Ticks, IterationOrder.DescentOrder);

            e2 = stock.quotes.Till(tMiddle).GetEnumerator();
            VerifyEnumerator(e2, tStart.Ticks, tMiddle.Ticks, IterationOrder.DescentOrder);

            e2 = stock.quotes.From(tMiddle).GetEnumerator();
            VerifyEnumerator(e2, tMiddle.Ticks, tEnd.Ticks);

            e2 = stock.quotes.Reverse().GetEnumerator();
            VerifyEnumerator(e2, tStart.Ticks, tEnd.Ticks, IterationOrder.DescentOrder);

            Tests.Assert(stock.quotes.FirstTime.Ticks == tStart.Ticks);
            Tests.Assert(stock.quotes.LastTime.Ticks == tEnd.Ticks);
            for (i = 0; i < 10; i++)
            {
                long  ticks = getTicks(startTimeInSecs + i);
                Quote qTmp  = stock.quotes[new DateTime(ticks)];
                Tests.Assert(qTmp.Ticks == ticks);
                Tests.Assert(stock.quotes.Contains(new DateTime(ticks)));
            }
            Tests.Assert(!stock.quotes.Contains(new DateTime(0)));

            Tests.AssertDatabaseException(
                () => { Quote tmp = stock.quotes[new DateTime(0)]; }, DatabaseException.ErrorCode.KEY_NOT_FOUND);

            stock.quotes.RemoveFrom(new DateTime(getTicks(startTimeInSecs + 8)));
            stock.quotes.RemoveFrom(new DateTime(getTicks(startTimeInSecs + 2)));
            stock.quotes.RemoveAll();
            db.Commit();
            stock.quotes.Deallocate();
            db.Commit();
            db.Close();
        }
Exemple #6
0
        public void Run(TestConfig config)
        {
            int i;
            int count = config.Count;
            var res   = new TestBackupResult();

            DateTime start = DateTime.Now;

            string    dbNameBackup = config.DatabaseName + ".backup.dbs";
            IDatabase db           = config.GetDatabase();
            Root      root         = (Root)db.Root;

            Tests.Assert(root == null);
            root               = new Root();
            root.strIndex      = db.CreateIndex <string, Record>(IndexType.Unique);
            root.intIndex      = db.CreateFieldIndex <long, Record>("intKey", IndexType.Unique);
            root.compoundIndex = db.CreateFieldIndex <Record>(new String[] { "strKey", "intKey" }, IndexType.Unique);
            db.Root            = root;
            IFieldIndex <long, Record> intIndex      = root.intIndex;
            IMultiFieldIndex <Record>  compoundIndex = root.compoundIndex;
            IIndex <string, Record>    strIndex      = root.strIndex;
            long key = 1999;

            for (i = 0; i < count; i++)
            {
                Record rec = new Record();
                rec.intKey  = key;
                rec.strKey  = System.Convert.ToString(key);
                rec.realKey = (double)key;
                intIndex.Put(rec);
                strIndex.Put(new Key(rec.strKey), rec);
                compoundIndex.Put(rec);
                key = (3141592621L * key + 2718281829L) % 1000000007L;
            }
            db.Commit();
            Tests.Assert(intIndex.Count == count);
            Tests.Assert(strIndex.Count == count);
            Tests.Assert(compoundIndex.Count == count);
            res.InsertTime = DateTime.Now - start;

            start = DateTime.Now;
            System.IO.FileStream stream = new System.IO.FileStream(dbNameBackup, FileMode.Create, FileAccess.Write);
            db.Backup(stream);
            stream.Close();
            db.Close();
            res.BackupTime = DateTime.Now - start;

            start = DateTime.Now;
            db.Open(dbNameBackup);
            root          = (Root)db.Root;
            intIndex      = root.intIndex;
            strIndex      = root.strIndex;
            compoundIndex = root.compoundIndex;

            key = 1999;
            for (i = 0; i < count; i++)
            {
                String strKey = System.Convert.ToString(key);
                Record rec1   = intIndex.Get(key);
                Record rec2   = strIndex.Get(strKey);
                Record rec3   = compoundIndex.Get(new Key(strKey, key));

                Tests.Assert(rec1 != null);
                Tests.Assert(rec1 == rec2);
                Tests.Assert(rec1 == rec3);
                Tests.Assert(rec1.intKey == key);
                Tests.Assert(rec1.realKey == (double)key);
                Tests.Assert(strKey.Equals(rec1.strKey));
                key = (3141592621L * key + 2718281829L) % 1000000007L;
            }
            db.Close();
        }