You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2020/02/20 21:27:01 UTC

[GitHub] [trafficcontrol] alficles commented on a change in pull request #4419: ORT Speed Improvements

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

 ##########
 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:
   This can also happen if the final line is missing the newline.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services