aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/comparer.go
blob: d33d5e9c78fcf566114dce757ffe4ea6e0f0ba46 (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package leveldb

import "github.com/syndtr/goleveldb/leveldb/comparer"

type iComparer struct {
    ucmp comparer.Comparer
}

func (icmp *iComparer) uName() string {
    return icmp.ucmp.Name()
}

func (icmp *iComparer) uCompare(a, b []byte) int {
    return icmp.ucmp.Compare(a, b)
}

func (icmp *iComparer) uSeparator(dst, a, b []byte) []byte {
    return icmp.ucmp.Separator(dst, a, b)
}

func (icmp *iComparer) uSuccessor(dst, b []byte) []byte {
    return icmp.ucmp.Successor(dst, b)
}

func (icmp *iComparer) Name() string {
    return icmp.uName()
}

func (icmp *iComparer) Compare(a, b []byte) int {
    x := icmp.ucmp.Compare(iKey(a).ukey(), iKey(b).ukey())
    if x == 0 {
        if m, n := iKey(a).num(), iKey(b).num(); m > n {
            x = -1
        } else if m < n {
            x = 1
        }
    }
    return x
}

func (icmp *iComparer) Separator(dst, a, b []byte) []byte {
    ua, ub := iKey(a).ukey(), iKey(b).ukey()
    dst = icmp.ucmp.Separator(dst, ua, ub)
    if dst == nil {
        return nil
    }
    if len(dst) < len(ua) && icmp.uCompare(ua, dst) < 0 {
        dst = append(dst, kMaxNumBytes...)
    } else {
        // Did not close possibilities that n maybe longer than len(ub).
        dst = append(dst, a[len(a)-8:]...)
    }
    return dst
}

func (icmp *iComparer) Successor(dst, b []byte) []byte {
    ub := iKey(b).ukey()
    dst = icmp.ucmp.Successor(dst, ub)
    if dst == nil {
        return nil
    }
    if len(dst) < len(ub) && icmp.uCompare(ub, dst) < 0 {
        dst = append(dst, kMaxNumBytes...)
    } else {
        // Did not close possibilities that n maybe longer than len(ub).
        dst = append(dst, b[len(b)-8:]...)
    }
    return dst
}