List of usage examples for java.lang Float floatValue
@HotSpotIntrinsicCandidate public float floatValue()
From source file:org.etudes.mneme.impl.SubmissionStorageSql.java
/** * {@inheritDoc}// w w w. j a v a2 s .c om */ public Float getSubmissionHighestScore(Assessment assessment, String userId) { // TODO: pre-compute into MNEME_SUBMISSION.TOTAL_SCORE? -ggolden StringBuilder sql = new StringBuilder(); sql.append("SELECT S.ID, S.EVAL_SCORE, SUM(A.EVAL_SCORE), SUM(A.AUTO_SCORE) FROM MNEME_SUBMISSION S"); sql.append(" JOIN MNEME_ANSWER A ON S.ID=A.SUBMISSION_ID"); sql.append(" WHERE S.ASSESSMENT_ID=? AND S.USERID=? AND S.COMPLETE='1' AND S.RELEASED='1'"); // TODO: the MNEME_SUBMISSION_IDX_AUC index should work here, then only needing to test released - if not, we can read it and filter it out here -ggolden sql.append(" GROUP BY S.ID, S.EVAL_SCORE"); Object[] fields = new Object[2]; fields[0] = Long.valueOf(assessment.getId()); fields[1] = userId; final Map<String, Float> scores = new HashMap<String, Float>(); this.sqlService.dbRead(sql.toString(), fields, new SqlReader() { public Object readSqlResultRecord(ResultSet result) { try { String sid = SqlHelper.readId(result, 1); Float sEval = SqlHelper.readFloat(result, 2); Float aEval = SqlHelper.readFloat(result, 3); Float aAuto = SqlHelper.readFloat(result, 4); Float total = Float.valueOf( (sEval == null ? 0f : sEval.floatValue()) + (aEval == null ? 0f : aEval.floatValue()) + (aAuto == null ? 0f : aAuto.floatValue())); // massage total - 2 decimal places if (total != null) { total = Float.valueOf(((float) Math.round(total.floatValue() * 100.0f)) / 100.0f); } scores.put(sid, total); return null; } catch (SQLException e) { M_log.warn("getSubmissionHighestScore: " + e); return null; } } }); // find the submission with the highest score String highestId = null; Float highestTotal = null; for (Map.Entry entry : scores.entrySet()) { String sid = (String) entry.getKey(); Float total = (Float) entry.getValue(); if (highestTotal == null) { highestId = sid; highestTotal = total; } else if (total.floatValue() > highestTotal.floatValue()) { highestId = sid; highestTotal = total; } } return highestTotal; }
From source file:de.ingrid.ibus.comm.Bus.java
private IngridHits normalizeScores(List<IngridHits> resultSet, boolean skipNormalization) { if (fLogger.isDebugEnabled()) { fLogger.debug("normalize the results"); }//from w w w . jav a2 s . c o m int totalHits = 0; int count = resultSet.size(); List<IngridHit> documents = new LinkedList<IngridHit>(); for (int i = 0; i < count; i++) { float maxScore = 1.0f; IngridHits hitContainer = resultSet.get(i); totalHits += hitContainer.length(); if (hitContainer.getHits().length > 0) { Float boost = this.fRegistry.getGlobalRankingBoost(hitContainer.getPlugId()); IngridHit[] resultHits = hitContainer.getHits(); if (null != boost) { for (int j = 0; j < resultHits.length; j++) { float score = 1.0f; if (hitContainer.isRanked()) { score = resultHits[j].getScore(); score = score * boost.floatValue(); } resultHits[j].setScore(score); } } // normalize scores of the results of this iPlug // so maxScore will never get bigger than 1 now! if (!skipNormalization && maxScore < resultHits[0].getScore()) { normalizeHits(hitContainer, resultHits[0].getScore()); } } IngridHit[] toAddHits = hitContainer.getHits(); if (toAddHits != null) { documents.addAll(Arrays.asList(toAddHits)); } } IngridHits result = new IngridHits(totalHits, sortHits((IngridHit[]) documents.toArray(new IngridHit[documents.size()]))); // add timings for the corresponding iplugs HashMap<String, Long> timings = new HashMap<String, Long>(); for (IngridHits hits : resultSet) { timings.putAll(hits.getSearchTimings()); } result.setSearchTimings(timings); documents.clear(); documents = null; return result; }
From source file:org.apache.flex.forks.velocity.runtime.configuration.Configuration.java
/** * Get a float associated with the given configuration key. * * @param key The configuration key.// w w w . j a v a2 s. c om * @return The associated float. * @exception NoSuchElementException is thrown if the key doesn't * map to an existing object. * @exception ClassCastException is thrown if the key maps to an * object that is not a Float. * @exception NumberFormatException is thrown if the value mapped * by the key has not a valid number format. */ public float getFloat(String key) { Float f = getFloat(key, null); if (f != null) { return f.floatValue(); } else { throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object"); } }
From source file:it.sauronsoftware.jave.Encoder.java
/** * Re-encode a multimedia file.//from w w w . java2s . c o m * * @param multimediaObject The source multimedia file. It cannot be null. Be sure this * file can be decoded (see {@link Encoder#getSupportedDecodingFormats()}, * {@link Encoder#getAudioDecoders()} and * {@link Encoder#getVideoDecoders()}). * @param target The target multimedia re-encoded file. It cannot be null. * If this file already exists, it will be overwrited. * @param attributes A set of attributes for the encoding process. * @param listener An optional progress listener for the encoding process. * It can be null. * @throws IllegalArgumentException If both audio and video parameters are * null. * @throws InputFormatException If the source multimedia file cannot be * decoded. * @throws EncoderException If a problems occurs during the encoding * process. */ public void encode(MultimediaObject multimediaObject, File target, EncodingAttributes attributes, EncoderProgressListener listener) throws IllegalArgumentException, InputFormatException, EncoderException { String formatAttribute = attributes.getFormat(); Float offsetAttribute = attributes.getOffset(); Float durationAttribute = attributes.getDuration(); AudioAttributes audioAttributes = attributes.getAudioAttributes(); VideoAttributes videoAttributes = attributes.getVideoAttributes(); if (audioAttributes == null && videoAttributes == null) { throw new IllegalArgumentException("Both audio and video attributes are null"); } target = target.getAbsoluteFile(); target.getParentFile().mkdirs(); FFMPEGExecutor ffmpeg = locator.createExecutor(); if (offsetAttribute != null) { ffmpeg.addArgument("-ss"); ffmpeg.addArgument(String.valueOf(offsetAttribute.floatValue())); } ffmpeg.addArgument("-i"); ffmpeg.addArgument(multimediaObject.getFile().getAbsolutePath()); if (durationAttribute != null) { ffmpeg.addArgument("-t"); ffmpeg.addArgument(String.valueOf(durationAttribute.floatValue())); } if (videoAttributes == null) { ffmpeg.addArgument("-vn"); } else { String codec = videoAttributes.getCodec(); if (codec != null) { ffmpeg.addArgument("-vcodec"); ffmpeg.addArgument(codec); } String tag = videoAttributes.getTag(); if (tag != null) { ffmpeg.addArgument("-vtag"); ffmpeg.addArgument(tag); } Integer bitRate = videoAttributes.getBitRate(); if (bitRate != null) { ffmpeg.addArgument("-vb"); ffmpeg.addArgument(String.valueOf(bitRate.intValue())); } Integer frameRate = videoAttributes.getFrameRate(); if (frameRate != null) { ffmpeg.addArgument("-r"); ffmpeg.addArgument(String.valueOf(frameRate.intValue())); } VideoSize size = videoAttributes.getSize(); if (size != null) { ffmpeg.addArgument("-s"); ffmpeg.addArgument(String.valueOf(size.getWidth()) + "x" + String.valueOf(size.getHeight())); } if (videoAttributes.isFaststart()) { ffmpeg.addArgument("-movflags"); ffmpeg.addArgument("faststart"); } if (videoAttributes.getX264Profile() != null) { ffmpeg.addArgument("-profile:v"); ffmpeg.addArgument(videoAttributes.getX264Profile().getModeName()); } if (videoAttributes.getVideoFilters().size() > 0) { for (VideoFilter videoFilter : videoAttributes.getVideoFilters()) { ffmpeg.addArgument("-vf"); ffmpeg.addArgument(videoFilter.getExpression()); } } } if (audioAttributes == null) { ffmpeg.addArgument("-an"); } else { String codec = audioAttributes.getCodec(); if (codec != null) { if (codec.equals("aac")) { codec = "libvo_aacenc"; } ffmpeg.addArgument("-acodec"); ffmpeg.addArgument(codec); } Integer bitRate = audioAttributes.getBitRate(); if (bitRate != null) { ffmpeg.addArgument("-ab"); ffmpeg.addArgument(String.valueOf(bitRate.intValue())); } Integer channels = audioAttributes.getChannels(); if (channels != null) { ffmpeg.addArgument("-ac"); ffmpeg.addArgument(String.valueOf(channels.intValue())); } Integer samplingRate = audioAttributes.getSamplingRate(); if (samplingRate != null) { ffmpeg.addArgument("-ar"); ffmpeg.addArgument(String.valueOf(samplingRate.intValue())); } Integer volume = audioAttributes.getVolume(); if (volume != null) { ffmpeg.addArgument("-vol"); ffmpeg.addArgument(String.valueOf(volume.intValue())); } } if (formatAttribute != null) { ffmpeg.addArgument("-f"); ffmpeg.addArgument(formatAttribute); } ffmpeg.addArgument("-y"); ffmpeg.addArgument(target.getAbsolutePath()); try { ffmpeg.execute(); } catch (IOException e) { throw new EncoderException(e); } try { String lastWarning = null; long duration; long progress = 0; RBufferedReader reader = new RBufferedReader(new InputStreamReader(ffmpeg.getErrorStream())); MultimediaInfo info = multimediaObject.getInfo(); if (durationAttribute != null) { duration = (long) Math.round((durationAttribute * 1000L)); } else { duration = info.getDuration(); if (offsetAttribute != null) { duration -= (long) Math.round((offsetAttribute * 1000L)); } } if (listener != null) { listener.sourceInfo(info); } int step = 0; int lineNR = 0; String line; while ((line = reader.readLine()) != null) { lineNR++; if (_log.isDebugEnabled()) { _log.debug("Input Line (" + lineNR + "): " + line); } if (step == 0) { if (line.startsWith("WARNING: ")) { if (listener != null) { listener.message(line); } } else if (!line.startsWith("Output #0")) { // throw new EncoderException(line); } else { step++; } } if (step == 1) { if (line.startsWith("WARNING: ")) { if (listener != null) { listener.message(line); } } else if (!line.startsWith("Output #0")) { // throw new EncoderException(line); } else { step++; } } else if (step == 2) { if (!line.startsWith(" ")) { step++; } } if (step == 3) { if (!line.startsWith("Stream mapping:")) { throw new EncoderException(line); } else { step++; } } else if (step == 4) { if (!line.startsWith(" ")) { step++; } } if (line.startsWith("frame=")) { try { line = line.trim(); if (line.length() > 0) { HashMap<String, String> table = parseProgressInfoLine(line); if (table == null) { if (listener != null) { listener.message(line); } lastWarning = line; } else { if (listener != null) { String time = table.get("time"); if (time != null) { String dParts[] = time.split(":"); // HH:MM:SS.xx Double seconds = Double.parseDouble(dParts[dParts.length - 1]); if (dParts.length > 1) { seconds += Double.parseDouble(dParts[dParts.length - 2]) * 60; if (dParts.length > 2) { seconds += Double.parseDouble(dParts[dParts.length - 3]) * 60 * 60; } } int perm = (int) Math.round((seconds * 1000L * 1000L) / (double) duration); if (perm > 1000) { perm = 1000; } listener.progress(perm); } } lastWarning = null; } } } catch (Exception ex) { _log.warn("Error in progress parsing for line: " + line); } } } if (lastWarning != null) { if (!SUCCESS_PATTERN.matcher(lastWarning).matches()) { throw new EncoderException("No match for: " + SUCCESS_PATTERN + " in " + lastWarning); } } } catch (IOException e) { throw new EncoderException(e); } finally { ffmpeg.destroy(); } }
From source file:edu.ku.brc.specify.config.Scriptlet.java
/** * Formats a Float to a string with "N","S","E", "W". * @param floatVal the Float value/*from w w w .j av a 2 s.c o m*/ * @param isLat whether it is a lat or lon * @return Formats a float to a string with "N","S","E", "W" */ public String getDirChar(final Float floatVal, final boolean isLat) { if (floatVal == null) { return ""; } String key; if (isLat) { key = floatVal.floatValue() > 0.0 ? SCRPLT_N : SCRPLT_S; } else { key = floatVal.floatValue() > 0.0 ? SCRPLT_E : SCRPLT_W; } return UIRegistry.getResourceString(key); }
From source file:edu.ku.brc.specify.config.Scriptlet.java
/** * Formats a float to a string.//from w ww. j a v a 2s . c om * @param floatVar the float variable * @return Formats a float to a string * @throws JRScriptletException */ public String format(Float floatVar) throws JRScriptletException { if (floatVar == null) { return ""; } DecimalFormat df = new DecimalFormat("#.####"); return df.format(floatVar.floatValue()); }
From source file:com.appcel.facade.encoder.service.impl.EncoderServiceImpl.java
/** * Re-encode a multimedia file.//from ww w. j a va 2 s .c o m * * @param source * The source multimedia file. It cannot be null. Be sure this * file can be decoded (see * {@link EncoderServiceImpl#getSupportedDecodingFormats()}, * {@link EncoderServiceImpl#getAudioDecoders()} and * {@link EncoderServiceImpl#getVideoDecoders()}). * @param target * The target multimedia re-encoded file. It cannot be null. If * this file already exists, it will be overwrited. * @param attributes * A set of attributes for the encoding process. * @param listener * An optional progress listener for the encoding process. It can * be null. * @throws IllegalArgumentException * If both audio and video parameters are null. * @throws InputFormatException * If the source multimedia file cannot be decoded. * @throws EncoderException * If a problems occurs during the encoding process. */ public void encode(File source, File target, EncodingAttributes attributes, EncoderProgressListener listener) throws IllegalArgumentException, InputFormatException, EncoderException { String formatAttribute = attributes.getFormat(); Float offsetAttribute = attributes.getOffset(); Float durationAttribute = attributes.getDuration(); AudioAttributes audioAttributes = attributes.getAudioAttributes(); VideoAttributes videoAttributes = attributes.getVideoAttributes(); if (audioAttributes == null && videoAttributes == null) { throw new IllegalArgumentException("Both audio and video attributes are null"); } target = target.getAbsoluteFile(); target.getParentFile().mkdirs(); if (offsetAttribute != null) { encoderExcutor.addArgument("-ss"); encoderExcutor.addArgument(String.valueOf(offsetAttribute.floatValue())); } encoderExcutor.addArgument("-i"); encoderExcutor.addArgument(source.getAbsolutePath()); if (durationAttribute != null) { encoderExcutor.addArgument("-t"); encoderExcutor.addArgument(String.valueOf(durationAttribute.floatValue())); } if (videoAttributes == null) { encoderExcutor.addArgument("-vn"); } else { String codec = videoAttributes.getCodec(); if (codec != null) { encoderExcutor.addArgument("-vcodec"); encoderExcutor.addArgument(codec); } String tag = videoAttributes.getTag(); if (tag != null) { encoderExcutor.addArgument("-vtag"); encoderExcutor.addArgument(tag); } Integer bitRate = videoAttributes.getBitRate(); if (bitRate != null) { encoderExcutor.addArgument("-b"); encoderExcutor.addArgument(String.valueOf(bitRate.intValue())); } Integer frameRate = videoAttributes.getFrameRate(); if (frameRate != null) { encoderExcutor.addArgument("-r"); encoderExcutor.addArgument(String.valueOf(frameRate.intValue())); } VideoSizeDTO size = videoAttributes.getSize(); if (size != null) { encoderExcutor.addArgument("-s"); encoderExcutor .addArgument(String.valueOf(size.getWidth()) + "x" + String.valueOf(size.getHeight())); } } if (audioAttributes == null) { encoderExcutor.addArgument("-an"); } else { String codec = audioAttributes.getCodec(); if (codec != null) { encoderExcutor.addArgument("-acodec"); encoderExcutor.addArgument(codec); } Integer bitRate = audioAttributes.getBitRate(); if (bitRate != null) { encoderExcutor.addArgument("-ab"); encoderExcutor.addArgument(String.valueOf(bitRate.intValue())); } Integer channels = audioAttributes.getChannels(); if (channels != null) { encoderExcutor.addArgument("-ac"); encoderExcutor.addArgument(String.valueOf(channels.intValue())); } Integer samplingRate = audioAttributes.getSamplingRate(); if (samplingRate != null) { encoderExcutor.addArgument("-ar"); encoderExcutor.addArgument(String.valueOf(samplingRate.intValue())); } Integer volume = audioAttributes.getVolume(); if (volume != null) { encoderExcutor.addArgument("-vol"); encoderExcutor.addArgument(String.valueOf(volume.intValue())); } } encoderExcutor.addArgument("-f"); encoderExcutor.addArgument(formatAttribute); encoderExcutor.addArgument("-y"); encoderExcutor.addArgument(target.getAbsolutePath()); try { encoderExcutor.execute(); } catch (EncoderException e) { throw new EncoderException(e); } try { String lastWarning = null; long duration; long progress = 0; EncoderBufferedReader reader = null; reader = new EncoderBufferedReader(new InputStreamReader(encoderExcutor.getErrorStream())); MultimediaInfo info = parseMultimediaInfo(source, reader); if (durationAttribute != null) { duration = (long) Math.round((durationAttribute.floatValue() * 1000L)); } else { duration = info.getDuration(); if (offsetAttribute != null) { duration -= (long) Math.round((offsetAttribute.floatValue() * 1000L)); } } if (listener != null) { listener.sourceInfo(info); } int step = 0; String line; while ((line = reader.readLine()) != null) { if (step == 0) { if (line.startsWith("WARNING: ")) { if (listener != null) { listener.message(line); } } else if (!line.startsWith("Output #0")) { throw new EncoderException(line); } else { step++; } } else if (step == 1) { if (!line.startsWith(" ")) { step++; } } if (step == 2) { if (!line.startsWith("Stream mapping:")) { throw new EncoderException(line); } else { step++; } } else if (step == 3) { if (!line.startsWith(" ")) { step++; } } if (step == 4) { line = line.trim(); if (line.length() > 0) { Map<String, String> table = parseProgressInfoLine(line); if (table == null) { if (listener != null) { listener.message(line); } lastWarning = line; } else { if (listener != null) { String time = (String) table.get("time"); if (time != null) { int dot = time.indexOf('.'); if (dot > 0 && dot == time.length() - 2 && duration > 0) { String p1 = time.substring(0, dot); String p2 = time.substring(dot + 1); try { long i1 = Long.parseLong(p1); long i2 = Long.parseLong(p2); progress = (i1 * 1000L) + (i2 * 100L); int perm = (int) Math .round((double) (progress * 1000L) / (double) duration); if (perm > 1000) { perm = 1000; } listener.progress(perm); } catch (NumberFormatException e) { ; } } } } lastWarning = null; } } } } if (lastWarning != null) { if (!SUCCESS_PATTERN.matcher(lastWarning).matches()) { throw new EncoderException(lastWarning); } } } catch (IOException e) { throw new EncoderException(e); } finally { encoderExcutor.destroy(); } }
From source file:com.mods.grx.settings.GrxSettingsActivity.java
private String do_restore(String arch) { int contador = 0; boolean error = false; String serror = ""; ObjectInputStream ois = null; FileInputStream fis;//from ww w . j av a 2 s. c om SharedPreferences sp = Common.sp; File f = new File(Common.BackupsDir + File.separator + arch + "." + getString(R.string.gs_backup_ext)); try { fis = new FileInputStream(f); ois = new ObjectInputStream(fis); } catch (Exception e) { serror = e.toString(); error = true; } if (!error) { sp.edit().clear().commit(); try { Map map = (Map) ois.readObject(); Set set = map.entrySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { contador++; Map.Entry entrada = (Map.Entry) iterator.next(); String clave = (String) entrada.getKey(); if (entrada.getValue() instanceof Boolean) { Boolean b = (Boolean) entrada.getValue(); sp.edit().putBoolean(clave, b.booleanValue()).commit(); } else if (entrada.getValue() instanceof Float) { Float flo = (Float) entrada.getValue(); sp.edit().putFloat(clave, flo.floatValue()).commit(); } else if (entrada.getValue() instanceof Integer) { Integer ent = (Integer) entrada.getValue(); sp.edit().putInt(clave, ent.intValue()).commit(); } else if (entrada.getValue() instanceof Long) { Long lo = (Long) entrada.getValue(); sp.edit().putLong(clave, lo.longValue()).commit(); } else if (entrada.getValue() instanceof String) { String str = (String) entrada.getValue(); sp.edit().putString(clave, str).commit(); } else if (entrada.getValue() instanceof Set) { Set s = (Set) entrada.getValue(); sp.edit().putStringSet(clave, s).commit(); } } } catch (Exception e) { serror = e.toString(); error = true; } } if (ois != null) try { ois.close(); } catch (IOException e) { e.printStackTrace(); } String ret; if (error) ret = "ERROR: " + serror; else ret = getString(R.string.gs_mens_resultado_restaurar, arch + "." + getString(R.string.gs_backup_ext), contador); if (!error) { if (!error) { String ori_icon_folder = Common.BackupsDir + File.separator + arch + File.separator + getString(R.string.grx_ico_sub_dir) + File.separator; String dest_icon_folder = Common.IconsDir + File.separator; Utils.delete_files_or_create_folder(dest_icon_folder, ".png"); Utils.copy_files(ori_icon_folder, dest_icon_folder, ".png"); Utils.fix_foler_permissions(dest_icon_folder, ".png"); Utils.delete_files_or_create_folder(dest_icon_folder, ".jpg"); Utils.copy_files(ori_icon_folder, dest_icon_folder, ".jpg"); Utils.fix_foler_permissions(dest_icon_folder, ".jpg"); } } return ret; }
From source file:org.apache.hadoop.hbase.master.RegionPlacementMaintainer.java
/** * Generate the assignment plan for the existing table * * @param tableName/*from ww w . j av a 2s. c om*/ * @param assignmentSnapshot * @param regionLocalityMap * @param plan * @param munkresForSecondaryAndTertiary if set on true the assignment plan * for the tertiary and secondary will be generated with Munkres algorithm, * otherwise will be generated using placeSecondaryAndTertiaryRS * @throws IOException */ private void genAssignmentPlan(TableName tableName, SnapshotOfRegionAssignmentFromMeta assignmentSnapshot, Map<String, Map<String, Float>> regionLocalityMap, FavoredNodesPlan plan, boolean munkresForSecondaryAndTertiary) throws IOException { // Get the all the regions for the current table List<HRegionInfo> regions = assignmentSnapshot.getTableToRegionMap().get(tableName); int numRegions = regions.size(); // Get the current assignment map Map<HRegionInfo, ServerName> currentAssignmentMap = assignmentSnapshot.getRegionToRegionServerMap(); // Get the all the region servers List<ServerName> servers = new ArrayList<ServerName>(); servers.addAll(getHBaseAdmin().getClusterStatus().getServers()); LOG.info("Start to generate assignment plan for " + numRegions + " regions from table " + tableName + " with " + servers.size() + " region servers"); int slotsPerServer = (int) Math.ceil((float) numRegions / servers.size()); int regionSlots = slotsPerServer * servers.size(); // Compute the primary, secondary and tertiary costs for each region/server // pair. These costs are based only on node locality and rack locality, and // will be modified later. float[][] primaryCost = new float[numRegions][regionSlots]; float[][] secondaryCost = new float[numRegions][regionSlots]; float[][] tertiaryCost = new float[numRegions][regionSlots]; if (this.enforceLocality && regionLocalityMap != null) { // Transform the locality mapping into a 2D array, assuming that any // unspecified locality value is 0. float[][] localityPerServer = new float[numRegions][regionSlots]; for (int i = 0; i < numRegions; i++) { Map<String, Float> serverLocalityMap = regionLocalityMap.get(regions.get(i).getEncodedName()); if (serverLocalityMap == null) { continue; } for (int j = 0; j < servers.size(); j++) { String serverName = servers.get(j).getHostname(); if (serverName == null) { continue; } Float locality = serverLocalityMap.get(serverName); if (locality == null) { continue; } for (int k = 0; k < slotsPerServer; k++) { // If we can't find the locality of a region to a server, which occurs // because locality is only reported for servers which have some // blocks of a region local, then the locality for that pair is 0. localityPerServer[i][j * slotsPerServer + k] = locality.floatValue(); } } } // Compute the total rack locality for each region in each rack. The total // rack locality is the sum of the localities of a region on all servers in // a rack. Map<String, Map<HRegionInfo, Float>> rackRegionLocality = new HashMap<String, Map<HRegionInfo, Float>>(); for (int i = 0; i < numRegions; i++) { HRegionInfo region = regions.get(i); for (int j = 0; j < regionSlots; j += slotsPerServer) { String rack = rackManager.getRack(servers.get(j / slotsPerServer)); Map<HRegionInfo, Float> rackLocality = rackRegionLocality.get(rack); if (rackLocality == null) { rackLocality = new HashMap<HRegionInfo, Float>(); rackRegionLocality.put(rack, rackLocality); } Float localityObj = rackLocality.get(region); float locality = localityObj == null ? 0 : localityObj.floatValue(); locality += localityPerServer[i][j]; rackLocality.put(region, locality); } } for (int i = 0; i < numRegions; i++) { for (int j = 0; j < regionSlots; j++) { String rack = rackManager.getRack(servers.get(j / slotsPerServer)); Float totalRackLocalityObj = rackRegionLocality.get(rack).get(regions.get(i)); float totalRackLocality = totalRackLocalityObj == null ? 0 : totalRackLocalityObj.floatValue(); // Primary cost aims to favor servers with high node locality and low // rack locality, so that secondaries and tertiaries can be chosen for // nodes with high rack locality. This might give primaries with // slightly less locality at first compared to a cost which only // considers the node locality, but should be better in the long run. primaryCost[i][j] = 1 - (2 * localityPerServer[i][j] - totalRackLocality); // Secondary cost aims to favor servers with high node locality and high // rack locality since the tertiary will be chosen from the same rack as // the secondary. This could be negative, but that is okay. secondaryCost[i][j] = 2 - (localityPerServer[i][j] + totalRackLocality); // Tertiary cost is only concerned with the node locality. It will later // be restricted to only hosts on the same rack as the secondary. tertiaryCost[i][j] = 1 - localityPerServer[i][j]; } } } if (this.enforceMinAssignmentMove && currentAssignmentMap != null) { // We want to minimize the number of regions which move as the result of a // new assignment. Therefore, slightly penalize any placement which is for // a host that is not currently serving the region. for (int i = 0; i < numRegions; i++) { for (int j = 0; j < servers.size(); j++) { ServerName currentAddress = currentAssignmentMap.get(regions.get(i)); if (currentAddress != null && !currentAddress.equals(servers.get(j))) { for (int k = 0; k < slotsPerServer; k++) { primaryCost[i][j * slotsPerServer + k] += NOT_CURRENT_HOST_PENALTY; } } } } } // Artificially increase cost of last slot of each server to evenly // distribute the slop, otherwise there will be a few servers with too few // regions and many servers with the max number of regions. for (int i = 0; i < numRegions; i++) { for (int j = 0; j < regionSlots; j += slotsPerServer) { primaryCost[i][j] += LAST_SLOT_COST_PENALTY; secondaryCost[i][j] += LAST_SLOT_COST_PENALTY; tertiaryCost[i][j] += LAST_SLOT_COST_PENALTY; } } RandomizedMatrix randomizedMatrix = new RandomizedMatrix(numRegions, regionSlots); primaryCost = randomizedMatrix.transform(primaryCost); int[] primaryAssignment = new MunkresAssignment(primaryCost).solve(); primaryAssignment = randomizedMatrix.invertIndices(primaryAssignment); // Modify the secondary and tertiary costs for each region/server pair to // prevent a region from being assigned to the same rack for both primary // and either one of secondary or tertiary. for (int i = 0; i < numRegions; i++) { int slot = primaryAssignment[i]; String rack = rackManager.getRack(servers.get(slot / slotsPerServer)); for (int k = 0; k < servers.size(); k++) { if (!rackManager.getRack(servers.get(k)).equals(rack)) { continue; } if (k == slot / slotsPerServer) { // Same node, do not place secondary or tertiary here ever. for (int m = 0; m < slotsPerServer; m++) { secondaryCost[i][k * slotsPerServer + m] = MAX_COST; tertiaryCost[i][k * slotsPerServer + m] = MAX_COST; } } else { // Same rack, do not place secondary or tertiary here if possible. for (int m = 0; m < slotsPerServer; m++) { secondaryCost[i][k * slotsPerServer + m] = AVOID_COST; tertiaryCost[i][k * slotsPerServer + m] = AVOID_COST; } } } } if (munkresForSecondaryAndTertiary) { randomizedMatrix = new RandomizedMatrix(numRegions, regionSlots); secondaryCost = randomizedMatrix.transform(secondaryCost); int[] secondaryAssignment = new MunkresAssignment(secondaryCost).solve(); secondaryAssignment = randomizedMatrix.invertIndices(secondaryAssignment); // Modify the tertiary costs for each region/server pair to ensure that a // region is assigned to a tertiary server on the same rack as its secondary // server, but not the same server in that rack. for (int i = 0; i < numRegions; i++) { int slot = secondaryAssignment[i]; String rack = rackManager.getRack(servers.get(slot / slotsPerServer)); for (int k = 0; k < servers.size(); k++) { if (k == slot / slotsPerServer) { // Same node, do not place tertiary here ever. for (int m = 0; m < slotsPerServer; m++) { tertiaryCost[i][k * slotsPerServer + m] = MAX_COST; } } else { if (rackManager.getRack(servers.get(k)).equals(rack)) { continue; } // Different rack, do not place tertiary here if possible. for (int m = 0; m < slotsPerServer; m++) { tertiaryCost[i][k * slotsPerServer + m] = AVOID_COST; } } } } randomizedMatrix = new RandomizedMatrix(numRegions, regionSlots); tertiaryCost = randomizedMatrix.transform(tertiaryCost); int[] tertiaryAssignment = new MunkresAssignment(tertiaryCost).solve(); tertiaryAssignment = randomizedMatrix.invertIndices(tertiaryAssignment); for (int i = 0; i < numRegions; i++) { List<ServerName> favoredServers = new ArrayList<ServerName>( FavoredNodeAssignmentHelper.FAVORED_NODES_NUM); ServerName s = servers.get(primaryAssignment[i] / slotsPerServer); favoredServers.add(ServerName.valueOf(s.getHostname(), s.getPort(), ServerName.NON_STARTCODE)); s = servers.get(secondaryAssignment[i] / slotsPerServer); favoredServers.add(ServerName.valueOf(s.getHostname(), s.getPort(), ServerName.NON_STARTCODE)); s = servers.get(tertiaryAssignment[i] / slotsPerServer); favoredServers.add(ServerName.valueOf(s.getHostname(), s.getPort(), ServerName.NON_STARTCODE)); // Update the assignment plan plan.updateAssignmentPlan(regions.get(i), favoredServers); } LOG.info("Generated the assignment plan for " + numRegions + " regions from table " + tableName + " with " + servers.size() + " region servers"); LOG.info("Assignment plan for secondary and tertiary generated " + "using MunkresAssignment"); } else { Map<HRegionInfo, ServerName> primaryRSMap = new HashMap<HRegionInfo, ServerName>(); for (int i = 0; i < numRegions; i++) { primaryRSMap.put(regions.get(i), servers.get(primaryAssignment[i] / slotsPerServer)); } FavoredNodeAssignmentHelper favoredNodeHelper = new FavoredNodeAssignmentHelper(servers, conf); favoredNodeHelper.initialize(); Map<HRegionInfo, ServerName[]> secondaryAndTertiaryMap = favoredNodeHelper .placeSecondaryAndTertiaryWithRestrictions(primaryRSMap); for (int i = 0; i < numRegions; i++) { List<ServerName> favoredServers = new ArrayList<ServerName>( FavoredNodeAssignmentHelper.FAVORED_NODES_NUM); HRegionInfo currentRegion = regions.get(i); ServerName s = primaryRSMap.get(currentRegion); favoredServers.add(ServerName.valueOf(s.getHostname(), s.getPort(), ServerName.NON_STARTCODE)); ServerName[] secondaryAndTertiary = secondaryAndTertiaryMap.get(currentRegion); s = secondaryAndTertiary[0]; favoredServers.add(ServerName.valueOf(s.getHostname(), s.getPort(), ServerName.NON_STARTCODE)); s = secondaryAndTertiary[1]; favoredServers.add(ServerName.valueOf(s.getHostname(), s.getPort(), ServerName.NON_STARTCODE)); // Update the assignment plan plan.updateAssignmentPlan(regions.get(i), favoredServers); } LOG.info("Generated the assignment plan for " + numRegions + " regions from table " + tableName + " with " + servers.size() + " region servers"); LOG.info("Assignment plan for secondary and tertiary generated " + "using placeSecondaryAndTertiaryWithRestrictions method"); } }
From source file:org.etudes.mneme.impl.SubmissionImpl.java
/** * {@inheritDoc}/*from w w w . j a va 2 s .co m*/ */ public Float getAnswersAutoScore() { // count the answer auto scores float total = 0; for (Answer answer : this.answers) { Float auto = answer.getAutoScore(); if (auto != null) { total += auto.floatValue(); } } // round away bogus decimals total = Math.round(total * 100.0f) / 100.0f; return Float.valueOf(total); }