bump(github.com/googleapis/gnostic):0c5108395e2de

Pick up performance improvements to OpenAPI serialization.
This commit is contained in:
Clayton Coleman
2017-07-31 01:24:19 -04:00
parent d8205661b7
commit 7a458730d7
18 changed files with 2099 additions and 309 deletions

View File

@@ -25,29 +25,30 @@ import (
"strings"
)
var file_cache map[string][]byte
var info_cache map[string]interface{}
var fileCache map[string][]byte
var infoCache map[string]interface{}
var count int64
var VERBOSE_READER = false
var verboseReader = false
func initializeFileCache() {
if file_cache == nil {
file_cache = make(map[string][]byte, 0)
if fileCache == nil {
fileCache = make(map[string][]byte, 0)
}
}
func initializeInfoCache() {
if info_cache == nil {
info_cache = make(map[string]interface{}, 0)
if infoCache == nil {
infoCache = make(map[string]interface{}, 0)
}
}
// FetchFile gets a specified file from the local filesystem or a remote location.
func FetchFile(fileurl string) ([]byte, error) {
initializeFileCache()
bytes, ok := file_cache[fileurl]
bytes, ok := fileCache[fileurl]
if ok {
if VERBOSE_READER {
if verboseReader {
log.Printf("Cache hit %s", fileurl)
}
return bytes, nil
@@ -56,30 +57,17 @@ func FetchFile(fileurl string) ([]byte, error) {
response, err := http.Get(fileurl)
if err != nil {
return nil, err
} else {
defer response.Body.Close()
bytes, err := ioutil.ReadAll(response.Body)
if err == nil {
file_cache[fileurl] = bytes
}
return bytes, err
}
defer response.Body.Close()
bytes, err = ioutil.ReadAll(response.Body)
if err == nil {
fileCache[fileurl] = bytes
}
return bytes, err
}
// read a file and unmarshal it as a yaml.MapSlice
func ReadInfoForFile(filename string) (interface{}, error) {
initializeInfoCache()
info, ok := info_cache[filename]
if ok {
if VERBOSE_READER {
log.Printf("Cache hit info for file %s", filename)
}
return info, nil
}
if VERBOSE_READER {
log.Printf("Reading info for file %s", filename)
}
// ReadBytesForFile reads the bytes of a file.
func ReadBytesForFile(filename string) ([]byte, error) {
// is the filename a url?
fileurl, _ := url.Parse(filename)
if fileurl.Scheme != "" {
@@ -88,43 +76,51 @@ func ReadInfoForFile(filename string) (interface{}, error) {
if err != nil {
return nil, err
}
var info yaml.MapSlice
err = yaml.Unmarshal(bytes, &info)
if err != nil {
return nil, err
}
info_cache[filename] = info
return info, nil
} else {
// no, it's a local filename
bytes, err := ioutil.ReadFile(filename)
if err != nil {
log.Printf("File error: %v\n", err)
return nil, err
}
var info yaml.MapSlice
err = yaml.Unmarshal(bytes, &info)
if err != nil {
return nil, err
}
info_cache[filename] = info
return info, nil
return bytes, nil
}
// no, it's a local filename
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return bytes, nil
}
// read a file and return the fragment needed to resolve a $ref
// ReadInfoFromBytes unmarshals a file as a yaml.MapSlice.
func ReadInfoFromBytes(filename string, bytes []byte) (interface{}, error) {
initializeInfoCache()
cachedInfo, ok := infoCache[filename]
if ok {
if verboseReader {
log.Printf("Cache hit info for file %s", filename)
}
return cachedInfo, nil
}
if verboseReader {
log.Printf("Reading info for file %s", filename)
}
var info yaml.MapSlice
err := yaml.Unmarshal(bytes, &info)
if err != nil {
return nil, err
}
infoCache[filename] = info
return info, nil
}
// ReadInfoForRef reads a file and return the fragment needed to resolve a $ref.
func ReadInfoForRef(basefile string, ref string) (interface{}, error) {
initializeInfoCache()
{
info, ok := info_cache[ref]
info, ok := infoCache[ref]
if ok {
if VERBOSE_READER {
if verboseReader {
log.Printf("Cache hit for ref %s#%s", basefile, ref)
}
return info, nil
}
}
if VERBOSE_READER {
if verboseReader {
log.Printf("Reading info for ref %s#%s", basefile, ref)
}
count = count + 1
@@ -136,7 +132,11 @@ func ReadInfoForRef(basefile string, ref string) (interface{}, error) {
} else {
filename = basefile
}
info, err := ReadInfoForFile(filename)
bytes, err := ReadBytesForFile(filename)
if err != nil {
return nil, err
}
info, err := ReadInfoFromBytes(filename, bytes)
if err != nil {
log.Printf("File error: %v\n", err)
} else {
@@ -154,7 +154,7 @@ func ReadInfoForRef(basefile string, ref string) (interface{}, error) {
}
}
if !found {
info_cache[ref] = nil
infoCache[ref] = nil
return nil, NewError(nil, fmt.Sprintf("could not resolve %s", ref))
}
}
@@ -162,6 +162,6 @@ func ReadInfoForRef(basefile string, ref string) (interface{}, error) {
}
}
}
info_cache[ref] = info
infoCache[ref] = info
return info, nil
}