/**
   * checks number of loads to be preserved and returns remaining valid segments
   *
   * @param segments
   * @return
   */
  private static List<LoadMetadataDetails> checkPreserveSegmentsPropertyReturnRemaining(
      List<LoadMetadataDetails> segments) {

    int numberOfSegmentsToBePreserved = 0;
    // check whether the preserving of the segments from merging is enabled or not.
    // get the number of loads to be preserved.
    numberOfSegmentsToBePreserved =
        CarbonProperties.getInstance().getNumberOfSegmentsToBePreserved();
    // get the number of valid segments and retain the latest loads from merging.
    return CarbonDataMergerUtil.getValidLoadDetailsWithRetaining(
        segments, numberOfSegmentsToBePreserved);
  }
  /**
   * Removing the already merged segments from list.
   *
   * @param segments
   * @param loadsToMerge
   * @return
   */
  public static List<LoadMetadataDetails> filterOutNewlyAddedSegments(
      List<LoadMetadataDetails> segments, LoadMetadataDetails lastSeg) {

    // take complete list of segments.
    List<LoadMetadataDetails> list = new ArrayList<>(segments);

    List<LoadMetadataDetails> trimmedList =
        new ArrayList<>(CarbonCommonConstants.DEFAULT_COLLECTION_SIZE);

    // sort list
    CarbonDataMergerUtil.sortSegments(list);

    // first filter out newly added segments.
    trimmedList = list.subList(0, list.indexOf(lastSeg) + 1);

    return trimmedList;
  }