Loads all segments from disk.

@return A collection of segments for the log.
175089
protected Collection<Segment> loadSegments() {
    // Ensure log directories are created.
    storage.directory().mkdirs();

    TreeMap<Long, Segment> segments = new TreeMap<>();

    // Iterate through all files in the log directory.
    for (File file : storage.directory().listFiles(File::isFile)) {

      // If the file looks like a segment file, attempt to load the segment.
      if (SegmentFile.isSegmentFile(name, file)) {
        SegmentFile segmentFile = new SegmentFile(file);
        SegmentDescriptor descriptor = new SegmentDescriptor(FileBuffer.allocate(file, SegmentDescriptor.BYTES));

        // Valid segments will have been locked. Segments that resulting from failures during log cleaning will be
        // unlocked and should ultimately be deleted from disk.
        if (descriptor.locked()) {

          // Load the segment.
          Segment segment = loadSegment(descriptor.id(), descriptor.version());

          // If a segment with an equal or lower index has already been loaded, ensure this segment is not superseded
          // by the earlier segment. This can occur due to segments being combined during log compaction.
          Map.Entry<Long, Segment> previousEntry = segments.floorEntry(segment.index());
          if (previousEntry != null) {

            // If an existing descriptor exists with a lower index than this segment's first index, check to determine
            // whether this segment's first index is contained in that existing index. If it is, determine which segment
            // should take precedence based on segment versions.
            Segment previousSegment = previousEntry.getValue();

            // If the two segments start at the same index, the segment with the higher version number is used.
            if (previousSegment.index() == segment.index()) {
              if (segment.descriptor().version() > previousSegment.descriptor().version()) {
                LOGGER.debug("Replaced segment {} with newer version: {} ({})", previousSegment.descriptor().id(), segment.descriptor().version(), segmentFile.file().getName());
                segments.remove(previousEntry.getKey());
                previousSegment.close();
                previousSegment.delete();
              } else {
                segment.close();
                segment.delete();
                continue;
              }
            }
            // If the existing segment's entries overlap with the loaded segment's entries, the existing segment always
            // supersedes the loaded segment. Log compaction processes ensure this is always the case.
            else if (previousSegment.index() + previousSegment.length() > segment.index()) {
              segment.close();
              segment.delete();
              continue;
            }
          }

          // Add the segment to the segments list.
          LOGGER.debug("Found segment: {} ({})", segment.descriptor().id(), segmentFile.file().getName());
          segments.put(segment.index(), segment);

          // Ensure any segments later in the log with which this segment overlaps are removed.
          Map.Entry<Long, Segment> nextEntry = segments.higherEntry(segment.index());
          while (nextEntry != null) {
            if (nextEntry.getValue().index() < segment.index() + segment.length()) {
              segments.remove(nextEntry.getKey());
              nextEntry = segments.higherEntry(segment.index());
            } else {
              break;
            }
          }

          descriptor.close();
        }
        // If the segment descriptor wasn't locked, close and delete the descriptor.
        else {
          LOGGER.debug("Deleting unlocked segment: {}-{} ({})", descriptor.id(), descriptor.version(), segmentFile.file().getName());
          descriptor.close();
          descriptor.delete();
        }
      }
    }

    for (Long segmentId : segments.keySet()) {
      Segment segment = segments.get(segmentId);
      Map.Entry<Long, Segment> previousEntry = segments.floorEntry(segmentId - 1);
      if (previousEntry != null) {
        Segment previousSegment = previousEntry.getValue();
        if (previousSegment.index() + previousSegment.length() - 1 < segment.index()) {
          previousSegment.skip(segment.index() - (previousSegment.index() + previousSegment.length()));
        }
      }
    }

    return segments.values();
  }
932419
private List<Segment> getCompactableSegments(SegmentManager manager) {
    List<Segment> segments = new ArrayList<>(manager.segments().size());
    Iterator<Segment> iterator = manager.segments().iterator();
    Segment segment = iterator.next();
    while (iterator.hasNext()) {
      Segment nextSegment = iterator.next();

      // Segments that have already been compacted are eligible for compaction. For uncompacted segments, the segment must be full, consist
      // of entries less than the minorIndex, and a later segment with at least one committed entry must exist in the log. This ensures that
      // a non-empty entry always remains at the end of the log.
      if (segment.isCompacted() || (segment.isFull() && segment.lastIndex() < compactor.minorIndex() && nextSegment.firstIndex() <= manager.commitIndex() && !nextSegment.isEmpty())) {
        segments.add(segment);
      } else {
        break;
      }

      segment = nextSegment;
    }
    return segments;
  }
1210951
def segment_list
      segments = ['Segments']

      job_steps.each do |job_step|
        if job_step.crawl_segment.present?
          segment = job_step.crawl_segment
          segments.push(segment.segment_desc)
        end
      end

      segments.push('')
    end
49319
public final List<AbstractTraceRegion> getNestedRegions() {
		if (nestedRegions == null)
			return Collections.emptyList();
		return Collections.unmodifiableList(nestedRegions);
	}
1065369
public Segment loadSegment(long segmentId, long segmentVersion) {
    switch (storage.level()) {
      case MEMORY:
        return loadMemorySegment(segmentId, segmentVersion);
      case MAPPED:
        return loadMappedSegment(segmentId, segmentVersion);
      case DISK:
        return loadDiskSegment(segmentId, segmentVersion);
      default:
        throw new AssertionError();
    }
  }
1016417
public static TreeSet<File> getLogFiles() {

        TreeSet<File> result = new TreeSet<>();
        for (File file : new File(CmsLogFileApp.LOG_FOLDER).listFiles()) {
            result.add(file);
        }
        for (String dir : getAdditionalLogDirectories()) {
            File file = new File(dir);
            if (file.exists()) {
                if (file.isDirectory()) {
                    for (File child : file.listFiles()) {
                        if (child.canRead()) {
                            result.add(child);
                        } else {
                            LOG.error("Can not read " + child.getAbsolutePath());
                        }
                    }
                }
            }
        }
        return result;
    }
331493
public void append(LogSegment segment) {
        while (true) {
            List<LogSegment> curr = contents.get();
            List<LogSegment> updated = new ArrayList<LogSegment>(curr);
            updated.add(segment);
            if (contents.compareAndSet(curr, updated)) {
                return;
            }
        }
    }
34653
private ArrayList<Segment10> tableSegments(TableEntry10 table)
  {
    ArrayList<Segment10> tableSegments = new ArrayList<>();
    
    for (Segment10 segment : _segments) {
      if (Arrays.equals(segment.key(), table.key())) {
        tableSegments.add(segment);
      }
    }
    
    Collections.sort(tableSegments,
                     (x,y)->Long.signum(y.sequence() - x.sequence()));
    
    return tableSegments;
  }
1246967
public SegmentIndexBuffer loadSegmentIndexBuffer(int segId) {
        File sibFile = getSegmentIndexBufferFile(segId);
        if(sibFile.exists()) {
            SegmentIndexBuffer sib = new SegmentIndexBuffer();
            try {
                _sibManager.getSegmentIndexBufferIO().read(sib, sibFile);
                return sib;
            } catch (Exception e) {
                _log.warn(sibFile.getAbsolutePath() + " corrupted");
                sibFile.delete();
            }
        }
        
        return null;
    }
270744
private Segment findSegmentToRelease() {
        Set<Segment> segments = sync.getState().getSegments(readerId);
        return segments.stream()
                       .max((s1, s2) -> Double.compare(hashHelper.hashToRange(s1.getScopedName()),
                                                       hashHelper.hashToRange(s2.getScopedName())))
                       .orElse(null);
    }
419480
Map<UUID, CacheBucketOffset> getTailHashes(long segmentId) {
        return forSegmentCache(segmentId, SegmentKeyCache::getTailBucketOffsets, Collections.emptyMap());
    }
78018
public Iterable<JpegSegmentType> getSegmentTypes()
    {
        Set<JpegSegmentType> segmentTypes = new HashSet<JpegSegmentType>();

        for (Byte segmentTypeByte : _segmentDataMap.keySet())
        {
            JpegSegmentType segmentType = JpegSegmentType.fromByte(segmentTypeByte);
            if (segmentType == null) {
                throw new IllegalStateException("Should not have a segmentTypeByte that is not in the enum: " + Integer.toHexString(segmentTypeByte));
            }
            segmentTypes.add(segmentType);
        }

        return segmentTypes;
    }
253861
private void readSegments()
    throws IOException
  {
    for (SegmentExtent10 extent : _segmentExtents) {
      try (ReadStream is = openRead(extent.address(), extent.length())) {
        is.skip(extent.length() - BLOCK_SIZE);
        
        long sequence = BitsUtil.readLong(is);
        
        byte []tableKey = new byte[TABLE_KEY_SIZE];
        is.readAll(tableKey, 0, tableKey.length);
        
        // XXX: crc
        
        if (sequence > 0) {
          Segment10 segment = new Segment10(sequence, tableKey, extent);
          
          _segments.add(segment);
        }
      }
    }
  }
1360551
public EJBObject[] loadEntireCollection() {

        EJBObject[] result = null;

        try {

            result = (EJBObject[]) allRemainingElements();

        } catch (NoMoreElementsException e) {

            // FFDCFilter.processException(e, CLASS_NAME + ".loadEntireCollection",
            //                             "270", this);
            return elements;

        } catch (EnumeratorException e) {

            // FFDCFilter.processException(e, CLASS_NAME + ".loadEntireCollection",
            //                             "276", this);
            throw new RuntimeException(e.toString());

        } catch (RemoteException e) {

            // FFDCFilter.processException(e, CLASS_NAME + ".loadEntireCollection",
            //                             "282", this);
            throw new RuntimeException(e.toString());
        }

        elements = result;
        return result;
    }
1256174
private Segment currentSegment() {
    Segment segment = segments.currentSegment();
    if (segment.isFull()) {
      segments.currentSegment().flush();
      segment = segments.nextSegment();
    }
    return segment;
  }
882459
public synchronized String[] list()
    {
        if(isDirectory() && _list==null)
        {
            ArrayList list = new ArrayList(32);

            checkConnection();
            
            JarFile jarFile=_jarFile;
            if(jarFile==null)
            {
                try
                {
                    jarFile=((JarURLConnection)
                             ((new URL(_jarUrl)).openConnection())).getJarFile();
                }
                catch(Exception e)
                {
                     LogSupport.ignore(log,e);
                }
            }
            
            Enumeration e=jarFile.entries();
            String dir=_urlString.substring(_urlString.indexOf("!/")+2);
            while(e.hasMoreElements())
            {
                JarEntry entry = (JarEntry) e.nextElement();
                String name=entry.getName().replace('\\','/');
                if(!name.startsWith(dir) || name.length()==dir.length())
                    continue;
                String listName=name.substring(dir.length());
                int dash=listName.indexOf('/');
                if (dash>=0)
                {
                    listName=listName.substring(0,dash+1);
                    if (list.contains(listName))
                        continue;
                }
                
                list.add(listName);
            }
            
            _list=new String[list.size()];
            list.toArray(_list);
        }
        return _list;
    }
207677
public static List<TrackSegment> nonEmptySegments(
		final List<TrackSegment> segments
	) {
		return segments.stream()
			.filter(TrackSegment::nonEmpty)
			.collect(toList());
	}