Added field options to pilosa import

This commit is contained in:
Yuce Tekol 2018-09-06 17:04:07 +03:00
parent 0ac1e25a07
commit 81f126dd34
No known key found for this signature in database
GPG key ID: CB59E46D2FB90573
3 changed files with 34 additions and 17 deletions

View file

@ -51,8 +51,13 @@ omitted. If it is present then its format should be YYYY-MM-DDTHH:MM.
flags.StringVarP(&Importer.Host, "host", "", "localhost:10101", "host:port of Pilosa.")
flags.StringVarP(&Importer.Index, "index", "i", "", "Pilosa index to import into.")
flags.StringVarP(&Importer.Field, "field", "f", "", "Field to import into.")
flags.BoolVar(&Importer.IndexKeys, "index-keys", false, "use keys=true when creating an index")
flags.BoolVar(&Importer.FieldKeys, "field-keys", false, "use keys=true when creating a field")
flags.BoolVar(&Importer.IndexOptions.Keys, "index-keys", false, "specify keys=true when creating an index")
flags.BoolVar(&Importer.FieldOptions.Keys, "field-keys", false, "specify keys=true when creating a field")
flags.Int64Var(&Importer.FieldOptions.Min, "field-min", 0, "specify the minimum for an int field on creation")
flags.Int64Var(&Importer.FieldOptions.Max, "field-max", 0, "specify the maximum for an int field on creation")
flags.StringVar(&Importer.FieldOptions.CacheType, "field-cache-type", "", "specify the cache type for a set field on creation")
flags.Uint32Var(&Importer.FieldOptions.CacheSize, "field-cache-size", 0, "specify the cache size for a set field on creation")
flags.Var(&Importer.FieldOptions.TimeQuantum, "field-time-quantum", "specify the time quantum for a time field on creation")
flags.IntVarP(&Importer.BufferSize, "buffer-size", "s", 10000000, "Number of bits to buffer/sort before importing.")
flags.BoolVarP(&Importer.Sort, "sort", "", false, "Enables sorting before import.")
flags.BoolVarP(&Importer.CreateSchema, "create", "e", false, "Create the schema if it does not exist before import.")

View file

@ -18,6 +18,8 @@ import (
"strings"
"testing"
"github.com/pilosa/pilosa"
"github.com/pilosa/pilosa/cmd"
)
@ -47,6 +49,20 @@ field = "f1"
return v.Error()
},
},
{
args: []string{"import", "--index", "i1", "--field", "f1", "--field-keys", "--field-max", "100"},
env: map[string]string{},
validation: func() error {
v := validator{}
v.Check(cmd.Importer.Index, "i1")
v.Check(cmd.Importer.Field, "f1")
v.Check(cmd.Importer.FieldOptions, pilosa.FieldOptions{
Keys: true,
Max: 100,
})
return v.Error()
},
},
}
executeDry(t, tests)
}

View file

@ -41,20 +41,14 @@ type ImportCommand struct { // nolint: maligned
Field string `json:"field"`
// Options for the index to be created if it doesn't exist
indexOptions pilosa.IndexOptions
IndexOptions pilosa.IndexOptions
// Options for the field to be created if it doesn't exist
fieldOptions pilosa.FieldOptions
FieldOptions pilosa.FieldOptions
// CreateSchema ensures the schema exists before import
CreateSchema bool
// IndexKeys makes the import command use keys=true when creating an index
IndexKeys bool `json:"indexKeys"`
// FieldKeys makes the import command use keys=true when creating a field
FieldKeys bool `json:"fieldKeys"`
// Filenames to import from.
Paths []string `json:"paths"`
@ -102,11 +96,13 @@ func (cmd *ImportCommand) Run(ctx context.Context) error {
cmd.client = client
if cmd.CreateSchema {
cmd.indexOptions = pilosa.IndexOptions{
Keys: cmd.IndexKeys,
}
cmd.fieldOptions = pilosa.FieldOptions{
Keys: cmd.FieldKeys,
// set the correct type for the field
if cmd.FieldOptions.TimeQuantum != "" {
cmd.FieldOptions.Type = "time"
} else if cmd.FieldOptions.Min != 0 || cmd.FieldOptions.Max != 0 {
cmd.FieldOptions.Type = "int"
} else {
cmd.FieldOptions.Type = "set"
}
err := cmd.ensureSchema(ctx)
if err != nil {
@ -148,11 +144,11 @@ func (cmd *ImportCommand) Run(ctx context.Context) error {
}
func (cmd *ImportCommand) ensureSchema(ctx context.Context) error {
err := cmd.client.EnsureIndex(ctx, cmd.Index, cmd.indexOptions)
err := cmd.client.EnsureIndex(ctx, cmd.Index, cmd.IndexOptions)
if err != nil {
return fmt.Errorf("Error Creating Index: %s", err)
}
err = cmd.client.EnsureFieldWithOptions(ctx, cmd.Index, cmd.Field, cmd.fieldOptions)
err = cmd.client.EnsureFieldWithOptions(ctx, cmd.Index, cmd.Field, cmd.FieldOptions)
if err != nil {
return fmt.Errorf("Error Creating Field: %s", err)
}