<commit_before>// Copyright (c) 2012, Cloudera, inc.

#include <boost/assign/list_of.hpp>
#include <boost/lexical_cast.hpp>
#include <gflags/gflags.h>
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <tr1/unordered_set>
#include <time.h>

#include "common/row.h"
#include "common/schema.h"
#include "tablet/layer.h"
#include "util/env.h"
#include "util/status.h"
#include "util/stopwatch.h"
#include "util/test_macros.h"

DEFINE_int32(roundtrip_num_rows, 10000,
             "Number of rows to use for the round-trip test");

namespace kudu {
namespace tablet {

using std::tr1::unordered_set;

static Schema CreateTestSchema() {
  ColumnSchema col1("key", STRING);
  ColumnSchema col2("val", UINT32);

  vector<ColumnSchema> cols = boost::assign::list_of
    (col1)(col2);
  return Schema(cols, 1);
}


class TestLayer : public ::testing::Test {
public:
  TestLayer() :
    ::testing::Test(),
    schema_(CreateTestSchema()),
    env_(Env::Default()),
    n_rows_(FLAGS_roundtrip_num_rows)
  {
    CHECK_GT(n_rows_, 0);
  }

protected:
  virtual void SetUp() {
    ASSERT_STATUS_OK(env_->GetTestDirectory(&test_dir_));
    test_dir_ += "/TestLayer.TestLayerRoundTrip." +
      boost::lexical_cast<string>(time(NULL));
  }

  // Write out a test layer with n_rows_ rows.
  // The data in the layer looks like:
  //   ("hello <00n>", <n>)
  // ... where n is the index of the row in the layer
  // The string values are padded out to 15 digits
  void WriteTestLayer() {
    // Write rows into a new Layer.
    LOG_TIMING(INFO, "Writing layer") {
      LayerWriter lw(env_, schema_, test_dir_);

      ASSERT_STATUS_OK(lw.Open());

      char buf[256];
      RowBuilder rb(schema_);
      for (int i = 0; i < n_rows_; i++) {
        rb.Reset();
        FormatKey(i, buf, sizeof(buf));
        rb.AddString(Slice(buf));
        rb.AddUint32(i);
        ASSERT_STATUS_OK_FAST(lw.WriteRow(rb.data()));
      }
      ASSERT_STATUS_OK(lw.Finish());
    }
  }

  // Picks some number of rows from the given layer and updates
  // them. Stores the indexes of the updated rows in *updated.
  void UpdateExistingRows(Layer *l, float update_ratio,
                          unordered_set<uint32_t> *updated) {<commit_msg>Fix layer-test to generate properly unique test dirs<commit_after>// Copyright (c) 2012, Cloudera, inc.

#include <boost/assign/list_of.hpp>
#include <gflags/gflags.h>
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <tr1/unordered_set>
#include <time.h>

#include "common/row.h"
#include "common/schema.h"
#include "gutil/stringprintf.h"
#include "tablet/layer.h"
#include "util/env.h"
#include "util/status.h"
#include "util/stopwatch.h"
#include "util/test_macros.h"

DEFINE_int32(roundtrip_num_rows, 10000,
             "Number of rows to use for the round-trip test");

namespace kudu {
namespace tablet {

using std::tr1::unordered_set;

static Schema CreateTestSchema() {
  ColumnSchema col1("key", STRING);
  ColumnSchema col2("val", UINT32);

  vector<ColumnSchema> cols = boost::assign::list_of
    (col1)(col2);
  return Schema(cols, 1);
}


class TestLayer : public ::testing::Test {
public:
  TestLayer() :
    ::testing::Test(),
    schema_(CreateTestSchema()),
    env_(Env::Default()),
    n_rows_(FLAGS_roundtrip_num_rows)
  {
    CHECK_GT(n_rows_, 0);
  }

protected:
  virtual void SetUp() {
    const ::testing::TestInfo* const test_info =
      ::testing::UnitTest::GetInstance()->current_test_info();

    ASSERT_STATUS_OK(env_->GetTestDirectory(&test_dir_));

    test_dir_ += StringPrintf(
      "/TestLayer.%s.%ld", test_info->name(), time(NULL));

  }

  // Write out a test layer with n_rows_ rows.
  // The data in the layer looks like:
  //   ("hello <00n>", <n>)
  // ... where n is the index of the row in the layer
  // The string values are padded out to 15 digits
  void WriteTestLayer() {
    // Write rows into a new Layer.
    LOG_TIMING(INFO, "Writing layer") {
      LayerWriter lw(env_, schema_, test_dir_);

      ASSERT_STATUS_OK(lw.Open());

      char buf[256];
      RowBuilder rb(schema_);
      for (int i = 0; i < n_rows_; i++) {
        rb.Reset();
        FormatKey(i, buf, sizeof(buf));
        rb.AddString(Slice(buf));
        rb.AddUint32(i);
        ASSERT_STATUS_OK_FAST(lw.WriteRow(rb.data()));
      }
      ASSERT_STATUS_OK(lw.Finish());
    }
  }

  // Picks some number of rows from the given layer and updates
  // them. Stores the indexes of the updated rows in *updated.
  void UpdateExistingRows(Layer *l, float update_ratio,
                          unordered_set<uint32_t> *updated) {
{'fraction_non_alphanumeric': 0.14030111280820423, 'fraction_numerical': 0.014401047348898102, 'mean_word_length': 3.3698760724499524, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7357982', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1554, 'n_words': 390}