Binary DataBase

binary database speed index text key
Skip Navigation Links

A normal database can be rather slow for looking up keys and returning rows if the index is a text column. This Binary DataBase provides a very fast way for looking up rows.

The binary database

note This class is described in depth in this article.

To create a new DataBase you should use the BinaryDataBaseWriter:

    M8.System.Data.IWriteToDataBase dbWriter = 
        new M8.System.Data.BinaryDataBaseWriter("indexFile.txt", "dataFile.txt", 
                                                    M8.System.IO.FileMode.Create);
    using(dbWriter as IDisposable)
    using(SqlConnection conn = new SqlConnection("ConnectionString"))
    {
        conn.Open();
        dbWriter.Open();
        
        String sql = "SELECT * FROM MySqlTable";
        IDataReader rdr = new SqlCommand(sql, conn).ExecuteReader();
        using(rdr as IDisposable)
        {
            while(rdr.Read())
                dbWriter.Write(rdr, "MyIndexColumn");
        }
    }
    

The main parts of this code are the first and last lines. In the first line the BinaryDataBaseWriter is instantiated. We're passing three parameters: a name for the index file, a name for the data file, and a FileMode. The FileMode can have one of the values Create or AppendOrCreate.

In the last line each IDataReader instance is written to the database. This time passing two parameters: the IDataReader and the name of the column to use as an index.

Retrieving elements from the database is equally easy:

    String hashFile = @"C:\mydb.hash";
    String dataFile = @"C:\mydb.data";
    String connectionString = "hash:" + hashFile + ";data:" + dataFile + ";";
    //i.e. the connectionstring is of the format "hash:filename.hash;data:filename.data;"
    
    M8.System.Data.IReadFromDataBase rdr = 
        new M8.System.Data.BinaryDataBaseReader(connectionString);
    using (rdr as IDisposable)
    {
        rdr.Open();
        IList<String> lst = rdr.GetRow("adorno");
        //the IList will contain the 'DataRow' where the primary key equals "adorno"
        
        foreach(String s in lst)
        {
            Console.WriteLine(s);
        }
    }
    

Download

   Søg   Kontakt    Udskriv