public Ddb(Options options, File databaseDir) throws IOException { super(options, databaseDir); seqs.put(GLOBAL_BUCKET, versions.getLastSequence()); this.logCache = new ChannelCache<String, LogReader0>() .removalListener( new RemovalListener<String, LogReader0>() { public void onRemoval(String fileNumber, LogReader0 logReader) { if (logReader != null) { logReader.close(); } // log } }) .loader( new CacheLoader<String, LogReader0>() { public LogReader0 load(String key) { File file = new File( Ddb.this.databaseDir, Filename.logFileName(Long.parseLong(key.split("#")[2]))); FileChannel channel = null; try { channel = new FileInputStream(file).getChannel(); LogMonitor logMonitor = new LogMonitor() { @Override public void corruption(long bytes, Throwable reason) { if (logger.isDebugEnabled()) { logger.debug( String.format("corruption of %s bytes", bytes), reason); } } @Override public void corruption(long bytes, String reason) { if (logger.isDebugEnabled()) { logger.debug("corruption of {} bytes: {}", bytes, reason); } } }; return new LogReader0(channel, logMonitor, true, 0); } catch (FileNotFoundException e) { throw new IfileNotFoundException("Log file does not exist.", e); } catch (Exception e) { if (channel != null) { try { channel.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } return null; } } }); }
public long getMaxSeq(int bucket) { Long seq = seqs.get(bucket); if (seq != null) { return seq; } else { return -1; } }
@Override public Snapshot writeInternal(WriteBatchImpl updates, WriteOptions options) throws DBException { checkBackgroundException(); mutex.lock(); try { long sequenceEnd; if (updates.size() != 0) { makeRoomForWrite(false); // Get sequence numbers for this change set final long sequenceBegin = versions.getLastSequence() + 1; sequenceEnd = sequenceBegin + updates.size() - 1; // Reserve this sequence in the version set versions.setLastSequence(sequenceEnd); // Log write Slice record = writeWriteBatch(updates, sequenceBegin); try { log.addRecord(record, options.sync()); } catch (IOException e) { throw Throwables.propagate(e); } // Update memtable updates.forEach(new InsertIntoHandler(memTable, sequenceBegin)); } else { sequenceEnd = versions.getLastSequence(); } seqs.put(options.bucket(), sequenceEnd); seqs.put(GLOBAL_BUCKET, sequenceEnd); // 全局seq if (options.snapshot()) { return new SnapshotImpl(versions.getCurrent(), sequenceEnd); } else { return null; } } finally { mutex.unlock(); } }
@Override protected void fireSweep(int bucket) { seqs.remove(bucket); }