From bda5f8c57a762ea63c6944262546a05cd0cc22b9 Mon Sep 17 00:00:00 2001 From: Robert Maticevic Date: Tue, 9 Jul 2024 18:01:38 +0200 Subject: [PATCH] OPTIM/MINOR: raw: don't concatenate lines when fetching configuration contents --- configuration/raw.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/configuration/raw.go b/configuration/raw.go index b396c968..3659c6de 100644 --- a/configuration/raw.go +++ b/configuration/raw.go @@ -62,13 +62,26 @@ func (c *client) getRawConfiguration(transactionID string, version int64) (int64 return 0, 0, "", "", err } } - file, err := os.Open(config) + ondiskV, ondiskClusterV, ondiskMD5Hash, metaErr := c.getConfigurationMetaData(config) + if metaErr != nil { + return 0, 0, "", "", metaErr + } + + data, err := os.ReadFile(config) if err != nil { return 0, 0, "", "", NewConfError(ErrCannotReadConfFile, err.Error()) } + + return ondiskV, ondiskClusterV, ondiskMD5Hash, string(data), nil +} + +func (c *client) getConfigurationMetaData(config string) (int64, int64, string, error) { + file, err := os.Open(config) + if err != nil { + return 0, 0, "", NewConfError(ErrCannotReadConfFile, err.Error()) + } defer file.Close() - dataStr := "" ondiskV := int64(0) ondiskClusterV := int64(0) ondiskMD5Hash := "" @@ -84,13 +97,11 @@ func (c *client) getRawConfiguration(transactionID string, version int64) (int64 ondiskV = int64(0) } } - dataStr += line + "\n" case strings.HasPrefix(line, "# _md5hash="): w := strings.Split(line, "=") if len(w) == 2 { ondiskMD5Hash = strings.TrimSpace(w[1]) } - dataStr += line + "\n" case strings.HasPrefix(line, "# _cluster_version="): w := strings.Split(line, "=") if len(w) == 2 { @@ -99,16 +110,16 @@ func (c *client) getRawConfiguration(transactionID string, version int64) (int64 ondiskClusterV = int64(0) } } - dataStr += line + "\n" - default: - dataStr += line + "\n" + } + if ondiskV != 0 && ondiskMD5Hash != "" && ondiskClusterV != 0 { + break } } if err = scanner.Err(); err != nil { - return ondiskV, 0, "", "", NewConfError(ErrCannotReadConfFile, err.Error()) + return ondiskV, 0, "", NewConfError(ErrCannotReadConfFile, err.Error()) } - return ondiskV, ondiskClusterV, ondiskMD5Hash, dataStr, nil + return ondiskV, ondiskClusterV, ondiskMD5Hash, nil } // PostRawConfiguration pushes given string to the config file if the version