docs: protected Collection<JournalSegment<E>> loadSegments() {
      // Ensure log directories are created.
      directory.mkdirs();
  
      TreeMap<Long, JournalSegment<E>> segments = new TreeMap<>();
  
      // Iterate through all files in the log directory.
      for (File file : directory.listFiles(File::isFile)) {
  
        // If the file looks like a segment file, attempt to load the segment.
        if (JournalSegmentFile.isSegmentFile(name, file)) {
          JournalSegmentFile segmentFile = new JournalSegmentFile(file);
          ByteBuffer buffer = ByteBuffer.allocate(JournalSegmentDescriptor.BYTES);
          try (FileChannel channel = openChannel(file)) {
            channel.read(buffer);
            buffer.flip();
          } catch (IOException e) {
            throw new StorageException(e);
          }
  
          JournalSegmentDescriptor descriptor = new JournalSegmentDescriptor(buffer);
  
          // Load the segment.
          JournalSegment<E> segment = loadSegment(descriptor.id());
  
          // Add the segment to the segments list.
          log.debug("Found segment: {} ({})", segment.descriptor().id(), segmentFile.file().getName());
          segments.put(segment.index(), segment);
        }
      }
  
      // Verify that all the segments in the log align with one another.
      JournalSegment<E> previousSegment = null;
      boolean corrupted = false;
      Iterator<Map.Entry<Long, JournalSegment<E>>> iterator = segments.entrySet().iterator();
      while (iterator.hasNext()) {
        JournalSegment<E> segment = iterator.next().getValue();
        if (previousSegment != null && previousSegment.lastIndex() != segment.index() - 1) {
          log.warn("Journal is inconsistent. {} is not aligned with prior segment {}", segment.file().file(), previousSegment.file().file());
          corrupted = true;
        }
        if (corrupted) {
          segment.close();
          segment.delete();
          iterator.remove();
        }
        previousSegment = segment;
      }
  
      return segments.values();
    }