aergoio / litetree

SQLite with Branches
MIT License
1.62k stars 35 forks source link

Need a go wrapper #3

Open yuanotes opened 6 years ago

yuanotes commented 6 years ago

Since aergo blockchain kernel is written in go, is there a golang wrapper for litetree?

kroggen commented 6 years ago

Hi Yuan!

LiteTree was just made public and it was not yet included in the Aergo blockchain.

Normally we can use common SQLite wrappers, just replacing the libsqlite3 library. Only in some cases we need to rebuild the whole wrapper.

For Go we can use the go-sqlite3 wrapper but we need to download and build it with commands to link it to the installed libsqlite3:

Linux

go build --tags "libsqlite3 linux"

MacOSX

go build --tags "libsqlite3 darwin"

More info here

But I confess I have not done it yet. I will let this open so we can post updates. Keep an eye on it.

yuanotes commented 6 years ago

Thanks. I have tried to build litetree with mattn/go-sqlite3 but failed. AFAIK, mattn/go-sqlite3 has hard-coded the journal mode.

kroggen commented 6 years ago

Could you inform the OS you tried, the commands/steps used, and the error message?

yuanotes commented 6 years ago

My OS is MacOS 10.13.6 and go version is go1.10.3 darwin/amd64

steps to reproduce this:

  1. get go-sqlite3:

    go get github.com/mattn/go-sqlite3
  2. build litetree and copy libs to correct path:

    
    # go-sqlite3 find libsqlite3 in the path blow
    mkdir -p /usr/local/opt/sqlite/lib 

build litetree and copy them to the right place

I'm quite sure that litetree is built successfully

and I write some simple go code to bind it and the branch works!

cd /litetree make cp ./libsqlite3.* /usr/local/opt/sqlite/lib

3. build `go-sqlite3`
```shell
go build --tags "libsqlite3 darwin" github.com/mattn/go-sqlite3
  1. test with go code
    
    package main

import ( "database/sql" "fmt" "log" "os"

_ "github.com/mattn/go-sqlite3"

)

func main() { os.Remove("./foo.db") db, err := sql.Open("sqlite3", "file:./foo.db?branches=on") if err != nil { log.Fatal(err) } defer db.Close()

var rows *sql.Rows
rows, err = db.Query("PRAGMA journal_mode;")
if err != nil {
    log.Fatal(err)
}
for rows.Next() {
    var mode string
    err = rows.Scan(&mode)
    fmt.Println("result: ", mode, err)
}
rows.Close()

var values interface{}
rows, err = db.Query("PRAGMA branches;")
if err != nil {
    log.Fatal(err)
}
for rows.Next() {
    err = rows.Scan(&values)
    fmt.Println("result: ", values, err)
}
rows.Close()

}


5. the output we got from step4 is
```shell
result:  delete <nil>

Notice: we got nothing from PRAGMA branches;

It's obvious that by this way go-sqlite3 was built with normal version sqlite3, instead of litetree.

kroggen commented 6 years ago

Hi Yuan!

Thank you for your collaboration!

I was having the same problem. After 2 hours trying I discovered a workaround made of 2 steps:

  1. Modify the file sqlite3_libsqlite3.go file from this:
#cgo darwin LDFLAGS: -L/usr/local/opt/sqlite/lib -lsqlite3

To this:

#cgo darwin LDFLAGS: -L/usr/local/lib/litetree -lsqlite3
  1. In the path of your go project use go build --tags libsqlite3 instead of just go build.
yuanotes commented 6 years ago

@kroggen oh, thanks! It works for me. The key point here is to run my own go project with argument --tags libsqlite3. Sorry for my poor undestanding of go build tools

kroggen commented 6 years ago

You're welcome!

I am not a Go developer as well. Maybe there are other ways to do this. The site says about "building the library" and installing it using go install. But I have not tried them yet.