Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 the original author or authors. See the notice.md file distributed with this work for additional information regarding copyright ownership. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.iq80.leveldb.impl;
 
 
 import java.io.File;
 
 public class TableCache
 {
     private final LoadingCache<LongTableAndFilecache;
     private final Finalizer<Tablefinalizer = new Finalizer<Table>(1);
 
     public TableCache(final File databaseDirint tableCacheSizefinal UserComparator userComparatorfinal boolean verifyChecksums)
     {
         Preconditions.checkNotNull(databaseDir"databaseName is null");
 
          = CacheBuilder.newBuilder()
                 .maximumSize(tableCacheSize)
                 .removalListener(new RemovalListener<LongTableAndFile>() {
                     public void onRemoval(RemovalNotification<LongTableAndFilenotification) {
                         Table table = notification.getValue().getTable();
                         .addCleanup(tabletable.closer());
                     }
                 })
                 .build(new CacheLoader<LongTableAndFile>() {
                     public TableAndFile load(Long fileNumber)
                             throws IOException {
                         return new TableAndFile(databaseDirfileNumberuserComparatorverifyChecksums);
                     }
                 });
     }
 
     {
         return newIterator(file.getNumber());
     }
 
     public InternalTableIterator newIterator(long number)
     {
         return new InternalTableIterator(getTable(number).iterator());
     }
 
     public long getApproximateOffsetOf(FileMetaData fileSlice key) {
         return getTable(file.getNumber()).getApproximateOffsetOf(key);
     }
 
     private Table getTable(long number)
     {
         Table table;
         try {
             table = .get(number).getTable();
         }
         catch (ExecutionException e) {
             Throwable cause = e;
             if (e.getCause() != null) {
                 cause = e.getCause();
             }
             throw new RuntimeException("Could not open table " + numbercause);
         }
         return table;
     }
 
     public void close() {
         .invalidateAll();
         .destroy();
     }
 
     public void evict(long number)
     {
         .invalidate(number);
    }
    private static final class TableAndFile
    {
        private final Table table;
        private final FileChannel fileChannel;
        private TableAndFile(File databaseDirlong fileNumberUserComparator userComparatorboolean verifyChecksums)
                throws IOException
        {
            String tableFileName = Filename.tableFileName(fileNumber);
            File tableFile = new File(databaseDirtableFileName);
             = new FileInputStream(tableFile).getChannel();
            try {
                if. ) {
                     = new MMapTable(tableFile.getAbsolutePath(), userComparatorverifyChecksums);
                } else {
                     = new FileChannelTable(tableFile.getAbsolutePath(), userComparatorverifyChecksums);
                }
            }
            catch (IOException e) {
                Closeables.closeQuietly();
                throw e;
            }
        }
        public Table getTable()
        {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X