move checks to api.go

This commit is contained in:
Samir Patel 2021-11-04 15:52:37 -05:00
parent c0e201d638
commit 67233d5720
2 changed files with 9 additions and 11 deletions

9
api.go
View file

@ -1688,6 +1688,15 @@ func (api *API) ImportValueWithTx(ctx context.Context, qcx *Qcx, req *ImportValu
return errors.Wrap(err, "validating api method")
}
numCols := len(req.ColumnIDs) + len(req.ColumnKeys)
numVals := len(req.Values) + len(req.FloatValues) + len(req.TimestampValues) + len(req.StringValues)
if numCols != numVals {
return errors.New(fmt.Sprintf("number of columns (%v) and number of values (%v) do not match", numCols, numVals))
}
if numCols == 0 {
return nil
}
idx, field, err := api.indexField(req.Index, req.Field, req.Shard)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("getting index '%v' and field '%v'; shard=%v", req.Index, req.Field, req.Shard))

View file

@ -2721,12 +2721,6 @@ func (h *Handler) handlePostImport(w http.ResponseWriter, r *http.Request) {
qcx := h.api.Txf().NewQcx()
defer qcx.Abort()
if isImportRequestEmpty(req) {
if len(req.ColumnIDs) > 0 || len(req.ColumnKeys) > 0 {
http.Error(w, "columns provided but no values", http.StatusBadRequest)
}
return
}
if err := h.api.ImportValue(r.Context(), qcx, req, opts...); err != nil {
switch errors.Cause(err) {
case pilosa.ErrClusterDoesNotOwnShard, pilosa.ErrPreconditionFailed:
@ -3469,8 +3463,3 @@ func (h *Handler) handlePostRestore(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK")) //nolint:errcheck
}
func isImportRequestEmpty(req *pilosa.ImportValueRequest) bool {
totalData := len(req.Values) + len(req.FloatValues) + len(req.TimestampValues) + len(req.StringValues)
return totalData == 0
}