metron-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From justinleet <...@git.apache.org>
Subject [GitHub] incubator-metron pull request #256: Metron 257 Enable pcap result pagination...
Date Fri, 16 Sep 2016 12:29:23 GMT
Github user justinleet commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/256#discussion_r79158991
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/hadoop/SequenceFileIterable.java
---
    @@ -0,0 +1,138 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.metron.common.hadoop;
    +
    +import com.google.common.collect.Iterators;
    +import org.apache.hadoop.conf.Configuration;
    +import org.apache.hadoop.fs.FileSystem;
    +import org.apache.hadoop.fs.Path;
    +import org.apache.hadoop.io.BytesWritable;
    +import org.apache.hadoop.io.LongWritable;
    +import org.apache.hadoop.io.SequenceFile;
    +import org.apache.log4j.Logger;
    +
    +import java.io.IOException;
    +import java.util.Iterator;
    +import java.util.List;
    +import java.util.NoSuchElementException;
    +
    +import static java.lang.String.format;
    +
    +public class SequenceFileIterable implements Iterable<byte[]> {
    +  private static final Logger LOGGER = Logger.getLogger(SequenceFileIterable.class);
    +  private List<Path> files;
    +  private Configuration config;
    +
    +  public SequenceFileIterable(List<Path> files, Configuration config) {
    +    this.files = files;
    +    this.config = config;
    +  }
    +
    +  @Override
    +  public Iterator<byte[]> iterator() {
    +    return Iterators.concat(getIterators(files, config));
    +  }
    +
    +  private Iterator<byte[]>[] getIterators(List<Path> files, Configuration
config) {
    +    return files.stream().map(f -> new SequenceFileIterator(f, config)).toArray(Iterator[]::new);
    +  }
    +
    +  /**
    +   * Cleans up all files read by this Iterable
    +   *
    +   * @return true if success, false if any files were not deleted
    +   * @throws IOException
    +   */
    +  public boolean cleanup() throws IOException {
    +    FileSystem fileSystem = FileSystem.get(config);
    +    boolean success = true;
    +    for (Path file : files) {
    +      success &= fileSystem.delete(file, false);
    +    }
    +    return success;
    +  }
    +
    +  private static class SequenceFileIterator implements Iterator<byte[]> {
    +    private Path path;
    +    private Configuration config;
    +    private SequenceFile.Reader reader;
    +    private LongWritable key = new LongWritable();
    +    private BytesWritable value = new BytesWritable();
    +    private byte[] next;
    +    private boolean finished = false;
    +
    +    public SequenceFileIterator(Path path, Configuration config) {
    +      this.path = path;
    +      this.config = config;
    +    }
    +
    +    @Override
    +    public boolean hasNext() {
    +      if (!finished && null == reader) {
    +        try {
    +          reader = new SequenceFile.Reader(config, SequenceFile.Reader.file(path));
    +          LOGGER.debug("Writing file: " + path.toString());
    +        } catch (IOException e) {
    +          throw new RuntimeException("Failed to get reader", e);
    +        }
    +      } else {
    +        LOGGER.debug(format("finished=%s, reader=%s, next=%s", finished, reader, next));
    +      }
    +      try {
    +        //ensure hasnext is idempotent
    +        if (!finished) {
    +          if (null == next && reader.next(key, value)) {
    +            next = value.copyBytes();
    +          } else if (null == next) {
    +            close();
    +          }
    +        }
    +      } catch (IOException e) {
    +        close();
    +        throw new RuntimeException("Failed to get next record", e);
    +      }
    +      return (null != next);
    +    }
    +
    +    private void close() {
    +      LOGGER.debug("Closing file: " + path.toString());
    +      finished = true;
    +      try {
    +        if (reader != null) {
    +          reader.close();
    +          reader = null;
    +        }
    +      } catch (IOException e) {
    +        // ah well, we tried...
    --- End diff --
    
    Can we log this Exception?  I'm not sure we can do anything about it, but it would be
nice to be able to ensure we can see it.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message