org.apache.lucene.util.IOUtils.closeWhileHandlingException()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(11.2k)|赞(0)|评价(0)|浏览(155)

本文整理了Java中org.apache.lucene.util.IOUtils.closeWhileHandlingException()方法的一些代码示例,展示了IOUtils.closeWhileHandlingException()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IOUtils.closeWhileHandlingException()方法的具体详情如下:
包路径:org.apache.lucene.util.IOUtils
类名称:IOUtils
方法名:closeWhileHandlingException

IOUtils.closeWhileHandlingException介绍

[英]Closes all given Closeables, suppressing all thrown exceptions.
[中]关闭所有给定的CloseTables,抑制所有抛出的异常。

代码示例

代码示例来源:origin: org.apache.lucene/lucene-core

void abort() {
  if (writer != null) {
   IOUtils.closeWhileHandlingException(writer);
   writer = null;
  }
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

/**
 * Closes all given <tt>Closeable</tt>s, suppressing all thrown exceptions.
 * Some of the <tt>Closeable</tt>s may be null, they are ignored.
 * 
 * @param objects
 *          objects to call <tt>close()</tt> on
 */
public static void closeWhileHandlingException(Closeable... objects) {
 closeWhileHandlingException(Arrays.asList(objects));
}

代码示例来源:origin: org.apache.lucene/lucene-core

/** Acquires write locks on all the directories; be sure
 *  to match with a call to {@link IOUtils#close} in a
 *  finally clause. */
private List<Lock> acquireWriteLocks(Directory... dirs) throws IOException {
 List<Lock> locks = new ArrayList<>(dirs.length);
 for(int i=0;i<dirs.length;i++) {
  boolean success = false;
  try {
   Lock lock = dirs[i].obtainLock(WRITE_LOCK_NAME);
   locks.add(lock);
   success = true;
  } finally {
   if (success == false) {
    // Release all previously acquired locks:
    // TODO: addSuppressed? it could be many...
    IOUtils.closeWhileHandlingException(locks);
   }
  }
 }
 return locks;
}

代码示例来源:origin: org.apache.lucene/lucene-core

@Override
public void abort() {
 hasVectors = false;
 try {
  super.abort();
 } finally {
  IOUtils.closeWhileHandlingException(writer);
  writer = null;
  lastDocID = 0;
  reset();
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

@Override
public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException {
 PostingsReaderBase postingsReader = new Lucene50PostingsReader(state);
 boolean success = false;
 try {
  FieldsProducer ret = new BlockTreeTermsReader(postingsReader, state);
  success = true;
  return ret;
 } finally {
  if (!success) {
   IOUtils.closeWhileHandlingException(postingsReader);
  }
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

@Override
public FieldsConsumer fieldsConsumer(SegmentWriteState state) throws IOException {
 PostingsWriterBase postingsWriter = new Lucene50PostingsWriter(state);
 boolean success = false;
 try {
  FieldsConsumer ret = new BlockTreeTermsWriter(state, 
                         postingsWriter,
                         minTermBlockSize, 
                         maxTermBlockSize);
  success = true;
  return ret;
 } finally {
  if (!success) {
   IOUtils.closeWhileHandlingException(postingsWriter);
  }
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

@Override
public void write(Fields fields) throws IOException {
 Map<PostingsFormat, FieldsGroup> formatToGroups = buildFieldsGroupMapping(fields);
 // Write postings
 boolean success = false;
 try {
  for (Map.Entry<PostingsFormat, FieldsGroup> ent : formatToGroups.entrySet()) {
   PostingsFormat format = ent.getKey();
   final FieldsGroup group = ent.getValue();
   // Exposes only the fields from this group:
   Fields maskedFields = new FilterFields(fields) {
    @Override
    public Iterator<String> iterator() {
     return group.fields.iterator();
    }
   };
   FieldsConsumer consumer = format.fieldsConsumer(group.state);
   toClose.add(consumer);
   consumer.write(maskedFields);
  }
  success = true;
 } finally {
  if (!success) {
   IOUtils.closeWhileHandlingException(toClose);
  }
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

} finally {
 if (!success) {
  IOUtils.closeWhileHandlingException(formats.values());

代码示例来源:origin: org.apache.lucene/lucene-core

@Override
public void merge(MergeState mergeState) throws IOException {
 Map<PostingsFormat, FieldsGroup> formatToGroups = buildFieldsGroupMapping(new MultiFields(mergeState.fieldsProducers, null));
 // Merge postings
 PerFieldMergeState pfMergeState = new PerFieldMergeState(mergeState);
 boolean success = false;
 try {
  for (Map.Entry<PostingsFormat, FieldsGroup> ent : formatToGroups.entrySet()) {
   PostingsFormat format = ent.getKey();
   final FieldsGroup group = ent.getValue();
   FieldsConsumer consumer = format.fieldsConsumer(group.state);
   toClose.add(consumer);
   consumer.merge(pfMergeState.apply(group.fields));
  }
  success = true;
 } finally {
  pfMergeState.reset();
  if (!success) {
   IOUtils.closeWhileHandlingException(toClose);
  }
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

} finally {
 if (!success) {
  IOUtils.closeWhileHandlingException(formats.values());

代码示例来源:origin: org.apache.lucene/lucene-core

/** Close this directory, which flushes any cached files
 *  to the delegate and then closes the delegate. */
@Override
public void close() throws IOException {
 // NOTE: technically we shouldn't have to do this, ie,
 // IndexWriter should have sync'd all files, but we do
 // it for defensive reasons... or in case the app is
 // doing something custom (creating outputs directly w/o
 // using IndexWriter):
 boolean success = false;
 try {
  if (cache.isOpen) {
   for(String fileName : cache.listAll()) {
    unCache(fileName);
   }
  }
  success = true;
 } finally {
  if (success) {
   IOUtils.close(cache, in);
  } else {
   IOUtils.closeWhileHandlingException(cache, in);
  }
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

synchronized private void persist() throws IOException {
 String fileName = SNAPSHOTS_PREFIX + nextWriteGen;
 IndexOutput out = dir.createOutput(fileName, IOContext.DEFAULT);
 boolean success = false;
 try {
  CodecUtil.writeHeader(out, CODEC_NAME, VERSION_CURRENT);   
  out.writeVInt(refCounts.size());
  for(Entry<Long,Integer> ent : refCounts.entrySet()) {
   out.writeVLong(ent.getKey());
   out.writeVInt(ent.getValue());
  }
  success = true;
 } finally {
  if (!success) {
   IOUtils.closeWhileHandlingException(out);
   IOUtils.deleteFilesIgnoringExceptions(dir, fileName);
  } else {
   IOUtils.close(out);
  }
 }
 dir.sync(Collections.singletonList(fileName));
 
 if (nextWriteGen > 0) {
  String lastSaveFile = SNAPSHOTS_PREFIX + (nextWriteGen-1);
  // exception OK: likely it didn't exist
  IOUtils.deleteFilesIgnoringExceptions(dir, lastSaveFile);
 }
 nextWriteGen++;
}

代码示例来源:origin: org.apache.lucene/lucene-core

@Override
public void close() throws IOException {
 boolean success = false;
 try {
  if (meta != null) {
   meta.writeInt(-1); // write EOF marker
   CodecUtil.writeFooter(meta); // write checksum
  }
  if (data != null) {
   CodecUtil.writeFooter(data); // write checksum
  }
  success = true;
 } finally {
  if (success) {
   IOUtils.close(data, meta);
  } else {
   IOUtils.closeWhileHandlingException(data, meta);
  }
  meta = data = null;
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

@Override
public void close() throws IOException {
 boolean success = false;
 try {
  if (meta != null) {
   meta.writeInt(-1); // write EOF marker
   CodecUtil.writeFooter(meta); // write checksum
  }
  if (data != null) {
   CodecUtil.writeFooter(data); // write checksum
  }
  success = true;
 } finally {
  if (success) {
   IOUtils.close(data, meta);
  } else {
   IOUtils.closeWhileHandlingException(data, meta);
  }
  meta = data = null;
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

@Override
 public void close() throws IOException {
  // TODO: add a finish() at least to PushBase? DV too...?
  boolean success = false;
  try {
   if (docOut != null) {
    CodecUtil.writeFooter(docOut);
   }
   if (posOut != null) {
    CodecUtil.writeFooter(posOut);
   }
   if (payOut != null) {
    CodecUtil.writeFooter(payOut);
   }
   success = true;
  } finally {
   if (success) {
    IOUtils.close(docOut, posOut, payOut);
   } else {
    IOUtils.closeWhileHandlingException(docOut, posOut, payOut);
   }
   docOut = posOut = payOut = null;
  }
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

/** Full constructor */
public Lucene60PointsWriter(SegmentWriteState writeState, int maxPointsInLeafNode, double maxMBSortInHeap) throws IOException {
 assert writeState.fieldInfos.hasPointValues();
 this.writeState = writeState;
 this.maxPointsInLeafNode = maxPointsInLeafNode;
 this.maxMBSortInHeap = maxMBSortInHeap;
 String dataFileName = IndexFileNames.segmentFileName(writeState.segmentInfo.name,
                            writeState.segmentSuffix,
                            Lucene60PointsFormat.DATA_EXTENSION);
 dataOut = writeState.directory.createOutput(dataFileName, writeState.context);
 boolean success = false;
 try {
  CodecUtil.writeIndexHeader(dataOut,
                Lucene60PointsFormat.DATA_CODEC_NAME,
                Lucene60PointsFormat.DATA_VERSION_CURRENT,
                writeState.segmentInfo.getId(),
                writeState.segmentSuffix);
  success = true;
 } finally {
  if (success == false) {
   IOUtils.closeWhileHandlingException(dataOut);
  }
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

private void write(Directory directory) throws IOException {
 long nextGeneration = getNextPendingGeneration();
 String segmentFileName = IndexFileNames.fileNameFromGeneration(IndexFileNames.PENDING_SEGMENTS,
                                 "",
                                 nextGeneration);
 // Always advance the generation on write:
 generation = nextGeneration;
 
 IndexOutput segnOutput = null;
 boolean success = false;
 try {
  segnOutput = directory.createOutput(segmentFileName, IOContext.DEFAULT);
  write(directory, segnOutput);
  segnOutput.close();
  directory.sync(Collections.singleton(segmentFileName));
  success = true;
 } finally {
  if (success) {
   pendingCommit = true;
  } else {
   // We hit an exception above; try to close the file
   // but suppress any exception:
   IOUtils.closeWhileHandlingException(segnOutput);
   // Try not to leave a truncated segments_N file in
   // the index:
   IOUtils.deleteFilesIgnoringExceptions(directory, segmentFileName);
  }
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

Lucene70NormsConsumer(SegmentWriteState state, String dataCodec, String dataExtension, String metaCodec, String metaExtension) throws IOException {
 boolean success = false;
 try {
  String dataName = IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, dataExtension);
  data = state.directory.createOutput(dataName, state.context);
  CodecUtil.writeIndexHeader(data, dataCodec, VERSION_CURRENT, state.segmentInfo.getId(), state.segmentSuffix);
  String metaName = IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, metaExtension);
  meta = state.directory.createOutput(metaName, state.context);
  CodecUtil.writeIndexHeader(meta, metaCodec, VERSION_CURRENT, state.segmentInfo.getId(), state.segmentSuffix);
  maxDoc = state.segmentInfo.maxDoc();
  success = true;
 } finally {
  if (!success) {
   IOUtils.closeWhileHandlingException(this);
  }
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

/** expert: Creates a new writer */
public Lucene70DocValuesConsumer(SegmentWriteState state, String dataCodec, String dataExtension, String metaCodec, String metaExtension) throws IOException {
 boolean success = false;
 try {
  String dataName = IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, dataExtension);
  data = state.directory.createOutput(dataName, state.context);
  CodecUtil.writeIndexHeader(data, dataCodec, Lucene70DocValuesFormat.VERSION_CURRENT, state.segmentInfo.getId(), state.segmentSuffix);
  String metaName = IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, metaExtension);
  meta = state.directory.createOutput(metaName, state.context);
  CodecUtil.writeIndexHeader(meta, metaCodec, Lucene70DocValuesFormat.VERSION_CURRENT, state.segmentInfo.getId(), state.segmentSuffix);
  maxDoc = state.segmentInfo.maxDoc();
  success = true;
 } finally {
  if (!success) {
   IOUtils.closeWhileHandlingException(this);
  }
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core

private void writeNorms(SegmentWriteState state, Sorter.DocMap sortMap) throws IOException {
 boolean success = false;
 NormsConsumer normsConsumer = null;
 try {
  if (state.fieldInfos.hasNorms()) {
   NormsFormat normsFormat = state.segmentInfo.getCodec().normsFormat();
   assert normsFormat != null;
   normsConsumer = normsFormat.normsConsumer(state);
   for (FieldInfo fi : state.fieldInfos) {
    PerField perField = getPerField(fi.name);
    assert perField != null;
    // we must check the final value of omitNorms for the fieldinfo: it could have 
    // changed for this field since the first time we added it.
    if (fi.omitsNorms() == false && fi.getIndexOptions() != IndexOptions.NONE) {
     assert perField.norms != null: "field=" + fi.name;
     perField.norms.finish(state.segmentInfo.maxDoc());
     perField.norms.flush(state, sortMap, normsConsumer);
    }
   }
  }
  success = true;
 } finally {
  if (success) {
   IOUtils.close(normsConsumer);
  } else {
   IOUtils.closeWhileHandlingException(normsConsumer);
  }
 }
}

相关文章