-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into becca/TestRestart-windows
- Loading branch information
Showing
13 changed files
with
230 additions
and
56 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
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 |
---|---|---|
@@ -1,5 +1,52 @@ | ||
package storage | ||
|
||
import "bytes" | ||
|
||
var ( | ||
// Well-known keys | ||
ObservabilityIngestAuthTokenKey = []byte("observability_ingest_auth_token") | ||
|
||
// Identifier types in complex keys | ||
IdentifierTypeRegistration = []byte("registration") | ||
|
||
defaultIdentifier = []byte("default") | ||
) | ||
|
||
const ( | ||
keyDelimiter byte = 58 // : | ||
) | ||
|
||
func KeyByIdentifier(key []byte, identifierType []byte, identifier []byte) []byte { | ||
// The default value is stored under `key`, without any identifier | ||
if len(identifier) == 0 || bytes.Equal(identifier, defaultIdentifier) { | ||
return key | ||
} | ||
|
||
// Key will take the form `<key>:<identifierType>:<identifier>` -- allocate | ||
// a new key with the appropriate capacity. | ||
totalSize := len(key) + 1 + len(identifierType) + 1 + len(identifier) | ||
newKey := make([]byte, 0, totalSize) | ||
|
||
newKey = append(newKey, key...) | ||
newKey = append(newKey, keyDelimiter) | ||
newKey = append(newKey, identifierType...) | ||
newKey = append(newKey, keyDelimiter) | ||
newKey = append(newKey, identifier...) | ||
|
||
return newKey | ||
} | ||
|
||
func SplitKey(key []byte) ([]byte, []byte, []byte) { | ||
if !bytes.Contains(key, []byte{keyDelimiter}) { | ||
return key, nil, defaultIdentifier | ||
} | ||
|
||
// Key takes the form `<key>:<identifierType>:<identifier>` -- split | ||
// on the keyDelimiter. | ||
parts := bytes.SplitN(key, []byte{keyDelimiter}, 3) | ||
if len(parts) != 3 { | ||
return key, nil, defaultIdentifier | ||
} | ||
|
||
return parts[0], parts[1], parts[2] | ||
} |
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,92 @@ | ||
package storage | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestKeyByIdentifier(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tt := range []struct { | ||
testCaseName string | ||
key []byte | ||
identifierType []byte | ||
identifier []byte | ||
expectedKey []byte | ||
}{ | ||
{ | ||
testCaseName: "default identifier", | ||
key: []byte("nodeKey"), | ||
identifierType: IdentifierTypeRegistration, | ||
identifier: []byte("default"), | ||
expectedKey: []byte("nodeKey"), | ||
}, | ||
{ | ||
testCaseName: "empty identifier", | ||
key: []byte("config"), | ||
identifierType: IdentifierTypeRegistration, | ||
identifier: nil, | ||
expectedKey: []byte("config"), | ||
}, | ||
{ | ||
testCaseName: "registration identifier", | ||
key: []byte("uuid"), | ||
identifierType: IdentifierTypeRegistration, | ||
identifier: []byte("some-test-registration-id"), | ||
expectedKey: []byte("uuid:registration:some-test-registration-id"), | ||
}, | ||
} { | ||
tt := tt | ||
t.Run(tt.testCaseName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
require.Equal(t, tt.expectedKey, KeyByIdentifier(tt.key, tt.identifierType, tt.identifier)) | ||
}) | ||
} | ||
} | ||
|
||
func TestSplitKey(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tt := range []struct { | ||
testCaseName string | ||
key []byte | ||
expectedKey []byte | ||
expectedIdentifierType []byte | ||
expectedIdentifier []byte | ||
}{ | ||
{ | ||
testCaseName: "default node key", | ||
key: []byte("nodeKey"), | ||
expectedKey: []byte("nodeKey"), | ||
expectedIdentifierType: nil, | ||
expectedIdentifier: []byte("default"), | ||
}, | ||
{ | ||
testCaseName: "uuid by registration", | ||
key: []byte("uuid:registration:some-test-registration-id"), | ||
expectedKey: []byte("uuid"), | ||
expectedIdentifierType: IdentifierTypeRegistration, | ||
expectedIdentifier: []byte("some-test-registration-id"), | ||
}, | ||
{ | ||
testCaseName: "katc table by registration", | ||
key: []byte("katc_some_test_table:registration:another-test-registration-id"), | ||
expectedKey: []byte("katc_some_test_table"), | ||
expectedIdentifierType: IdentifierTypeRegistration, | ||
expectedIdentifier: []byte("another-test-registration-id"), | ||
}, | ||
} { | ||
tt := tt | ||
t.Run(tt.testCaseName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
splitKey, identifierType, identifier := SplitKey(tt.key) | ||
require.Equal(t, tt.expectedKey, splitKey) | ||
require.Equal(t, tt.expectedIdentifierType, identifierType) | ||
require.Equal(t, tt.expectedIdentifier, identifier) | ||
}) | ||
} | ||
} |
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.