Skip to content

Commit

Permalink
Create index table from local table client for boltdb (#2441)
Browse files Browse the repository at this point in the history
Signed-off-by: Aditya C S <[email protected]>
  • Loading branch information
adityacs authored Apr 30, 2020
1 parent ef28d34 commit 65d3b34
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
56 changes: 56 additions & 0 deletions pkg/chunk/local/boltdb_index_client_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package local

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"go.etcd.io/bbolt"

"github.com/cortexproject/cortex/pkg/chunk"
)

var (
Expand Down Expand Up @@ -114,3 +118,55 @@ func TestBoltDB_GetDB(t *testing.T) {
_, err = boltdbIndexClient.GetDB(testDb1, DBOperationRead)
require.NoError(t, err)
}

func Test_CreateTable_BoltdbRW(t *testing.T) {
tableName := "test"
dirname, err := ioutil.TempDir(os.TempDir(), "boltdb")
require.NoError(t, err)

indexClient, err := NewBoltDBIndexClient(BoltDBConfig{
Directory: dirname,
})
require.NoError(t, err)

tableClient, err := NewTableClient(dirname)
require.NoError(t, err)

err = tableClient.CreateTable(context.Background(), chunk.TableDesc{
Name: tableName,
})
require.NoError(t, err)

batch := indexClient.NewWriteBatch()
batch.Add(tableName, fmt.Sprintf("hash%s", "test"), []byte(fmt.Sprintf("range%s", "value")), nil)

err = indexClient.BatchWrite(context.Background(), batch)
require.NoError(t, err)

// try to create the same file which is already existing
err = tableClient.CreateTable(context.Background(), chunk.TableDesc{
Name: tableName,
})
require.NoError(t, err)

// make sure file content is not modified
entry := chunk.IndexQuery{
TableName: tableName,
HashValue: fmt.Sprintf("hash%s", "test"),
}
var have []chunk.IndexEntry
err = indexClient.query(context.Background(), entry, func(read chunk.ReadBatch) bool {
iter := read.Iterator()
for iter.Next() {
have = append(have, chunk.IndexEntry{
RangeValue: iter.RangeValue(),
})
}
return true
})
require.NoError(t, err)
require.Equal(t, []chunk.IndexEntry{
{RangeValue: []byte(fmt.Sprintf("range%s", "value"))},
}, have)

}
7 changes: 6 additions & 1 deletion pkg/chunk/local/boltdb_table_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ func (c *TableClient) ListTables(ctx context.Context) ([]string, error) {
}

func (c *TableClient) CreateTable(ctx context.Context, desc chunk.TableDesc) error {
return nil
file, err := os.OpenFile(filepath.Join(c.directory, desc.Name), os.O_CREATE|os.O_RDONLY, 0666)
if err != nil {
return err
}

return file.Close()
}

func (c *TableClient) DeleteTable(ctx context.Context, name string) error {
Expand Down
4 changes: 4 additions & 0 deletions pkg/chunk/local/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (f *fixture) Clients() (
Prefix: "chunks",
Period: 10 * time.Minute,
},
IndexTables: chunk.PeriodicTableConfig{
Prefix: "index",
Period: 10 * time.Minute,
},
}},
}

Expand Down

0 comments on commit 65d3b34

Please sign in to comment.