這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
簡介
項目開源地址:https://github.com/HouzuoGuo/tiedot
發起者留下了他的Twitter,貌似姓郭,是個美籍華人
項目簡介中,有關於對效能的描述,有人用此資料庫抓取了維基百科,儲存5900萬資料,共73G。
安裝
配置好Go環境後,運行
go get github.com/HouzuoGuo/tiedot
入門
使用有2種方式,使用HTTP做介面,適用任何語言;使用嵌入式,使用Go語言,這裡介紹使用Go語言,資料庫的嵌入模式,足以應付百萬請求/天了。
項目內建了示範代碼,通過下面的命令執行
./tiedot -mode=example
效能評估命令
./tiedot -mode=bench # 對40萬資料,進行增刪改查./tiedot -mode=bench2 # 還沒仔細看
項目中,example.go 檔案是Go語言的使用樣本
// It is very important to initialize random number generator seed!rand.Seed(time.Now().UTC().UnixNano())// Create and open database建立並開啟資料庫dir := "/tmp/MyDatabase"os.RemoveAll(dir)defer os.RemoveAll(dir)myDB, err := db.OpenDB(dir)if err != nil {panic(err)}// Create two collections Feeds and Votes建立2張表// "2" means collection data and indexes are divided into two halves, allowing concurrent access from two threadsif err := myDB.Create("Feeds", 2); err != nil {panic(err)}if err := myDB.Create("Votes", 2); err != nil {panic(err)}// What collections do I now have?查詢都有哪些表for name := range myDB.StrCol {fmt.Printf("I have a collection called %s\n", name)}// Rename collection "Votes" to "Points"把表"Votes"重新命名為"Points"if err := myDB.Rename("Votes", "Points"); err != nil {panic(err)}// Drop (delete) collection "Points"刪除表"Points"if err := myDB.Drop("Points"); err != nil {panic(err)}// Scrub (repair and compact) "Feeds"修複並壓縮表"Feeds"myDB.Scrub("Feeds")// ****************** Document Management ******************// Start using a collection使用表"Feeds"feeds := myDB.Use("Feeds")// Insert document (document must be map[string]interface{})插入資料docID, err := feeds.Insert(map[string]interface{}{"name": "Go 1.2 is released","url": "golang.org"})if err != nil {panic(err)}// Read document根據id查詢資料var readBack interface{}feeds.Read(docID, &readBack) // pass in document's physical IDfmt.Println(readBack)// Update document (document must be map[string]interface{})改資料err = feeds.Update(docID, map[string]interface{}{"name": "Go is very popular","url": "google.com"})if err != nil {panic(err)}// Delete document刪除資料feeds.Delete(docID)// Delete documentfeeds.Delete(123) // An ID which does not exist does no harm// ****************** Index Management ******************索引管理// Secondary indexes assist in many types of queries// Create index (path leads to document JSON attribute)建索引if err := feeds.Index([]string{"author", "name", "first_name"}); err != nil {panic(err)}if err := feeds.Index([]string{"Title"}); err != nil {panic(err)}if err := feeds.Index([]string{"Source"}); err != nil {panic(err)}// What indexes do I have on collection A?查詢有哪些索引for path := range feeds.SecIndexes {fmt.Printf("I have an index on path %s\n", path)}// Remove index刪索引if err := feeds.Unindex([]string{"author", "name", "first_name"}); err != nil {panic(err)}// ****************** Queries ******************查詢表// Let's prepare a number of docments for a startfeeds.Insert(map[string]interface{}{"Title": "New Go release", "Source": "golang.org", "Age": 3})feeds.Insert(map[string]interface{}{"Title": "Kitkat is here", "Source": "google.com", "Age": 2})feeds.Insert(map[string]interface{}{"Title": "Good Slackware", "Source": "slackware.com", "Age": 1})queryStr := `[{"eq": "New Go release", "in": ["Title"]}, {"eq": "slackware.com", "in": ["Source"]}]`var query interface{}json.Unmarshal([]byte(queryStr), &query)查詢條件queryResult := make(map[uint64]struct{}) // query result (document IDs) goes into map keys儲存查詢結果的變數if err := db.EvalQuery(query, feeds, &queryResult); err != nil {執行查詢panic(err)}// Query results are physical document IDs列印查詢結果for id := range queryResult {fmt.Printf("Query returned document ID %d\n", id)}// To use the document itself, simply read it backfor id := range queryResult {feeds.Read(id, &readBack)fmt.Printf("Query returned document %v\n", readBack)}// Gracefully close database關閉資料庫myDB.Close()