示例#1
0
文件: Test.cs 项目: armano2/fastdb
        static void Main(string[] args)
        {
            int  rc;
              uint oid = 0;
              int  nRecords = 3;

              FastDbConnection connection = new FastDbConnection("test");
              connection.InitDbSize       = FastDbConnection.DefaultInitDatabaseSize*2;
              connection.InitIdxSize      = FastDbConnection.DefaultInitIndexSize*2;
              connection.ExtensionQuantum = FastDbConnection.DefaultExtensionQuantum*2;
              connection.Open();

              FastDbFields fields = new FastDbFields();
              fields.Add("int_field", CLI.FieldType.cli_int8,   CLI.FieldFlags.cli_hashed);
              fields.Add("str_field", CLI.FieldType.cli_asciiz, CLI.FieldFlags.cli_indexed);

              rc = connection.CreateTable("test", fields);
              if (rc < 0) Console.WriteLine("cli_create_table: {0}", CLI.CliErrorToStr(rc));

              FastDbCommand command = connection.CreateCommand("insert into test");
              command.Fields.Assign(fields);  // You could also do:  command.Describe();

              DateTime time = DateTime.Now;

              long key = 1999;

              for (int i = 0; i < nRecords; i++)
              {
            key = (3141592621L * key + 2718281829L) % 1000000007L;
            command.Fields[0].asInt64 = key;
            command.Fields[1].asString = key.ToString();
            oid = command.Insert();
              }

              connection.Commit();
              Console.WriteLine(String.Format("Elapsed time for inserting {0} records: {1} (speed: {2:F2})", nRecords, DateTime.Now - time, nRecords*1000 / (DateTime.Now - time).TotalMilliseconds));

              command = connection.CreateCommand("insert into persons");

              command.Fields.Add("name",          CLI.FieldType.cli_asciiz, CLI.FieldFlags.cli_indexed);
              command.Fields.Add("salary",        CLI.FieldType.cli_int8);
              command.Fields.Add("address",       CLI.FieldType.cli_asciiz);
              command.Fields.Add("weight",        CLI.FieldType.cli_real8);
              command.Fields.Add("subordinates",  CLI.FieldType.cli_array_of_oid, CLI.FieldFlags.cli_noindex, "persons");

              rc = connection.CreateTable("persons", command.Fields);
              if (rc < 0)
            Console.WriteLine("cli_create_table: {0}", rc);

              Console.WriteLine("Name\tType\tSize\tCapacity\tFlags");
              for (int i=0; i < command.Fields.Count; ++i)
              {
            FastDbField f = command.Fields[i];
            Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}",
              f.Name, f.Type, f.Size, f.Capacity, f.Flags);
              }

              connection.Commit();

              time = DateTime.Now;

              for (int i=0; i < nRecords; ++i)
              {
            int n = 0;
            command.Fields[n++].asString = String.Format("Name{0}", i+1);
            command.Fields[n++].asInt64  = (i+1) * 1000;
            command.Fields[n++].asString = String.Format("Address {0}", 100-i);
            command.Fields[n++].asDouble = (i+1) * 100;
            command.Fields[n].ArraySize = (i==0) ? 0 : 1;
            if (i > 0)
              command.Fields[n].SetArrayValue(0, oid);

            oid = command.Insert();
              }
              connection.Commit();

              Console.WriteLine("Elapsed time for inserting {0} records: {1} (speed: {2:F2})", nRecords, DateTime.Now - time, nRecords*1000 / (DateTime.Now - time).TotalMilliseconds);

              command = connection.CreateCommand("select * from persons");
              //command.Execute();

              DumpRecords(command);
              connection.Commit();

              command = connection.CreateCommand(@"select * from persons where length(subordinates) < %subordinates and salary > %salary");
              command.Parameters.Add("subordinates", CLI.FieldType.cli_int4);
              command.Parameters.Add("salary",       CLI.FieldType.cli_int8);

              command.Parameters["subordinates"].asInteger = 1;
              command.Parameters["salary"].asInteger       = 100;

              //command.Execute();
              DumpRecords(command);
        }
示例#2
0
        static void Main(string[] args)
        {
            int  rc, i, n;
              uint oid = 0;
              int  nRecords = 200000;

              FastDbConnection connection = new FastDbConnection("test");
              connection.InitDbSize       = FastDbConnection.DefaultInitDatabaseSize*2;
              connection.InitIdxSize      = FastDbConnection.DefaultInitIndexSize*2;
              connection.ExtensionQuantum = FastDbConnection.DefaultExtensionQuantum*2;
              connection.Open();

              FastDbFields fields = new FastDbFields();
              fields.Add("int_field", CLI.FieldType.cli_int8,   CLI.FieldFlags.cli_hashed);
              fields.Add("str_field", CLI.FieldType.cli_asciiz, CLI.FieldFlags.cli_hashed);

              rc = connection.CreateTable("test", fields);
              if (rc < 0) Console.WriteLine("cli_create_table: {0}", CLI.CliErrorToStr(rc));

              FastDbCommand command = connection.CreateCommand("insert into test");
              command.Fields.Assign(fields);  // You could also do:  command.Describe();

              DateTime start = DateTime.Now;

              long key = 1999;

              for (i = 0; i < nRecords; i++) {
            key = (3141592621L * key + 2718281829L) % 1000000007L;
            command.Fields[0].asInt64 = key;
            command.Fields[1].asString = key.ToString();
            oid = command.Insert();
              }

              connection.Commit();
              Console.WriteLine("Elapsed time for inserting {0} records: {1:F2} (speed: {2:F2})",
            nRecords,
            (DateTime.Now - start).TotalSeconds,
            nRecords / (DateTime.Now - start).TotalSeconds);

              FastDbCommand command1 = connection.CreateCommand("select * from test where int_field = %key");
              FastDbCommand command2 = connection.CreateCommand("select * from test where str_field = %key");
              command1.Parameters.Add("key", CLI.FieldType.cli_int8);
              command2.Parameters.Add("key", CLI.FieldType.cli_asciiz);
              command1.Fields.Assign(fields);
              command2.Fields.Assign(fields);

              int rc1;
              int rc2;

              key = 1999;
              start = System.DateTime.Now;
              for (i = 0; i < nRecords; i++) {
            key = (3141592621L * key + 2718281829L) % 1000000007L;
            command1.Parameters[0].asInt64  = key;
            command2.Parameters[0].asString = key.ToString();
            rc1 = command1.Execute();
            rc2 = command2.Execute();
              }
              System.Console.WriteLine("Elapsed time for performing {0} index searches {1:F2} (speed: {2:F2})",
            nRecords * 2, (DateTime.Now - start).TotalSeconds,
            nRecords * 2 / (DateTime.Now - start).TotalSeconds);

              command.SQL = "select * from test";

              start = System.DateTime.Now;

              n = command.Execute();
              for(i=0; i < n; i++) {
            command.Next();
              }

              Console.WriteLine(String.Format("Elapsed time for iteration through {0} records: {1:F2} (speed: {2:F2})",
            nRecords,
            (DateTime.Now - start).TotalSeconds,
            nRecords / (DateTime.Now - start).TotalSeconds));

              start = System.DateTime.Now;
              /*
              key = 1999;
              for (i = 0; i < nRecords; i++) {
            key = (3141592621L * key + 2718281829L) % 1000000007L;
            Record rec = (Record) intIndex.Get(new Key(key));
            Record removed = (Record)intIndex.Remove(new Key(key));
            Debug.Assert(removed == rec);
            strIndex.Remove(new Key(System.Convert.ToString(key)), rec);
            rec.Deallocate();
              }*/
              command.Execute(true);
              command.Delete();
              connection.Commit();
              System.Console.WriteLine("Elapsed time for deleting {0} records: {1:F2} (speed: {2:F2})",
            nRecords,
            (DateTime.Now - start).TotalSeconds,
            nRecords/(DateTime.Now - start).TotalSeconds);
        }