-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GODRIVER-3090 Optimize logging truncation for large documents (#1699)
- Loading branch information
1 parent
8f1eac5
commit b0d91d0
Showing
14 changed files
with
1,060 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (C) MongoDB, Inc. 2024-present. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package bsoncoreutil | ||
|
||
// Truncate truncates a given string for a certain width | ||
func Truncate(str string, width int) string { | ||
if width == 0 { | ||
return "" | ||
} | ||
|
||
if len(str) <= width { | ||
return str | ||
} | ||
|
||
// Truncate the byte slice of the string to the given width. | ||
newStr := str[:width] | ||
|
||
// Check if the last byte is at the beginning of a multi-byte character. | ||
// If it is, then remove the last byte. | ||
if newStr[len(newStr)-1]&0xC0 == 0xC0 { | ||
return newStr[:len(newStr)-1] | ||
} | ||
|
||
// Check if the last byte is a multi-byte character | ||
if newStr[len(newStr)-1]&0xC0 == 0x80 { | ||
// If it is, step back until you we are at the start of a character | ||
for i := len(newStr) - 1; i >= 0; i-- { | ||
if newStr[i]&0xC0 == 0xC0 { | ||
// Truncate at the end of the character before the character we stepped back to | ||
return newStr[:i] | ||
} | ||
} | ||
} | ||
|
||
return newStr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (C) MongoDB, Inc. 2024-present. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package bsoncoreutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"go.mongodb.org/mongo-driver/v2/internal/assert" | ||
) | ||
|
||
func TestTruncate(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tcase := range []struct { | ||
name string | ||
arg string | ||
width int | ||
expected string | ||
}{ | ||
{ | ||
name: "empty", | ||
arg: "", | ||
width: 0, | ||
expected: "", | ||
}, | ||
{ | ||
name: "short", | ||
arg: "foo", | ||
width: 1000, | ||
expected: "foo", | ||
}, | ||
{ | ||
name: "long", | ||
arg: "foo bar baz", | ||
width: 9, | ||
expected: "foo bar b", | ||
}, | ||
{ | ||
name: "multi-byte", | ||
arg: "你好", | ||
width: 4, | ||
expected: "你", | ||
}, | ||
} { | ||
tcase := tcase | ||
|
||
t.Run(tcase.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
actual := Truncate(tcase.arg, tcase.width) | ||
assert.Equal(t, tcase.expected, actual) | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.