1// Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
2// All rights reserved.
3//
4// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6
7package leveldb
8
9import (
10	"github.com/syndtr/goleveldb/leveldb/comparer"
11)
12
13type iComparer struct {
14	ucmp comparer.Comparer
15}
16
17func (icmp *iComparer) uName() string {
18	return icmp.ucmp.Name()
19}
20
21func (icmp *iComparer) uCompare(a, b []byte) int {
22	return icmp.ucmp.Compare(a, b)
23}
24
25func (icmp *iComparer) uSeparator(dst, a, b []byte) []byte {
26	return icmp.ucmp.Separator(dst, a, b)
27}
28
29func (icmp *iComparer) uSuccessor(dst, b []byte) []byte {
30	return icmp.ucmp.Successor(dst, b)
31}
32
33func (icmp *iComparer) Name() string {
34	return icmp.uName()
35}
36
37func (icmp *iComparer) Compare(a, b []byte) int {
38	x := icmp.uCompare(internalKey(a).ukey(), internalKey(b).ukey())
39	if x == 0 {
40		if m, n := internalKey(a).num(), internalKey(b).num(); m > n {
41			return -1
42		} else if m < n {
43			return 1
44		}
45	}
46	return x
47}
48
49func (icmp *iComparer) Separator(dst, a, b []byte) []byte {
50	ua, ub := internalKey(a).ukey(), internalKey(b).ukey()
51	dst = icmp.uSeparator(dst, ua, ub)
52	if dst != nil && len(dst) < len(ua) && icmp.uCompare(ua, dst) < 0 {
53		// Append earliest possible number.
54		return append(dst, keyMaxNumBytes...)
55	}
56	return nil
57}
58
59func (icmp *iComparer) Successor(dst, b []byte) []byte {
60	ub := internalKey(b).ukey()
61	dst = icmp.uSuccessor(dst, ub)
62	if dst != nil && len(dst) < len(ub) && icmp.uCompare(ub, dst) < 0 {
63		// Append earliest possible number.
64		return append(dst, keyMaxNumBytes...)
65	}
66	return nil
67}
68