2015-02-16 15:28:33 +02:00
|
|
|
// 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
|
|
|
|
|
2016-10-20 15:56:34 +03:00
|
|
|
import (
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/comparer"
|
|
|
|
)
|
2015-02-16 15:28:33 +02:00
|
|
|
|
|
|
|
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 {
|
2016-10-20 15:56:34 +03:00
|
|
|
x := icmp.uCompare(internalKey(a).ukey(), internalKey(b).ukey())
|
2015-02-16 15:28:33 +02:00
|
|
|
if x == 0 {
|
2016-03-10 11:39:20 +02:00
|
|
|
if m, n := internalKey(a).num(), internalKey(b).num(); m > n {
|
2016-10-20 15:56:34 +03:00
|
|
|
return -1
|
2015-02-16 15:28:33 +02:00
|
|
|
} else if m < n {
|
2016-10-20 15:56:34 +03:00
|
|
|
return 1
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
func (icmp *iComparer) Separator(dst, a, b []byte) []byte {
|
2016-03-10 11:39:20 +02:00
|
|
|
ua, ub := internalKey(a).ukey(), internalKey(b).ukey()
|
2016-10-20 15:56:34 +03:00
|
|
|
dst = icmp.uSeparator(dst, ua, ub)
|
|
|
|
if dst != nil && len(dst) < len(ua) && icmp.uCompare(ua, dst) < 0 {
|
|
|
|
// Append earliest possible number.
|
|
|
|
return append(dst, keyMaxNumBytes...)
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
2016-10-20 15:56:34 +03:00
|
|
|
return nil
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (icmp *iComparer) Successor(dst, b []byte) []byte {
|
2016-03-10 11:39:20 +02:00
|
|
|
ub := internalKey(b).ukey()
|
2016-10-20 15:56:34 +03:00
|
|
|
dst = icmp.uSuccessor(dst, ub)
|
|
|
|
if dst != nil && len(dst) < len(ub) && icmp.uCompare(ub, dst) < 0 {
|
|
|
|
// Append earliest possible number.
|
|
|
|
return append(dst, keyMaxNumBytes...)
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
2016-10-20 15:56:34 +03:00
|
|
|
return nil
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|