<commit_before>/* Copyright 2016 <NAME>, NVIDIA Corporation
 *
 * Licensed 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.
 */

#include "lightscan/ingest.h"
#include "lightscan/engine.h"

#include "lightscan/storage/storage_config.h"
#include "lightscan/storage/storage_backend.h"
#include "lightscan/util/common.h"
#include "lightscan/util/video.h"
#include "lightscan/util/caffe.h"
#include "lightscan/util/queue.h"
#include "lightscan/util/profiler.h"

#include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/errors.hpp>

#include <cuda.h>
#include "lightscan/util/cuda.h"

#include <mpi.h>
#include <cstdlib>
#include <string>
#include <libgen.h>
#include <atomic>

extern "C" {
#include <libavformat/avformat.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}

using namespace lightscan;

namespace po = boost::program_options;

namespace {

int read_last_processed_video(
  StorageBackend* storage,
  const std::string& dataset_name)
{
  StoreResult result;

  const std::string last_written_path =
    dataset_name + "_dataset/last_written.bin";
  std::unique_ptr<RandomReadFile> file;
  result = make_unique_random_read_file(storage, last_written_path, file);

  if (result == StoreResult::FileDoesNotExist) {
    return -1;
  }

  uint64_t pos = 0;
  size_t size_read;

  int32_t last_processed_video;
  EXP_BACKOFF(
    file->read(pos,
               sizeof(int32_t),
               reinterpret_cast<char*>(&last_processed_video),
               size_read),
    result);
  assert(result == StoreResult::Success ||
         result == StoreResult::EndOfFile);
  assert(size_read == sizeof(int32_t));

  return last_processed_video;
}

void write_last_processed_video(
  StorageBackend* storage,
  const std::string& dataset_name,
  int file_index)
{
  const std::string last_written_path =
    dataset_name + "_dataset/last_written.bin";<commit_msg>Handle file DNE case<commit_after>/* Copyright 2016 <NAME>, NVIDIA Corporation
 *
 * Licensed 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.
 */

#include "lightscan/ingest.h"
#include "lightscan/engine.h"

#include "lightscan/storage/storage_config.h"
#include "lightscan/storage/storage_backend.h"
#include "lightscan/util/common.h"
#include "lightscan/util/video.h"
#include "lightscan/util/caffe.h"
#include "lightscan/util/queue.h"
#include "lightscan/util/profiler.h"

#include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/errors.hpp>

#include <cuda.h>
#include "lightscan/util/cuda.h"

#include <mpi.h>
#include <cstdlib>
#include <string>
#include <libgen.h>
#include <atomic>

extern "C" {
#include <libavformat/avformat.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}

using namespace lightscan;

namespace po = boost::program_options;

namespace {

int read_last_processed_video(
  StorageBackend* storage,
  const std::string& dataset_name)
{
  StoreResult result;

  const std::string last_written_path =
    dataset_name + "_dataset/last_written.bin";

  // File will not exist when first running ingest so check first
  // and return default value if not there
  FileInfo info;
  result = storage->get_file_info(last_written_path, info);
  (void) info;
  if (result == StoreResult::FileDoesNotExist) {
    return -1;
  }

  std::unique_ptr<RandomReadFile> file;
  result = make_unique_random_read_file(storage, last_written_path, file);

  uint64_t pos = 0;
  size_t size_read;

  int32_t last_processed_video;
  EXP_BACKOFF(
    file->read(pos,
               sizeof(int32_t),
               reinterpret_cast<char*>(&last_processed_video),
               size_read),
    result);
  assert(result == StoreResult::Success ||
         result == StoreResult::EndOfFile);
  assert(size_read == sizeof(int32_t));

  return last_processed_video;
}

void write_last_processed_video(
  StorageBackend* storage,
  const std::string& dataset_name,
  int file_index)
{
  const std::string last_written_path =
    dataset_name + "_dataset/last_written.bin";
{'fraction_non_alphanumeric': 0.1088500671913995, 'fraction_numerical': 0.006911115377231714, 'mean_word_length': 4.554371002132196, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1412142', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1642, 'n_words': 425}