Gkvdb-go Language Embedded Database

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The DRH (deep-re-hash) deep hash Partitioning algorithm developed by the go language is a high-performance, highly available key-value embedded transaction database.
Gkvdb is open source, free, distributed based on the MIT Protocol, Open Source project address (gitee in real-time sync with GitHub warehouse):
Gitee (HTTPS://GITEE.COM/JOHNG/GKVDB),Github(https://github.com/johng-cn/g ...)

Characteristics

    1. Based on the pure Go language implementation, with excellent cross-platform;
    2. The database file adopts DRH algorithm to improve the operation performance of random data.
    3. Good IO multiplexing design to improve operational performance of the underlying database files;
    4. Good high-availability design to ensure data integrity in any abnormal situation;
    5. Basic operating interface provided: Set (), Get (), Remove ();
    6. Transaction operation Interface provided: Begin (), Commit (), Rollback ();
    7. Multi-table Operation interface provided: Table (), Setto (), Getfrom (), Removefrom ();
    8. Supports atomic operations, bulk operations, transactional operations, multi-table operations, multi-table transactions, random traversal, and other features;

Limit

    1. (default) Table name maximum 255B;
    2. (default) Key name maximum 255B;
    3. (default) The maximum value of the key is 16MB;
    4. (default) Single-table data 1TB;
    5. Support random traversal, do not support range traversal;
    6. Embedded database, no built-in C/s architecture;

Installation

go get -u gitee.com/johng/gfgo get -u gitee.com/johng/gkvdb

Use

1. Basic usage

import "gitee.com/johng/gkvdb/gkvdb"// 创建数据库,指定数据库存放目录// gkvdb支持多表,默认数据表名称为defaultdb, err := gkvdb.New("/tmp/gkvdb")if err != nil {    fmt.Println(err)}key   := []byte("name")value := []byte("john")// 插入数据if err := db.Set(key, value); err != nil {    fmt.Println(err)}// 查询数据fmt.Println(db.Get(key))// 删除数据if err := db.Remove(key); err != nil {    fmt.Println(err)}// 关闭数据库链接,让GC自动回收数据库相关资源db.Close()

2. Transaction operations

// 开启事务tx := db.Begin()// 事务写入tx.Set(key, value)// 事务查询fmt.Println(tx.Get(key))// 事务提交tx.Commit()// 事务删除tx.Remove(key)// 事务回滚tx.Rollback()

3. Batch Operation

// 批量操作需要使用事务来实现tx := db.Begin()// 批量写入for i := 0; i < 100; i++ {    key   := []byte("k_" + strconv.Itoa(i))    value := []byte("v_" + strconv.Itoa(i))    tx.Set(key, value)}tx.Commit()// 批量删除for i := 0; i < 100; i++ {    key   := []byte("k_" + strconv.Itoa(i))    tx.Remove(key)}tx.Commit()

4. Multi-table operation

// 创建user表name    := "user"tu, err := db.Table(name)if err != nil {    fmt.Println(err)}// user表写入数据tu.Set([]byte("user_0"), []byte("name_0"))// user表查询数据fmt.Println(tu.Get([]byte("user_0")))// user表删除数据tu.Remove([]byte("user_0"))// 通过db对象操作user表写入数据db.SetTo([]byte("user_1"), []byte("name_1"), name)// 通过db对象操作user表查询数据fmt.Println(db.GetFrom([]byte("user_1"), name))// 通过db对象操作user表删除数据db.RemoveFrom([]byte("user_1"), name)// 手动关闭表,释放表资源// 一般不用手动关闭,在数据库关闭时会自动关闭所有的表tu.Close()

5. Multi-Table transaction

 //two sheets name1: = "user1" name2: = "user2"//Create transaction object tx: = db. Begin ()//Transaction Operation user table writes data tx. Setto ([]byte ("user_1"), []byte ("Name_1"), Name1) Tx. Setto ([]byte ("user_2"), []byte ("name_2"), name2)//Transaction Operations User table querying data fmt. Println ("TX get1:", TX.) Getfrom ([]byte ("user_1"), name1)) fmt. Println ("TX get2:", TX.) Getfrom ([]byte ("user_2"), name2)) Tx.commit () fmt. Println ("DB Get1:", db.) Getfrom ([]byte ("user_1"), name1)) fmt. Println ("DB Get2:", db.) Getfrom ([]byte ("user_2"), name2))//Transaction Operations User table deletes data tx. Removefrom ([]byte ("user_1"), Name1) Tx. Removefrom ([]byte ("user_2"), name2) fmt. Println ("TX removed1:", TX.) Getfrom ([]byte ("user_1"), name1)) fmt. Println ("TX removed2:", TX.) Getfrom ([]byte ("user_2"), name2))//The delete operation will be rolled back to TX. Rollback ()//re-query FMT. Println ("TX get1:", TX.) Getfrom ([]byte ("user_1"), name1)) fmt. Println ("TX get2:", TX.) Getfrom ([]byte ("user_2"), name2)) fmt. Println ("DB Get1:", db.) Getfrom ([]byte ("user_1"), name1)) fmt. Println ("DB Get2:", db.) Getfrom ([]byte ("user_2"), name2))  

6. Random traversal

// ======默认default表的遍历=====// 随机获取10条数据fmt.Println(db.Items(10))// 获取所有的键值对数据fmt.Println(db.Items(-1))// 获取所有的键键名fmt.Println(db.Keys(-1))// 获取所有的键键值fmt.Println(db.Values(-1))// ======指定表的遍历=====t1, err := db.Table("user1")if err != nil {    fmt.Println(err)}t2, err := db.Table("user2")if err != nil {    fmt.Println(err)}for i := 0; i < 10; i++ {    key   := []byte("k_" + strconv.Itoa(i))    value := []byte("v_" + strconv.Itoa(i))    t1.Set(key, value)}for i := 10; i < 20; i++ {    key   := []byte("k_" + strconv.Itoa(i))    value := []byte("v_" + strconv.Itoa(i))    t2.Set(key, value)}fmt.Println(t1.Items(-1))fmt.Println(t2.Items(-1))

Performance

 john@workstation:~/gkvdb/gkvdb_test/benchmark_test$ go Test *.go-bench= ". *" GOOS: Linuxgoarch:amd64benchmarkset-8 300000 5130 ns/opbenchmarkget-8 1000000 9628 ns/op BenchmarkRemove-8 500000 4053 ns/oppassok command-line-arguments 13.964s  
Related Article

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.