Now we need to create a class that will create the indices we need, as well as being the root element of the database./// <summary> /// . /// </summary> class MyData : Persistent { public int intKey; public string strData; } * This source code was highlighted with Source Code Highlighter .
/// <summary> /// . /// </summary> class MyData : Persistent { public int intKey; public string strData; } * This source code was highlighted with Source Code Highlighter .
/// <summary> /// . /// </summary> class MyData : Persistent { public int intKey; public string strData; } * This source code was highlighted with Source Code Highlighter .
/// <summary> /// . /// </summary> class MyData : Persistent { public int intKey; public string strData; } * This source code was highlighted with Source Code Highlighter .
/// <summary> /// . /// </summary> class MyData : Persistent { public int intKey; public string strData; } * This source code was highlighted with Source Code Highlighter .
/// <summary> /// . /// </summary> class MyData : Persistent { public int intKey; public string strData; } * This source code was highlighted with Source Code Highlighter .
/// <summary> /// . /// </summary> class MyData : Persistent { public int intKey; public string strData; } * This source code was highlighted with Source Code Highlighter .
/// <summary> /// . /// </summary> class MyData : Persistent { public int intKey; public string strData; } * This source code was highlighted with Source Code Highlighter .
/// <summary> /// . /// </summary> class MyData : Persistent { public int intKey; public string strData; } * This source code was highlighted with Source Code Highlighter .
/// <summary> /// . /// </summary> class MyData : Persistent { public int intKey; public string strData; } * This source code was highlighted with Source Code Highlighter .
Well, now it remains only to connect to the database and work with it. At the same time we will test the speed.* This source code was highlighted with Source Code Highlighter .
- /// <summary>
- /// The root element of the database.
- /// </ summary>
- class MyRoot: Persistent
- {
- /// <summary>
- /// Index across the intKey field in the MyData class.
- /// </ summary>
- public FieldIndex < int , MyData> intKeyIndex;
- public MyRoot (Storage db)
- : base (db)
- {
- intKeyIndex = db.CreateFieldIndex < int , MyData> ( "intKey" , false ); // False indicates non-unique key.
- }
- public MyRoot ()
- {
- }
- }
On my laptop, the creation of 100,000 objects is performed in 2.4 seconds, i.e. ~ 42k records per second* This source code was highlighted with Source Code Highlighter .
- class program
- {
- static void Main ( string [] args)
- {
- DateTime t1;
- DateTime t2;
- Storage db = StorageFactory.Instance.CreateStorage ();
- MyRoot Root;
- MyData Obj;
- MyData [] ObjList;
- // Select Perst 10MB of RAM for working with the database, which would slightly reduce the load on the disk and
- // increase speed.
- db.Open ( "MyDB.pdb" , 10 * 1024 * 1024, "My_SecreT_k3y" );
- // If the database does not have a root element yet, create it.
- if (db.Root == null )
- {
- db.Root = new MyRoot (db);
- }
- Root = (MyRoot) db.Root;
- // Add the MyData object to the database.
- t1 = DateTime .Now;
- for ( int i = 0; i <100000; i ++)
- {
- Obj = new MyData ();
- Obj.intKey = i;
- Obj.strData = "Hello world !!!" ;
- Root.intKeyIndex.Put (Obj);
- }
- t2 = DateTime .Now;
- Console .WriteLine ((t2 - t1) .TotalMilliseconds.ToString () + "ms" );
- // Now try to get it.
- //T.k. we created an index with a non-unique key, then we use the overloaded function Get (from, till).
- ObjList = Root.intKeyIndex.Get (1000, 1000);
- foreach (MyData Obj1 in ObjList)
- {
- Console .WriteLine (Obj1.strData);
- }
- db.Close ();
- }
- }
Source: https://habr.com/ru/post/89150/
All Articles