Skip to content

Commit

Permalink
Add class UniqueDirectory to create a unique, temporary directory wit…
Browse files Browse the repository at this point in the history
…h the PRNG
  • Loading branch information
bekadavis9 committed Nov 7, 2023
1 parent c377557 commit 3d0c3b6
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 112 deletions.
86 changes: 34 additions & 52 deletions test/src/unit-capi-nullable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* The MIT License
*
* @copyright Copyright (c) 2020-2021 TileDB, Inc.
* @copyright Copyright (c) 2020-2023 TileDB, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -32,10 +32,13 @@

#include <test/support/tdb_catch.h>
#include "test/support/src/helpers.h"
#include "tiledb/api/c_api/vfs/vfs_api_internal.h"
#include "tiledb/sm/c_api/tiledb.h"
#include "tiledb/sm/filesystem/unique_directory.h"
#include "tiledb/sm/misc/utils.h"

#include <iostream>
#include <optional>
#include <vector>

using namespace std;
Expand All @@ -44,8 +47,6 @@ using namespace tiledb::test;

class NullableArrayFx {
public:
const string& FILE_TEMP_DIR = tiledb::test::get_temp_path();

// Serialization parameters
bool serialize_ = false;
bool refactored_query_v2_ = false;
Expand Down Expand Up @@ -144,32 +145,21 @@ class NullableArrayFx {
/** The C-API VFS object. */
tiledb_vfs_t* vfs_;

/**
* Creates a directory using `vfs_`.
*
* @param path The directory path.
*/
void create_dir(const string& path);

/**
* Removes a directory using `vfs_`.
*
* @param path The directory path.
*/
void remove_dir(const string& path);
/** The unique directory object. */
std::optional<UniqueDirectory> temp_dir_{std::nullopt};

/**
* Creates a TileDB array.
*
* @param array_name The name of the array.
* @param array_path The path to the array.
* @param array_type The type of the array (dense/sparse).
* @param test_dims The dimensions in the array.
* @param test_attrs The attributes in the array.
* @param cell_order The cell order of the array.
* @param tile_order The tile order of the array.
*/
void create_array(
const string& array_name,
const string& array_path,
tiledb_array_type_t array_type,
const vector<test_dim_t>& test_dims,
const vector<test_attr_t>& test_attrs,
Expand All @@ -179,24 +169,24 @@ class NullableArrayFx {
/**
* Creates and executes a single write query.
*
* @param array_name The name of the array.
* @param array_path The path to the array.
* @param test_query_buffers The query buffers to write.
* @param layout The write layout.
*/
void write(
const string& array_name,
const string& array_path,
const vector<test_query_buffer_t>& test_query_buffers,
tiledb_layout_t layout);

/**
* Creates and executes a single read query.
*
* @param array_name The name of the array.
* @param array_path The path to the array.
* @param test_query_buffers The query buffers to read.
* @param subarray The subarray to read.
*/
void read(
const string& array_name,
const string& array_path,
const vector<test_query_buffer_t>& test_query_buffers,
const void* subarray);
};
Expand All @@ -217,35 +207,30 @@ NullableArrayFx::NullableArrayFx() {

tiledb_config_free(&config);

// Create the UniqueDirectory.
temp_dir_.emplace(*vfs_->vfs());

serialize_ = false;
}

NullableArrayFx::~NullableArrayFx() {
remove_dir(FILE_TEMP_DIR);
tiledb_ctx_free(&ctx_);
tiledb_vfs_free(&vfs_);
}

void NullableArrayFx::create_dir(const string& path) {
REQUIRE(tiledb_vfs_create_dir(ctx_, vfs_, path.c_str()) == TILEDB_OK);
}

void NullableArrayFx::remove_dir(const string& path) {
int is_dir = 0;
REQUIRE(tiledb_vfs_is_dir(ctx_, vfs_, path.c_str(), &is_dir) == TILEDB_OK);
if (is_dir)
REQUIRE(tiledb_vfs_remove_dir(ctx_, vfs_, path.c_str()) == TILEDB_OK);
}

void NullableArrayFx::create_array(
const string& array_name,
const string& array_path,
const tiledb_array_type_t array_type,
const vector<test_dim_t>& test_dims,
const vector<test_attr_t>& test_attrs,
const tiledb_layout_t cell_order,
const tiledb_layout_t tile_order) {
remove_dir(FILE_TEMP_DIR);
create_dir(FILE_TEMP_DIR);
// Remove array if it already exists
int is_dir = 0;
REQUIRE(
tiledb_vfs_is_dir(ctx_, vfs_, array_path.c_str(), &is_dir) == TILEDB_OK);
if (is_dir)
REQUIRE(tiledb_vfs_remove_dir(ctx_, vfs_, array_path.c_str()) == TILEDB_OK);

// Create the dimensions.
vector<tiledb_dimension_t*> dims;
Expand Down Expand Up @@ -313,8 +298,7 @@ void NullableArrayFx::create_array(
REQUIRE(rc == TILEDB_OK);

// Create array
rc = tiledb_array_create(
ctx_, (FILE_TEMP_DIR + array_name).c_str(), array_schema);
rc = tiledb_array_create(ctx_, array_path.c_str(), array_schema);
REQUIRE(rc == TILEDB_OK);

// Free attributes.
Expand All @@ -335,13 +319,12 @@ void NullableArrayFx::create_array(
}

void NullableArrayFx::write(
const string& array_name,
const string& array_path,
const vector<test_query_buffer_t>& test_query_buffers,
const tiledb_layout_t layout) {
// Open the array for writing.
tiledb_array_t* array;
int rc =
tiledb_array_alloc(ctx_, (FILE_TEMP_DIR + array_name).c_str(), &array);
int rc = tiledb_array_alloc(ctx_, array_path.c_str(), &array);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_array_open(ctx_, array, TILEDB_WRITE);
REQUIRE(rc == TILEDB_OK);
Expand Down Expand Up @@ -427,7 +410,7 @@ void NullableArrayFx::write(
// Submit query
rc = submit_query_wrapper(
ctx_,
FILE_TEMP_DIR + array_name,
array_path,
&query,
server_buffers_,
serialize_,
Expand All @@ -442,13 +425,12 @@ void NullableArrayFx::write(
}

void NullableArrayFx::read(
const string& array_name,
const string& array_path,
const vector<test_query_buffer_t>& test_query_buffers,
const void* const subarray) {
// Open the array for reading.
tiledb_array_t* array;
int rc =
tiledb_array_alloc(ctx_, (FILE_TEMP_DIR + array_name).c_str(), &array);
int rc = tiledb_array_alloc(ctx_, array_path.c_str(), &array);
REQUIRE(rc == TILEDB_OK);
rc = tiledb_array_open(ctx_, array, TILEDB_READ);
REQUIRE(rc == TILEDB_OK);
Expand Down Expand Up @@ -535,7 +517,7 @@ void NullableArrayFx::read(
// Submit query
rc = submit_query_wrapper(
ctx_,
FILE_TEMP_DIR + array_name,
array_path,
&query,
server_buffers_,
serialize_,
Expand All @@ -555,7 +537,7 @@ void NullableArrayFx::do_2d_nullable_test(
const tiledb_layout_t cell_order,
const tiledb_layout_t tile_order,
const tiledb_layout_t write_order) {
const string array_name = "2d_nullable_array";
const string array_path = temp_dir_.value().path() + "2d_nullable_array";

// Skip row-major and col-major writes for sparse arrays.
if (array_type == TILEDB_SPARSE &&
Expand All @@ -578,7 +560,7 @@ void NullableArrayFx::do_2d_nullable_test(

// Create the array.
create_array(
array_name, array_type, test_dims, test_attrs, cell_order, tile_order);
array_path, array_type, test_dims, test_attrs, cell_order, tile_order);

// Define the write query buffers for "a1".
vector<test_query_buffer_t> write_query_buffers;
Expand Down Expand Up @@ -703,7 +685,7 @@ void NullableArrayFx::do_2d_nullable_test(
}

// Execute the write query.
write(array_name, write_query_buffers, write_order);
write(array_path, write_query_buffers, write_order);

// Define the read query buffers for "a1".
vector<test_query_buffer_t> read_query_buffers;
Expand Down Expand Up @@ -756,7 +738,7 @@ void NullableArrayFx::do_2d_nullable_test(

// Execute a read query over the entire domain.
const uint64_t subarray_full[] = {1, 4, 1, 4};
read(array_name, read_query_buffers, subarray_full);
read(array_path, read_query_buffers, subarray_full);

// Each value in `a1_read_buffer` corresponds to its index in
// the original `a1_write_buffer`. Check that the ordering of
Expand Down Expand Up @@ -811,7 +793,7 @@ void NullableArrayFx::do_2d_nullable_test(

// Execute a read query over a partial domain.
const uint64_t subarray_partial[] = {2, 3, 2, 3};
read(array_name, read_query_buffers, subarray_partial);
read(array_path, read_query_buffers, subarray_partial);

// Each value in `a1_read_buffer` corresponds to its index in
// the original `a1_write_buffer`. Check that the ordering of
Expand Down
58 changes: 21 additions & 37 deletions test/src/unit-capi-smoke-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* The MIT License
*
* @copyright Copyright (c) 2021-2022 TileDB, Inc.
* @copyright Copyright (c) 2021-2023 TileDB, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -33,11 +33,14 @@

#include <test/support/tdb_catch.h>
#include "test/support/src/helpers.h"
#include "tiledb/api/c_api/vfs/vfs_api_internal.h"
#include "tiledb/sm/c_api/tiledb.h"
#include "tiledb/sm/enums/encryption_type.h"
#include "tiledb/sm/filesystem/unique_directory.h"
#include "tiledb/sm/misc/utils.h"

#include <iostream>
#include <optional>
#include <vector>

using namespace std;
Expand All @@ -49,8 +52,6 @@ static const string array_name = "smoke_test_array";

class SmokeTestFx {
public:
const string& FILE_TEMP_DIR = tiledb::test::get_temp_path();

/**
* Wraps data to build a dimension with the C-API.
*/
Expand Down Expand Up @@ -215,19 +216,8 @@ class SmokeTestFx {
/** The C-API VFS object. */
tiledb_vfs_t* vfs_;

/**
* Creates a directory using `vfs_`.
*
* @param path The directory path.
*/
void create_dir(const string& path);

/**
* Removes a directory using `vfs_`.
*
* @param path The directory path.
*/
void remove_dir(const string& path);
/** The unique directory object. */
std::optional<UniqueDirectory> temp_dir_{std::nullopt};

/**
* Creates a TileDB array.
Expand Down Expand Up @@ -387,36 +377,31 @@ SmokeTestFx::SmokeTestFx() {

// Create the VFS.
REQUIRE(tiledb_vfs_alloc(ctx_, config, &vfs_) == TILEDB_OK);

tiledb_config_free(&config);

// Create the UniqueDirectory.
temp_dir_.emplace(*vfs_->vfs());
}

SmokeTestFx::~SmokeTestFx() {
remove_dir(FILE_TEMP_DIR);
tiledb_ctx_free(&ctx_);
tiledb_vfs_free(&vfs_);
}

void SmokeTestFx::create_dir(const string& path) {
REQUIRE(tiledb_vfs_create_dir(ctx_, vfs_, path.c_str()) == TILEDB_OK);
}

void SmokeTestFx::remove_dir(const string& path) {
int is_dir = 0;
REQUIRE(tiledb_vfs_is_dir(ctx_, vfs_, path.c_str(), &is_dir) == TILEDB_OK);
if (is_dir)
REQUIRE(tiledb_vfs_remove_dir(ctx_, vfs_, path.c_str()) == TILEDB_OK);
}

void SmokeTestFx::create_array(
tiledb_array_type_t array_type,
const vector<test_dim_t>& test_dims,
const vector<test_attr_t>& test_attrs,
tiledb_layout_t cell_order,
tiledb_layout_t tile_order,
tiledb_encryption_type_t encryption_type) {
remove_dir(FILE_TEMP_DIR);
create_dir(FILE_TEMP_DIR);
// Remove array if it already exists
const string array_path = temp_dir_.value().path() + array_name;
int is_dir = 0;
REQUIRE(
tiledb_vfs_is_dir(ctx_, vfs_, array_path.c_str(), &is_dir) == TILEDB_OK);
if (is_dir)
REQUIRE(tiledb_vfs_remove_dir(ctx_, vfs_, array_path.c_str()) == TILEDB_OK);

// Create the dimensions.
vector<tiledb_dimension_t*> dims;
Expand Down Expand Up @@ -504,8 +489,7 @@ void SmokeTestFx::create_array(
REQUIRE(tiledb_ctx_alloc(config, &ctx_) == TILEDB_OK);
tiledb_config_free(&config);
}
rc = tiledb_array_create(
ctx_, (FILE_TEMP_DIR + array_name).c_str(), array_schema);
rc = tiledb_array_create(ctx_, array_path.c_str(), array_schema);
REQUIRE(rc == TILEDB_OK);

// Free attributes.
Expand All @@ -531,8 +515,8 @@ void SmokeTestFx::write(
tiledb_encryption_type_t encryption_type) {
// Open the array for writing (with or without encryption).
tiledb_array_t* array;
int rc =
tiledb_array_alloc(ctx_, (FILE_TEMP_DIR + array_name).c_str(), &array);
int rc = tiledb_array_alloc(
ctx_, (temp_dir_.value().path() + array_name).c_str(), &array);
REQUIRE(rc == TILEDB_OK);
if (encryption_type != TILEDB_NO_ENCRYPTION) {
tiledb_config_t* cfg;
Expand Down Expand Up @@ -661,8 +645,8 @@ void SmokeTestFx::read(
tiledb_query_condition_combination_op_t combination_op) {
// Open the array for reading (with or without encryption).
tiledb_array_t* array;
int rc =
tiledb_array_alloc(ctx_, (FILE_TEMP_DIR + array_name).c_str(), &array);
int rc = tiledb_array_alloc(
ctx_, (temp_dir_.value().path() + array_name).c_str(), &array);
REQUIRE(rc == TILEDB_OK);
if (encryption_type != TILEDB_NO_ENCRYPTION) {
tiledb_config_t* cfg;
Expand Down
Loading

0 comments on commit 3d0c3b6

Please sign in to comment.