List of usage examples for java.util TreeMap entrySet
EntrySet entrySet
To view the source code for java.util TreeMap entrySet.
Click Source Link
From source file:org.lockss.config.TdbAu.java
void pprintSortedMap(String title, Map<String, String> map, PrintStream ps, int indent) { ps.println(StringUtil.tab(indent) + title); indent += 2;// w w w .java 2s. c o m TreeMap<String, String> sorted = new TreeMap<String, String>(map); for (Map.Entry<String, String> ent : sorted.entrySet()) { ps.println(StringUtil.tab(indent) + ent.getKey() + " = " + ent.getValue()); } }
From source file:org.outofbits.sesame.schemagen.SchemaGeneration.java
/** * Generates the body of the class that shall be created. At first the base namespace will be * detected. If the base namespace is given in the {@link VocabularyOptions}, this namespace * will be preferred. An {@link SchemaGenerationException} will be thrown, if the namespace * cannot be detected automatically.//from w ww .j a v a2s . com * * @param vocabModel {@link Model} that contains all statements of the vocabulary for which the * class shall be created. * @return the body of the class in form of a string. * @throws SchemaGenerationException if the body of the class cannot be generated. */ private String generateClassBody(Resource rootResource, Model vocabModel) throws StringMapFormatter.StringMapFormatterException, SchemaGenerationException { assert vocabModel != null; Map<String, String> classBodyValueMap = new HashMap<>(formattingMap); // Detect the base namespace String basenameSpace; if (vocabularyOptions.baseNamespace() != null) { basenameSpace = vocabularyOptions.baseNamespace(); } else { basenameSpace = detectBaseNamespace(vocabModel, rootResource); if (basenameSpace == null) { if (rootResource != null) { basenameSpace = rootResource.stringValue(); } else { throw new SchemaGenerationException( "The base namespace could not be detected. Please specify it manually."); } } } // Detect the preferred prefix String prefix = null; if (vocabularyOptions.preferredPrefix() != null) { prefix = vocabularyOptions.preferredPrefix(); } else { for (Namespace ns : vocabModel.getNamespaces()) { if (ns.getName().equals(basenameSpace)) { prefix = ns.getPrefix(); break; } } } // Declaration of resources Pattern nsPattern = Pattern.compile(String.format("%s(.+)", basenameSpace)); TreeMap<String, Resource> resourceMap = new TreeMap<>(); for (Resource subject : vocabModel.subjects()) { Matcher matcher = nsPattern.matcher(subject.stringValue()); if (matcher.find()) { resourceMap.put(JavaIdentifier.getJavaIdFrom(matcher.group(1)), subject); } } // Definition and declaration of resources. StringBuilder declarationStringBuilder = new StringBuilder(); StringBuilder definitionStringBuilder = new StringBuilder( stringMapFormatter.format(DEFAULT_VALUE_FACTORY_TEMPLATE, formattingMap)); for (Map.Entry<String, Resource> entry : resourceMap.entrySet()) { Resource subject = entry.getValue(); Map<String, String> valueMap = new HashMap<>(formattingMap); valueMap.put("resource-name", entry.getKey()); valueMap.put("resource-iri", subject.stringValue()); valueMap.put("resource-label", getFirstLiteralFor(vocabModel, subject, LABEL_PROPERTIES).orElse(entry.getKey())); valueMap.put("resource-comment", getFirstLiteralFor(vocabModel, subject, COMMENT_PROPERTIES).orElse("")); declarationStringBuilder.append(stringMapFormatter.format(DEFAULT_RESOURCE_DEC_TEMPLATE, valueMap)); definitionStringBuilder.append(stringMapFormatter.format(DEFAULT_RESOURCE_DEF_TEMPLATE, valueMap)); } classBodyValueMap.put("resource-declarations", declarationStringBuilder.toString()); classBodyValueMap.put("resource-definitions", definitionStringBuilder.toString()); // Namespace and prefix. Map<String, String> valueMap = new HashMap<>(formattingMap); valueMap.put("namespace-iri", basenameSpace); valueMap.put("prefix", prefix); classBodyValueMap.put("namespace", stringMapFormatter.format(DEFAULT_NAMESPACE_TEMPLATE, valueMap)); classBodyValueMap.put("prefix", prefix == null ? "" : stringMapFormatter.format(DEFAULT_PREFIX_TEMPLATE, valueMap)); return stringMapFormatter.format(DEFAULT_CLASS_BODY_TEMPLATE, classBodyValueMap); }
From source file:org.apache.catalina.manager.HTMLManagerServlet.java
/** * Render a HTML list of the currently active Contexts in our virtual host, * and memory and server status information. * * @param writer Writer to render to/* www . ja v a 2s. c o m*/ * @param message a message to display */ public void list(HttpServletRequest request, HttpServletResponse response, String message) throws IOException { if (debug >= 1) log("list: Listing contexts for virtual host '" + deployer.getName() + "'"); PrintWriter writer = response.getWriter(); // HTML Header Section writer.print(Constants.HTML_HEADER_SECTION); // Body Header Section Object[] args = new Object[2]; args[0] = request.getContextPath(); args[1] = sm.getString("htmlManagerServlet.title"); writer.print(MessageFormat.format(Constants.BODY_HEADER_SECTION, args)); // Message Section args = new Object[3]; args[0] = sm.getString("htmlManagerServlet.messageLabel"); args[1] = (message == null || message.length() == 0) ? "OK" : message; writer.print(MessageFormat.format(Constants.MESSAGE_SECTION, args)); // Manager Section args = new Object[9]; args[0] = sm.getString("htmlManagerServlet.manager"); args[1] = response.encodeURL(request.getContextPath() + "/html/list"); args[2] = sm.getString("htmlManagerServlet.list"); args[3] = response .encodeURL(request.getContextPath() + "/" + sm.getString("htmlManagerServlet.helpHtmlManagerFile")); args[4] = sm.getString("htmlManagerServlet.helpHtmlManager"); args[5] = response .encodeURL(request.getContextPath() + "/" + sm.getString("htmlManagerServlet.helpManagerFile")); args[6] = sm.getString("htmlManagerServlet.helpManager"); args[7] = response.encodeURL(request.getContextPath() + "/status"); args[8] = sm.getString("statusServlet.title"); writer.print(MessageFormat.format(Constants.MANAGER_SECTION, args)); // Apps Header Section args = new Object[6]; args[0] = sm.getString("htmlManagerServlet.appsTitle"); args[1] = sm.getString("htmlManagerServlet.appsPath"); args[2] = sm.getString("htmlManagerServlet.appsName"); args[3] = sm.getString("htmlManagerServlet.appsAvailable"); args[4] = sm.getString("htmlManagerServlet.appsSessions"); args[5] = sm.getString("htmlManagerServlet.appsTasks"); writer.print(MessageFormat.format(APPS_HEADER_SECTION, args)); // Apps Row Section // Create sorted map of deployed applications context paths. String contextPaths[] = deployer.findDeployedApps(); TreeMap sortedContextPathsMap = new TreeMap(); for (int i = 0; i < contextPaths.length; i++) { String displayPath = contextPaths[i]; sortedContextPathsMap.put(displayPath, contextPaths[i]); } String appsStart = sm.getString("htmlManagerServlet.appsStart"); String appsStop = sm.getString("htmlManagerServlet.appsStop"); String appsReload = sm.getString("htmlManagerServlet.appsReload"); String appsUndeploy = sm.getString("htmlManagerServlet.appsUndeploy"); Iterator iterator = sortedContextPathsMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); String displayPath = (String) entry.getKey(); String contextPath = (String) entry.getKey(); Context context = deployer.findDeployedApp(contextPath); if (displayPath.equals("")) { displayPath = "/"; } if (context != null) { args = new Object[5]; args[0] = displayPath; args[1] = context.getDisplayName(); if (args[1] == null) { args[1] = " "; } args[2] = new Boolean(context.getAvailable()); args[3] = response.encodeURL(request.getContextPath() + "/html/sessions?path=" + displayPath); if (context.getManager() != null) { args[4] = new Integer(context.getManager().findSessions().length); } else { args[4] = new Integer(0); } writer.print(MessageFormat.format(APPS_ROW_DETAILS_SECTION, args)); args = new Object[8]; args[0] = response.encodeURL(request.getContextPath() + "/html/start?path=" + displayPath); args[1] = appsStart; args[2] = response.encodeURL(request.getContextPath() + "/html/stop?path=" + displayPath); args[3] = appsStop; args[4] = response.encodeURL(request.getContextPath() + "/html/reload?path=" + displayPath); args[5] = appsReload; args[6] = response.encodeURL(request.getContextPath() + "/html/undeploy?path=" + displayPath); args[7] = appsUndeploy; if (context.getPath().equals(this.context.getPath())) { writer.print(MessageFormat.format(MANAGER_APP_ROW_BUTTON_SECTION, args)); } else if (context.getAvailable()) { writer.print(MessageFormat.format(STARTED_APPS_ROW_BUTTON_SECTION, args)); } else { writer.print(MessageFormat.format(STOPPED_APPS_ROW_BUTTON_SECTION, args)); } } } // Deploy Section args = new Object[7]; args[0] = sm.getString("htmlManagerServlet.deployTitle"); args[1] = sm.getString("htmlManagerServlet.deployServer"); args[2] = response.encodeURL(request.getContextPath() + "/html/deploy"); args[3] = sm.getString("htmlManagerServlet.deployPath"); args[4] = sm.getString("htmlManagerServlet.deployConfig"); args[5] = sm.getString("htmlManagerServlet.deployWar"); args[6] = sm.getString("htmlManagerServlet.deployButton"); writer.print(MessageFormat.format(DEPLOY_SECTION, args)); args = new Object[4]; args[0] = sm.getString("htmlManagerServlet.deployUpload"); args[1] = response.encodeURL(request.getContextPath() + "/html/upload"); args[2] = sm.getString("htmlManagerServlet.deployUploadFile"); args[3] = sm.getString("htmlManagerServlet.deployButton"); writer.print(MessageFormat.format(UPLOAD_SECTION, args)); // Server Header Section args = new Object[7]; args[0] = sm.getString("htmlManagerServlet.serverTitle"); args[1] = sm.getString("htmlManagerServlet.serverVersion"); args[2] = sm.getString("htmlManagerServlet.serverJVMVersion"); args[3] = sm.getString("htmlManagerServlet.serverJVMVendor"); args[4] = sm.getString("htmlManagerServlet.serverOSName"); args[5] = sm.getString("htmlManagerServlet.serverOSVersion"); args[6] = sm.getString("htmlManagerServlet.serverOSArch"); writer.print(MessageFormat.format(Constants.SERVER_HEADER_SECTION, args)); // Server Row Section args = new Object[6]; args[0] = ServerInfo.getServerInfo(); args[1] = System.getProperty("java.runtime.version"); args[2] = System.getProperty("java.vm.vendor"); args[3] = System.getProperty("os.name"); args[4] = System.getProperty("os.version"); args[5] = System.getProperty("os.arch"); writer.print(MessageFormat.format(Constants.SERVER_ROW_SECTION, args)); // HTML Tail Section writer.print(Constants.HTML_TAIL_SECTION); // Finish up the response writer.flush(); writer.close(); }
From source file:org.opendatakit.common.persistence.engine.gae.TaskLockImpl.java
/** * Remove the given lockId from the (formId, taskType) entry. * /* w ww . j ava 2s . c o m*/ * NOTE: We make 10 attempts. If all of these fail, the lockId will be left * active. This can cause lock-outs for the duration of the locking period. * * @param lockId * @param formId * @param taskType */ private synchronized void deleteLockIdMemCache(String lockId, String formId, ITaskLockType taskType) { if (syncCache != null) { int i = 0; try { String formTask = ((formId == null) ? "" : formId) + "@" + taskType.getName(); for (i = 0; i < 10; i++) { IdentifiableValue v = syncCache.contains(formTask) ? syncCache.getIdentifiable(formTask) : null; if (v == null || v.getValue() == null) { break; } else { @SuppressWarnings("unchecked") TreeMap<Long, String> tmOrig = (TreeMap<Long, String>) v.getValue(); TreeMap<Long, String> tm = new TreeMap<Long, String>(tmOrig); // remove any old entries for lockId and any that are very old Long currentTimestamp = System.currentTimeMillis(); Long oldTimestamp; do { oldTimestamp = null; for (Map.Entry<Long, String> entry : tm.entrySet()) { if (entry.getKey() + 300000L < currentTimestamp) { // more than 5 minutes old -- remove it oldTimestamp = entry.getKey(); break; } if (entry.getValue().equals(lockId)) { oldTimestamp = entry.getKey(); break; } } if (oldTimestamp != null) { tm.remove(oldTimestamp); } } while (oldTimestamp != null); if (syncCache.putIfUntouched(formTask, v, tm)) { break; } } } } catch (Throwable t) { t.printStackTrace(); // ignore } // don't care if we had contention and didn't do anything. // This will eventually self-correct. if (i == 10) { log.warn("deleteLockIdMemCache -- stall has been introduced lock : " + lockId + " " + formId + " " + taskType.getName()); } } }
From source file:org.apache.catalina.servlets.HTMLManagerServlet.java
/** * Render a HTML list of the currently active Contexts in our virtual host, * and memory and server status information. * * @param writer Writer to render to//w w w.j a va 2 s . c om * @param message a message to display */ public void list(HttpServletRequest request, HttpServletResponse response, String message) throws IOException { if (debug >= 1) log("list: Listing contexts for virtual host '" + deployer.getName() + "'"); PrintWriter writer = response.getWriter(); // HTML Header Section writer.print(HTML_HEADER_SECTION); // Body Header Section Object[] args = new Object[2]; args[0] = request.getContextPath(); args[1] = sm.getString("htmlManagerServlet.title"); writer.print(MessageFormat.format(BODY_HEADER_SECTION, args)); // Message Section args = new Object[3]; args[0] = sm.getString("htmlManagerServlet.messageLabel"); args[1] = (message == null || message.length() == 0) ? "OK" : message; writer.print(MessageFormat.format(MESSAGE_SECTION, args)); // Manager Section args = new Object[7]; args[0] = sm.getString("htmlManagerServlet.manager"); args[1] = response.encodeURL(request.getContextPath() + "/html/list"); args[2] = sm.getString("htmlManagerServlet.list"); args[3] = response .encodeURL(request.getContextPath() + "/" + sm.getString("htmlManagerServlet.helpHtmlManagerFile")); args[4] = sm.getString("htmlManagerServlet.helpHtmlManager"); args[5] = response .encodeURL(request.getContextPath() + "/" + sm.getString("htmlManagerServlet.helpManagerFile")); args[6] = sm.getString("htmlManagerServlet.helpManager"); writer.print(MessageFormat.format(MANAGER_SECTION, args)); // Apps Header Section args = new Object[6]; args[0] = sm.getString("htmlManagerServlet.appsTitle"); args[1] = sm.getString("htmlManagerServlet.appsPath"); args[2] = sm.getString("htmlManagerServlet.appsName"); args[3] = sm.getString("htmlManagerServlet.appsAvailable"); args[4] = sm.getString("htmlManagerServlet.appsSessions"); args[5] = sm.getString("htmlManagerServlet.appsTasks"); writer.print(MessageFormat.format(APPS_HEADER_SECTION, args)); // Apps Row Section // Create sorted map of deployed applications context paths. String contextPaths[] = deployer.findDeployedApps(); TreeMap sortedContextPathsMap = new TreeMap(); for (int i = 0; i < contextPaths.length; i++) { String displayPath = contextPaths[i]; sortedContextPathsMap.put(displayPath, contextPaths[i]); } String appsStart = sm.getString("htmlManagerServlet.appsStart"); String appsStop = sm.getString("htmlManagerServlet.appsStop"); String appsReload = sm.getString("htmlManagerServlet.appsReload"); String appsRemove = sm.getString("htmlManagerServlet.appsRemove"); Iterator iterator = sortedContextPathsMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); String displayPath = (String) entry.getKey(); String contextPath = (String) entry.getKey(); Context context = deployer.findDeployedApp(contextPath); if (displayPath.equals("")) { displayPath = "/"; } if (context != null) { args = new Object[5]; args[0] = displayPath; args[1] = context.getDisplayName(); if (args[1] == null) { args[1] = " "; } args[2] = new Boolean(context.getAvailable()); args[3] = response.encodeURL(request.getContextPath() + "/html/sessions?path=" + displayPath); args[4] = new Integer(context.getManager().findSessions().length); writer.print(MessageFormat.format(APPS_ROW_DETAILS_SECTION, args)); args = new Object[8]; args[0] = response.encodeURL(request.getContextPath() + "/html/start?path=" + displayPath); args[1] = appsStart; args[2] = response.encodeURL(request.getContextPath() + "/html/stop?path=" + displayPath); args[3] = appsStop; args[4] = response.encodeURL(request.getContextPath() + "/html/reload?path=" + displayPath); args[5] = appsReload; args[6] = response.encodeURL(request.getContextPath() + "/html/remove?path=" + displayPath); args[7] = appsRemove; if (context.getPath().equals(this.context.getPath())) { writer.print(MessageFormat.format(MANAGER_APP_ROW_BUTTON_SECTION, args)); } else if (context.getAvailable()) { writer.print(MessageFormat.format(STARTED_APPS_ROW_BUTTON_SECTION, args)); } else { writer.print(MessageFormat.format(STOPPED_APPS_ROW_BUTTON_SECTION, args)); } } } // Install Section args = new Object[7]; args[0] = sm.getString("htmlManagerServlet.installTitle"); args[1] = sm.getString("htmlManagerServlet.installServer"); args[2] = response.encodeURL(request.getContextPath() + "/html/install"); args[3] = sm.getString("htmlManagerServlet.installPath"); args[4] = sm.getString("htmlManagerServlet.installConfig"); args[5] = sm.getString("htmlManagerServlet.installWar"); args[6] = sm.getString("htmlManagerServlet.installButton"); writer.print(MessageFormat.format(INSTALL_SECTION, args)); args = new Object[4]; args[0] = sm.getString("htmlManagerServlet.installUpload"); args[1] = response.encodeURL(request.getContextPath() + "/html/upload"); args[2] = sm.getString("htmlManagerServlet.installUploadFile"); args[3] = sm.getString("htmlManagerServlet.installButton"); writer.print(MessageFormat.format(UPLOAD_SECTION, args)); // Server Header Section args = new Object[7]; args[0] = sm.getString("htmlManagerServlet.serverTitle"); args[1] = sm.getString("htmlManagerServlet.serverVersion"); args[2] = sm.getString("htmlManagerServlet.serverJVMVersion"); args[3] = sm.getString("htmlManagerServlet.serverJVMVendor"); args[4] = sm.getString("htmlManagerServlet.serverOSName"); args[5] = sm.getString("htmlManagerServlet.serverOSVersion"); args[6] = sm.getString("htmlManagerServlet.serverOSArch"); writer.print(MessageFormat.format(SERVER_HEADER_SECTION, args)); // Server Row Section args = new Object[6]; args[0] = ServerInfo.getServerInfo(); args[1] = System.getProperty("java.runtime.version"); args[2] = System.getProperty("java.vm.vendor"); args[3] = System.getProperty("os.name"); args[4] = System.getProperty("os.version"); args[5] = System.getProperty("os.arch"); writer.print(MessageFormat.format(SERVER_ROW_SECTION, args)); // HTML Tail Section writer.print(HTML_TAIL_SECTION); // Finish up the response writer.flush(); writer.close(); }
From source file:com.hichinaschool.flashcards.anki.Preferences.java
private void initializeLanguageDialog() { TreeMap<String, String> items = new TreeMap<String, String>(); for (String localeCode : mAppLanguages) { Locale loc;/* www . j av a 2s. co m*/ if (localeCode.length() > 2) { loc = new Locale(localeCode.substring(0, 2), localeCode.substring(3, 5)); } else { loc = new Locale(localeCode); } items.put(loc.getDisplayName(), loc.toString()); } mLanguageDialogLabels = new CharSequence[items.size() + 1]; mLanguageDialogValues = new CharSequence[items.size() + 1]; mLanguageDialogLabels[0] = getResources().getString(R.string.language_system); mLanguageDialogValues[0] = ""; int i = 1; for (Map.Entry<String, String> e : items.entrySet()) { mLanguageDialogLabels[i] = e.getKey(); mLanguageDialogValues[i] = e.getValue(); i++; } mLanguageSelection = (ListPreference) getPreferenceScreen().findPreference("language"); mLanguageSelection.setEntries(mLanguageDialogLabels); mLanguageSelection.setEntryValues(mLanguageDialogValues); }
From source file:org.opencms.workplace.editors.CmsDialogProperty.java
/** * Builds the html for the page template select box.<p> * //w w w .j a va2 s .c o m * @param attributes optional attributes for the <select> tag * @return the html for the page template select box */ public String buildSelectTemplates(String attributes) { List options = new ArrayList(); List values = new ArrayList(); int selectedValue = -1; String currentTemplate = null; TreeMap templates = null; try { // read the current template currentTemplate = getCms() .readPropertyObject(getParamResource(), CmsPropertyDefinition.PROPERTY_TEMPLATE, true) .getValue(); // get all available templates templates = CmsNewResourceXmlPage.getTemplates(getCms(), getParamResource()); } catch (CmsException e) { // ignore this exception if (LOG.isInfoEnabled()) { LOG.info(Messages.get().getBundle().key(Messages.LOG_READ_TEMPLATE_FAILED_0), e); } } if (currentTemplate == null) { currentTemplate = ""; } if (templates == null) { // no valid template found, use only current one addCurrentTemplate(currentTemplate, options, values); } else { boolean found = false; // templates found, create option and value lists Iterator i = templates.entrySet().iterator(); int counter = 0; while (i.hasNext()) { Map.Entry entry = (Map.Entry) i.next(); String key = (String) entry.getKey(); String path = (String) entry.getValue(); if (currentTemplate.equals(path)) { // mark the currently selected template selectedValue = counter; found = true; } options.add(key); values.add(path); counter++; } if (!found) { // current template was not found among module templates, add current template as option addCurrentTemplate(currentTemplate, options, values); selectedValue = 0; } } String hiddenField = "<input type=\"hidden\" name=\"" + PREFIX_HIDDEN + CmsPropertyDefinition.PROPERTY_TEMPLATE + "\" value=\"" + currentTemplate + "\">"; return buildSelect(attributes, options, values, selectedValue, false) + hiddenField; }
From source file:org.lockss.servlet.DisplayContentTab.java
private Page doHtmlStatusTable0() throws IOException { page = new Page(); addJS("js/DisplayContentTab.js"); Table divTable = createTabDiv(auStart); TreeMap<String, TreeMap<String, TreeSet<ArchivalUnit>>> aus; if ("plugin".equals(groupKey)) { aus = getAusByPluginName(auStart, auEnd, type, filterKey); } else {//from ww w. j av a2 s.c o m aus = getAusByPublisherName(auStart, auEnd, type, filterKey); } for (Map.Entry<String, TreeMap<String, TreeSet<ArchivalUnit>>> entry : aus.entrySet()) { createTabContent(divTable, entry.getKey(), entry.getValue()); } Form tabForm = new Form(); tabForm.attribute("onsubmit", "return confirm('Do you wish to delete the selected items?');"); tabForm.method("GET"); tabForm.add(divTable); if (aus.size() > 0) { // Input formAdd = new Input("submit", "addSubmit"); // formAdd.attribute("value", "Add selected"); // formAdd.attribute("id", "add-submit"); // formAdd.attribute("class", "submit-button"); // tabForm.add(formAdd); Input formDelete = new Input("submit", "deleteSubmit"); formDelete.attribute("value", "Delete selected"); formDelete.attribute("id", "delete-submit"); formDelete.attribute("class", "submit-button"); tabForm.add(formDelete); } page.add(tabForm); return page; }
From source file:com.datatorrent.contrib.hdht.HDHTWriter.java
/** * Write data to size based rolling files * * @param bucket/*from w w w.j ava 2 s . com*/ * @param bucketMeta * @param data * @throws IOException */ private void writeFile(Bucket bucket, BucketMeta bucketMeta, TreeMap<Slice, Slice> data) throws IOException { BucketIOStats ioStats = getOrCretaStats(bucket.bucketKey); long startTime = System.currentTimeMillis(); FileWriter fw = null; BucketFileMeta fileMeta = null; int keysWritten = 0; for (Map.Entry<Slice, Slice> dataEntry : data.entrySet()) { if (fw == null) { // next file fileMeta = bucketMeta.addFile(bucket.bucketKey, dataEntry.getKey()); LOG.debug("writing data file {} {}", bucket.bucketKey, fileMeta.name); fw = this.store.getWriter(bucket.bucketKey, fileMeta.name + ".tmp"); keysWritten = 0; } if (Arrays.equals(dataEntry.getValue().toByteArray(), DELETED)) { continue; } fw.append(dataEntry.getKey().toByteArray(), dataEntry.getValue().toByteArray()); keysWritten++; if (fw.getBytesWritten() > this.maxFileSize) { ioStats.dataFilesWritten++; ioStats.filesWroteInCurrentWriteCycle++; // roll file fw.close(); ioStats.dataBytesWritten += fw.getBytesWritten(); this.store.rename(bucket.bucketKey, fileMeta.name + ".tmp", fileMeta.name); LOG.debug("created data file {} {} with {} entries", bucket.bucketKey, fileMeta.name, keysWritten); fw = null; keysWritten = 0; } } if (fw != null) { ioStats.dataFilesWritten++; ioStats.filesWroteInCurrentWriteCycle++; fw.close(); ioStats.dataBytesWritten += fw.getBytesWritten(); this.store.rename(bucket.bucketKey, fileMeta.name + ".tmp", fileMeta.name); LOG.debug("created data file {} {} with {} entries", bucket.bucketKey, fileMeta.name, keysWritten); } ioStats.dataWriteTime += System.currentTimeMillis() - startTime; }
From source file:org.apache.hadoop.mrunit.MapReduceDriverBase.java
/** Take the outputs from the Mapper, combine all values for the * same key, and sort them by key.//from w w w.j ava 2 s.c om * @param mapOutputs An unordered list of (key, val) pairs from the mapper * @return the sorted list of (key, list(val))'s to present to the reducer */ public List<Pair<K2, List<V2>>> shuffle(List<Pair<K2, V2>> mapOutputs) { // step 1 - use the key group comparator to organise map outputs final TreeMap<K2, List<Pair<K2, V2>>> groupedByKey = new TreeMap<K2, List<Pair<K2, V2>>>( keyGroupComparator); List<Pair<K2, V2>> groupedKeyList; for (Pair<K2, V2> mapOutput : mapOutputs) { groupedKeyList = groupedByKey.get(mapOutput.getFirst()); if (groupedKeyList == null) { groupedKeyList = new ArrayList<Pair<K2, V2>>(); groupedByKey.put(mapOutput.getFirst(), groupedKeyList); } groupedKeyList.add(mapOutput); } // step 2 - sort each key group using the key order comparator (if set) Comparator<Pair<K2, V2>> pairKeyComparator = new Comparator<Pair<K2, V2>>() { @Override public int compare(Pair<K2, V2> o1, Pair<K2, V2> o2) { return keyValueOrderComparator.compare(o1.getFirst(), o2.getFirst()); } }; // create shuffle stage output list List<Pair<K2, List<V2>>> outputKeyValuesList = new ArrayList<Pair<K2, List<V2>>>(); // populate output list for (Entry<K2, List<Pair<K2, V2>>> groupedByKeyEntry : groupedByKey.entrySet()) { if (keyValueOrderComparator != null) { // sort the key/value pairs using the key order comparator (if set) Collections.sort(groupedByKeyEntry.getValue(), pairKeyComparator); } // create list to hold values for the grouped key List<V2> valuesList = new ArrayList<V2>(); for (Pair<K2, V2> pair : groupedByKeyEntry.getValue()) { valuesList.add(pair.getSecond()); } // add key and values to output list outputKeyValuesList.add(new Pair<K2, List<V2>>(groupedByKeyEntry.getKey(), valuesList)); } // return output list return outputKeyValuesList; }