Skip to content

Commit

Permalink
bigquery: cache schema inference
Browse files Browse the repository at this point in the history
This will speed up struct uploading.

Change-Id: I73266ebd1003ab0536b89270d5e65a2aa0e694a6
Reviewed-on: https://code-review.googlesource.com/9832
Reviewed-by: Sarah Adams <shadams@google.com>
  • Loading branch information
jba committed Dec 10, 2016
1 parent dd37f36 commit c116c79
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
16 changes: 15 additions & 1 deletion bigquery/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"reflect"

"cloud.google.com/go/internal/atomiccache"

bq "google.golang.org/api/bigquery/v2"
)

Expand Down Expand Up @@ -141,9 +143,21 @@ func InferSchema(st interface{}) (Schema, error) {
return inferSchemaReflect(reflect.TypeOf(st))
}

var schemaCache atomiccache.Cache

type cacheVal struct {
schema Schema
err error
}

func inferSchemaReflect(t reflect.Type) (Schema, error) {
return inferStruct(t, map[reflect.Type]bool{})
cv := schemaCache.Get(t, func() interface{} {
s, err := inferStruct(t, map[reflect.Type]bool{})
return cacheVal{s, err}
}).(cacheVal)
return cv.schema, cv.err
}

func inferStruct(t reflect.Type, seen map[reflect.Type]bool) (Schema, error) {
if seen[t] {
return nil, fmt.Errorf("bigquery: schema inference for recursive type %s", t)
Expand Down
1 change: 0 additions & 1 deletion bigquery/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func toValueSaver(x interface{}) (ValueSaver, bool, error) {
if v.Kind() != reflect.Struct {
return nil, false, nil
}
// TODO(jba): cache schema inference to speed this up.
schema, err := inferSchemaReflect(v.Type())
if err != nil {
return nil, false, err
Expand Down

0 comments on commit c116c79

Please sign in to comment.