/** * Tests that a request is split into bunches of 500 and results are gathered in the correct way. * @throws JMSException */ @Test public void getManyTags() throws JMSException { Collection<ClientRequestResult> returnCollection = Arrays.asList(new TagConfigImpl(1), new TagConfigImpl(1)); EasyMock.expect(jmsProxy.sendRequest(EasyMock.isA(JsonRequest.class), EasyMock.eq("c2mon.client.request"), EasyMock.eq(10000), //10000 is timeout (ClientRequestReportListener) EasyMock.isNull() )).andReturn(returnCollection).times(20); EasyMock.replay(jmsProxy); LongRange range = new LongRange(1, 10000); long[] arrayRange = range.toArray(); Collection<Long> ids = Arrays.asList(ArrayUtils.toObject(arrayRange)); Collection result = requestHandlerImpl.requestTags(ids); Assert.assertEquals(40,result.size()); //each request for 500 tags returns 2 objects (faked list back) EasyMock.verify(jmsProxy); }
public static TimeSpan fromPropertyString(String pString) { String[] split = pString.split(","); TimeSpan t = new TimeSpan(); try { long start = Long.parseLong(split[0]); long end = Long.parseLong(split[1]); boolean daily = Boolean.parseBoolean(split[2]); int dir = Integer.parseInt(split[3]); t.init(new LongRange(start, end), daily); switch (dir) { case 0: t.setDirection(DIRECTION.SEND); break; case 1: t.setDirection(DIRECTION.ARRIVE); break; } } catch (Exception e) { return null; } return t; }
/** * Check if a movement with the provided runtime is possible for this * AttackFitter * * @param pRuntime Runtime to check * @param pVillage Village for which the runtime is valid * @return boolean TRUE=Runtime might be fitted if not all send times are * already used */ public boolean isMovementPossible(long pRuntime) { if (startRanges == null) { startRanges = startTimespansToRanges(); } if (arriveRanges == null) { arriveRanges = arriveTimespansToRanges(); } for (LongRange currentStartRange : startRanges) { LongRange arriveRangeForStartRange = new LongRange(currentStartRange.getMinimumLong() + pRuntime, currentStartRange.getMaximumLong() + pRuntime); for (LongRange currentArriveRange : arriveRanges) { if (currentArriveRange.overlapsRange(arriveRangeForStartRange)) { //movement with 'pRuntime' starting in 'currentStartRange' will arrive withing 'currentArriveRange' return true; } } } //no overlapping range was found return false; }
/** * Testing for verifying the intersection is computed correctly. */ @Test public void testIntersection() { // [2, 7] intersection [0, 1] = null final LongRange intersection1 = LongRangeUtils.getIntersection(ORIGINAL, OUTSIDE); // The result should be null assertNull(intersection1); // [2, 7] intersection [2, 3] = [2, 3] final LongRange intersection2 = LongRangeUtils.getIntersection(ORIGINAL, SAME_EDGE_INSIDE); assertEquals(SAME_EDGE_INSIDE, intersection2); // [2, 7] intersection [1, 2] = [2, 2] final LongRange intersection3 = LongRangeUtils.getIntersection(ORIGINAL, SAME_EDGE_OUTSIDE); assertEquals(new LongRange(2, 2), intersection3); // [2, 7] intersection [4, 5] = [4, 5] final LongRange intersection4 = LongRangeUtils.getIntersection(ORIGINAL, INSIDE); assertEquals(new LongRange(4, 5), intersection4); }
@Test public void testIsTimeIncluded() throws ParseException { Date startTime = DATE_FORMATTER.parse("04.04.2012 16:00:00:000"); Date endTime = DATE_FORMATTER.parse("04.04.2012 19:15:00:000"); Date included = DATE_FORMATTER.parse("04.04.2012 19:00:00:000"); Date excluded_before = DATE_FORMATTER.parse("04.04.2012 15:59:59:999"); Date excluded_after = DATE_FORMATTER.parse("04.04.2012 19:15:00:001"); LongRange timeRange = new LongRange(startTime.getTime(), endTime.getTime()); calendar.addTimeRange(timeRange); Assert.assertEquals(true, calendar.isTimeIncluded(included.getTime())); Assert.assertEquals(false, calendar.isTimeIncluded(excluded_before.getTime())); Assert.assertEquals(false, calendar.isTimeIncluded(excluded_after.getTime())); }
@Test public void testAddRemoveExcludedDate() throws ParseException { Date startTime = DATE_FORMATTER.parse("04.04.2012 16:00:00:000"); Date endTime = DATE_FORMATTER.parse("04.04.2012 19:15:00:000"); LongRange timeRange_1 = new LongRange(startTime.getTime(), endTime.getTime()); LongRange timeRange_2 = new LongRange(startTime.getTime(), endTime.getTime()); calendar.addTimeRange(timeRange_2); calendar.addTimeRange(timeRange_1); Assert.assertEquals(2, calendar.getExcludedRanges().size()); Assert.assertEquals(timeRange_1, calendar.getExcludedRanges().get(1)); Assert.assertEquals(timeRange_2, calendar.getExcludedRanges().get(0)); calendar.removeExcludedDate(timeRange_1); Assert.assertEquals(1, calendar.getExcludedRanges().size()); Assert.assertEquals(timeRange_2, calendar.getExcludedRanges().get(0)); }
/** * Determine the next time (in milliseconds) that is 'included' by the * Calendar after the given time. */ public long getNextIncludedTime(long timeStamp) { long nextIncludedTime = timeStamp + ONE_MILLI; while (isTimeIncluded(nextIncludedTime)) { LongRange timeRange = findTimeRange(timeStamp); if (nextIncludedTime >= timeRange.getMinimumLong() && nextIncludedTime <= timeRange.getMaximumLong()) { nextIncludedTime = timeRange.getMaximumLong() + ONE_MILLI; } else if ((getBaseCalendar() != null) && (!getBaseCalendar().isTimeIncluded(nextIncludedTime))){ nextIncludedTime = getBaseCalendar().getNextIncludedTime(nextIncludedTime); } else { nextIncludedTime++; } } return nextIncludedTime; }
private LongRange getRange(HttpServletRequest request, MediaFile file) { // First, look for "Range" HTTP header. LongRange range = StringUtil.parseRange(request.getHeader("Range")); if (range != null) { return range; } // Second, look for "offsetSeconds" request parameter. String offsetSeconds = request.getParameter("offsetSeconds"); range = parseAndConvertOffsetSeconds(offsetSeconds, file); if (range != null) { return range; } return null; }
private LongRange parseAndConvertOffsetSeconds(String offsetSeconds, MediaFile file) { if (offsetSeconds == null) { return null; } try { Integer duration = file.getDurationSeconds(); Long fileSize = file.getFileSize(); if (duration == null || fileSize == null) { return null; } float offset = Float.parseFloat(offsetSeconds); // Convert from time offset to byte offset. long byteOffset = (long) (fileSize * (offset / duration)); return new LongRange(byteOffset, Long.MAX_VALUE); } catch (Exception x) { LOG.error("Failed to parse and convert time offset: " + offsetSeconds, x); return null; } }
/** * Parses the given string as a HTTP header byte range. See chapter 14.36.1 in RFC 2068 * for details. * <p/> * Only a subset of the allowed syntaxes are supported. Only ranges which specify first-byte-pos * are supported. The last-byte-pos is optional. * * @param range The range from the HTTP header, for instance "bytes=0-499" or "bytes=500-" * @return A range object (using inclusive values). If the last-byte-pos is not given, the end of * the returned range is {@link Long#MAX_VALUE}. The method returns <code>null</code> if the syntax * of the given range is not supported. */ public static LongRange parseRange(String range) { if (range == null) { return null; } Pattern pattern = Pattern.compile("bytes=(\\d+)-(\\d*)"); Matcher matcher = pattern.matcher(range); if (matcher.matches()) { String firstString = matcher.group(1); String lastString = StringUtils.trimToNull(matcher.group(2)); long first = Long.parseLong(firstString); long last = lastString == null ? Long.MAX_VALUE : Long.parseLong(lastString); if (first > last) { return null; } return new LongRange(first, last); } return null; }
/** * Determine the next time (in milliseconds) that is 'included' by the * Calendar after the given time. */ @Override public long getNextIncludedTime(long timeStamp) { long nextIncludedTime = timeStamp + ONE_MILLI; while (isTimeIncluded(nextIncludedTime)) { LongRange timeRange = findTimeRange(timeStamp); if (nextIncludedTime >= timeRange.getMinimumLong() && nextIncludedTime <= timeRange.getMaximumLong()) { nextIncludedTime = timeRange.getMaximumLong() + ONE_MILLI; } else if ((getBaseCalendar() != null) && (!getBaseCalendar().isTimeIncluded(nextIncludedTime))) { nextIncludedTime = getBaseCalendar().getNextIncludedTime(nextIncludedTime); } else { nextIncludedTime++; } } return nextIncludedTime; }
private List<String> getInterval(LongRange i) { final Properties p = new Properties(); p.setProperty("start", Long.toString(i.getMinimumLong())); //$NON-NLS-1$ p.setProperty("end", Long.toString(i.getMaximumLong())); //$NON-NLS-1$ try { return request.doGet("getConnectionHistory", p); //$NON-NLS-1$ } // FIXME: review error message catch (IOException e) { e.printStackTrace(); } return null; }
@Override public LongRange getStartRange() { if (this.start == null) { GetApplicationsRequestProtoOrBuilder p = viaProto ? proto: builder; if (p.hasStartBegin() || p.hasStartEnd()) { long begin = p.hasStartBegin() ? p.getStartBegin() : 0L; long end = p.hasStartEnd() ? p.getStartEnd() : Long.MAX_VALUE; this.start = new LongRange(begin, end); } } return this.start; }
@Override public void setStartRange(long begin, long end) throws IllegalArgumentException { if (begin > end) { throw new IllegalArgumentException("begin > end in range (begin, " + "end): (" + begin + ", " + end + ")"); } this.start = new LongRange(begin, end); }
@Override public LongRange getFinishRange() { if (this.finish == null) { GetApplicationsRequestProtoOrBuilder p = viaProto ? proto: builder; if (p.hasFinishBegin() || p.hasFinishEnd()) { long begin = p.hasFinishBegin() ? p.getFinishBegin() : 0L; long end = p.hasFinishEnd() ? p.getFinishEnd() : Long.MAX_VALUE; this.finish = new LongRange(begin, end); } } return this.finish; }
@Override public void setFinishRange(long begin, long end) { if (begin > end) { throw new IllegalArgumentException("begin > end in range (begin, " + "end): (" + begin + ", " + end + ")"); } this.finish = new LongRange(begin, end); }
/** * <p> * The request from clients to get a report of Applications matching the * giving application types in the cluster from the * <code>ResourceManager</code>. * </p> * * @see ApplicationClientProtocol#getApplications(GetApplicationsRequest) * * <p>Setting any of the parameters to null, would just disable that * filter</p> * * @param scope {@link ApplicationsRequestScope} to filter by * @param users list of users to filter by * @param queues list of scheduler queues to filter by * @param applicationTypes types of applications * @param applicationTags application tags to filter by * @param applicationStates application states to filter by * @param startRange range of application start times to filter by * @param finishRange range of application finish times to filter by * @param limit number of applications to limit to * @return {@link GetApplicationsRequest} to be used with * {@link ApplicationClientProtocol#getApplications(GetApplicationsRequest)} */ @Public @Stable public static GetApplicationsRequest newInstance( ApplicationsRequestScope scope, Set<String> users, Set<String> queues, Set<String> applicationTypes, Set<String> applicationTags, EnumSet<YarnApplicationState> applicationStates, LongRange startRange, LongRange finishRange, Long limit) { GetApplicationsRequest request = Records.newRecord(GetApplicationsRequest.class); if (scope != null) { request.setScope(scope); } request.setUsers(users); request.setQueues(queues); request.setApplicationTypes(applicationTypes); request.setApplicationTags(applicationTags); request.setApplicationStates(applicationStates); if (startRange != null) { request.setStartRange( startRange.getMinimumLong(), startRange.getMaximumLong()); } if (finishRange != null) { request.setFinishRange( finishRange.getMinimumLong(), finishRange.getMaximumLong()); } if (limit != null) { request.setLimit(limit); } return request; }
/** * Given that we start reading after lastReadId and need to get * ROW_FETCH_SIZE rows from the audit log, figure out the min and max row * IDs to read. * * @returns a range of ID's to read from the audit log table based on the fetch size * @throws SQLException if there is an error reading from the DB */ private LongRange getIdsToRead() throws SQLException { String queryFormatString = "SELECT MIN(id) min_id, MAX(id) max_id " + "FROM (SELECT id FROM %s WHERE id > %s " + "AND (command_type IS NULL OR command_type NOT IN('SHOWTABLES', 'SHOWPARTITIONS', " + "'SWITCHDATABASE')) " + "ORDER BY id " + "LIMIT %s)" + " subquery " // Get read locks on the specified rows to prevent skipping of rows that haven't committed // yet, but have an id that matches the where clause. // For example, one transaction starts and // inserts id = 1, but another transaction starts, inserts, and commits i = 2 before the // first transaction commits. Locking can also be done with serializable isolation level. + "LOCK IN SHARE MODE"; String query = String.format(queryFormatString, auditLogTableName, lastReadId, ROW_FETCH_SIZE); Connection connection = dbConnectionFactory.getConnection(); PreparedStatement ps = connection.prepareStatement(query); LOG.debug("Executing: " + query); ResultSet rs = ps.executeQuery(); if (rs.next()) { long minId = rs.getLong("min_id"); long maxId = rs.getLong("max_id"); return new LongRange(minId, maxId); } return new LongRange(0, 0); }