Simple to use embedded database BerkeleyDb

Database documentation official website: the Oracle Berkeley DB

Database access methods Explanation
Btree Btree access method is to achieve a balanced tree structure of the sort. Search tree, insertion and deletion require O (height) time, where height is Btree from root to leaf layers page. The upper limit of the height is log base_b N, which is the minimum number of keys base_b page, and N is the total number of stored keys. The disorder may cause data into Btree only half a page. DB so ordered (or reverse order) is inserted into the best case, resulting in almost full page space utilization.
Hash The method of accessing hash data structure is implemented extended linear hashing, as described in "Linear Hash: new tools for addressing tables and files"
Heap Heap access method records are stored in piles of papers. Referenced only by the recording and page offset is written thereof. Because the record is written in piles of papers, so no compression when deleting records, which can be more efficient use of space than Btree. Heap access method is suitable for disk space-constrained platform, especially in the case of those systems are performing large numbers of records to create and delete operations.
Queue Queue storage access method having a logical record number of fixed-length record as the key. It is designed to quickly insert the tail, and has a special consumption cursor operation that removed from the recording head of the queue and returns. Queue access method using record-level locking
RECN Recno access method to the logical record number as a key store fixed length and variable length records, and optionally supported by plain text (byte stream) file.

Select access methods

Ultimately, you can only access method to determine which is right for your application by using two access methods of performance testing. To be effective, this performance testing must be used with considerable production workloads.
In other words, at some point that you absolutely must use Btree:

  • If you want to use the bulk placement and acquisition operations.

  • If the cluster database sort order is important to you.

  • If you want to be able to use the cursor to create a record.

  • If you have multiple threads / processes are creating a new record, and want to be able to effectively use the cursor to traverse these records.

However, in addition to these limitations, there are some application features may cause you to suspect Heap for your application better than Btree. they are:

  • Your application will run in a resource-constrained environment, and you want to set a hard limit on the size of the database file.

  • You want to limit the growth of disk space for database files, and your application performs about the same number of records creation and deletion.

Btree need to be inserted into the new record in sorted to the correct page. This operation may need to read multiple pages. Heap database can simply reuse any space blank page which can be found in the cache. Insert-intensive applications often will find much more than Btree Heap efficiency high, especially with the increase in database size.

In the absence of information about the application and data access patterns of a case, for a small data set, or a Btree Hash access method is sufficient. For larger than the cached data is generally recommended Btree access method. If you have a really big data, the hash access method might be a better choice. The db_stat utility is monitoring your cache is a useful tool for how to implement it.

The sample code


            
            HashDatabaseConfig bTreeDatabaseConfig = new HashDatabaseConfig();
            //文件不存在则创建
            bTreeDatabaseConfig.Creation = CreatePolicy.IF_NEEDED;
            //页大小
            bTreeDatabaseConfig.PageSize = 512;
            //缓存大小
            bTreeDatabaseConfig.CacheSize = new CacheInfo(0, 64 * 1024, 1);
            //hash 类型的数据库
            HashDatabase bTreeDatabase = HashDatabase.Open("file.db", bTreeDatabaseConfig);
            string content = "HelloWorld";
            DatabaseEntry key = new DatabaseEntry(BitConverter.GetBytes(12));
            DatabaseEntry value = new DatabaseEntry(Encoding.ASCII.GetBytes(content));
            bTreeDatabase.Put(key, value);
            Console.WriteLine("写入成功");
            KeyValuePair<DatabaseEntry, DatabaseEntry> pair = bTreeDatabase.Get(key);
            Console.WriteLine("读取写入");
            Console.WriteLine(Encoding.ASCII.GetString(pair.Value.Data));
            bTreeDatabase.Close();

Required library download here dotnet dll library, the other can go to the official website to download, compile it yourself.

Guess you like

Origin www.cnblogs.com/panyan/p/12417047.html