/**
 * Copyright (c) 2018-present, heineiuo.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import { assert } from "./DBHelper" ; import Slice from "./Slice" ; export interface Comparator { compare ( a , b ) : number ; getName ( ) : string ; findShortestSeparator ( start , limit ) : void ; findShortSuccessor ( key ) : void ; } export class BytewiseComparator implements Comparator { getName ( ) { return "leveldb.BytewiseComparator" ; } compare ( a , b ) { return a . compare ( b ) ; } findShortestSeparator ( start , limit ) { const minLength = Math . min ( start . length , limit . size ) ; let diffIndex = 0 ; while ( diffIndex < minLength && start . buffer [ diffIndex ] == limit . buffer [ diffIndex ] ) { diffIndex ++ ; } if ( diffIndex >= minLength ) { } else { const diffByte = start . buffer [ diffIndex ] ; if ( diffByte < 0xff && diffByte + 1 < limit . buffer [ diffIndex ] ) { start . buffer [ diffIndex ] ++ ; start . buffer = start . buffer . slice ( 0 , diffIndex + 1 ) ; assert ( this . compare ( start , limit ) < 0 ) ; } } } findShortSuccessor ( key ) { const n = key . length ; for ( let i = 0 ; i < n ; i ++ ) { const byte = key . buffer [ i ] ; if ( byte != 0xff ) { key . buffer [ i ] = byte + 1 ; key . buffer = key . buffer . slice ( 0 , i + 1 ) ; return ; } } } }