Merge branch 'dedup4'

This commit is contained in:
Miek Gieben 2015-08-31 17:06:42 +01:00
commit 1d2215f5e8
5 changed files with 177 additions and 8 deletions

84
sanitize.go Normal file
View File

@ -0,0 +1,84 @@
package dns
// Dedup removes identical RRs from rrs. It preserves the original ordering.
// The lowest TTL of any duplicates is used in the remaining one. Dedup modifies
// rrs.
// m is used to store the RRs temporay. If it is nil a new map will be allocated.
func Dedup(rrs []RR, m map[string]RR) []RR {
if m == nil {
m = make(map[string]RR)
}
// Save the keys, so we don't have to call normalizedString twice.
keys := make([]*string, 0, len(rrs))
for _, r := range rrs {
key := normalizedString(r)
keys = append(keys, &key)
if _, ok := m[key]; ok {
// Shortest TTL wins.
if m[key].Header().Ttl > r.Header().Ttl {
m[key].Header().Ttl = r.Header().Ttl
}
continue
}
m[key] = r
}
// If the length of the result map equals the amount of RRs we got,
// it means they were all different. We can then just return the original rrset.
if len(m) == len(rrs) {
return rrs
}
j := 0
for i, r := range rrs {
// If keys[i] lives in the map, we should copy and remove it.
if _, ok := m[*keys[i]]; ok {
delete(m, *keys[i])
rrs[j] = r
j++
}
if len(m) == 0 {
break
}
}
return rrs[:j]
}
// normalizedString returns a normalized string from r. The TTL
// is removed and the domain name is lowercased. We go from this:
// DomainName<TAB>TTL<TAB>CLASS<TAB>TYPE<TAB>RDATA to:
// lowercasename<TAB>CLASS<TAB>TYPE...
func normalizedString(r RR) string {
// A string Go DNS makes has: domainname<TAB>TTL<TAB>...
b := []byte(r.String())
// find the first non-escaped tab, then another, so we capture where the TTL lives.
esc := false
ttlStart, ttlEnd := 0, 0
for i := 0; i < len(b) && ttlEnd == 0; i++ {
switch {
case b[i] == '\\':
esc = !esc
case b[i] == '\t' && !esc:
if ttlStart == 0 {
ttlStart = i
continue
}
if ttlEnd == 0 {
ttlEnd = i
}
case b[i] >= 'A' && b[i] <= 'Z' && !esc:
b[i] += 32
default:
esc = false
}
}
// remove TTL.
copy(b[ttlStart:], b[ttlEnd:])
cut := ttlEnd - ttlStart
return string(b[:len(b)-cut])
}

85
sanitize_test.go Normal file
View File

@ -0,0 +1,85 @@
package dns
import "testing"
func TestDedup(t *testing.T) {
// make it []string
testcases := map[[3]RR][]string{
[...]RR{
newRR(t, "mIek.nl. IN A 127.0.0.1"),
newRR(t, "mieK.nl. IN A 127.0.0.1"),
newRR(t, "miek.Nl. IN A 127.0.0.1"),
}: []string{"mIek.nl.\t3600\tIN\tA\t127.0.0.1"},
[...]RR{
newRR(t, "miEk.nl. 2000 IN A 127.0.0.1"),
newRR(t, "mieK.Nl. 1000 IN A 127.0.0.1"),
newRR(t, "Miek.nL. 500 IN A 127.0.0.1"),
}: []string{"miEk.nl.\t500\tIN\tA\t127.0.0.1"},
[...]RR{
newRR(t, "miek.nl. IN A 127.0.0.1"),
newRR(t, "miek.nl. CH A 127.0.0.1"),
newRR(t, "miek.nl. IN A 127.0.0.1"),
}: []string{"miek.nl.\t3600\tIN\tA\t127.0.0.1",
"miek.nl.\t3600\tCH\tA\t127.0.0.1",
},
[...]RR{
newRR(t, "miek.nl. CH A 127.0.0.1"),
newRR(t, "miek.nl. IN A 127.0.0.1"),
newRR(t, "miek.de. IN A 127.0.0.1"),
}: []string{"miek.nl.\t3600\tCH\tA\t127.0.0.1",
"miek.nl.\t3600\tIN\tA\t127.0.0.1",
"miek.de.\t3600\tIN\tA\t127.0.0.1",
},
[...]RR{
newRR(t, "miek.de. IN A 127.0.0.1"),
newRR(t, "miek.nl. 200 IN A 127.0.0.1"),
newRR(t, "miek.nl. 300 IN A 127.0.0.1"),
}: []string{"miek.de.\t3600\tIN\tA\t127.0.0.1",
"miek.nl.\t200\tIN\tA\t127.0.0.1",
},
}
for rr, expected := range testcases {
out := Dedup([]RR{rr[0], rr[1], rr[2]}, nil)
for i, o := range out {
if o.String() != expected[i] {
t.Fatalf("expected %v, got %v", expected[i], o.String())
}
}
}
}
func BenchmarkDedup(b *testing.B) {
rrs := []RR{
newRR(nil, "miEk.nl. 2000 IN A 127.0.0.1"),
newRR(nil, "mieK.Nl. 1000 IN A 127.0.0.1"),
newRR(nil, "Miek.nL. 500 IN A 127.0.0.1"),
}
m := make(map[string]RR)
for i := 0; i < b.N; i++ {
Dedup(rrs,m )
}
}
func TestNormalizedString(t *testing.T) {
tests := map[RR]string{
newRR(t, "mIEk.Nl. 3600 IN A 127.0.0.1"): "miek.nl.\tIN\tA\t127.0.0.1",
newRR(t, "m\\ iek.nL. 3600 IN A 127.0.0.1"): "m\\ iek.nl.\tIN\tA\t127.0.0.1",
newRR(t, "m\\\tIeK.nl. 3600 in A 127.0.0.1"): "m\\tiek.nl.\tIN\tA\t127.0.0.1",
}
for tc, expected := range tests {
n := normalizedString(tc)
if n != expected {
t.Logf("expected %s, got %s", expected, n)
t.Fail()
}
}
}
func newRR(t *testing.T, s string) RR {
r, e := NewRR(s)
if e != nil {
t.Logf("newRR: %s", e)
}
return r
}