List of usage examples for java.text NumberFormat setMinimumIntegerDigits
public void setMinimumIntegerDigits(int newValue)
From source file:mx.edu.um.mateo.inventario.dao.impl.SalidaDaoHibernate.java
private String getFolioTemporal(Almacen almacen) { Query query = currentSession() .createQuery("select f from Folio f where f.nombre = :nombre and f.almacen.id = :almacenId"); query.setString("nombre", "SALIDA-TEMPORAL"); query.setLong("almacenId", almacen.getId()); query.setLockOptions(LockOptions.UPGRADE); Folio folio = (Folio) query.uniqueResult(); if (folio == null) { folio = new Folio("SALIDA-TEMPORAL"); folio.setAlmacen(almacen);//from ww w .j av a2 s.c om currentSession().save(folio); currentSession().flush(); return getFolioTemporal(almacen); } folio.setValor(folio.getValor() + 1); java.text.NumberFormat nf = java.text.DecimalFormat.getInstance(); nf.setGroupingUsed(false); nf.setMinimumIntegerDigits(9); nf.setMaximumIntegerDigits(9); nf.setMaximumFractionDigits(0); StringBuilder sb = new StringBuilder(); sb.append("TS-"); sb.append(almacen.getEmpresa().getOrganizacion().getCodigo()); sb.append(almacen.getEmpresa().getCodigo()); sb.append(almacen.getCodigo()); sb.append(nf.format(folio.getValor())); return sb.toString(); }
From source file:org.globus.workspace.client.modes.ContextMonitor.java
protected void writeReports(Node_Type[] nodes) { final short numDigits; if (nodes.length > 1000) { numDigits = 4;/*w ww. ja va 2 s .c o m*/ } else if (nodes.length > 100) { numDigits = 3; } else if (nodes.length > 10) { numDigits = 2; } else { numDigits = 1; } final NumberFormat format = NumberFormat.getInstance(); format.setMinimumIntegerDigits(numDigits); this._writeReports(nodes, this.args.reportDir, format); if (this.pr.enabled()) { final String msg = "wrote reports to '" + this.args.reportDir + "'"; if (this.pr.useThis()) { this.pr.infoln(PrCodes.CTXMONITOR__REPORT_DIR, " - " + msg); } else if (this.pr.useLogging()) { logger.info(msg); } } }
From source file:mx.edu.um.mateo.inventario.dao.impl.EntradaDaoHibernate.java
private String getFolio(Almacen almacen) { Query query = currentSession() .createQuery("select f from Folio f where f.nombre = :nombre and f.almacen.id = :almacenId"); query.setString("nombre", "ENTRADA"); query.setLong("almacenId", almacen.getId()); query.setLockOptions(LockOptions.UPGRADE); Folio folio = (Folio) query.uniqueResult(); if (folio == null) { folio = new Folio("ENTRADA"); folio.setAlmacen(almacen);// w w w. ja v a2 s .c o m currentSession().save(folio); return getFolio(almacen); } folio.setValor(folio.getValor() + 1); java.text.NumberFormat nf = java.text.DecimalFormat.getInstance(); nf.setGroupingUsed(false); nf.setMinimumIntegerDigits(9); nf.setMaximumIntegerDigits(9); nf.setMaximumFractionDigits(0); StringBuilder sb = new StringBuilder(); sb.append("E-"); sb.append(almacen.getEmpresa().getOrganizacion().getCodigo()); sb.append(almacen.getEmpresa().getCodigo()); sb.append(almacen.getCodigo()); sb.append(nf.format(folio.getValor())); return sb.toString(); }
From source file:mx.edu.um.mateo.inventario.dao.impl.EntradaDaoHibernate.java
private String getFolioTemporal(Almacen almacen) { Query query = currentSession() .createQuery("select f from Folio f where f.nombre = :nombre and f.almacen.id = :almacenId"); query.setString("nombre", "ENTRADA-TEMPORAL"); query.setLong("almacenId", almacen.getId()); query.setLockOptions(LockOptions.UPGRADE); Folio folio = (Folio) query.uniqueResult(); if (folio == null) { folio = new Folio("ENTRADA-TEMPORAL"); folio.setAlmacen(almacen);//from w ww. j ava2 s . com currentSession().save(folio); currentSession().flush(); return getFolioTemporal(almacen); } folio.setValor(folio.getValor() + 1); java.text.NumberFormat nf = java.text.DecimalFormat.getInstance(); nf.setGroupingUsed(false); nf.setMinimumIntegerDigits(9); nf.setMaximumIntegerDigits(9); nf.setMaximumFractionDigits(0); StringBuilder sb = new StringBuilder(); sb.append("TE-"); sb.append(almacen.getEmpresa().getOrganizacion().getCodigo()); sb.append(almacen.getEmpresa().getCodigo()); sb.append(almacen.getCodigo()); sb.append(nf.format(folio.getValor())); return sb.toString(); }
From source file:org.efaps.esjp.common.uiform.Edit_Base.java
private void updateFieldSetsOld(final Parameter _parameter, final Instance _instance, final List<FieldSet> _fieldsets) throws EFapsException { final NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumIntegerDigits(2); nf.setMaximumIntegerDigits(2);/* www . ja v a 2 s . c om*/ for (final FieldSet fieldset : _fieldsets) { final String setName = fieldset.getAttribute(); final AttributeSet set = AttributeSet.find(_instance.getType().getName(), setName); // first already existing values must be updated, if they were // altered boolean updateExisting = true; int yCoord = 0; while (updateExisting) { // check in the context if already existing values might have // been altered, by // using the hidden field that is added when existing values for // a fieldset are shown final String idfield = "hiddenId_" + fieldset.getName() + "_" + nf.format(yCoord); if (_parameter.getParameters().containsKey(idfield)) { final String id = _parameter.getParameterValue(idfield); // check the values in the database final PrintQuery printQuery = new PrintQuery(set, id); for (final Attribute attr : set.getAttributes().values()) { printQuery.addAttribute(attr); } printQuery.execute(); final Update setupdate = new Update(set, id); int xCoord = 0; boolean update = false; for (final String attrName : fieldset.getOrder()) { final Attribute child = set.getAttribute(attrName); final String fieldName = fieldset.getName() + "_" + nf.format(yCoord) + nf.format(xCoord); if (_parameter.getParameters().containsKey(fieldName)) { final Object object = printQuery.getAttribute(attrName); final String oldValue = object != null ? object.toString() : null; final String newValue = _parameter.getParameterValue(fieldName); if (!newValue.equals(oldValue)) { add2Update(_parameter, setupdate, child, fieldName); update = true; } } xCoord++; } if (update) { setupdate.execute(); } } else { updateExisting = false; } yCoord++; } // add new values final Map<?, ?> others = (HashMap<?, ?>) _parameter.get(ParameterValues.OTHERS); if (others != null) { // add new Values final String[] yCoords = (String[]) others.get(fieldset.getName() + "_eFapsNew"); if (yCoords != null) { for (final String ayCoord : yCoords) { final Insert insert = new Insert(set); insert.add(set.getAttribute(setName), ((Long) _instance.getId()).toString()); int xCoord = 0; for (final String attrName : fieldset.getOrder()) { final Attribute child = set.getAttribute(attrName); final String fieldName = fieldset.getName() + "_eFapsNew_" + nf.format(Integer.parseInt(ayCoord)) + nf.format(xCoord); if (_parameter.getParameters().containsKey(fieldName)) { add2Update(_parameter, insert, child, fieldName); } xCoord++; } insert.execute(); } } // remove deleted Values final String[] removeOnes = (String[]) others.get(fieldset.getName() + "eFapsRemove"); if (removeOnes != null) { for (final String removeOne : removeOnes) { final Delete delete = new Delete(set, removeOne); delete.execute(); } } } } }
From source file:org.apache.tajo.storage.StorageManager.java
/** * Finalizes result data. Tajo stores result data in the staging directory. * If the query fails, clean up the staging directory. * Otherwise the query is successful, move to the final directory from the staging directory. * * @param queryContext The query property * @param finalEbId The final execution block id * @param plan The query plan//from www . j a v a 2s .c om * @param schema The final output schema * @param tableDesc The description of the target table * @param changeFileSeq If true change result file name with max sequence. * @return Saved path * @throws java.io.IOException */ protected Path commitOutputData(OverridableConf queryContext, ExecutionBlockId finalEbId, LogicalPlan plan, Schema schema, TableDesc tableDesc, boolean changeFileSeq) throws IOException { Path stagingDir = new Path(queryContext.get(QueryVars.STAGING_DIR)); Path stagingResultDir = new Path(stagingDir, TajoConstants.RESULT_DIR_NAME); Path finalOutputDir; if (!queryContext.get(QueryVars.OUTPUT_TABLE_PATH, "").isEmpty()) { finalOutputDir = new Path(queryContext.get(QueryVars.OUTPUT_TABLE_PATH)); try { FileSystem fs = stagingResultDir.getFileSystem(conf); if (queryContext.getBool(QueryVars.OUTPUT_OVERWRITE, false)) { // INSERT OVERWRITE INTO // It moves the original table into the temporary location. // Then it moves the new result table into the original table location. // Upon failed, it recovers the original table if possible. boolean movedToOldTable = false; boolean committed = false; Path oldTableDir = new Path(stagingDir, TajoConstants.INSERT_OVERWIRTE_OLD_TABLE_NAME); ContentSummary summary = fs.getContentSummary(stagingResultDir); if (!queryContext.get(QueryVars.OUTPUT_PARTITIONS, "").isEmpty() && summary.getFileCount() > 0L) { // This is a map for existing non-leaf directory to rename. A key is current directory and a value is // renaming directory. Map<Path, Path> renameDirs = TUtil.newHashMap(); // This is a map for recovering existing partition directory. A key is current directory and a value is // temporary directory to back up. Map<Path, Path> recoveryDirs = TUtil.newHashMap(); try { if (!fs.exists(finalOutputDir)) { fs.mkdirs(finalOutputDir); } visitPartitionedDirectory(fs, stagingResultDir, finalOutputDir, stagingResultDir.toString(), renameDirs, oldTableDir); // Rename target partition directories for (Map.Entry<Path, Path> entry : renameDirs.entrySet()) { // Backup existing data files for recovering if (fs.exists(entry.getValue())) { String recoveryPathString = entry.getValue().toString() .replaceAll(finalOutputDir.toString(), oldTableDir.toString()); Path recoveryPath = new Path(recoveryPathString); fs.rename(entry.getValue(), recoveryPath); fs.exists(recoveryPath); recoveryDirs.put(entry.getValue(), recoveryPath); } // Delete existing directory fs.delete(entry.getValue(), true); // Rename staging directory to final output directory fs.rename(entry.getKey(), entry.getValue()); } } catch (IOException ioe) { // Remove created dirs for (Map.Entry<Path, Path> entry : renameDirs.entrySet()) { fs.delete(entry.getValue(), true); } // Recovery renamed dirs for (Map.Entry<Path, Path> entry : recoveryDirs.entrySet()) { fs.delete(entry.getValue(), true); fs.rename(entry.getValue(), entry.getKey()); } throw new IOException(ioe.getMessage()); } } else { // no partition try { // if the final output dir exists, move all contents to the temporary table dir. // Otherwise, just make the final output dir. As a result, the final output dir will be empty. if (fs.exists(finalOutputDir)) { fs.mkdirs(oldTableDir); for (FileStatus status : fs.listStatus(finalOutputDir, StorageManager.hiddenFileFilter)) { fs.rename(status.getPath(), oldTableDir); } movedToOldTable = fs.exists(oldTableDir); } else { // if the parent does not exist, make its parent directory. fs.mkdirs(finalOutputDir); } // Move the results to the final output dir. for (FileStatus status : fs.listStatus(stagingResultDir)) { fs.rename(status.getPath(), finalOutputDir); } // Check the final output dir committed = fs.exists(finalOutputDir); } catch (IOException ioe) { // recover the old table if (movedToOldTable && !committed) { // if commit is failed, recover the old data for (FileStatus status : fs.listStatus(finalOutputDir, StorageManager.hiddenFileFilter)) { fs.delete(status.getPath(), true); } for (FileStatus status : fs.listStatus(oldTableDir)) { fs.rename(status.getPath(), finalOutputDir); } } throw new IOException(ioe.getMessage()); } } } else { String queryType = queryContext.get(QueryVars.COMMAND_TYPE); if (queryType != null && queryType.equals(NodeType.INSERT.name())) { // INSERT INTO an existing table NumberFormat fmt = NumberFormat.getInstance(); fmt.setGroupingUsed(false); fmt.setMinimumIntegerDigits(3); if (!queryContext.get(QueryVars.OUTPUT_PARTITIONS, "").isEmpty()) { for (FileStatus eachFile : fs.listStatus(stagingResultDir)) { if (eachFile.isFile()) { LOG.warn("Partition table can't have file in a staging dir: " + eachFile.getPath()); continue; } moveResultFromStageToFinal(fs, stagingResultDir, eachFile, finalOutputDir, fmt, -1, changeFileSeq); } } else { int maxSeq = StorageUtil.getMaxFileSequence(fs, finalOutputDir, false) + 1; for (FileStatus eachFile : fs.listStatus(stagingResultDir)) { if (eachFile.getPath().getName().startsWith("_")) { continue; } moveResultFromStageToFinal(fs, stagingResultDir, eachFile, finalOutputDir, fmt, maxSeq++, changeFileSeq); } } // checking all file moved and remove empty dir verifyAllFileMoved(fs, stagingResultDir); FileStatus[] files = fs.listStatus(stagingResultDir); if (files != null && files.length != 0) { for (FileStatus eachFile : files) { LOG.error("There are some unmoved files in staging dir:" + eachFile.getPath()); } } } else { // CREATE TABLE AS SELECT (CTAS) if (fs.exists(finalOutputDir)) { for (FileStatus status : fs.listStatus(stagingResultDir)) { fs.rename(status.getPath(), finalOutputDir); } } else { fs.rename(stagingResultDir, finalOutputDir); } LOG.info("Moved from the staging dir to the output directory '" + finalOutputDir); } } // remove the staging directory if the final output dir is given. Path stagingDirRoot = stagingDir.getParent(); fs.delete(stagingDirRoot, true); } catch (Throwable t) { LOG.error(t); throw new IOException(t); } } else { finalOutputDir = new Path(stagingDir, TajoConstants.RESULT_DIR_NAME); } return finalOutputDir; }
From source file:org.kuali.kfs.module.ld.batch.service.impl.LaborScrubberProcess.java
/** * Generate the flag for the end of specific descriptions. This will be used in the demerger step *//*from w w w . j a v a 2s . c o m*/ protected void setOffsetString() { NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(0); nf.setMaximumIntegerDigits(2); nf.setMinimumFractionDigits(0); nf.setMinimumIntegerDigits(2); offsetString = "***" + nf.format(runCal.get(Calendar.MONTH) + 1) + nf.format(runCal.get(Calendar.DAY_OF_MONTH)); }
From source file:se.technipelago.weather.chart.Generator.java
private void createWindDial(float speed, float high, final String filename) throws IOException { ValueDataset dataset1 = new DefaultValueDataset(speed); ValueDataset dataset2 = new DefaultValueDataset(high); // get data for diagrams DialPlot plot = new DialPlot(); plot.setView(0.0, 0.0, 1.0, 1.0);//from w w w . j av a 2 s .c o m plot.setDataset(0, dataset1); plot.setDataset(1, dataset2); StandardDialFrame dialFrame = new StandardDialFrame(); dialFrame.setBackgroundPaint(Color.lightGray); dialFrame.setForegroundPaint(Color.darkGray); plot.setDialFrame(dialFrame); GradientPaint gp = new GradientPaint(new Point(), new Color(255, 255, 255), new Point(), new Color(170, 170, 220)); DialBackground db = new DialBackground(gp); db.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL)); plot.setBackground(db); // Wind Speed DialTextAnnotation annotation1 = new DialTextAnnotation("m/s"); annotation1.setFont(new Font("Dialog", Font.BOLD, 10)); annotation1.setRadius(0.76); plot.addLayer(annotation1); DialValueIndicator dvi = new DialValueIndicator(0); dvi.setFont(new Font("Dialog", Font.PLAIN, 9)); dvi.setOutlinePaint(Color.darkGray); //dvi.setBackgroundPaint(new Color(0xee, 0xee, 0xf6)); NumberFormat fmt = new DecimalFormat("#"); fmt.setMaximumFractionDigits(1); fmt.setMinimumIntegerDigits(1); dvi.setNumberFormat(fmt); dvi.setRadius(0.71); dvi.setAngle(-89.0); // -103 dvi.setInsets(new RectangleInsets(0.0, 2.0, 0.0, 2.0)); // top, left, bottom, right plot.addLayer(dvi); StandardDialScale scale = new StandardDialScale(0, 20, -120, -300, 1, 0); scale.setTickRadius(0.88); scale.setTickLabelOffset(0.15); scale.setTickLabelFont(new Font("Dialog", Font.PLAIN, 10)); NumberFormat fmt3 = new DecimalFormat("#"); fmt3.setMaximumFractionDigits(0); scale.setTickLabelFormatter(fmt3); plot.addScale(0, scale); // Add needles. // To make the average speed needle the front-most needle, // it must be added after high speed needle. // High speed needle. DialPointer needle2 = new DialPointer.Pin(1); needle2.setRadius(0.62); plot.addLayer(needle2); // Average speed needle. DialPointer needle = new DialPointer.Pointer(0); Color darkGreen = new Color(0x15, 0x49, 0x1f); ((DialPointer.Pointer) needle).setFillPaint(darkGreen); plot.addLayer(needle); // Add a cap at the dial center. DialCap cap = new DialCap(); cap.setRadius(0.10); plot.setCap(cap); JFreeChart chart = new JFreeChart(plot); //TextTitle title = new TextTitle("Vindhastighet", new Font("Dialog", Font.BOLD, 12)); //title.setPaint(Color.DARK_GRAY); //chart.setTitle(title); chart.setBackgroundPaint(VERY_LIGHT_GRAY); OutputStream out = null; try { out = new FileOutputStream(outputDir != null ? outputDir + "/" + filename : filename); ChartUtilities.writeChartAsPNG(out, chart, DIAL_WIDTH, DIAL_HEIGHT); } finally { if (out != null) { out.close(); } } }
From source file:se.technipelago.weather.chart.Generator.java
private void createTemperatureDial(float temperature, int humidity, final String filename) throws IOException { ValueDataset dataset1 = new DefaultValueDataset(temperature); ValueDataset dataset2 = new DefaultValueDataset(humidity); // get data for diagrams DialPlot plot = new DialPlot(); plot.setView(0.0, 0.0, 1.0, 1.0);//w ww . j a va2 s .c o m plot.setDataset(0, dataset1); plot.setDataset(1, dataset2); StandardDialFrame dialFrame = new StandardDialFrame(); dialFrame.setBackgroundPaint(Color.lightGray); dialFrame.setForegroundPaint(Color.darkGray); plot.setDialFrame(dialFrame); GradientPaint gp = new GradientPaint(new Point(), new Color(255, 255, 255), new Point(), new Color(170, 170, 220)); DialBackground db = new DialBackground(gp); db.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL)); plot.setBackground(db); // Temperature DialTextAnnotation annotation1 = new DialTextAnnotation("\u00B0C"); annotation1.setFont(new Font("Dialog", Font.BOLD, 10)); annotation1.setRadius(0.76); plot.addLayer(annotation1); // Humidity DialTextAnnotation annotation2 = new DialTextAnnotation("%"); annotation2.setFont(new Font("Dialog", Font.BOLD, 10)); annotation2.setPaint(Color.blue); annotation2.setRadius(0.4); plot.addLayer(annotation2); // Temperature DialValueIndicator dvi = new DialValueIndicator(0); dvi.setFont(new Font("Dialog", Font.PLAIN, 9)); dvi.setOutlinePaint(Color.darkGray); //dvi.setBackgroundPaint(new Color(0xee, 0xee, 0xf6)); NumberFormat fmt = new DecimalFormat("#"); fmt.setMaximumFractionDigits(1); fmt.setMinimumIntegerDigits(1); dvi.setNumberFormat(fmt); dvi.setRadius(0.71); dvi.setAngle(-88.0); // -103 dvi.setInsets(new RectangleInsets(0.0, 8.0, 0.0, 2.0)); // top, left, bottom, right plot.addLayer(dvi); StandardDialScale scale = new StandardDialScale(-30, 30, -120, -300, 5, 4); scale.setTickRadius(0.88); scale.setTickLabelOffset(0.15); scale.setTickLabelFont(new Font("Dialog", Font.PLAIN, 10)); NumberFormat fmt3 = new DecimalFormat("#"); fmt3.setMaximumFractionDigits(0); scale.setTickLabelFormatter(fmt3); plot.addScale(0, scale); // Humidity DialValueIndicator dvi2 = new DialValueIndicator(1); dvi2.setFont(new Font("Dialog", Font.PLAIN, 9)); dvi2.setOutlinePaint(Color.blue); //dvi2.setBackgroundPaint(new Color(0xee, 0xee, 0xf6)); NumberFormat fmt2 = new DecimalFormat("#"); fmt2.setMaximumFractionDigits(0); dvi2.setNumberFormat(fmt2); dvi2.setRadius(0.59); dvi2.setAngle(-90.0); // -77 dvi2.setInsets(new RectangleInsets(0.0, 1.0, 0.0, 1.0)); plot.addLayer(dvi2); StandardDialScale scale2 = new StandardDialScale(0, 100, -120, -300, 10, 4); scale2.setTickRadius(0.50); scale2.setTickLabelOffset(0.15); scale2.setTickLabelFont(new Font("Dialog", Font.PLAIN, 9)); scale2.setTickLabelFormatter(fmt3); scale2.setMajorTickPaint(Color.blue); plot.addScale(1, scale2); plot.mapDatasetToScale(1, 1); // Add needles. // To make the temperature needle the front-most needle, // it must be added after humidity needle. // Humidity needle. DialPointer needle2 = new DialPointer.Pin(1); needle2.setRadius(0.50); plot.addLayer(needle2); // Temperature needle. DialPointer needle = new DialPointer.Pointer(0); Color darkGreen = new Color(0x15, 0x49, 0x1f); ((DialPointer.Pointer) needle).setFillPaint(darkGreen); plot.addLayer(needle); // Add a cap at the dial center. DialCap cap = new DialCap(); cap.setRadius(0.10); plot.setCap(cap); JFreeChart chart = new JFreeChart(plot); //TextTitle title = new TextTitle("Temperatur/luftfuktighet", new Font("Dialog", Font.BOLD, 12)); //title.setPaint(Color.DARK_GRAY); //chart.setTitle(title); chart.setBackgroundPaint(VERY_LIGHT_GRAY); OutputStream out = null; try { out = new FileOutputStream(outputDir != null ? outputDir + "/" + filename : filename); ChartUtilities.writeChartAsPNG(out, chart, DIAL_WIDTH, DIAL_HEIGHT); } finally { if (out != null) { out.close(); } } }
From source file:org.apache.tajo.storage.FileTablespace.java
/** * Finalizes result data. Tajo stores result data in the staging directory. * If the query fails, clean up the staging directory. * Otherwise the query is successful, move to the final directory from the staging directory. * * @param queryContext The query property * @param changeFileSeq If true change result file name with max sequence. * @return Saved path/*from w w w.ja va 2 s. c o m*/ * @throws java.io.IOException */ protected Path commitOutputData(OverridableConf queryContext, boolean changeFileSeq) throws IOException { Path stagingDir = new Path(queryContext.get(QueryVars.STAGING_DIR)); Path stagingResultDir = new Path(stagingDir, TajoConstants.RESULT_DIR_NAME); Path finalOutputDir; if (!queryContext.get(QueryVars.OUTPUT_TABLE_URI, "").isEmpty()) { finalOutputDir = new Path(queryContext.get(QueryVars.OUTPUT_TABLE_URI)); try { FileSystem fs = stagingResultDir.getFileSystem(conf); if (queryContext.getBool(QueryVars.OUTPUT_OVERWRITE, false)) { // INSERT OVERWRITE INTO // It moves the original table into the temporary location. // Then it moves the new result table into the original table location. // Upon failed, it recovers the original table if possible. boolean movedToOldTable = false; boolean committed = false; Path oldTableDir = new Path(stagingDir, TajoConstants.INSERT_OVERWIRTE_OLD_TABLE_NAME); ContentSummary summary = fs.getContentSummary(stagingResultDir); // When inserting empty data into a partitioned table, check if keep existing data need to be remove or not. boolean overwriteEnabled = queryContext .getBool(SessionVars.PARTITION_NO_RESULT_OVERWRITE_ENABLED); // If existing data doesn't need to keep, check if there are some files. if ((!queryContext.get(QueryVars.OUTPUT_PARTITIONS, "").isEmpty()) && (!overwriteEnabled || (overwriteEnabled && summary.getFileCount() > 0L))) { // This is a map for existing non-leaf directory to rename. A key is current directory and a value is // renaming directory. Map<Path, Path> renameDirs = new HashMap<>(); // This is a map for recovering existing partition directory. A key is current directory and a value is // temporary directory to back up. Map<Path, Path> recoveryDirs = new HashMap<>(); try { if (!fs.exists(finalOutputDir)) { fs.mkdirs(finalOutputDir); } visitPartitionedDirectory(fs, stagingResultDir, finalOutputDir, stagingResultDir.toString(), renameDirs, oldTableDir); // Rename target partition directories for (Map.Entry<Path, Path> entry : renameDirs.entrySet()) { // Backup existing data files for recovering if (fs.exists(entry.getValue())) { String recoveryPathString = entry.getValue().toString() .replaceAll(finalOutputDir.toString(), oldTableDir.toString()); Path recoveryPath = new Path(recoveryPathString); fs.rename(entry.getValue(), recoveryPath); fs.exists(recoveryPath); recoveryDirs.put(entry.getValue(), recoveryPath); } // Delete existing directory fs.delete(entry.getValue(), true); // Rename staging directory to final output directory fs.rename(entry.getKey(), entry.getValue()); } } catch (IOException ioe) { // Remove created dirs for (Map.Entry<Path, Path> entry : renameDirs.entrySet()) { fs.delete(entry.getValue(), true); } // Recovery renamed dirs for (Map.Entry<Path, Path> entry : recoveryDirs.entrySet()) { fs.delete(entry.getValue(), true); fs.rename(entry.getValue(), entry.getKey()); } throw new IOException(ioe.getMessage()); } } else { // no partition try { // if the final output dir exists, move all contents to the temporary table dir. // Otherwise, just make the final output dir. As a result, the final output dir will be empty. if (fs.exists(finalOutputDir)) { fs.mkdirs(oldTableDir); for (FileStatus status : fs.listStatus(finalOutputDir, hiddenFileFilter)) { fs.rename(status.getPath(), oldTableDir); } movedToOldTable = fs.exists(oldTableDir); } else { // if the parent does not exist, make its parent directory. fs.mkdirs(finalOutputDir); } // Move the results to the final output dir. for (FileStatus status : fs.listStatus(stagingResultDir)) { fs.rename(status.getPath(), finalOutputDir); } // Check the final output dir committed = fs.exists(finalOutputDir); } catch (IOException ioe) { // recover the old table if (movedToOldTable && !committed) { // if commit is failed, recover the old data for (FileStatus status : fs.listStatus(finalOutputDir, hiddenFileFilter)) { fs.delete(status.getPath(), true); } for (FileStatus status : fs.listStatus(oldTableDir)) { fs.rename(status.getPath(), finalOutputDir); } } throw new IOException(ioe.getMessage()); } } } else { String queryType = queryContext.get(QueryVars.COMMAND_TYPE); if (queryType != null && queryType.equals(NodeType.INSERT.name())) { // INSERT INTO an existing table NumberFormat fmt = NumberFormat.getInstance(); fmt.setGroupingUsed(false); fmt.setMinimumIntegerDigits(3); if (!queryContext.get(QueryVars.OUTPUT_PARTITIONS, "").isEmpty()) { for (FileStatus eachFile : fs.listStatus(stagingResultDir)) { if (eachFile.isFile()) { LOG.warn("Partition table can't have file in a staging dir: " + eachFile.getPath()); continue; } moveResultFromStageToFinal(fs, stagingResultDir, eachFile, finalOutputDir, fmt, -1, changeFileSeq); } } else { int maxSeq = StorageUtil.getMaxFileSequence(fs, finalOutputDir, false) + 1; for (FileStatus eachFile : fs.listStatus(stagingResultDir)) { if (eachFile.getPath().getName().startsWith("_")) { continue; } moveResultFromStageToFinal(fs, stagingResultDir, eachFile, finalOutputDir, fmt, maxSeq++, changeFileSeq); } } // checking all file moved and remove empty dir verifyAllFileMoved(fs, stagingResultDir); FileStatus[] files = fs.listStatus(stagingResultDir); if (files != null && files.length != 0) { for (FileStatus eachFile : files) { LOG.error("There are some unmoved files in staging dir:" + eachFile.getPath()); } } } else { // CREATE TABLE AS SELECT (CTAS) if (fs.exists(finalOutputDir)) { for (FileStatus status : fs.listStatus(stagingResultDir)) { fs.rename(status.getPath(), finalOutputDir); } } else { fs.rename(stagingResultDir, finalOutputDir); } LOG.info("Moved from the staging dir to the output directory '" + finalOutputDir); } } // remove the staging directory if the final output dir is given. Path stagingDirRoot = stagingDir.getParent(); fs.delete(stagingDirRoot, true); } catch (Throwable t) { LOG.error(t); throw new IOException(t); } } else { finalOutputDir = new Path(stagingDir, TajoConstants.RESULT_DIR_NAME); } return finalOutputDir; }