private SegmentIdWithShardSpec getSegmentIdentifierAndMaybePush(long truncatedTime) {

    DateTime truncatedDateTime = segmentGranularity.bucketStart(DateTimes.utc(truncatedTime));
    final Interval interval = new Interval(truncatedDateTime, segmentGranularity.increment(truncatedDateTime));

    SegmentIdWithShardSpec retVal;
    if (currentOpenSegment == null) {
      currentOpenSegment =
          new SegmentIdWithShardSpec(dataSchema.getDataSource(),
              interval,
              tuningConfig.getVersioningPolicy().getVersion(interval),
              new LinearShardSpec(0));
      return currentOpenSegment;
    } else if (currentOpenSegment.getInterval().equals(interval)) {
      retVal = currentOpenSegment;
      int rowCount = appenderator.getRowCount(retVal);
      if (rowCount < maxPartitionSize) {
        return retVal;
      } else {
        retVal =
            new SegmentIdWithShardSpec(dataSchema.getDataSource(),
                interval,
                tuningConfig.getVersioningPolicy().getVersion(interval),
                new LinearShardSpec(currentOpenSegment.getShardSpec().getPartitionNum() + 1));
        pushSegments(Lists.newArrayList(currentOpenSegment));
        LOG.info("Creating new partition for segment {}, partition num {}",
            retVal.toString(),
            retVal.getShardSpec().getPartitionNum());
        currentOpenSegment = retVal;
        return retVal;
      }
    } else {
      retVal =
          new SegmentIdWithShardSpec(dataSchema.getDataSource(),
              interval,
              tuningConfig.getVersioningPolicy().getVersion(interval),
              new LinearShardSpec(0));
      pushSegments(Lists.newArrayList(currentOpenSegment));
      LOG.info("Creating segment {}", retVal.toString());
      currentOpenSegment = retVal;
      return retVal;
    }
  }
This function computes the segment identifier and push the current open segment
The push will occur if max size is reached or the event belongs to the next interval.
Note that this function assumes that timestamps are pseudo sorted.
This function will close and move to the next segment granularity as soon as
an event from the next interval appears. The sorting is done by the previous stage.

@return segmentIdentifier with of the truncatedTime and maybe push the current open segment.
druid-handler/src/java/org/apache/hadoop/hive/druid/io/DruidRecordWriter.java