tiedot 内存NoSQL数据库
最后更新于:2022-04-02 02:38:07
[TOC]
> [github](https://github.com/HouzuoGuo/tiedot)
## 简述
使用有2种方式,使用HTTP做接口,适用任何语言;使用嵌入式,使用Go语言,这里介绍使用Go语言,数据库的嵌入模式,足以应付百万请求/天了。
## 实例
### hello world
运行结果 ``` I have a collection called Feeds I have a collection called Votes Document 6562749276262285211 is map[name:Go 1.2 is released url:golang.org] Document 6562749276262285211 is {"name":"Go is very popular","url":"google.com"} The document was already deleted I have an index on path [Title] I have an index on path [Source] I have an index on path [author name first_name] Query returned document map[Age:3 Source:golang.org Title:New Go release] Query returned document map[Age:1 Source:slackware.com Title:Good Slackware] ```
';
main.go
``` package main import ( "encoding/json" "fmt" "os" "github.com/HouzuoGuo/tiedot/db" "github.com/HouzuoGuo/tiedot/dberr" ) func EmbeddedExample() { myDBDir := "MyDatabase" os.RemoveAll(myDBDir) defer os.RemoveAll(myDBDir) myDB, err := db.OpenDB(myDBDir) if err != nil { panic(err) } // 创建两个数据库 if err := myDB.Create("Feeds"); err != nil { panic(err) } if err := myDB.Create("Votes"); err != nil { panic(err) } // 查询有哪些数据库 for _, name := range myDB.AllCols() { fmt.Printf("I have a collection called %s\n", name) } // 重命名数据库 if err := myDB.Rename("Votes", "Points"); err != nil { panic(err) } // 删除数据库 if err := myDB.Drop("Points"); err != nil { panic(err) } // "Feeds"修复并压缩表"Feeds" if err := myDB.Scrub("Feeds"); err != nil { panic(err) } // use feeds feeds := myDB.Use("Feeds") // 添加,返回自己生成的 id, 此种格式:6562749276262285211 docID, err := feeds.Insert(map[string]interface{}{ "name": "Go 1.2 is released", "url": "golang.org"}) if err != nil { panic(err) } // 查询 readBack, err := feeds.Read(docID) if err != nil { panic(err) } fmt.Println("Document", docID, "is", readBack) // 更新 err = feeds.Update(docID, map[string]interface{}{ "name": "Go is very popular", "url": "google.com"}) if err != nil { panic(err) } // 处理所有文件(注意,文档顺序是不确定的) feeds.ForEachDoc(func(id int, docContent []byte) (willMoveOn bool) { fmt.Println("Document", id, "is", string(docContent)) return true // 转到下一个文档或 return false // 不要移动到下一个文件吗 }) // 删除文档 if err := feeds.Delete(docID); err != nil { panic(err) } // 处理错误类型 if err := feeds.Delete(docID); dberr.Type(err) == dberr.ErrorNoDoc { fmt.Println("The document was already deleted") } // 创建索引 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) } // 查看所有索引 for _, path := range feeds.AllIndexes() { fmt.Printf("I have an index on path %v\n", path) } // 移除索引 if err := feeds.Unindex([]string{"author", "name", "first_name"}); err != nil { panic(err) } feeds.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}) // 查询两条结果 var query interface{} json.Unmarshal([]byte(`[{"eq": "New Go release", "in": ["Title"]}, {"eq": "slackware.com", "in": ["Source"]}]`), &query) queryResult := make(map[int]struct{}) if err := db.EvalQuery(query, feeds, &queryResult); err != nil { panic(err) } // 遍历查询结果 for id := range queryResult { readBack, err := feeds.Read(id) if err != nil { panic(err) } fmt.Printf("Query returned document %v\n", readBack) } // 关闭数据库 if err := myDB.Close(); err != nil { panic(err) } } func main() { EmbeddedExample() } ```运行结果 ``` I have a collection called Feeds I have a collection called Votes Document 6562749276262285211 is map[name:Go 1.2 is released url:golang.org] Document 6562749276262285211 is {"name":"Go is very popular","url":"google.com"} The document was already deleted I have an index on path [Title] I have an index on path [Source] I have an index on path [author name first_name] Query returned document map[Age:3 Source:golang.org Title:New Go release] Query returned document map[Age:1 Source:slackware.com Title:Good Slackware] ```