The implementation of a simple key-value storage system

Source: Internet
Author: User
Tags file size int size

Body:

The so-called key-value is to store one data at a time, according to the key to store the index. To achieve the quick lookup of key, I used the b-tree storage structure. B-tree is heavily used in the index of the database, so the selection of B-tree presumably will not have too much problem. Value is the values that should be key, and his length is unknown, so to implement such a storage system, you must resolve the mapping relationship from key value to file location.

Problem one: Realizing the "free" reading and writing of documents
Question two: realizing Btree

Problem one: Realizing the "free" reading and writing of documents.

Basic idea: All content is stored in a file, the file is divided into the same size granularity, you can freely apply for different size space, you can release the space that has been applied. Enables file operations to be consistent with the memory operation interface.

Based on the above requirements, my file storage structure is shown in the following illustration:

typedef struct _diskatom{
    int64 self;    Self index
    int64 next;    Next index
    int64 pre;    Previous index
    int64 ext;    Extended index
    int size;    Size
    unsigned char info;    information [1:head|1:used|0|0|0|0|0|0]
    unsigned char dirty;
    unsigned char data[disk_allo_size];
} diskatom_t;
    
typedef struct _diskctx{
    int64 ctxtop;
    Int64 num_used;
    Int64 Num_free;
    diskatom_t list_used;
    diskatom_t list_free;
} diskctx_t;

Application space:

1. It takes several spaces to calculate the application size.

2. Check to see if there is enough space in the free list.

3. There are enough free blocks to move the free blocks to the use list.

4. Insufficient free space, enlarge file size, insert new block into "use list".

Free space:

Just the opposite of the application space.

1. Move the use block to the free list. So that the block can be re applied.

Processing application space and free space, but also realized read, write, reapply space function.

Question two: realizing Btree

Excerpts from Wikipedia's description of Btree:

B-tree (multiple search trees, not binary) is a common data structure. The use of the B-TREE structure can significantly reduce the intermediate process that is experienced in locating records, thereby speeding the access rate. According to translation, B is generally considered to be the abbreviation of balance. This data structure is generally used in the index of the database, the comprehensive efficiency is high.

Http://baike.baidu.com/view/363832.htm

We have the ability to read and write files above (problem one), then the B-tree is built into the file, and it is built into memory, in fact, the same. B-tree's content can be found on the Internet. We may have to read and write files while manipulating the contents of the node. The original pointer in memory now becomes the location in the file.

Test function:

The software now implements the following functions to manipulate the Key-value database. In terms of efficiency, write 10,000 data roughly in 350ms, find read faster than write about one time; because it is written directly to the hard disk, there is no caching mechanism, efficiency can only be so. First of all, to achieve the optimization of the function.

Open (DBPath) opens a database

Set (container, key, value) Add/Set a key

Get (container, key) gets the contents of a key

Del (container, key) Delete a key

Close () closes a database

SOURCE Download: Http://files.cnblogs.com/linxr/kvfs.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.