Skip to content

Commit 3a97d1f

Browse files
committed
Test that RW-mode in group-based mode still works
Also simplify control flow in JSONIOHandler to work more similar to RW mode
1 parent f9397d1 commit 3a97d1f

File tree

2 files changed

+109
-17
lines changed

2 files changed

+109
-17
lines changed

src/IO/JSON/JSONIOHandlerImpl.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,25 +123,17 @@ namespace openPMD
123123
"[JSON] Could not create directory." );
124124
}
125125

126-
associateWithFile(
127-
writable,
128-
shared_name
129-
);
130-
this->m_dirty
131-
.emplace( shared_name );
132-
// make sure to overwrite!
133-
this->m_jsonVals[shared_name] =
134-
std::make_shared< nlohmann::json >( );
135-
136-
// start off with already written contents if available
137-
// @todo Do the same in READ_WRITE mode in ::openFile()?
138-
auto const & fullName = fullPath(name);
139-
if( m_handler->m_backendAccess == Access::APPEND
140-
&& auxiliary::file_exists(fullName))
126+
associateWithFile( writable, shared_name );
127+
this->m_dirty.emplace( shared_name );
128+
129+
if( m_handler->m_backendAccess != Access::APPEND )
141130
{
142-
std::ifstream handle(fullName);
143-
handle >> *this->m_jsonVals[shared_name];
131+
// make sure to overwrite!
132+
this->m_jsonVals[ shared_name ] =
133+
std::make_shared< nlohmann::json >();
144134
}
135+
// else: the JSON value is not available in m_jsonVals and will be
136+
// read from the file later on before overwriting
145137

146138
writable->written = true;
147139
writable->abstractFilePosition =

test/SerialIOTest.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4855,3 +4855,103 @@ TEST_CASE( "append_mode_filebased", "[serial]" )
48554855
append_mode_filebased( t );
48564856
}
48574857
}
4858+
4859+
void groupbased_read_write( std::string const & ext )
4860+
{
4861+
int data = 0;
4862+
Dataset ds( Datatype::INT, { 1 } );
4863+
std::string filename = "../samples/groupbased_read_write." + ext;
4864+
4865+
{
4866+
Series write( filename, Access::CREATE );
4867+
auto E_x = write.iterations[ 0 ].meshes[ "E" ][ "x" ];
4868+
auto E_y = write.iterations[ 0 ].meshes[ "E" ][ "y" ];
4869+
E_x.resetDataset( ds );
4870+
E_y.resetDataset( ds );
4871+
E_x.storeChunk( shareRaw( &data ), { 0 }, { 1 } );
4872+
E_y.storeChunk( shareRaw( &data ), { 0 }, { 1 } );
4873+
4874+
E_x.setAttribute( "updated_in_run", 0 );
4875+
E_y.setAttribute( "updated_in_run", 0 );
4876+
}
4877+
4878+
{
4879+
Series write( filename, Access::READ_WRITE );
4880+
// create a new iteration
4881+
auto E_x = write.iterations[ 1 ].meshes[ "E" ][ "x" ];
4882+
E_x.resetDataset( ds );
4883+
4884+
// overwrite old dataset
4885+
auto E_y = write.iterations[ 0 ].meshes[ "E" ][ "y" ];
4886+
4887+
data = 1;
4888+
4889+
E_x.storeChunk( shareRaw( &data ), { 0 }, { 1 } );
4890+
E_y.storeChunk( shareRaw( &data ), { 0 }, { 1 } );
4891+
4892+
E_x.setAttribute( "updated_in_run", 1 );
4893+
E_y.setAttribute( "updated_in_run", 1 );
4894+
}
4895+
4896+
{
4897+
Series read( filename, Access::READ_ONLY );
4898+
auto E_x_0_fromRun0 = read.iterations[ 0 ].meshes[ "E" ][ "x" ];
4899+
auto E_x_1_fromRun1 = read.iterations[ 1 ].meshes[ "E" ][ "x" ];
4900+
auto E_y_0_fromRun1 = read.iterations[ 0 ].meshes[ "E" ][ "y" ];
4901+
4902+
REQUIRE(
4903+
E_x_0_fromRun0.getAttribute( "updated_in_run" ).get< int >() == 0 );
4904+
REQUIRE(
4905+
E_x_1_fromRun1.getAttribute( "updated_in_run" ).get< int >() == 1 );
4906+
REQUIRE(
4907+
E_y_0_fromRun1.getAttribute( "updated_in_run" ).get< int >() == 1 );
4908+
4909+
auto chunk_E_x_0_fromRun0 =
4910+
E_x_0_fromRun0.loadChunk< int >( { 0 }, { 1 } );
4911+
auto chunk_E_x_1_fromRun1 =
4912+
E_x_1_fromRun1.loadChunk< int >( { 0 }, { 1 } );
4913+
auto chunk_E_y_0_fromRun1 =
4914+
E_y_0_fromRun1.loadChunk< int >( { 0 }, { 1 } );
4915+
4916+
read.flush();
4917+
4918+
REQUIRE( *chunk_E_x_0_fromRun0 == 0 );
4919+
REQUIRE( *chunk_E_x_1_fromRun1 == 1 );
4920+
REQUIRE( *chunk_E_y_0_fromRun1 == 1 );
4921+
}
4922+
4923+
// check that truncation works correctly
4924+
{
4925+
Series write( filename, Access::CREATE );
4926+
// create a new iteration
4927+
auto E_x = write.iterations[ 2 ].meshes[ "E" ][ "x" ];
4928+
E_x.resetDataset( ds );
4929+
4930+
data = 2;
4931+
4932+
E_x.storeChunk( shareRaw( &data ), { 0 }, { 1 } );
4933+
E_x.setAttribute( "updated_in_run", 2 );
4934+
}
4935+
4936+
{
4937+
Series read( filename, Access::READ_ONLY );
4938+
REQUIRE( read.iterations.size() == 1 );
4939+
REQUIRE( read.iterations.count( 2 ) == 1 );
4940+
}
4941+
}
4942+
4943+
TEST_CASE( "groupbased_read_write", "[serial]" )
4944+
{
4945+
constexpr char const * supportsGroupbasedRW[] = { "h5", "json" };
4946+
for( auto const & t : testedFileExtensions() )
4947+
{
4948+
for( auto const supported : supportsGroupbasedRW )
4949+
{
4950+
if( t == supported )
4951+
{
4952+
groupbased_read_write( t );
4953+
break;
4954+
}
4955+
}
4956+
}
4957+
}

0 commit comments

Comments
 (0)