forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_rewriter.go
56 lines (51 loc) · 1.93 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2016 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (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 sqlccl
import (
"github.com/cockroachdb/cockroach/pkg/ccl/storageccl"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
)
// MakeKeyRewriterForNewTableID creates a KeyRewriter that rewrites all keys
// from a table to have a new tableID. For dependency reasons, the
// implementation of the matching is in storageccl, but the interesting
// constructor is here.
func MakeKeyRewriterForNewTableID(
desc *sqlbase.TableDescriptor, newTableID sqlbase.ID,
) storageccl.KeyRewriter {
newDesc := *desc
newDesc.ID = newTableID
// The PrefixEnd() of index 1 is the same as the prefix of index 2, so use a
// map to avoid duplicating entries.
prefixes := make(map[string]struct{})
var kr storageccl.KeyRewriter
for _, index := range desc.AllNonDropIndexes() {
oldPrefix := roachpb.Key(sqlbase.MakeIndexKeyPrefix(desc, index.ID))
newPrefix := roachpb.Key(sqlbase.MakeIndexKeyPrefix(&newDesc, index.ID))
if _, ok := prefixes[string(oldPrefix)]; !ok {
prefixes[string(oldPrefix)] = struct{}{}
kr = append(kr, roachpb.KeyRewrite{
OldPrefix: oldPrefix,
NewPrefix: newPrefix,
})
}
// All the encoded data for a index will have the prefix just added, but
// if you need to translate a half-open range describing that prefix
// (and we do), the prefix end needs to be in the map too.
oldPrefix = oldPrefix.PrefixEnd()
newPrefix = newPrefix.PrefixEnd()
if _, ok := prefixes[string(oldPrefix)]; !ok {
prefixes[string(oldPrefix)] = struct{}{}
kr = append(kr, roachpb.KeyRewrite{
OldPrefix: oldPrefix,
NewPrefix: newPrefix,
})
}
}
return kr
}