当前位置: 首页>>代码示例>>Java>>正文


Java IOUtils.closeWhileHandlingException方法代码示例

本文整理汇总了Java中org.apache.lucene.util.IOUtils.closeWhileHandlingException方法的典型用法代码示例。如果您正苦于以下问题:Java IOUtils.closeWhileHandlingException方法的具体用法?Java IOUtils.closeWhileHandlingException怎么用?Java IOUtils.closeWhileHandlingException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.lucene.util.IOUtils的用法示例。


在下文中一共展示了IOUtils.closeWhileHandlingException方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: buildAndPutCluster

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
private TestCluster buildAndPutCluster(Scope currentClusterScope, long seed) throws Exception {
    final Class<?> clazz = this.getClass();
    TestCluster testCluster = clusters.remove(clazz); // remove this cluster first
    clearClusters(); // all leftovers are gone by now... this is really just a double safety if we miss something somewhere
    switch (currentClusterScope) {
        case SUITE:
            if (testCluster == null) { // only build if it's not there yet
                testCluster = buildWithPrivateContext(currentClusterScope, seed);
            }
            break;
        case TEST:
            // close the previous one and create a new one
            IOUtils.closeWhileHandlingException(testCluster);
            testCluster = buildTestCluster(currentClusterScope, seed);
            break;
    }
    clusters.put(clazz, testCluster);
    return testCluster;
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:20,代码来源:ESIntegTestCase.java

示例2: copy

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
/**
 * Copies the file <i>src</i> to {@link Directory} <i>to</i> under the new
 * file name <i>dest</i>.
 * <p>
 * If you want to copy the entire source directory to the destination one, you
 * can do so like this:
 * 
 * <pre class="prettyprint">
 * Directory to; // the directory to copy to
 * for (String file : dir.listAll()) {
 *   dir.copy(to, file, newFile, IOContext.DEFAULT); // newFile can be either file, or a new name
 * }
 * </pre>
 * <p>
 * <b>NOTE:</b> this method does not check whether <i>dest</i> exist and will
 * overwrite it if it does.
 */
public void copy(Directory to, String src, String dest, IOContext context) throws IOException {
  IndexOutput os = null;
  IndexInput is = null;
  boolean success = false;
  try {
    os = to.createOutput(dest, context);
    is = openInput(src, context);
    os.copyBytes(is, is.length());
    success = true;
  } finally {
    if (success) {
      IOUtils.close(os, is);
    } else {
      IOUtils.closeWhileHandlingException(os, is);
      try {
        to.deleteFile(dest);
      } catch (Throwable t) {
      }
    }
  }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:39,代码来源:Directory.java

示例3: resolve

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
/**
 * @param type the ec2 hostname type to discover.
 * @return the appropriate host resolved from ec2 meta-data, or null if it cannot be obtained.
 * @see CustomNameResolver#resolveIfPossible(String)
 */
@SuppressForbidden(reason = "We call getInputStream in doPrivileged and provide SocketPermission")
public InetAddress[] resolve(Ec2HostnameType type) throws IOException {
    InputStream in = null;
    String metadataUrl = AwsEc2ServiceImpl.EC2_METADATA_URL + type.ec2Name;
    try {
        URL url = new URL(metadataUrl);
        logger.debug("obtaining ec2 hostname from ec2 meta-data url {}", url);
        URLConnection urlConnection = SocketAccess.doPrivilegedIOException(url::openConnection);
        urlConnection.setConnectTimeout(2000);
        in = SocketAccess.doPrivilegedIOException(urlConnection::getInputStream);
        BufferedReader urlReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));

        String metadataResult = urlReader.readLine();
        if (metadataResult == null || metadataResult.length() == 0) {
            throw new IOException("no gce metadata returned from [" + url + "] for [" + type.configName + "]");
        }
        // only one address: because we explicitly ask for only one via the Ec2HostnameType
        return new InetAddress[] { InetAddress.getByName(metadataResult) };
    } catch (IOException e) {
        throw new IOException("IOException caught when fetching InetAddress from [" + metadataUrl + "]", e);
    } finally {
        IOUtils.closeWhileHandlingException(in);
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:30,代码来源:Ec2NameResolver.java

示例4: CompletionFieldsConsumer

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
public CompletionFieldsConsumer(SegmentWriteState state) throws IOException {
    this.delegatesFieldsConsumer = delegatePostingsFormat.fieldsConsumer(state);
    String suggestFSTFile = IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, EXTENSION);
    IndexOutput output = null;
    boolean success = false;
    try {
        output = state.directory.createOutput(suggestFSTFile, state.context);
        CodecUtil.writeHeader(output, CODEC_NAME, SUGGEST_VERSION_CURRENT);
        /*
         * we write the delegate postings format name so we can load it
         * without getting an instance in the ctor
         */
        output.writeString(delegatePostingsFormat.getName());
        output.writeString(writeProvider.getName());
        this.suggestFieldsConsumer = writeProvider.consumer(output);
        success = true;
    } finally {
        if (!success) {
            IOUtils.closeWhileHandlingException(output);
        }
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:23,代码来源:Completion090PostingsFormat.java

示例5: fieldsProducer

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
@Override
public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException {
  PostingsReaderBase postingsReader = new Lucene41PostingsReader(state.directory,
                                                              state.fieldInfos,
                                                              state.segmentInfo,
                                                              state.context,
                                                              state.segmentSuffix);
  boolean success = false;
  try {
    FieldsProducer ret = new BlockTreeTermsReader(state.directory,
                                                  state.fieldInfos,
                                                  state.segmentInfo,
                                                  postingsReader,
                                                  state.context,
                                                  state.segmentSuffix,
                                                  state.termsIndexDivisor);
    success = true;
    return ret;
  } finally {
    if (!success) {
      IOUtils.closeWhileHandlingException(postingsReader);
    }
  }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:25,代码来源:Lucene41PostingsFormat.java

示例6: analyze

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
/** NOTE: this method closes the TokenStream, even on exception, which is awkward
 *  because really the caller who called {@link Analyzer#tokenStream} should close it,
 *  but when trying that there are recursion issues when we try to use the same
 *  TokenStream twice in the same recursion... */
public static int analyze(TokenStream stream, TokenConsumer consumer) throws IOException {
    int numTokens = 0;
    boolean success = false;
    try {
        stream.reset();
        consumer.reset(stream);
        while (stream.incrementToken()) {
            consumer.nextToken();
            numTokens++;
        }
        consumer.end();
        success = true;
    } finally {
        if (success) {
            stream.close();
        } else {
            IOUtils.closeWhileHandlingException(stream);
        }
    }
    return numTokens;
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:26,代码来源:DirectCandidateGenerator.java

示例7: create

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
public static TranslogWriter create(Type type, ShardId shardId, String translogUUID, long fileGeneration, Path file, Callback<ChannelReference> onClose, int bufferSize, ChannelFactory channelFactory) throws IOException {
    final BytesRef ref = new BytesRef(translogUUID);
    final int headerLength = getHeaderLength(ref.length);
    final FileChannel channel = channelFactory.open(file);
    try {
        // This OutputStreamDataOutput is intentionally not closed because
        // closing it will close the FileChannel
        final OutputStreamDataOutput out = new OutputStreamDataOutput(java.nio.channels.Channels.newOutputStream(channel));
        CodecUtil.writeHeader(out, TRANSLOG_CODEC, VERSION);
        out.writeInt(ref.length);
        out.writeBytes(ref.bytes, ref.offset, ref.length);
        channel.force(true);
        writeCheckpoint(headerLength, 0, file.getParent(), fileGeneration, StandardOpenOption.WRITE);
        final TranslogWriter writer = type.create(shardId, fileGeneration, new ChannelReference(file, fileGeneration, channel, onClose), bufferSize);
        return writer;
    } catch (Throwable throwable){
        // if we fail to bake the file-generation into the checkpoint we stick with the file and once we recover and that
        // file exists we remove it. We only apply this logic to the checkpoint.generation+1 any other file with a higher generation is an error condition
        IOUtils.closeWhileHandlingException(channel);
        throw throwable;
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:23,代码来源:TranslogWriter.java

示例8: trimUnreferencedReaders

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
void trimUnreferencedReaders() {
    try (ReleasableLock ignored = writeLock.acquire()) {
        if (closed.get()) {
            // we're shutdown potentially on some tragic event - don't delete anything
            return;
        }
        long minReferencedGen = outstandingViews.stream().mapToLong(View::minTranslogGeneration).min().orElse(Long.MAX_VALUE);
        minReferencedGen = Math.min(lastCommittedTranslogFileGeneration, minReferencedGen);
        final long finalMinReferencedGen = minReferencedGen;
        List<TranslogReader> unreferenced = readers.stream().filter(r -> r.getGeneration() < finalMinReferencedGen).collect(Collectors.toList());
        for (final TranslogReader unreferencedReader : unreferenced) {
            Path translogPath = unreferencedReader.path();
            logger.trace("delete translog file - not referenced and not current anymore {}", translogPath);
            IOUtils.closeWhileHandlingException(unreferencedReader);
            IOUtils.deleteFilesIgnoringExceptions(translogPath,
                    translogPath.resolveSibling(getCommitCheckpointFileName(unreferencedReader.getGeneration())));
        }
        readers.removeAll(unreferenced);
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:21,代码来源:Translog.java

示例9: analyzeMultitermTerm

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
protected BytesRef analyzeMultitermTerm(String field, String part, Analyzer analyzerIn) {
  if (analyzerIn == null) analyzerIn = getAnalyzer();

  TokenStream source = null;
  try {
    source = analyzerIn.tokenStream(field, part);
    source.reset();
    
    TermToBytesRefAttribute termAtt = source.getAttribute(TermToBytesRefAttribute.class);
    BytesRef bytes = termAtt.getBytesRef();

    if (!source.incrementToken())
      throw new IllegalArgumentException("analyzer returned no terms for multiTerm term: " + part);
    termAtt.fillBytesRef();
    if (source.incrementToken())
      throw new IllegalArgumentException("analyzer returned too many terms for multiTerm term: " + part);
    source.end();
    return BytesRef.deepCopyOf(bytes);
  } catch (IOException e) {
    throw new RuntimeException("Error analyzing multiTerm term: " + part, e);
  } finally {
    IOUtils.closeWhileHandlingException(source);
  }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:25,代码来源:QueryParserBase.java

示例10: copy

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
/**
 * Copy the contents of the given InputStream to the given OutputStream.
 * Closes both streams when done.
 *
 * @param in  the stream to copy from
 * @param out the stream to copy to
 * @return the number of bytes copied
 * @throws IOException in case of I/O errors
 */
public static long copy(InputStream in, OutputStream out, byte[] buffer) throws IOException {
    Objects.requireNonNull(in, "No InputStream specified");
    Objects.requireNonNull(out, "No OutputStream specified");
    boolean success = false;
    try {
        long byteCount = 0;
        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
            byteCount += bytesRead;
        }
        out.flush();
        success = true;
        return byteCount;
    } finally {
        if (success) {
            IOUtils.close(in, out);
        } else {
            IOUtils.closeWhileHandlingException(in, out);
        }
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:32,代码来源:Streams.java

示例11: abort

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
@Override
public void abort() {
  IOUtils.closeWhileHandlingException(this);
  IOUtils.deleteFilesIgnoringExceptions(directory,
      IndexFileNames.segmentFileName(segment, segmentSuffix, FIELDS_EXTENSION),
      IndexFileNames.segmentFileName(segment, segmentSuffix, FIELDS_INDEX_EXTENSION));
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:8,代码来源:CompressingStoredFieldsWriter.java

示例12: ShadowEngine

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
public ShadowEngine(EngineConfig engineConfig)  {
    super(engineConfig);
    SearcherFactory searcherFactory = new EngineSearcherFactory(engineConfig);
    final long nonexistentRetryTime = engineConfig.getIndexSettings()
            .getAsTime(NONEXISTENT_INDEX_RETRY_WAIT, DEFAULT_NONEXISTENT_INDEX_RETRY_WAIT)
            .getMillis();
    try {
        DirectoryReader reader = null;
        store.incRef();
        boolean success = false;
        try {
            if (Lucene.waitForIndex(store.directory(), nonexistentRetryTime)) {
                reader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(store.directory()), shardId);
                this.searcherManager = new SearcherManager(reader, searcherFactory);
                this.lastCommittedSegmentInfos = readLastCommittedSegmentInfos(searcherManager, store);
                success = true;
            } else {
                throw new IllegalStateException("failed to open a shadow engine after" +
                        nonexistentRetryTime + "ms, " +
                        "directory is not an index");
            }
        } catch (Throwable e) {
            logger.warn("failed to create new reader", e);
            throw e;
        } finally {
            if (success == false) {
                IOUtils.closeWhileHandlingException(reader);
                store.decRef();
            }
        }
    } catch (IOException ex) {
        throw new EngineCreationFailureException(shardId, "failed to open index reader", ex);
    }
    logger.trace("created new ShadowEngine");
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:36,代码来源:ShadowEngine.java

示例13: write

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
/** Save a single segment's info. */
@Override
public void write(Directory dir, SegmentInfo si, FieldInfos fis, IOContext ioContext) throws IOException {
  final String fileName = IndexFileNames.segmentFileName(si.name, "", Lucene46SegmentInfoFormat.SI_EXTENSION);
  si.addFile(fileName);

  final IndexOutput output = dir.createOutput(fileName, ioContext);

  boolean success = false;
  try {
    CodecUtil.writeHeader(output, Lucene46SegmentInfoFormat.CODEC_NAME, Lucene46SegmentInfoFormat.VERSION_CURRENT);
    Version version = si.getVersion();
    if (version.major < 3 || version.major > 4) {
      throw new IllegalArgumentException("invalid major version: should be 3 or 4 but got: " + version.major + " segment=" + si);
    }
    // Write the Lucene version that created this segment, since 3.1
    output.writeString(version.toString());
    output.writeInt(si.getDocCount());

    output.writeByte((byte) (si.getUseCompoundFile() ? SegmentInfo.YES : SegmentInfo.NO));
    output.writeStringStringMap(si.getDiagnostics());
    output.writeStringSet(si.files());
    CodecUtil.writeFooter(output);
    success = true;
  } finally {
    if (!success) {
      IOUtils.closeWhileHandlingException(output);
      // TODO: are we doing this outside of the tracking wrapper? why must SIWriter cleanup like this?
      IOUtils.deleteFilesIgnoringExceptions(si.dir, fileName);
    } else {
      output.close();
    }
  }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:35,代码来源:Lucene46SegmentInfoWriter.java

示例14: mergeNorms

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
private void mergeNorms(SegmentWriteState segmentWriteState) throws IOException {
  DocValuesConsumer consumer = codec.normsFormat().normsConsumer(segmentWriteState);
  boolean success = false;
  try {
    for (FieldInfo field : mergeState.fieldInfos) {
      if (field.hasNorms()) {
        List<NumericDocValues> toMerge = new ArrayList<>();
        List<Bits> docsWithField = new ArrayList<>();
        for (AtomicReader reader : mergeState.readers) {
          NumericDocValues norms = reader.getNormValues(field.name);
          if (norms == null) {
            norms = DocValues.emptyNumeric();
          }
          toMerge.add(norms);
          docsWithField.add(new Bits.MatchAllBits(reader.maxDoc()));
        }
        consumer.mergeNumericField(field, mergeState, toMerge, docsWithField);
      }
    }
    success = true;
  } finally {
    if (success) {
      IOUtils.close(consumer);
    } else {
      IOUtils.closeWhileHandlingException(consumer);            
    }
  }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:29,代码来源:SegmentMerger.java

示例15: closeInternal

import org.apache.lucene.util.IOUtils; //导入方法依赖的package包/类
@Override
protected void closeInternal() {
    try {
        IOUtils.closeWhileHandlingException(channel);
    } finally {
        if (onClose != null) {
            onClose.handle(this);
        }
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:11,代码来源:ChannelReference.java


注:本文中的org.apache.lucene.util.IOUtils.closeWhileHandlingException方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。