private SegmentFile getLastSegmentFile(final long logIndex, final int waitToWroteSize,
                                           final boolean createIfNecessary, final WriteContext ctx) throws IOException,
                                                                                                   InterruptedException {
        SegmentFile lastFile = null;
        while (true) {
            int segmentCount = 0;
            this.readLock.lock();
            try {

                if (!this.segments.isEmpty()) {
                    segmentCount = this.segments.size();
                    final SegmentFile currLastFile = getLastSegmentWithoutLock();
                    if (waitToWroteSize <= 0 || !currLastFile.reachesFileEndBy(waitToWroteSize)) {
                        lastFile = currLastFile;
                    }
                }
            } finally {
                this.readLock.unlock();
            }
            if (lastFile == null && createIfNecessary) {
                lastFile = createNewSegmentFile(logIndex, segmentCount, ctx);
                if (lastFile != null) {
                    return lastFile;
                } else {
                    // Try again
                    continue;
                }
            }
            return lastFile;
        }
    }
Creates a RocksDBSegmentLogStorage builder.
@return a builder instance.
jraft-core/src/main/java/com/alipay/sofa/jraft/storage/log/RocksDBSegmentLogStorage.java