rob05c commented on a change in pull request #4419: ORT Speed Improvements
URL: https://github.com/apache/trafficcontrol/pull/4419#discussion_r382274716
 
 

 ##########
 File path: traffic_ops/ort/atstccfg/toreq/caching.go
 ##########
 @@ -177,3 +215,65 @@ func GetCookiesFromFile(tempDir string, cacheFileMaxAge 
time.Duration) (string,
        }
        return string(bts), nil
 }
+
+// DSSExtension is the file extension for the format read and written by 
DSSEncode and DSSDecode.
+const DSSExtension = ".csv"
+
+// DSSEncode is an EncodeFunc optimized for Delivery Service Servers.
+// If iObj is not a *[]tc.DeliveryServiceServer it immediately returns an 
error.
+func DSSEncode(w io.Writer, iObj interface{}) error {
+       // Please don't change this to use encoding/csv unless you benchmark 
and prove it's at least as fast. DSS is massive, and performance is important.
+       obj, ok := iObj.(*[]tc.DeliveryServiceServer)
+       if !ok {
+               return fmt.Errorf("object is '%T' must be a 
*[]tc.DeliveryServiceServer\n", iObj)
+       }
+       for _, dss := range *obj {
+               if dss.DeliveryService == nil || dss.Server == nil {
+                       continue // TODO warn?
+               }
+               if _, err := io.WriteString(w, 
strconv.Itoa(*dss.DeliveryService)+`,`+strconv.Itoa(*dss.Server)+"\n"); err != 
nil {
+                       return fmt.Errorf("writing object cache file: " + 
err.Error())
+               }
+       }
+       return nil
+}
+
+// DSSDecode is a DecodeFunc optimized for Delivery Service Servers.
+// If iObj is not a *[]tc.DeliveryServiceServer it immediately returns an 
error.
+func DSSDecode(r io.Reader, iObj interface{}) error {
+       // Please don't change this to use encoding/csv unless you benchmark 
and prove it's at least as fast. DSS is massive, and performance is important.
+       obj, ok := iObj.(*[]tc.DeliveryServiceServer)
+       if !ok {
+               return fmt.Errorf("object is '%T' must be a 
*[]tc.DeliveryServiceServer\n", iObj)
+       }
+       objFileReader := bufio.NewReader(r)
+       for {
+               dsStr, err := objFileReader.ReadString(',')
+               if err != nil {
+                       if err == io.EOF {
+                               return nil
+                       }
+                       return errors.New("malformed object file:" + 
err.Error())
+               }
+               dsStr = dsStr[:len(dsStr)-1] // ReadString(',') includes the 
comma; remove it
+
+               ds, err := strconv.Atoi(dsStr)
+               if err != nil {
+                       return errors.New("malformed object file: first field 
should be ds id, but is not an integer: '" + dsStr + "'")
+               }
+               svStr, err := objFileReader.ReadString('\n')
+               if err != nil {
+                       if err == io.EOF {
+                               return errors.New("malformed object file: final 
line had ds but not server")
 
 Review comment:
   Which will never happen unless someone manually modifies the cache file. 
But, I changed the message to be more informative.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to