-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: segfault on Put or Delete for getTransaction error * fix: batching * refactor: cache queries * fix: use single connection for commit * fix: use shorter option name * docs: readme updates
- Loading branch information
Alan Shaw
authored
May 13, 2020
1 parent
5d3312a
commit 3edef88
Showing
5 changed files
with
306 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,61 @@ | ||
# SQL Datastore | ||
|
||
[](https://circleci.com/gh/ipfs/go-ds-sql) | ||
[](https://codecov.io/gh/ipfs/go-ds-sql) | ||
[](https://github.com/RichardLitt/standard-readme) | ||
[](https://godoc.org/github.com/ipfs/go-ds-sql) | ||
[](https://golang.org/) | ||
[](https://goreportcard.com/report/github.com/ipfs/go-ds-sql) | ||
|
||
An implementation of [the datastore interface](https://github.com/ipfs/go-datastore) | ||
that can be backed by any sql database. | ||
|
||
## Install | ||
|
||
```sh | ||
go get github.com/ipfs/go-ds-sql | ||
``` | ||
|
||
## Usage | ||
|
||
Ensure a database is created and a table exists with `key` and `data` columns. For example, in PostgreSQL you can create a table with the following structure (replacing `table_name` with the name of the table the datastore will use - by default this is `blocks`): | ||
|
||
```sql | ||
CREATE TABLE IF NOT EXISTS table_name (key TEXT NOT NULL UNIQUE, data BYTEA) | ||
``` | ||
|
||
It's recommended to create an index on the `key` column that is optimised for prefix scans. For example, in PostgreSQL you can create a `text_pattern_ops` index on the table: | ||
|
||
```sql | ||
CREATE INDEX IF NOT EXISTS table_name_key_text_pattern_ops_idx ON table_name (key text_pattern_ops) | ||
``` | ||
|
||
Import and use in your application: | ||
|
||
```go | ||
import ( | ||
"database/sql" | ||
"github.com/ipfs/go-ds-sql" | ||
pg "github.com/ipfs/go-ds-sql/postgres" | ||
) | ||
|
||
mydb, _ := sql.Open("yourdb", "yourdbparameters") | ||
|
||
ds := sqlds.NewDatastore(mydb) | ||
// Implement the Queries interface for your SQL impl. | ||
// ...or use the provided PostgreSQL queries | ||
queries := pg.NewQueries("blocks") | ||
|
||
ds := sqlds.NewDatastore(mydb, queries) | ||
``` | ||
|
||
## API | ||
|
||
[GoDoc Reference](https://godoc.org/github.com/ipfs/go-ds-sql) | ||
|
||
## Contribute | ||
|
||
Feel free to dive in! [Open an issue](https://github.com/ipfs/go-ds-sql/issues/new) or submit PRs. | ||
|
||
## License | ||
MIT | ||
|
||
[MIT](LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package sqlds | ||
|
||
import ( | ||
"context" | ||
|
||
ds "github.com/ipfs/go-datastore" | ||
) | ||
|
||
type op struct { | ||
delete bool | ||
value []byte | ||
} | ||
|
||
type batch struct { | ||
ds *Datastore | ||
ops map[ds.Key]op | ||
} | ||
|
||
// Batch creates a set of deferred updates to the database. | ||
// Since SQL does not support a true batch of updates, | ||
// operations are buffered and then executed sequentially | ||
// over a single connection when Commit is called. | ||
func (d *Datastore) Batch() (ds.Batch, error) { | ||
return &batch{ | ||
ds: d, | ||
ops: make(map[ds.Key]op), | ||
}, nil | ||
} | ||
|
||
func (bt *batch) Put(key ds.Key, val []byte) error { | ||
bt.ops[key] = op{value: val} | ||
return nil | ||
} | ||
|
||
func (bt *batch) Delete(key ds.Key) error { | ||
bt.ops[key] = op{delete: true} | ||
return nil | ||
} | ||
|
||
func (bt *batch) Commit() error { | ||
return bt.CommitContext(context.Background()) | ||
} | ||
|
||
func (bt *batch) CommitContext(ctx context.Context) error { | ||
conn, err := bt.ds.db.Conn(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
|
||
for k, op := range bt.ops { | ||
if op.delete { | ||
_, err = conn.ExecContext(ctx, bt.ds.queries.Delete(), k.String()) | ||
} else { | ||
_, err = conn.ExecContext(ctx, bt.ds.queries.Put(), k.String(), op.value) | ||
} | ||
if err != nil { | ||
break | ||
} | ||
} | ||
|
||
return err | ||
} | ||
|
||
var _ ds.Batching = (*Datastore)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.