Skip to content

Commit

Permalink
Some more improvements (#136)
Browse files Browse the repository at this point in the history
* Move to badger v4

* adjust variable names

* variable names should not start with the package name

* ioutil is deprecated

* does it run with go 1.20?

* fmt not available?

* use correct replacement

* last few fixes

* old protobuf repo is still used by badger v4

* don't run tests recursively

* the recursive tests don't throw an error when ran locally
tests should be run recursively (undo test commit)

* recreate go.mod and move to uuid v5

* tests on base

* go get -u .

* switch to uuid v5

* adjust variable names

* fix: name starts with package name

* ioutils is deprecated

* move to badger v4

* rename workflow to linux.yml and add build

* add workflow files for macos and windows

* Removed windows and macos workflows since TestPagedQueryUsingIndex is currently failing for them

* use newer actions

* remove v2-improvements-base
  • Loading branch information
nbdy authored Sep 27, 2023
1 parent 2a43ec5 commit aa688ad
Show file tree
Hide file tree
Showing 17 changed files with 1,057 additions and 101 deletions.
21 changes: 13 additions & 8 deletions .github/workflows/ test.yml → .github/workflows/linux.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
name: Unit test Go code
name: Linux Build & Unit tests

on:
push:
tags:
- v*
branches: [ "v2" ]
pull_request:
types: [ opened, synchronize, reopened, ready_for_review ]

jobs:
test:
strategy:
matrix:
go-version: [ 1.21.x ]
go-version: [ 1.20.x ]
os: [ ubuntu-latest ]

name: Checking
runs-on: ${{ matrix.os }}
steps:
- name: Setup Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}

- name: Check out code
uses: actions/checkout@v2
- name: run tests
run: go test ./...
uses: actions/checkout@v3

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ To store documents inside collections, you have to open a Clover database using
```go
import (
"log"
"github.com/dgraph-io/badger/v3"
"github.com/dgraph-io/badger/v4"
c "github.com/ostafen/clover"
badgerstore "github.com/ostafen/store/badger"
)
Expand Down
20 changes: 10 additions & 10 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"sync/atomic"

"github.com/gofrs/uuid"
"github.com/gofrs/uuid/v5"
d "github.com/ostafen/clover/v2/document"
"github.com/ostafen/clover/v2/index"
"github.com/ostafen/clover/v2/internal"
Expand Down Expand Up @@ -38,7 +38,7 @@ type DB struct {

type collectionMetadata struct {
Size int
Indexes []index.IndexInfo
Indexes []index.Info
}

// CreateCollection creates a new empty collection with the given name.
Expand Down Expand Up @@ -267,11 +267,11 @@ func (db *DB) InsertOne(collectionName string, doc *d.Document) (string, error)

// Open opens a new clover database on the supplied path. If such a folder doesn't exist, it is automatically created.
func Open(dir string) (*DB, error) {
store, err := bbolt.Open(dir)
dataStore, err := bbolt.Open(dir)
if err != nil {
return nil, err
}
return OpenWithStore(store)
return OpenWithStore(dataStore)
}

// OpenWithStore opens a new clover database using the provided store.
Expand Down Expand Up @@ -706,10 +706,10 @@ func iteratePrefix(prefix []byte, tx store.Tx, itemConsumer func(item store.Item

// CreateIndex creates an index for the specified for the specified (index, collection) pair.
func (db *DB) CreateIndex(collection, field string) error {
return db.createIndex(collection, field, index.IndexSingleField)
return db.createIndex(collection, field, index.SingleField)
}

func (db *DB) createIndex(collection, field string, indexType index.IndexType) error {
func (db *DB) createIndex(collection, field string, indexType index.Type) error {
tx, err := db.store.Begin(true)
if err != nil {
return err
Expand All @@ -728,9 +728,9 @@ func (db *DB) createIndex(collection, field string, indexType index.IndexType) e
}

if meta.Indexes == nil {
meta.Indexes = make([]index.IndexInfo, 0)
meta.Indexes = make([]index.Info, 0)
}
meta.Indexes = append(meta.Indexes, index.IndexInfo{Field: field, Type: indexType})
meta.Indexes = append(meta.Indexes, index.Info{Field: field, Type: indexType})

idx := index.CreateIndex(collection, field, indexType, tx)

Expand Down Expand Up @@ -815,7 +815,7 @@ func (db *DB) DropIndex(collection, field string) error {
}

// ListIndexes returns a list containing the names of all the indexes for the specified collection.
func (db *DB) ListIndexes(collection string) ([]index.IndexInfo, error) {
func (db *DB) ListIndexes(collection string) ([]index.Info, error) {
txn, err := db.store.Begin(false)
if err != nil {
return nil, err
Expand All @@ -825,7 +825,7 @@ func (db *DB) ListIndexes(collection string) ([]index.IndexInfo, error) {
return db.listIndexes(collection, txn)
}

func (db *DB) listIndexes(collection string, tx store.Tx) ([]index.IndexInfo, error) {
func (db *DB) listIndexes(collection string, tx store.Tx) ([]index.Info, error) {
meta, err := db.getCollectionMeta(collection, tx)
return meta.Indexes, err
}
Expand Down
35 changes: 17 additions & 18 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -64,7 +63,7 @@ func getDBFactories() []dbFactory {

func runCloverTest(t *testing.T, test func(t *testing.T, db *c.DB)) {
for _, createDB := range getDBFactories() {
dir, err := ioutil.TempDir("", "clover-test")
dir, err := os.MkdirTemp("", "clover-test")
require.NoError(t, err)

db, err := createDB(dir)
Expand All @@ -79,20 +78,20 @@ func runCloverTest(t *testing.T, test func(t *testing.T, db *c.DB)) {

func TestErrCollectionNotExist(t *testing.T) {
runCloverTest(t, func(t *testing.T, db *c.DB) {
q := q.NewQuery("myCollection")
_, err := db.Count(q)
query := q.NewQuery("myCollection")
_, err := db.Count(query)
require.Equal(t, c.ErrCollectionNotExist, err)

_, err = db.FindById("myCollection", "objectId")
require.Equal(t, c.ErrCollectionNotExist, err)

_, err = db.FindAll(q)
_, err = db.FindAll(query)
require.Equal(t, c.ErrCollectionNotExist, err)

err = db.Update(q, nil)
err = db.Update(query, nil)
require.Equal(t, c.ErrCollectionNotExist, err)

err = db.Delete(q)
err = db.Delete(query)
require.Equal(t, c.ErrCollectionNotExist, err)

err = db.DeleteById("myCollection", "objectId")
Expand Down Expand Up @@ -250,13 +249,13 @@ func TestInsertAndGet(t *testing.T) {
require.NoError(t, err)
require.Equal(t, nInserts, n)

q := q.NewQuery("myCollection").MatchFunc(func(doc *d.Document) bool {
query := q.NewQuery("myCollection").MatchFunc(func(doc *d.Document) bool {
require.True(t, doc.Has("myField"))

v, _ := doc.Get("myField").(int64)
return int(v)%2 == 0
})
n, err = db.Count(q)
n, err = db.Count(query)

require.NoError(t, err)

Expand All @@ -267,7 +266,7 @@ func TestInsertAndGet(t *testing.T) {
func loadFromJson(db *c.DB, filename string, model interface{}) error {
var objects []interface{}

data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
return err
}
Expand Down Expand Up @@ -407,7 +406,7 @@ func TestInsertAndDelete(t *testing.T) {
}

func TestOpenExisting(t *testing.T) {
dir, err := ioutil.TempDir("", "clover-test")
dir, err := os.MkdirTemp("", "clover-test")
defer os.RemoveAll(dir)
require.NoError(t, err)

Expand All @@ -430,7 +429,7 @@ func TestOpenExisting(t *testing.T) {
}

func TestReloadIndex(t *testing.T) {
dir, err := ioutil.TempDir("", "clover-test")
dir, err := os.MkdirTemp("", "clover-test")
defer os.RemoveAll(dir)
require.NoError(t, err)

Expand Down Expand Up @@ -1193,7 +1192,7 @@ func TestExportAndImportCollection(t *testing.T) {
runCloverTest(t, func(t *testing.T, db *c.DB) {
require.NoError(t, loadFromJson(db, todosPath, &TodoModel{}))

exportPath, err := ioutil.TempDir("", "export-dir")
exportPath, err := os.MkdirTemp("", "export-dir")
require.NoError(t, err)
defer os.RemoveAll(exportPath)

Expand Down Expand Up @@ -1338,7 +1337,7 @@ func TestCreateIndex(t *testing.T) {
indexes, err := db.ListIndexes("collection")
require.NoError(t, err)

require.Equal(t, []index.IndexInfo{{Field: "field", Type: index.IndexSingleField}}, indexes)
require.Equal(t, []index.Info{{Field: "field", Type: index.SingleField}}, indexes)
})
}

Expand Down Expand Up @@ -1475,13 +1474,13 @@ func TestIndexQueryWithSort(t *testing.T) {
require.NoError(t, loadFromJson(db, airlinesPath, nil))

criteria := q.Field("Statistics.Flights.Cancelled").Gt(100).And(q.Field("Statistics.Flights.Cancelled").Lt(200))
q := q.NewQuery("airlines").Where(criteria).Sort(q.SortOption{Field: "Statistics.Flights.Cancelled", Direction: -1})
docs, err := db.FindAll(q)
query := q.NewQuery("airlines").Where(criteria).Sort(q.SortOption{Field: "Statistics.Flights.Cancelled", Direction: -1})
docs, err := db.FindAll(query)
require.NoError(t, err)

require.NoError(t, db.CreateIndex("airlines", "Statistics.Flights.Cancelled"))

indexDocs, err := db.FindAll(q)
indexDocs, err := db.FindAll(query)
require.NoError(t, err)

require.Equal(t, len(docs), len(indexDocs))
Expand Down Expand Up @@ -1601,7 +1600,7 @@ func TestListIndexes(t *testing.T) {

indexes, err = db.ListIndexes("test")
require.NoError(t, err)
require.Equal(t, []index.IndexInfo{{Field: "index", Type: index.IndexSingleField}}, indexes)
require.Equal(t, []index.Info{{Field: "index", Type: index.SingleField}}, indexes)

require.NoError(t, db.DropIndex("test", "index"))

Expand Down
2 changes: 1 addition & 1 deletion document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"time"

"github.com/gofrs/uuid"
"github.com/gofrs/uuid/v5"
"github.com/ostafen/clover/v2/internal"
"github.com/ostafen/clover/v2/util"
)
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-store/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"log"

"github.com/dgraph-io/badger/v3"
"github.com/dgraph-io/badger/v4"
c "github.com/ostafen/clover/v2"
badgerstore "github.com/ostafen/clover/v2/store/badger"
)
Expand Down
9 changes: 3 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@ module github.com/ostafen/clover/v2
go 1.13

require (
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/brianvoe/gofakeit/v6 v6.23.2
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgraph-io/badger/v3 v3.2103.5
github.com/dgraph-io/badger/v4 v4.2.0
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gofrs/uuid v4.4.0+incompatible
github.com/gofrs/uuid/v5 v5.0.0
github.com/golang/glog v1.1.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v23.5.26+incompatible // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/orderedcode v0.0.1
github.com/klauspost/compress v1.17.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/stretchr/testify v1.8.4
github.com/vmihailenco/msgpack/v5 v5.3.5
go.etcd.io/bbolt v1.3.7
go.opencensus.io v0.24.0 // indirect
golang.org/x/net v0.15.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
Loading

0 comments on commit aa688ad

Please sign in to comment.