Esempio n. 1
0
    static public void  Main(string[] args)
    {
        int     i;
        Storage db = StorageFactory.Instance.CreateStorage();

        db.Open("testnullable.dbs", pagePoolSize);

        Root root = (Root)db.Root;

        if (root == null)
        {
            root = new Root();
#if USE_GENERICS
            root.pki = db.CreateFieldIndex <int, Record>("pk", true);
            root.ski = db.CreateFieldIndex <int?, Record>("sk", true);
#else
            root.pki = db.CreateFieldIndex(typeof(Record), "pk", true);
            root.ski = db.CreateFieldIndex(typeof(Record), "sk", true);
#endif
            db.Root = root;
        }
#if USE_GENERICS
        FieldIndex <int, Record>  pki = root.pki;
        FieldIndex <int?, Record> ski = root.ski;
#else
        FieldIndex pki = root.pki;
        FieldIndex ski = root.ski;
#endif
        DateTime start = DateTime.Now;
        for (i = 0; i < nRecords; i++)
        {
            Record rec = new Record();
            rec.pk = i;
            if ((i & 1) != 0)
            {
                rec.sk = i;
            }
            pki.Put(rec);
            ski.Put(rec);
        }
        db.Commit();
        System.Console.WriteLine("Elapsed time for inserting " + nRecords + " records: " + (DateTime.Now - start));

        start = System.DateTime.Now;
        for (i = 0; i < nRecords; i++)
        {
#if USE_GENERICS
            Record rec1 = pki[i];
            Record rec2 = ski[i];
#else
            Record rec1 = (Record)pki[i];
            Record rec2 = (Record)ski[i];
#endif
            Debug.Assert(rec1 != null);
            if ((i & 1) != 0)
            {
                Debug.Assert(rec2 == rec1);
            }
            else
            {
                Debug.Assert(rec2 == null);
            }
        }
        System.Console.WriteLine("Elapsed time for performing " + nRecords * 2 + " index searches: " + (DateTime.Now - start));

        start = System.DateTime.Now;
        i     = 0;
        foreach (Record rec in pki)
        {
            Debug.Assert(rec.pk == i);
            if ((i & 1) != 0)
            {
                Debug.Assert(rec.sk == i);
            }
            else
            {
                Debug.Assert(rec.sk == null);
            }
            i += 1;
        }
        Debug.Assert(i == nRecords);
        i = 1;
        foreach (Record rec in ski)
        {
            Debug.Assert(rec.pk == i && rec.sk == i);
            i += 2;
        }
        Debug.Assert(i == nRecords + 1);
        System.Console.WriteLine("Elapsed time for iteration through " + nRecords * 3 / 2 + " records: " + (DateTime.Now - start));

        start = System.DateTime.Now;
        i     = 1;
        foreach (Record rec in pki.Select("sk = pk"))
        {
            Debug.Assert(rec.pk == i && rec.sk == i);
            i += 2;
        }
        Debug.Assert(i == nRecords + 1);
        System.Console.WriteLine("Elapsed time for first sequential SubSQL search in " + nRecords + " records: " + (DateTime.Now - start));

        i = 1;
        foreach (Record rec in pki.Select("sk+1 = pk+1 and (sk and 1) <> 0"))
        {
            Debug.Assert(rec.pk == i && rec.sk == i);
            i += 2;
        }
        Debug.Assert(i == nRecords + 1);
        System.Console.WriteLine("Elapsed time for second sequential SubSQL search in " + nRecords + " records: " + (DateTime.Now - start));

        start = System.DateTime.Now;
        for (i = 0; i < nRecords; i++)
        {
#if USE_GENERICS
            Record rec = pki[i];
#else
            Record rec = (Record)pki[i];
#endif
            bool removed = pki.Remove(rec);
            Debug.Assert(removed);
            removed = ski.Remove(rec);
            if ((i & 1) != 0)
            {
                Debug.Assert(removed);
            }
            else
            {
                Debug.Assert(!removed);
            }
            rec.Deallocate();
        }
        System.Console.WriteLine("Elapsed time for deleting " + nRecords + " records: " + (DateTime.Now - start));
        db.Close();
    }
    static public void  Main(string[] args)
    {
        int     i;
        Storage db = StorageFactory.Instance.CreateStorage();

        db.Open("testjsql.dbs", pagePoolSize);


        Root root = (Root)db.Root;

        if (root == null)
        {
            root = new Root();
#if USE_GENERICS
            root.strIndex  = db.CreateFieldIndex <string, Record>("strKey", true);
            root.intIndex  = db.CreateFieldIndex <long, Record>("intKey", true);
            root.dateIndex = db.CreateFieldIndex <DateTime, Record>("dateKey", false);
#else
            root.strIndex  = db.CreateFieldIndex(typeof(Record), "strKey", true);
            root.intIndex  = db.CreateFieldIndex(typeof(Record), "intKey", true);
            root.dateIndex = db.CreateFieldIndex(typeof(Record), "dateKey", false);
#endif
            db.Root = root;
        }
#if USE_GENERICS
        FieldIndex <string, Record>   strIndex  = root.strIndex;
        FieldIndex <long, Record>     intIndex  = root.intIndex;
        FieldIndex <DateTime, Record> dateIndex = root.dateIndex;
        IEnumerator <Record>          enumerator;
#else
        FieldIndex  intIndex  = root.intIndex;
        FieldIndex  strIndex  = root.strIndex;
        FieldIndex  dateIndex = root.dateIndex;
        IEnumerator enumerator;
#endif
        DateTime start = DateTime.Now;
        DateTime begin = start;
        long     key   = 1999;
        for (i = 0; i < nRecords; i++)
        {
            Record rec = new Record();
            key                    = (3141592621L * key + 2718281829L) % 1000000007L;
            rec.intKey             = key;
            rec.strKey             = System.Convert.ToString(key);
            rec.dateKey            = DateTime.Now;
            intIndex[rec.intKey]   = rec;
            strIndex[rec.strKey]   = rec;
            dateIndex[rec.dateKey] = rec;
        }

        db.Commit();
        DateTime end = DateTime.Now;
        System.Console.WriteLine("Elapsed time for inserting " + nRecords + " records: " + (end - start));

        start = System.DateTime.Now;
        key   = 1999;
#if USE_GENERICS
        Query <Record> q1 = db.CreateQuery <Record>();
        q1.Prepare("strKey=?");
        Query <Record> q2 = db.CreateQuery <Record>();
        q2.Prepare("intKey=?");
        Query <Record> q3 = db.CreateQuery <Record>();
        q3.Prepare("dateKey between ? and ?");
#else
        Query q1 = db.CreateQuery();
        q1.Prepare(typeof(Record), "strKey=?");
        Query q2 = db.CreateQuery();
        q2.Prepare(typeof(Record), "intKey=?");
        Query q3 = db.CreateQuery();
        q3.Prepare(typeof(Record), "dateKey between ? and ?");
#endif
        q1.AddIndex("strKey", strIndex);
        q2.AddIndex("intKey", intIndex);
        q3.AddIndex("dateKey", dateIndex);
        for (i = 0; i < nRecords; i++)
        {
            key        = (3141592621L * key + 2718281829L) % 1000000007L;
            q1[1]      = Convert.ToString(key);
            enumerator = q1.Execute(intIndex).GetEnumerator();
            enumerator.MoveNext();
#if USE_GENERICS
            Record rec1 = enumerator.Current;
#else
            Record rec1 = (Record)enumerator.Current;
#endif
            Debug.Assert(!enumerator.MoveNext());

            q2[1]      = key;
            enumerator = q2.Execute(strIndex).GetEnumerator();
            enumerator.MoveNext();
#if USE_GENERICS
            Record rec2 = enumerator.Current;
#else
            Record rec2 = (Record)enumerator.Current;
#endif
            Debug.Assert(rec1 != null && rec1 == rec2);
        }
        System.Console.WriteLine("Elapsed time for performing " + nRecords * 2 + " index searches: " + (DateTime.Now - start));

        start = System.DateTime.Now;
        key   = Int64.MinValue;
        i     = 0;
        foreach (Record rec in intIndex.Select("strKey=string(intKey)"))
        {
            Debug.Assert(rec.intKey >= key);
            key = rec.intKey;
            i  += 1;
        }
        Debug.Assert(i == nRecords);
        System.Console.WriteLine("Elapsed time for iteration through " + nRecords + " records: " + (DateTime.Now - start));


        start = System.DateTime.Now;
        key   = Int64.MinValue;
        i     = 0;
        foreach (Record rec in strIndex.Select("(intKey and 1023) = 0 order by intKey"))
        {
            Debug.Assert(rec.intKey >= key);
            key = rec.intKey;
            i  += 1;
        }
        System.Console.WriteLine("Elapsed time for ordering " + i + " records: " + (DateTime.Now - start));

        start = System.DateTime.Now;
        DateTime curr = begin;
        i     = 0;
        q3[1] = begin;
        q3[2] = end;
        foreach (Record rec in q3.Execute(dateIndex))
        {
            Debug.Assert(rec.dateKey >= curr);
            curr = rec.dateKey;
            i   += 1;
        }
        Debug.Assert(i == nRecords);
        System.Console.WriteLine("Elapsed time for iteration through date index for " + nRecords + " records: " + (DateTime.Now - start));

        start = System.DateTime.Now;
        key   = 1999;
        foreach (Record rec in intIndex)
        {
            rec.Deallocate();
        }
        intIndex.Deallocate();
        strIndex.Deallocate();
        dateIndex.Deallocate();
        System.Console.WriteLine("Elapsed time for deleting " + nRecords + " records: " + (DateTime.Now - start));
        db.Close();
    }