diff --git a/flatdata-cpp/include/flatdata/ArchiveBuilder.h b/flatdata-cpp/include/flatdata/ArchiveBuilder.h index c84ba3df..1cbbe875 100644 --- a/flatdata-cpp/include/flatdata/ArchiveBuilder.h +++ b/flatdata-cpp/include/flatdata/ArchiveBuilder.h @@ -52,6 +52,7 @@ class ArchiveBuilder const flatdata::ResourceStorage& storage( ) const; bool is_created( ) const; void check_created( ) const; + bool clean_up_signature( flatdata::ResourceStorage& storage ); private: std::shared_ptr< flatdata::ResourceStorage > m_storage; diff --git a/flatdata-cpp/include/flatdata/FileResourceStorage.h b/flatdata-cpp/include/flatdata/FileResourceStorage.h index 699541d5..aedb5c3b 100644 --- a/flatdata-cpp/include/flatdata/FileResourceStorage.h +++ b/flatdata-cpp/include/flatdata/FileResourceStorage.h @@ -41,6 +41,7 @@ class FileResourceStorage : public ResourceStorage std::unique_ptr< ResourceStorage > directory( const char* key ) override; bool exists( const char* key ) override; + bool remove( const char* key ) override; protected: std::shared_ptr< std::ostream > create_output_stream( const char* key ) override; @@ -150,4 +151,13 @@ FileResourceStorage::exists( const char* key ) return !ec; } +inline bool +FileResourceStorage::remove( const char* key ) +{ + boost::system::error_code ec; + std::string file_path = get_path( key ); + boost::filesystem::remove( file_path.c_str( ), ec ); + return !ec; +} + } // namespace flatdata diff --git a/flatdata-cpp/include/flatdata/MemoryResourceStorage.h b/flatdata-cpp/include/flatdata/MemoryResourceStorage.h index f8dbf72b..ca9d6f55 100644 --- a/flatdata-cpp/include/flatdata/MemoryResourceStorage.h +++ b/flatdata-cpp/include/flatdata/MemoryResourceStorage.h @@ -32,6 +32,8 @@ class MemoryResourceStorage : public ResourceStorage public: static std::unique_ptr< MemoryResourceStorage > create( ); bool exists( const char* key ) override; + bool remove( const char* key ) override; + std::unique_ptr< ResourceStorage > create_directory( const char* key ) override; std::unique_ptr< ResourceStorage > directory( const char* key ) override; @@ -259,6 +261,26 @@ MemoryResourceStorage::exists( const char* key ) || helpers::prefix_exists( m_storage->streams, path + "/" ); } +inline bool +MemoryResourceStorage::remove( const char* key ) +{ + const auto path = get_path( key ); + std::lock_guard< std::mutex > lock( m_storage_mutex ); + + m_storage->streams.erase( path ); + auto found_resource = m_storage->resources.find( path ); + if ( found_resource != m_storage->resources.end( ) ) + { +#ifdef DEBUG_DATA_ACCESS_STATISTICS + auto& string = found_resource->second; + DebugDataAccessStatistics::deregister_mapping( MemoryDescriptor( + reinterpret_cast< const unsigned char* >( string->c_str( ) ), string->size( ) ) ); +#endif + m_storage->resources.erase( found_resource ); + } + return true; +} + template < typename ResourceSerializer > std::string MemoryResourceStorage::dump_resources( bool dump_schemas, ResourceSerializer&& f ) const diff --git a/flatdata-cpp/include/flatdata/ResourceStorage.h b/flatdata-cpp/include/flatdata/ResourceStorage.h index 2085167f..60285137 100644 --- a/flatdata-cpp/include/flatdata/ResourceStorage.h +++ b/flatdata-cpp/include/flatdata/ResourceStorage.h @@ -95,6 +95,11 @@ class ResourceStorage : private boost::noncopyable * @brief Check if resource exists */ virtual bool exists( const char* key ) = 0; + virtual bool remove( const char* key ) = 0; + + bool remove_resource( const char* resource_name, const char* expected_schema ); + bool remove_multivector_index_resource( const char* resource_name, + const char* expected_schema ); protected: /** diff --git a/flatdata-cpp/include/flatdata/TarArchiveResourceStorage.h b/flatdata-cpp/include/flatdata/TarArchiveResourceStorage.h index 870fc0a5..18bf1f00 100644 --- a/flatdata-cpp/include/flatdata/TarArchiveResourceStorage.h +++ b/flatdata-cpp/include/flatdata/TarArchiveResourceStorage.h @@ -32,6 +32,7 @@ class TarArchiveResourceStorage : public ResourceStorage std::unique_ptr< ResourceStorage > create_directory( const char* key ) override; std::unique_ptr< ResourceStorage > directory( const char* key ) override; bool exists( const char* key ) override; + bool remove( const char* key ) override; protected: std::shared_ptr< std::ostream > create_output_stream( const char* key ) override; @@ -123,4 +124,11 @@ TarArchiveResourceStorage::exists( const char* key ) return m_storage->read( get_path( key ).c_str( ) ).data( ) != nullptr; } +inline bool +TarArchiveResourceStorage::remove( const char* /* key */ ) +{ + // Removing parts of TAR files is not supported + return false; +} + } // namespace flatdata diff --git a/flatdata-cpp/include/flatdata/internal/ResourceStorage.inl b/flatdata-cpp/include/flatdata/internal/ResourceStorage.inl index 0520da23..fc41b24e 100644 --- a/flatdata-cpp/include/flatdata/internal/ResourceStorage.inl +++ b/flatdata-cpp/include/flatdata/internal/ResourceStorage.inl @@ -207,4 +207,42 @@ ResourceStorage::create_multi_vector( const char* resource_name, const char* sch internal::ResourceReader( this, resource_name, schema ) ) ); } +inline bool +ResourceStorage::remove_resource( const char* resource_name, const char* expected_schema ) +{ + const std::string schema_name = std::string( resource_name ) + ".schema"; + const bool resource_exists = exists( resource_name ); + const bool schema_exists = exists( schema_name.c_str( ) ); + + if ( !resource_exists && !schema_exists ) + { + return true; + } + + if ( resource_exists != schema_exists ) + { + return false; + } + + auto schema = read_resource( ( schema_name.c_str( ) ) ); + std::string stored_schema( reinterpret_cast< const char* >( schema.data( ) ), + schema.size_in_bytes( ) ); + + if ( stored_schema != expected_schema ) + { + return false; + } + return remove( resource_name ) && remove( schema_name.c_str( ) ); +} + +inline bool +ResourceStorage::remove_multivector_index_resource( const char* resource_name, + const char* expected_schema ) +{ + std::string index_name = std::string( resource_name ) + internal::INDEX_SUFFIX; + std::string index_schema = internal::multivector_index_schema( expected_schema ); + + return remove_resource( index_name.c_str( ), index_schema.c_str( ) ); +} + } // namespace flatdata diff --git a/flatdata-cpp/src/ArchiveBuilder.cpp b/flatdata-cpp/src/ArchiveBuilder.cpp index 4333b5d9..bf3de9d6 100644 --- a/flatdata-cpp/src/ArchiveBuilder.cpp +++ b/flatdata-cpp/src/ArchiveBuilder.cpp @@ -67,4 +67,11 @@ ArchiveBuilder::initialize( ) return static_cast< bool >( signature ); } +bool +ArchiveBuilder::clean_up_signature( flatdata::ResourceStorage& storage ) +{ + const auto signature_name = internal::signature_name( name( ) ); + return storage.remove_resource( signature_name.c_str( ), schema( ) ); +} + } // namespace flatdata diff --git a/flatdata-cpp/test/ResourceStorageTest.cpp b/flatdata-cpp/test/ResourceStorageTest.cpp index 0b5c148d..6be503af 100644 --- a/flatdata-cpp/test/ResourceStorageTest.cpp +++ b/flatdata-cpp/test/ResourceStorageTest.cpp @@ -81,5 +81,28 @@ TEST_CASE( "schema is checked for written vector", "[ResourceStorage]" ) ( a->read< ArrayView< AStruct > >( resource_name, correct_schema ).is_initialized( ) ) ); } +TEST_CASE( "resource doesn't exist after removing it", "[ResourceStorage]" ) +{ + std::unique_ptr< ResourceStorage > a = MemoryResourceStorage::create( ); + auto create = [ & ] { + auto v = a->create_external_vector< AStruct >( resource_name, correct_schema ); + v.close( ); + }; + REQUIRE_NOTHROW( create( ) ); + + REQUIRE_FALSE( + ( a->read< ArrayView< AStruct > >( resource_name, incorrect_schema ).is_initialized( ) ) ); + REQUIRE( + ( a->read< ArrayView< AStruct > >( resource_name, correct_schema ).is_initialized( ) ) ); + + std::string schema_name = std::string( resource_name ) + ".schema"; + REQUIRE( a->exists( resource_name ) ); + REQUIRE( a->exists( schema_name.c_str( ) ) ); + + a->remove_resource( resource_name, correct_schema ); + REQUIRE_FALSE( a->exists( resource_name ) ); + REQUIRE_FALSE( a->exists( schema_name.c_str( ) ) ); +} + } // namespace test } // namespace flatdata diff --git a/flatdata-generator/flatdata/generator/app.py b/flatdata-generator/flatdata/generator/app.py index 35e3cc1d..20547e6e 100755 --- a/flatdata-generator/flatdata/generator/app.py +++ b/flatdata-generator/flatdata/generator/app.py @@ -80,8 +80,7 @@ def _run(args: argparse.Namespace) -> None: sys.exit(1) dirname = os.path.dirname(os.path.abspath(args.output_file)) - if not os.path.exists(dirname): - os.makedirs(dirname) + os.makedirs(dirname, exist_ok=True) with open(args.output_file, "w") as output: output.write(output_content) logging.info("Code for %s is written to %s", args.gen, args.output_file) diff --git a/flatdata-generator/flatdata/generator/templates/cpp/archive.jinja2 b/flatdata-generator/flatdata/generator/templates/cpp/archive.jinja2 index 97b5e7de..50caaf3a 100644 --- a/flatdata-generator/flatdata/generator/templates/cpp/archive.jinja2 +++ b/flatdata-generator/flatdata/generator/templates/cpp/archive.jinja2 @@ -51,7 +51,7 @@ class {{ builder_name }} : public flatdata::ArchiveBuilder { public: /// Creates Archive builder - static {{ builder_name }} open( std::shared_ptr< flatdata::ResourceStorage > storage ); + static {{ builder_name }} open( std::shared_ptr< flatdata::ResourceStorage > storage, bool force_overwrite = false ); /// Archive schema static const char* schema_definition( ); @@ -65,18 +65,18 @@ public: /// Resources using {{ r|typedef_name }} = {{ resource.writer_type(r) }}; using {{ r|typedef_name("Reader") }} = {{ resource.reader_type(r) }}; {% if r|resource_provides_incremental_builder %} - {{ r|typedef_name }} start_{{ r.name }}( ); + {{ r|typedef_name }} start_{{ r.name }}( bool force_overwrite = false ); {% endif %} {% if r|provides_setter %} - bool set_{{ r.name }}( {{ r|typedef_name("Reader") }} data ); + bool set_{{ r.name }}( {{ r|typedef_name("Reader") }} data, bool force_overwrite = false ); {% endif %} - + static bool clean_up_{{ r.name }}( flatdata::ResourceStorage& storage ); {% endfor %} - {% for r in archive.resources|archive_resources %} using {{ r|typedef_name }} = {{ resource.writer_type(r) }}; - {{ r|typedef_name }}& {{ r.name }}( ); + {{ r|typedef_name }}& {{ r.name }}( bool force_overwrite = false ); {% endfor %} + bool clean_up( flatdata::ResourceStorage& storage ); private: {{ builder_name }}( std::shared_ptr< flatdata::ResourceStorage > storage ); @@ -205,9 +205,13 @@ inline inline {{ builder_name }} -{{ builder_name }}::open(std::shared_ptr< flatdata::ResourceStorage > storage ) +{{ builder_name }}::open(std::shared_ptr< flatdata::ResourceStorage > storage, bool force_overwrite ) { {{ builder_name }} result( storage ); + if ( force_overwrite && !result.clean_up( *storage ) ) + { + return {{ builder_name }}( ); + } if ( !result.initialize( ) ) { return {{ builder_name }}( ); @@ -217,31 +221,64 @@ inline {{ builder_name }} {% for r in archive.resources|simple_resources %} {% if r|resource_provides_incremental_builder %} -inline auto {{ builder_name }}::start_{{ r.name }}( ) -> {{ r|typedef_name }} +inline auto {{ builder_name }}::start_{{ r.name }}( bool force_overwrite ) -> {{ r|typedef_name }} { - check_created( ); + if ( !force_overwrite || !clean_up_{{ r.name }}( storage( ) ) ) + { + check_created( ); + } return storage( ).{{ resource.incremental_write_call(r) }}( "{{ r.name }}", internal::{{ archive.name }}__{{ r.name }}__schema__ ); } {% endif %} {% if r|provides_setter %} inline bool -{{ builder_name }}::set_{{ r.name }}( {{ r|typedef_name("Reader") }} data ) +{{ builder_name }}::set_{{ r.name }}( {{ r|typedef_name("Reader") }} data, bool force_overwrite ) { - check_created( ); + if ( !force_overwrite || !clean_up_{{ r.name }}( storage( ) ) ) + { + check_created( ); + } return storage( ).write< {{ r|typedef_name("Reader") }} >( "{{ r.name }}", internal::{{ archive.name }}__{{ r.name }}__schema__, data ); } {% endif %} +inline bool +{{ builder_name }}::clean_up_{{ r.name }}( flatdata::ResourceStorage& storage ) +{ + bool res = storage.remove_resource( "{{ r.name }}", internal::{{ archive.name }}__{{ r.name }}__schema__ ); + {% if r|is_multivector %} + res = res && storage.remove_multivector_index_resource( "{{ r.name }}", internal::{{ archive.name }}__{{ r.name }}__schema__ ); + {% endif %} + return res; +} {% endfor %} {% for r in archive.resources|archive_resources %} inline auto -{{ builder_name }}::{{ r.name }}( ) -> {{ r|typedef_name }}& +{{ builder_name }}::{{ r.name }}( bool force_overwrite ) -> {{ r|typedef_name }}& { if ( !m_{{ r.name }}.is_open( ) ) { - m_{{ r.name }} = {{ r|typedef_name }}::open( this->storage( ).create_directory( "{{ r.name }}" ) ); + m_{{ r.name }} = {{ r|typedef_name }}::open( this->storage( ).create_directory( "{{ r.name }}" ), force_overwrite ); } return m_{{ r.name }}; } {% endfor %} + +inline bool +{{ builder_name }}::clean_up( flatdata::ResourceStorage& storage ) +{ + bool res = true; +{% for r in archive.resources|simple_resources %} + res = res && {{ builder_name }}::clean_up_{{ r.name }}( storage ); +{% endfor %} +{% for r in archive.resources|archive_resources %} + if ( auto subarchive_storage = storage.directory( "{{ r.name }}" ) ) + { + res = res && m_{{ r.name }}.clean_up( *subarchive_storage ); + } +{% endfor %} + res = res && clean_up_signature( storage ); + return res; +} + {% endmacro %} diff --git a/flatdata-generator/flatdata/generator/templates/rust/archive.jinja2 b/flatdata-generator/flatdata/generator/templates/rust/archive.jinja2 index a3867cbb..00789e2c 100644 --- a/flatdata-generator/flatdata/generator/templates/rust/archive.jinja2 +++ b/flatdata-generator/flatdata/generator/templates/rust/archive.jinja2 @@ -256,7 +256,31 @@ impl {{archive.name}}Builder { } {% endif %} + #[inline] + pub fn clean_up_{{ r.name }}_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + {% if [r] | subarchive_resources %} + {% set t = fully_qualified_name(archive, r.target.node) %} + {{t}}Builder::clean_up(storage.subdir("{{r.name}}")) + {% else %} + {% if [r] | multivector_resources %} + storage.remove_multivector_index_resource("{{r.name}}", schema::{{ archive_ns }}::resources::{{ r.name | upper }})?; + {% endif %} + storage.remove_resource("{{r.name}}", schema::{{ archive_ns }}::resources::{{ r.name | upper }}) + {% endif %} + } + #[inline] + pub fn clean_up_{{ r.name }}(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_{{ r.name }}_storage(self.storage.clone()) + } {% endfor %} + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + {% for r in archive.resources | supported_resources %} + Self::clean_up_{{ r.name }}_storage(storage.clone())?; + {% endfor %} + storage.remove_resource("{{archive.name}}.archive", schema::{{ archive_ns }}::{{ archive.name | camel_to_snake_case | upper }}) + } } {% endif %} diff --git a/flatdata-generator/tests/generators/cpp_expectations/archives/empty.h b/flatdata-generator/tests/generators/cpp_expectations/archives/empty.h index 0dd2d2c3..d7600669 100644 --- a/flatdata-generator/tests/generators/cpp_expectations/archives/empty.h +++ b/flatdata-generator/tests/generators/cpp_expectations/archives/empty.h @@ -34,7 +34,7 @@ class ABuilder : public flatdata::ArchiveBuilder { public: /// Creates Archive builder - static ABuilder open( std::shared_ptr< flatdata::ResourceStorage > storage ); + static ABuilder open( std::shared_ptr< flatdata::ResourceStorage > storage, bool force_overwrite = false ); /// Archive schema static const char* schema_definition( ); @@ -44,7 +44,7 @@ class ABuilder : public flatdata::ArchiveBuilder const char* schema( ) const override; public: /// Resources - + bool clean_up( flatdata::ResourceStorage& storage ); private: ABuilder( std::shared_ptr< flatdata::ResourceStorage > storage ); diff --git a/flatdata-generator/tests/generators/cpp_expectations/archives/multivector.h.2 b/flatdata-generator/tests/generators/cpp_expectations/archives/multivector.h.2 index 76ef0421..fe563d5f 100644 --- a/flatdata-generator/tests/generators/cpp_expectations/archives/multivector.h.2 +++ b/flatdata-generator/tests/generators/cpp_expectations/archives/multivector.h.2 @@ -1,11 +1,13 @@ using DataType = flatdata::MultiVector< ::n::_builtin::multivector::IndexType8, ::n::S, ::n::T >; using DataReaderType = flatdata::MultiArrayView< ::n::_builtin::multivector::IndexType8, ::n::S, ::n::T >; - DataType start_data( ); - + DataType start_data( bool force_overwrite = false ); + static bool clean_up_data( flatdata::ResourceStorage& storage ); using OptionalDataType = flatdata::MultiVector< ::n::_builtin::multivector::IndexType16, ::n::S, ::n::T >; using OptionalDataReaderType = flatdata::MultiArrayView< ::n::_builtin::multivector::IndexType16, ::n::S, ::n::T >; - OptionalDataType start_optional_data( ); - + OptionalDataType start_optional_data( bool force_overwrite = false ); + static bool clean_up_optional_data( flatdata::ResourceStorage& storage ); using DataU64IndexType = flatdata::MultiVector< ::n::_builtin::multivector::IndexType64, ::n::S, ::n::T >; using DataU64IndexReaderType = flatdata::MultiArrayView< ::n::_builtin::multivector::IndexType64, ::n::S, ::n::T >; - DataU64IndexType start_data_u64_index( ); \ No newline at end of file + DataU64IndexType start_data_u64_index( bool force_overwrite = false ); + static bool clean_up_data_u64_index( flatdata::ResourceStorage& storage ); + bool clean_up( flatdata::ResourceStorage& storage ); \ No newline at end of file diff --git a/flatdata-generator/tests/generators/cpp_expectations/archives/namespaces.h b/flatdata-generator/tests/generators/cpp_expectations/archives/namespaces.h index 06c8bf03..9c85284a 100644 --- a/flatdata-generator/tests/generators/cpp_expectations/archives/namespaces.h +++ b/flatdata-generator/tests/generators/cpp_expectations/archives/namespaces.h @@ -102,7 +102,7 @@ class XBuilder : public flatdata::ArchiveBuilder { public: /// Creates Archive builder - static XBuilder open( std::shared_ptr< flatdata::ResourceStorage > storage ); + static XBuilder open( std::shared_ptr< flatdata::ResourceStorage > storage, bool force_overwrite = false ); /// Archive schema static const char* schema_definition( ); @@ -114,9 +114,9 @@ class XBuilder : public flatdata::ArchiveBuilder public: /// Resources using PayloadType = flatdata::MemoryDescriptor; using PayloadReaderType = flatdata::MemoryDescriptor; - bool set_payload( PayloadReaderType data ); - - + bool set_payload( PayloadReaderType data, bool force_overwrite = false ); + static bool clean_up_payload( flatdata::ResourceStorage& storage ); + bool clean_up( flatdata::ResourceStorage& storage ); private: XBuilder( std::shared_ptr< flatdata::ResourceStorage > storage ); @@ -221,7 +221,7 @@ class XBuilder : public flatdata::ArchiveBuilder { public: /// Creates Archive builder - static XBuilder open( std::shared_ptr< flatdata::ResourceStorage > storage ); + static XBuilder open( std::shared_ptr< flatdata::ResourceStorage > storage, bool force_overwrite = false ); /// Archive schema static const char* schema_definition( ); @@ -233,9 +233,9 @@ class XBuilder : public flatdata::ArchiveBuilder public: /// Resources using PayloadType = flatdata::MemoryDescriptor; using PayloadReaderType = flatdata::MemoryDescriptor; - bool set_payload( PayloadReaderType data ); - - + bool set_payload( PayloadReaderType data, bool force_overwrite = false ); + static bool clean_up_payload( flatdata::ResourceStorage& storage ); + bool clean_up( flatdata::ResourceStorage& storage ); private: XBuilder( std::shared_ptr< flatdata::ResourceStorage > storage ); @@ -356,7 +356,7 @@ class ABuilder : public flatdata::ArchiveBuilder { public: /// Creates Archive builder - static ABuilder open( std::shared_ptr< flatdata::ResourceStorage > storage ); + static ABuilder open( std::shared_ptr< flatdata::ResourceStorage > storage, bool force_overwrite = false ); /// Archive schema static const char* schema_definition( ); @@ -368,20 +368,20 @@ class ABuilder : public flatdata::ArchiveBuilder public: /// Resources using SingleType = ::n::S; using SingleReaderType = ::n::S; - bool set_single( SingleReaderType data ); - + bool set_single( SingleReaderType data, bool force_overwrite = false ); + static bool clean_up_single( flatdata::ResourceStorage& storage ); using ListType = flatdata::ExternalVector< ::m::S >; using ListReaderType = flatdata::ArrayView< ::m::S >; - ListType start_list( ); - bool set_list( ListReaderType data ); - + ListType start_list( bool force_overwrite = false ); + bool set_list( ListReaderType data, bool force_overwrite = false ); + static bool clean_up_list( flatdata::ResourceStorage& storage ); using MultiType = flatdata::MultiVector< ::a::_builtin::multivector::IndexType32, ::n::S >; using MultiReaderType = flatdata::MultiArrayView< ::a::_builtin::multivector::IndexType32, ::n::S >; - MultiType start_multi( ); - - + MultiType start_multi( bool force_overwrite = false ); + static bool clean_up_multi( flatdata::ResourceStorage& storage ); using InnerType = ::n::XBuilder; - InnerType& inner( ); + InnerType& inner( bool force_overwrite = false ); + bool clean_up( flatdata::ResourceStorage& storage ); private: ABuilder( std::shared_ptr< flatdata::ResourceStorage > storage ); @@ -623,9 +623,13 @@ XBuilder::XBuilder( std::shared_ptr< flatdata::ResourceStorage > storage ) inline XBuilder -XBuilder::open(std::shared_ptr< flatdata::ResourceStorage > storage ) +XBuilder::open(std::shared_ptr< flatdata::ResourceStorage > storage, bool force_overwrite ) { XBuilder result( storage ); + if ( force_overwrite && !result.clean_up( *storage ) ) + { + return XBuilder( ); + } if ( !result.initialize( ) ) { return XBuilder( ); @@ -634,11 +638,30 @@ XBuilder::open(std::shared_ptr< flatdata::ResourceStorage > storage ) } inline bool -XBuilder::set_payload( PayloadReaderType data ) +XBuilder::set_payload( PayloadReaderType data, bool force_overwrite ) { - check_created( ); + if ( !force_overwrite || !clean_up_payload( storage( ) ) ) + { + check_created( ); + } return storage( ).write< PayloadReaderType >( "payload", internal::X__payload__schema__, data ); } +inline bool +XBuilder::clean_up_payload( flatdata::ResourceStorage& storage ) +{ + bool res = storage.remove_resource( "payload", internal::X__payload__schema__ ); + return res; +} + + +inline bool +XBuilder::clean_up( flatdata::ResourceStorage& storage ) +{ + bool res = true; + res = res && XBuilder::clean_up_payload( storage ); + res = res && clean_up_signature( storage ); + return res; +} } // namespace n @@ -869,9 +892,13 @@ XBuilder::XBuilder( std::shared_ptr< flatdata::ResourceStorage > storage ) inline XBuilder -XBuilder::open(std::shared_ptr< flatdata::ResourceStorage > storage ) +XBuilder::open(std::shared_ptr< flatdata::ResourceStorage > storage, bool force_overwrite ) { XBuilder result( storage ); + if ( force_overwrite && !result.clean_up( *storage ) ) + { + return XBuilder( ); + } if ( !result.initialize( ) ) { return XBuilder( ); @@ -880,11 +907,30 @@ XBuilder::open(std::shared_ptr< flatdata::ResourceStorage > storage ) } inline bool -XBuilder::set_payload( PayloadReaderType data ) +XBuilder::set_payload( PayloadReaderType data, bool force_overwrite ) { - check_created( ); + if ( !force_overwrite || !clean_up_payload( storage( ) ) ) + { + check_created( ); + } return storage( ).write< PayloadReaderType >( "payload", internal::X__payload__schema__, data ); } +inline bool +XBuilder::clean_up_payload( flatdata::ResourceStorage& storage ) +{ + bool res = storage.remove_resource( "payload", internal::X__payload__schema__ ); + return res; +} + + +inline bool +XBuilder::clean_up( flatdata::ResourceStorage& storage ) +{ + bool res = true; + res = res && XBuilder::clean_up_payload( storage ); + res = res && clean_up_signature( storage ); + return res; +} } // namespace m @@ -1210,9 +1256,13 @@ ABuilder::ABuilder( std::shared_ptr< flatdata::ResourceStorage > storage ) inline ABuilder -ABuilder::open(std::shared_ptr< flatdata::ResourceStorage > storage ) +ABuilder::open(std::shared_ptr< flatdata::ResourceStorage > storage, bool force_overwrite ) { ABuilder result( storage ); + if ( force_overwrite && !result.clean_up( *storage ) ) + { + return ABuilder( ); + } if ( !result.initialize( ) ) { return ABuilder( ); @@ -1221,36 +1271,83 @@ ABuilder::open(std::shared_ptr< flatdata::ResourceStorage > storage ) } inline bool -ABuilder::set_single( SingleReaderType data ) +ABuilder::set_single( SingleReaderType data, bool force_overwrite ) { - check_created( ); + if ( !force_overwrite || !clean_up_single( storage( ) ) ) + { + check_created( ); + } return storage( ).write< SingleReaderType >( "single", internal::A__single__schema__, data ); } -inline auto ABuilder::start_list( ) -> ListType +inline bool +ABuilder::clean_up_single( flatdata::ResourceStorage& storage ) +{ + bool res = storage.remove_resource( "single", internal::A__single__schema__ ); + return res; +} +inline auto ABuilder::start_list( bool force_overwrite ) -> ListType { - check_created( ); + if ( !force_overwrite || !clean_up_list( storage( ) ) ) + { + check_created( ); + } return storage( ).create_external_vector< ::m::S >( "list", internal::A__list__schema__ ); } inline bool -ABuilder::set_list( ListReaderType data ) +ABuilder::set_list( ListReaderType data, bool force_overwrite ) { - check_created( ); + if ( !force_overwrite || !clean_up_list( storage( ) ) ) + { + check_created( ); + } return storage( ).write< ListReaderType >( "list", internal::A__list__schema__, data ); } -inline auto ABuilder::start_multi( ) -> MultiType +inline bool +ABuilder::clean_up_list( flatdata::ResourceStorage& storage ) +{ + bool res = storage.remove_resource( "list", internal::A__list__schema__ ); + return res; +} +inline auto ABuilder::start_multi( bool force_overwrite ) -> MultiType { - check_created( ); + if ( !force_overwrite || !clean_up_multi( storage( ) ) ) + { + check_created( ); + } return storage( ).create_multi_vector< ::a::_builtin::multivector::IndexType32, ::n::S >( "multi", internal::A__multi__schema__ ); } +inline bool +ABuilder::clean_up_multi( flatdata::ResourceStorage& storage ) +{ + bool res = storage.remove_resource( "multi", internal::A__multi__schema__ ); + res = res && storage.remove_multivector_index_resource( "multi", internal::A__multi__schema__ ); + return res; +} inline auto -ABuilder::inner( ) -> InnerType& +ABuilder::inner( bool force_overwrite ) -> InnerType& { if ( !m_inner.is_open( ) ) { - m_inner = InnerType::open( this->storage( ).create_directory( "inner" ) ); + m_inner = InnerType::open( this->storage( ).create_directory( "inner" ), force_overwrite ); } return m_inner; } + +inline bool +ABuilder::clean_up( flatdata::ResourceStorage& storage ) +{ + bool res = true; + res = res && ABuilder::clean_up_single( storage ); + res = res && ABuilder::clean_up_list( storage ); + res = res && ABuilder::clean_up_multi( storage ); + if ( auto subarchive_storage = storage.directory( "inner" ) ) + { + res = res && m_inner.clean_up( *subarchive_storage ); + } + res = res && clean_up_signature( storage ); + return res; +} + } // namespace a diff --git a/flatdata-generator/tests/generators/cpp_expectations/archives/raw_data.h.2 b/flatdata-generator/tests/generators/cpp_expectations/archives/raw_data.h.2 index 5f4447f8..d6764cc5 100644 --- a/flatdata-generator/tests/generators/cpp_expectations/archives/raw_data.h.2 +++ b/flatdata-generator/tests/generators/cpp_expectations/archives/raw_data.h.2 @@ -1,7 +1,9 @@ using DataType = flatdata::MemoryDescriptor; using DataReaderType = flatdata::MemoryDescriptor; - bool set_data( DataReaderType data ); - + bool set_data( DataReaderType data, bool force_overwrite = false ); + static bool clean_up_data( flatdata::ResourceStorage& storage ); using OptionalDataType = flatdata::MemoryDescriptor; using OptionalDataReaderType = flatdata::MemoryDescriptor; - bool set_optional_data( OptionalDataReaderType data ); \ No newline at end of file + bool set_optional_data( OptionalDataReaderType data, bool force_overwrite = false ); + static bool clean_up_optional_data( flatdata::ResourceStorage& storage ); + bool clean_up( flatdata::ResourceStorage& storage ); \ No newline at end of file diff --git a/flatdata-generator/tests/generators/cpp_expectations/archives/struct.h.2 b/flatdata-generator/tests/generators/cpp_expectations/archives/struct.h.2 index 999552bf..2331ba6b 100644 --- a/flatdata-generator/tests/generators/cpp_expectations/archives/struct.h.2 +++ b/flatdata-generator/tests/generators/cpp_expectations/archives/struct.h.2 @@ -1,7 +1,9 @@ using DataType = ::n::S; using DataReaderType = ::n::S; - bool set_data( DataReaderType data ); - + bool set_data( DataReaderType data, bool force_overwrite = false ); + static bool clean_up_data( flatdata::ResourceStorage& storage ); using OptionalDataType = ::n::S; using OptionalDataReaderType = ::n::S; - bool set_optional_data( OptionalDataReaderType data ); \ No newline at end of file + bool set_optional_data( OptionalDataReaderType data, bool force_overwrite = false ); + static bool clean_up_optional_data( flatdata::ResourceStorage& storage ); + bool clean_up( flatdata::ResourceStorage& storage ); \ No newline at end of file diff --git a/flatdata-generator/tests/generators/cpp_expectations/archives/subarchive.h.2 b/flatdata-generator/tests/generators/cpp_expectations/archives/subarchive.h.2 index 9622ea10..9ea79558 100644 --- a/flatdata-generator/tests/generators/cpp_expectations/archives/subarchive.h.2 +++ b/flatdata-generator/tests/generators/cpp_expectations/archives/subarchive.h.2 @@ -1,4 +1,5 @@ using DataType = ::n::XBuilder; - DataType& data( ); + DataType& data( bool force_overwrite = false ); using OptionalDataType = ::n::XBuilder; - OptionalDataType& optional_data( ); \ No newline at end of file + OptionalDataType& optional_data( bool force_overwrite = false ); + bool clean_up( flatdata::ResourceStorage& storage ); \ No newline at end of file diff --git a/flatdata-generator/tests/generators/cpp_expectations/archives/vector.h.2 b/flatdata-generator/tests/generators/cpp_expectations/archives/vector.h.2 index b87fa5c5..2277ec64 100644 --- a/flatdata-generator/tests/generators/cpp_expectations/archives/vector.h.2 +++ b/flatdata-generator/tests/generators/cpp_expectations/archives/vector.h.2 @@ -1,9 +1,11 @@ using DataType = flatdata::ExternalVector< ::n::S >; using DataReaderType = flatdata::ArrayView< ::n::S >; - DataType start_data( ); - bool set_data( DataReaderType data ); - + DataType start_data( bool force_overwrite = false ); + bool set_data( DataReaderType data, bool force_overwrite = false ); + static bool clean_up_data( flatdata::ResourceStorage& storage ); using OptionalDataType = flatdata::ExternalVector< ::n::S >; using OptionalDataReaderType = flatdata::ArrayView< ::n::S >; - OptionalDataType start_optional_data( ); - bool set_optional_data( OptionalDataReaderType data ); \ No newline at end of file + OptionalDataType start_optional_data( bool force_overwrite = false ); + bool set_optional_data( OptionalDataReaderType data, bool force_overwrite = false ); + static bool clean_up_optional_data( flatdata::ResourceStorage& storage ); + bool clean_up( flatdata::ResourceStorage& storage ); \ No newline at end of file diff --git a/flatdata-generator/tests/generators/rust_expectations/archives/comments.rs.1 b/flatdata-generator/tests/generators/rust_expectations/archives/comments.rs.1 index 05ce18a3..09062977 100644 --- a/flatdata-generator/tests/generators/rust_expectations/archives/comments.rs.1 +++ b/flatdata-generator/tests/generators/rust_expectations/archives/comments.rs.1 @@ -72,6 +72,20 @@ impl FooBuilder { self.storage.write("bar", schema::foo::resources::BAR, data) } + #[inline] + pub fn clean_up_bar_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("bar", schema::foo::resources::BAR) + } + #[inline] + pub fn clean_up_bar(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_bar_storage(self.storage.clone()) + } + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + Self::clean_up_bar_storage(storage.clone())?; + storage.remove_resource("Foo.archive", schema::foo::FOO) + } } impl FooBuilder { @@ -160,6 +174,20 @@ impl BarBuilder { self.storage.write("foo", schema::bar::resources::FOO, data) } + #[inline] + pub fn clean_up_foo_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("foo", schema::bar::resources::FOO) + } + #[inline] + pub fn clean_up_foo(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_foo_storage(self.storage.clone()) + } + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + Self::clean_up_foo_storage(storage.clone())?; + storage.remove_resource("Bar.archive", schema::bar::BAR) + } } impl BarBuilder { diff --git a/flatdata-generator/tests/generators/rust_expectations/archives/multivector.rs.1 b/flatdata-generator/tests/generators/rust_expectations/archives/multivector.rs.1 index e2786251..d6cc58ab 100644 --- a/flatdata-generator/tests/generators/rust_expectations/archives/multivector.rs.1 +++ b/flatdata-generator/tests/generators/rust_expectations/archives/multivector.rs.1 @@ -1014,6 +1014,15 @@ impl ABuilder { flatdata::create_multi_vector(&*self.storage, "data", schema::a::resources::DATA) } + #[inline] + pub fn clean_up_data_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_multivector_index_resource("data", schema::a::resources::DATA)?; + storage.remove_resource("data", schema::a::resources::DATA) + } + #[inline] + pub fn clean_up_data(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_data_storage(self.storage.clone()) + } /// Opens [`optional_data`] in the archive for buffered writing. /// /// Elements can be added to the multivector until the [`MultiVector::close`] method @@ -1027,6 +1036,15 @@ impl ABuilder { flatdata::create_multi_vector(&*self.storage, "optional_data", schema::a::resources::OPTIONAL_DATA) } + #[inline] + pub fn clean_up_optional_data_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_multivector_index_resource("optional_data", schema::a::resources::OPTIONAL_DATA)?; + storage.remove_resource("optional_data", schema::a::resources::OPTIONAL_DATA) + } + #[inline] + pub fn clean_up_optional_data(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_optional_data_storage(self.storage.clone()) + } /// Opens [`data_u64_index`] in the archive for buffered writing. /// /// Elements can be added to the multivector until the [`MultiVector::close`] method @@ -1040,6 +1058,23 @@ impl ABuilder { flatdata::create_multi_vector(&*self.storage, "data_u64_index", schema::a::resources::DATA_U64_INDEX) } + #[inline] + pub fn clean_up_data_u64_index_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_multivector_index_resource("data_u64_index", schema::a::resources::DATA_U64_INDEX)?; + storage.remove_resource("data_u64_index", schema::a::resources::DATA_U64_INDEX) + } + #[inline] + pub fn clean_up_data_u64_index(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_data_u64_index_storage(self.storage.clone()) + } + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + Self::clean_up_data_storage(storage.clone())?; + Self::clean_up_optional_data_storage(storage.clone())?; + Self::clean_up_data_u64_index_storage(storage.clone())?; + storage.remove_resource("A.archive", schema::a::A) + } } impl ABuilder { diff --git a/flatdata-generator/tests/generators/rust_expectations/archives/namespaces.rs.1 b/flatdata-generator/tests/generators/rust_expectations/archives/namespaces.rs.1 index 15719c94..066793df 100644 --- a/flatdata-generator/tests/generators/rust_expectations/archives/namespaces.rs.1 +++ b/flatdata-generator/tests/generators/rust_expectations/archives/namespaces.rs.1 @@ -190,6 +190,20 @@ impl XBuilder { self.storage.write("payload", schema::x::resources::PAYLOAD, data) } + #[inline] + pub fn clean_up_payload_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("payload", schema::x::resources::PAYLOAD) + } + #[inline] + pub fn clean_up_payload(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_payload_storage(self.storage.clone()) + } + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + Self::clean_up_payload_storage(storage.clone())?; + storage.remove_resource("X.archive", schema::x::X) + } } impl XBuilder { @@ -421,6 +435,20 @@ impl XBuilder { self.storage.write("payload", schema::x::resources::PAYLOAD, data) } + #[inline] + pub fn clean_up_payload_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("payload", schema::x::resources::PAYLOAD) + } + #[inline] + pub fn clean_up_payload(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_payload_storage(self.storage.clone()) + } + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + Self::clean_up_payload_storage(storage.clone())?; + storage.remove_resource("X.archive", schema::x::X) + } } impl XBuilder { @@ -806,6 +834,14 @@ impl ABuilder { self.storage.write("single", schema::a::resources::SINGLE, data) } + #[inline] + pub fn clean_up_single_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("single", schema::a::resources::SINGLE) + } + #[inline] + pub fn clean_up_single(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_single_storage(self.storage.clone()) + } #[inline] /// Stores [`list`] in the archive. /// @@ -828,6 +864,14 @@ impl ABuilder { flatdata::create_external_vector(&*self.storage, "list", schema::a::resources::LIST) } + #[inline] + pub fn clean_up_list_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("list", schema::a::resources::LIST) + } + #[inline] + pub fn clean_up_list(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_list_storage(self.storage.clone()) + } /// Opens [`multi`] in the archive for buffered writing. /// /// Elements can be added to the multivector until the [`MultiVector::close`] method @@ -841,6 +885,15 @@ impl ABuilder { flatdata::create_multi_vector(&*self.storage, "multi", schema::a::resources::MULTI) } + #[inline] + pub fn clean_up_multi_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_multivector_index_resource("multi", schema::a::resources::MULTI)?; + storage.remove_resource("multi", schema::a::resources::MULTI) + } + #[inline] + pub fn clean_up_multi(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_multi_storage(self.storage.clone()) + } /// Stores [`inner`] in the archive. /// /// [`inner`]: struct.A.html#method.inner @@ -850,6 +903,23 @@ impl ABuilder { super::n::XBuilder::new(storage) } + #[inline] + pub fn clean_up_inner_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + super::n::XBuilder::clean_up(storage.subdir("inner")) + } + #[inline] + pub fn clean_up_inner(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_inner_storage(self.storage.clone()) + } + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + Self::clean_up_single_storage(storage.clone())?; + Self::clean_up_list_storage(storage.clone())?; + Self::clean_up_multi_storage(storage.clone())?; + Self::clean_up_inner_storage(storage.clone())?; + storage.remove_resource("A.archive", schema::a::A) + } } impl ABuilder { diff --git a/flatdata-generator/tests/generators/rust_expectations/archives/ranges.rs.1 b/flatdata-generator/tests/generators/rust_expectations/archives/ranges.rs.1 index c40868e8..324f1513 100644 --- a/flatdata-generator/tests/generators/rust_expectations/archives/ranges.rs.1 +++ b/flatdata-generator/tests/generators/rust_expectations/archives/ranges.rs.1 @@ -173,6 +173,20 @@ impl ABuilder { flatdata::create_external_vector(&*self.storage, "data", schema::a::resources::DATA) } + #[inline] + pub fn clean_up_data_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("data", schema::a::resources::DATA) + } + #[inline] + pub fn clean_up_data(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_data_storage(self.storage.clone()) + } + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + Self::clean_up_data_storage(storage.clone())?; + storage.remove_resource("A.archive", schema::a::A) + } } impl ABuilder { diff --git a/flatdata-generator/tests/generators/rust_expectations/archives/raw_data.rs.1 b/flatdata-generator/tests/generators/rust_expectations/archives/raw_data.rs.1 index c71afbdf..4867dee5 100644 --- a/flatdata-generator/tests/generators/rust_expectations/archives/raw_data.rs.1 +++ b/flatdata-generator/tests/generators/rust_expectations/archives/raw_data.rs.1 @@ -83,6 +83,14 @@ impl ABuilder { self.storage.write("data", schema::a::resources::DATA, data) } + #[inline] + pub fn clean_up_data_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("data", schema::a::resources::DATA) + } + #[inline] + pub fn clean_up_data(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_data_storage(self.storage.clone()) + } /// Stores [`optional_data`] in the archive. /// /// [`optional_data`]: struct.A.html#method.optional_data @@ -91,6 +99,21 @@ impl ABuilder { self.storage.write("optional_data", schema::a::resources::OPTIONAL_DATA, data) } + #[inline] + pub fn clean_up_optional_data_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("optional_data", schema::a::resources::OPTIONAL_DATA) + } + #[inline] + pub fn clean_up_optional_data(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_optional_data_storage(self.storage.clone()) + } + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + Self::clean_up_data_storage(storage.clone())?; + Self::clean_up_optional_data_storage(storage.clone())?; + storage.remove_resource("A.archive", schema::a::A) + } } impl ABuilder { diff --git a/flatdata-generator/tests/generators/rust_expectations/archives/references.rs.1 b/flatdata-generator/tests/generators/rust_expectations/archives/references.rs.1 index 55c1e11a..b6669be9 100644 --- a/flatdata-generator/tests/generators/rust_expectations/archives/references.rs.1 +++ b/flatdata-generator/tests/generators/rust_expectations/archives/references.rs.1 @@ -877,6 +877,14 @@ impl ABuilder { flatdata::create_external_vector(&*self.storage, "list1", schema::a::resources::LIST1) } + #[inline] + pub fn clean_up_list1_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("list1", schema::a::resources::LIST1) + } + #[inline] + pub fn clean_up_list1(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_list1_storage(self.storage.clone()) + } #[inline] /// Stores [`list2`] in the archive. /// @@ -899,6 +907,14 @@ impl ABuilder { flatdata::create_external_vector(&*self.storage, "list2", schema::a::resources::LIST2) } + #[inline] + pub fn clean_up_list2_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("list2", schema::a::resources::LIST2) + } + #[inline] + pub fn clean_up_list2(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_list2_storage(self.storage.clone()) + } /// Opens [`multilist1`] in the archive for buffered writing. /// /// Elements can be added to the multivector until the [`MultiVector::close`] method @@ -912,6 +928,15 @@ impl ABuilder { flatdata::create_multi_vector(&*self.storage, "multilist1", schema::a::resources::MULTILIST1) } + #[inline] + pub fn clean_up_multilist1_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_multivector_index_resource("multilist1", schema::a::resources::MULTILIST1)?; + storage.remove_resource("multilist1", schema::a::resources::MULTILIST1) + } + #[inline] + pub fn clean_up_multilist1(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_multilist1_storage(self.storage.clone()) + } /// Opens [`multilist2`] in the archive for buffered writing. /// /// Elements can be added to the multivector until the [`MultiVector::close`] method @@ -925,6 +950,15 @@ impl ABuilder { flatdata::create_multi_vector(&*self.storage, "multilist2", schema::a::resources::MULTILIST2) } + #[inline] + pub fn clean_up_multilist2_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_multivector_index_resource("multilist2", schema::a::resources::MULTILIST2)?; + storage.remove_resource("multilist2", schema::a::resources::MULTILIST2) + } + #[inline] + pub fn clean_up_multilist2(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_multilist2_storage(self.storage.clone()) + } /// Stores [`raw1`] in the archive. /// /// [`raw1`]: struct.A.html#method.raw1 @@ -933,6 +967,14 @@ impl ABuilder { self.storage.write("raw1", schema::a::resources::RAW1, data) } + #[inline] + pub fn clean_up_raw1_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("raw1", schema::a::resources::RAW1) + } + #[inline] + pub fn clean_up_raw1(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_raw1_storage(self.storage.clone()) + } /// Stores [`raw2`] in the archive. /// /// [`raw2`]: struct.A.html#method.raw2 @@ -941,6 +983,14 @@ impl ABuilder { self.storage.write("raw2", schema::a::resources::RAW2, data) } + #[inline] + pub fn clean_up_raw2_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("raw2", schema::a::resources::RAW2) + } + #[inline] + pub fn clean_up_raw2(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_raw2_storage(self.storage.clone()) + } #[inline] /// Stores [`refs`] in the archive. /// @@ -963,6 +1013,14 @@ impl ABuilder { flatdata::create_external_vector(&*self.storage, "refs", schema::a::resources::REFS) } + #[inline] + pub fn clean_up_refs_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("refs", schema::a::resources::REFS) + } + #[inline] + pub fn clean_up_refs(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_refs_storage(self.storage.clone()) + } /// Opens [`multirefs`] in the archive for buffered writing. /// /// Elements can be added to the multivector until the [`MultiVector::close`] method @@ -976,6 +1034,28 @@ impl ABuilder { flatdata::create_multi_vector(&*self.storage, "multirefs", schema::a::resources::MULTIREFS) } + #[inline] + pub fn clean_up_multirefs_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_multivector_index_resource("multirefs", schema::a::resources::MULTIREFS)?; + storage.remove_resource("multirefs", schema::a::resources::MULTIREFS) + } + #[inline] + pub fn clean_up_multirefs(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_multirefs_storage(self.storage.clone()) + } + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + Self::clean_up_list1_storage(storage.clone())?; + Self::clean_up_list2_storage(storage.clone())?; + Self::clean_up_multilist1_storage(storage.clone())?; + Self::clean_up_multilist2_storage(storage.clone())?; + Self::clean_up_raw1_storage(storage.clone())?; + Self::clean_up_raw2_storage(storage.clone())?; + Self::clean_up_refs_storage(storage.clone())?; + Self::clean_up_multirefs_storage(storage.clone())?; + storage.remove_resource("A.archive", schema::a::A) + } } impl ABuilder { diff --git a/flatdata-generator/tests/generators/rust_expectations/archives/struct.rs.1 b/flatdata-generator/tests/generators/rust_expectations/archives/struct.rs.1 index 2a785e63..a623ea4a 100644 --- a/flatdata-generator/tests/generators/rust_expectations/archives/struct.rs.1 +++ b/flatdata-generator/tests/generators/rust_expectations/archives/struct.rs.1 @@ -201,6 +201,14 @@ impl ABuilder { self.storage.write("data", schema::a::resources::DATA, data) } + #[inline] + pub fn clean_up_data_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("data", schema::a::resources::DATA) + } + #[inline] + pub fn clean_up_data(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_data_storage(self.storage.clone()) + } #[inline] /// Stores [`optional_data`] in the archive. /// @@ -211,6 +219,21 @@ impl ABuilder { self.storage.write("optional_data", schema::a::resources::OPTIONAL_DATA, data) } + #[inline] + pub fn clean_up_optional_data_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("optional_data", schema::a::resources::OPTIONAL_DATA) + } + #[inline] + pub fn clean_up_optional_data(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_optional_data_storage(self.storage.clone()) + } + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + Self::clean_up_data_storage(storage.clone())?; + Self::clean_up_optional_data_storage(storage.clone())?; + storage.remove_resource("A.archive", schema::a::A) + } } impl ABuilder { diff --git a/flatdata-generator/tests/generators/rust_expectations/archives/subarchive.rs.1 b/flatdata-generator/tests/generators/rust_expectations/archives/subarchive.rs.1 index 187aec6f..ee6ad41a 100644 --- a/flatdata-generator/tests/generators/rust_expectations/archives/subarchive.rs.1 +++ b/flatdata-generator/tests/generators/rust_expectations/archives/subarchive.rs.1 @@ -69,6 +69,20 @@ impl XBuilder { self.storage.write("payload", schema::x::resources::PAYLOAD, data) } + #[inline] + pub fn clean_up_payload_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("payload", schema::x::resources::PAYLOAD) + } + #[inline] + pub fn clean_up_payload(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_payload_storage(self.storage.clone()) + } + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + Self::clean_up_payload_storage(storage.clone())?; + storage.remove_resource("X.archive", schema::x::X) + } } impl XBuilder { @@ -169,6 +183,14 @@ impl ABuilder { super::n::XBuilder::new(storage) } + #[inline] + pub fn clean_up_data_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + super::n::XBuilder::clean_up(storage.subdir("data")) + } + #[inline] + pub fn clean_up_data(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_data_storage(self.storage.clone()) + } /// Stores [`optional_data`] in the archive. /// /// [`optional_data`]: struct.A.html#method.optional_data @@ -178,6 +200,21 @@ impl ABuilder { super::n::XBuilder::new(storage) } + #[inline] + pub fn clean_up_optional_data_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + super::n::XBuilder::clean_up(storage.subdir("optional_data")) + } + #[inline] + pub fn clean_up_optional_data(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_optional_data_storage(self.storage.clone()) + } + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + Self::clean_up_data_storage(storage.clone())?; + Self::clean_up_optional_data_storage(storage.clone())?; + storage.remove_resource("A.archive", schema::a::A) + } } impl ABuilder { diff --git a/flatdata-generator/tests/generators/rust_expectations/archives/vector.rs.1 b/flatdata-generator/tests/generators/rust_expectations/archives/vector.rs.1 index dc0fb38b..d003e3a7 100644 --- a/flatdata-generator/tests/generators/rust_expectations/archives/vector.rs.1 +++ b/flatdata-generator/tests/generators/rust_expectations/archives/vector.rs.1 @@ -213,6 +213,14 @@ impl ABuilder { flatdata::create_external_vector(&*self.storage, "data", schema::a::resources::DATA) } + #[inline] + pub fn clean_up_data_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("data", schema::a::resources::DATA) + } + #[inline] + pub fn clean_up_data(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_data_storage(self.storage.clone()) + } #[inline] /// Stores [`optional_data`] in the archive. /// @@ -235,6 +243,21 @@ impl ABuilder { flatdata::create_external_vector(&*self.storage, "optional_data", schema::a::resources::OPTIONAL_DATA) } + #[inline] + pub fn clean_up_optional_data_storage(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + storage.remove_resource("optional_data", schema::a::resources::OPTIONAL_DATA) + } + #[inline] + pub fn clean_up_optional_data(&self) -> Result<(), ::std::io::Error> { + Self::clean_up_optional_data_storage(self.storage.clone()) + } + + #[inline] + pub fn clean_up(storage: flatdata::StorageHandle) -> Result<(), ::std::io::Error> { + Self::clean_up_data_storage(storage.clone())?; + Self::clean_up_optional_data_storage(storage.clone())?; + storage.remove_resource("A.archive", schema::a::A) + } } impl ABuilder { diff --git a/flatdata-rs/lib/src/filestorage.rs b/flatdata-rs/lib/src/filestorage.rs index 8e1e14d9..523f81a6 100644 --- a/flatdata-rs/lib/src/filestorage.rs +++ b/flatdata-rs/lib/src/filestorage.rs @@ -77,6 +77,16 @@ impl ResourceStorage for FileResourceStorage { self.path.join(resource_name).exists() } + fn remove(&self, resource_name: &str) -> Result<(), io::Error> { + let resource_path = self.path.join(resource_name); + fs::remove_file(&resource_path).map_err(|e| { + io::Error::new( + e.kind(), + String::from(resource_path.to_str().unwrap_or(resource_name)), + ) + }) + } + fn read_resource(&self, resource_name: &str) -> Result<&[u8], io::Error> { let resource_path = self.path.join(resource_name); if !resource_path.exists() { diff --git a/flatdata-rs/lib/src/memstorage.rs b/flatdata-rs/lib/src/memstorage.rs index 04fdee21..64eeb797 100644 --- a/flatdata-rs/lib/src/memstorage.rs +++ b/flatdata-rs/lib/src/memstorage.rs @@ -104,6 +104,17 @@ impl ResourceStorage for MemoryResourceStorage { .contains_key(&resource_path) } + fn remove(&self, resource_name: &str) -> Result<(), io::Error> { + let resource_path = self.path.join(resource_name); + self.storage.streams.lock().unwrap().remove(&resource_path); + self.storage + .resources + .lock() + .unwrap() + .remove(&resource_path); + Ok(()) + } + fn read_resource(&self, resource_name: &str) -> Result<&[u8], io::Error> { let resource_path = self.path.join(resource_name); if !self diff --git a/flatdata-rs/lib/src/storage.rs b/flatdata-rs/lib/src/storage.rs index d3c4eabf..43b38bbd 100644 --- a/flatdata-rs/lib/src/storage.rs +++ b/flatdata-rs/lib/src/storage.rs @@ -50,6 +50,42 @@ pub trait ResourceStorage: std::fmt::Debug { write_schema(schema, &mut stream) } + /// remove resource and the corresponding schema + /// fail if resource or schema is missing or the existing schema doesn't match the expected + /// schema + fn remove_resource(&self, resource_name: &str, expected_schema: &str) -> Result<(), io::Error> { + let schema_name = format!("{}.schema", resource_name); + let resource_exists = self.exists(resource_name); + let schema_exists = self.exists(&schema_name); + if !resource_exists && !schema_exists { + return Ok(()); + } else if resource_exists != schema_exists { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + format!("resource or schema missing for {}", resource_name), + )); + } + let stored_schema = self.read_resource(&schema_name)?; + if stored_schema != expected_schema.as_bytes() { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + format!("unexpected schema for {}", resource_name), + )); + } + self.remove(resource_name)?; + self.remove(&schema_name) + } + + /// remove multivector index resource + fn remove_multivector_index_resource( + &self, + resource_name: &str, + expected_schema: &str, + ) -> Result<(), io::Error> { + let index_name = format!("{}_index", resource_name); + let index_schema = format!("index({})", expected_schema); + self.remove_resource(&index_name, &index_schema) + } // // Virtual // @@ -60,6 +96,9 @@ pub trait ResourceStorage: std::fmt::Debug { /// Returns `true` if resource exists in the storage. fn exists(&self, resource_name: &str) -> bool; + /// remove the resource with key `resouce_name` + fn remove(&self, resource_name: &str) -> Result<(), io::Error>; + /// Reads a resource in storage and returns a pointer to its raw data. /// /// This is a low level facility for opening and reading resources. Cf. diff --git a/flatdata-rs/lib/src/tarstorage.rs b/flatdata-rs/lib/src/tarstorage.rs index 6809fc81..2b4be66b 100644 --- a/flatdata-rs/lib/src/tarstorage.rs +++ b/flatdata-rs/lib/src/tarstorage.rs @@ -112,6 +112,12 @@ impl ResourceStorage for TarArchiveResourceStorage { .is_some() } + fn remove(&self, _resource_name: &str) -> Result<(), io::Error> { + Err(io::Error::other( + "Removing parts of tar archives is not supported", + )) + } + fn read_resource(&self, resource_name: &str) -> Result<&[u8], io::Error> { let resource_path = self.sub_path.join(resource_name); if let Some(data) = self.storage.read(&resource_path) { diff --git a/flatdata-rs/lib/src/test/mod.rs b/flatdata-rs/lib/src/test/mod.rs index 01869db3..fdb46825 100644 --- a/flatdata-rs/lib/src/test/mod.rs +++ b/flatdata-rs/lib/src/test/mod.rs @@ -18,6 +18,8 @@ // TODO: Implement missing docs // TODO: Implement missing debugs #![allow(dead_code, missing_debug_implementations, missing_docs)] +// The generated file defines `mod test`, which is included inside `mod test`. +#![allow(clippy::module_inception)] include!("test_generated.rs"); diff --git a/flatdata-rs/tests/coappearances/src/lib.rs b/flatdata-rs/tests/coappearances/src/lib.rs index 20f4aa44..27db72a4 100644 --- a/flatdata-rs/tests/coappearances/src/lib.rs +++ b/flatdata-rs/tests/coappearances/src/lib.rs @@ -177,11 +177,9 @@ fn copy_coappearances_archive( // open for writing let archive_path = env::temp_dir().join(to_path); println!("Will create archive in: {}", archive_path.to_str().unwrap()); - if archive_path.exists() { - fs::remove_dir_all(&archive_path).expect("could not remove already existing archive"); - } fs::create_dir_all(&archive_path).expect("could not create archive dir"); let storage = flatdata::FileResourceStorage::new(archive_path.clone()); + coappearances::GraphBuilder::clean_up(storage.clone()).expect("Cleanup failed"); let gb = coappearances::GraphBuilder::new(storage).expect("could not create archive"); // copy data