/**
 * 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 { InternalKeyComparator , InternalKey } from "./Format" ; import { Options } from "./Options" ; export class FileMetaData { refs : number ; allowedSeeks : number ; fileSize : number ; number ! : number ; smallest : InternalKey ; largest : InternalKey ; constructor ( ) { this . refs = 0 ; this . allowedSeeks = 1 << 30 ; this . fileSize = 0 ; this . smallest = new InternalKey ( ) ; this . largest = new InternalKey ( ) ; } } export class BySmallestKey { internalComparator ! : InternalKeyComparator ; constructor ( cmp ? ) { if ( cmp ) this . internalComparator = cmp ; } operator ( file1 , file2 ) { const r = this . internalComparator . compare ( file1 . smallest , file2 . smallest ) ; if ( r === 0 ) return file1 . number < file2 . number ; return r < 0 ; } } export class FileSet { _set : FileMetaData [ ] ; compare : BySmallestKey ; constructor ( cmp ) { this . compare = cmp ; this . _set = [ ] ; } add ( file ) { if ( this . _set . find ( ( item ) => item . number === file . number ) ) { return ; } const setLength = this . _set . length ; if ( setLength === 0 ) { this . _set . push ( file ) ; } else { for ( let i = 0 ; i < setLength ; i ++ ) { const file1 = this . _set [ i ] ; const b = this . compare . operator ( file , file1 ) ; if ( b ) { this . _set . splice ( i , 0 , file ) ; return ; } } this . _set . push ( file ) ; } } * iterator ( ) { const setLength = this . _set . length ; for ( let i = 0 ; i < setLength ; i ++ ) { yield this . _set [ i ] ; } } } export type FileMetaDataLeveldb = { fileNum : number ; fileSize : number ; smallestKey : InternalKey ; largestKey : InternalKey ; } ; export type CompactPointer = { level : number ; internalKey : InternalKey ; } ; export type DeletedFile = { level : number ; fileNum : number ; } ; export type NewFile = { level : number ; fileMetaData : FileMetaData ; } ; export function getMaxBytesForLevel ( level ) { let result = 10.0 * 1048576.0 ; while ( level > 1 ) { result *= 10 ; level -- ; } return result ; } export function getExpandedCompactionByteSizeLimit ( options ) { return 25 * options . maxFileSize ; } export interface GetStats { seekFile : FileMetaData ; seekFileLevel : number ; }