forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_rewriter.go
39 lines (34 loc) · 1.32 KB
/
key_rewriter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Cockroach Community Licence (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/LICENSE
package storageccl
import (
"bytes"
"github.com/cockroachdb/cockroach/pkg/roachpb"
)
// KeyRewriter is an matcher for an ordered list of pairs of byte prefix rewrite
// rules. For dependency reasons, the implementation of the matching is here,
// but the interesting constructor is in sqlccl.
type KeyRewriter []roachpb.KeyRewrite
// RewriteKey modifies key using the first matching rule and returns it. If no
// rules matched, returns false and the original input key.
func (kr KeyRewriter) RewriteKey(key []byte) ([]byte, bool) {
for _, rewrite := range kr {
if bytes.HasPrefix(key, rewrite.OldPrefix) {
if len(rewrite.OldPrefix) == len(rewrite.NewPrefix) {
copy(key[:len(rewrite.OldPrefix)], rewrite.NewPrefix)
return key, true
}
// TODO(dan): Special case when key's cap() is enough.
newKey := make([]byte, 0, len(rewrite.NewPrefix)+len(key)-len(rewrite.OldPrefix))
newKey = append(newKey, rewrite.NewPrefix...)
newKey = append(newKey, key[len(rewrite.OldPrefix):]...)
return newKey, true
}
}
return key, false
}