youbbs
youbbs
1195 0 0

GitDB,一个基于 Git 的分布式可嵌入数据库

基于 git 的分布式嵌入式数据库,GitDB 只是一个库,需要调用 git 来操作远程数据库。类似下面的操作:

  • Git pull 拉远程数据
  • Git push 提交本地数据到远程

但这是由程序去做,程序 API 也比较简单

db, err := gitdb.Open(cfg)

db.Insert(account)
gitdb.Id(account)
db.Get("Accounts/202003/0123456789", &account)
db.Fetch("Accounts")
db.Fetch("Accounts", "b0", "b1")
db.Delete("Accounts/202003/0123456789")

searchParam := &db.SearchParam{Index: "AccountType", Value: "Savings"}
records, err := dbconn.Search("Accounts", []*db.SearchParam{searchParam}, gitdb.SearchEquals)

tx := db.StartTransaction("AccountUpgrade")
tx.AddOperation(accountUpgradeFuncOne)
tx.AddOperation(accountUpgradeFuncTwo)
tx.AddOperation(accountUpgradeFuncThree)
terr := tx.Commit()

使用

package main

import (
  "fmt"
  "log"
  "github.com/gogitdb/gitdb/v2"
)

func main(){
  cfg := gitdb.NewConfig("/tmp/data")
  db, err := gitdb.Open(cfg)
  if err != nil {
    log.Fatal(err)
  }
  defer db.Close()

  records, err := db.Fetch("Accounts")
  if err != nil {
    log.Print(err)
    return
  }

  accounts := []*BankAccount{}
  for _, r := range records {
    b := &BankAccount{}
    r.Hydrate(b)
    accounts = append(accounts, b)
    log.Print(fmt.Sprintf("%s-%s", gitdb.ID(b), b.AccountNo))
  }
}

围观地址 https://github.com/gogitdb/gitdb

0

See Also

Nearby


Discussion

Login Topics