Skip to content

Commit

Permalink
[EN-7411] Implement ConvertTapeFile sample
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatolyKalin committed Aug 30, 2023
1 parent 99e72d9 commit c289f02
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ if (DXFCXX_BUILD_SAMPLES)
add_subdirectory(samples/cpp/PrintQuoteEvents)
add_subdirectory(samples/cpp/DxFeedSample)
add_subdirectory(samples/cpp/WriteTapeFile)
add_subdirectory(samples/cpp/ConvertTapeFile)
endif ()

if (DXFCXX_BUILD_TOOLS)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ be downloaded from [Release](https://github.com/dxFeed/dxfeed-graal-cxx-api/rele
-->

## Samples
<!--
* [ConvertTapeFile](https://github.com/dxFeed/dxfeed-graal-cxx-api/blob/main/samples/cpp/ConvertTapeFile/src/main.cpp)
demonstrates how to convert one tape file to another tape file with optional intermediate processing or filtering
<!--
* [DxFeedConnect](https://github.com/dxFeed/dxfeed-graal-cxx-api/blob/main/samples/cpp/DxFeedConnect/src/main.cpp)
demonstrates how to subscribe various market events for the specified symbols
* [DxFeedFileParser](https://github.com/dxFeed/dxfeed-graal-cxx-api/blob/main/samples/cpp/DxFeedFileParser/src/main.cpp)
Expand Down
2 changes: 2 additions & 0 deletions include/dxfeed_graal_cpp_api/event/EventTypeEnum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class DXFCPP_EXPORT EventTypeEnum {
static const EventTypeEnum SERIES;
static const EventTypeEnum OPTION_SALE;

static const std::vector<std::reference_wrapper<const EventTypeEnum>> ALL;

explicit EventTypeEnum() noexcept : EventTypeEnum{static_cast<std::uint32_t>(-1), "INVALID", false} {
}

Expand Down
81 changes: 81 additions & 0 deletions samples/cpp/ConvertTapeFile/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright (c) 2023 Devexperts LLC.
# SPDX-License-Identifier: MPL-2.0

cmake_minimum_required(VERSION 3.21)

if (POLICY CMP0092)
cmake_policy(SET CMP0092 NEW)
endif ()

if (POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
endif ()

project(ConvertTapeFile LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 11)
set(CXX_EXTENSIONS OFF)
set(C_EXTENSIONS OFF)

if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(CMAKE_MACOSX_RPATH ON)
set(CMAKE_SKIP_BUILD_RPATH ON)
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH OFF)
set(CMAKE_BUILD_RPATH_USE_ORIGIN ON)
set(CMAKE_INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR};@loader_path;@executable_path;@executable_path/../Frameworks")
elseif (UNIX)
set(CMAKE_SKIP_BUILD_RPATH ON)
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH OFF)
set(CMAKE_BUILD_RPATH_USE_ORIGIN ON)
set(CMAKE_INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}:$ORIGIN/../lib64:$ORIGIN/../lib:$ORIGIN")
endif ()

add_executable(${PROJECT_NAME} src/main.cpp)

if (DXFCXX_ENABLE_VS_ASAN)
target_compile_options(${PROJECT_NAME} PRIVATE "/fsanitize=address")
target_link_options(${PROJECT_NAME} PRIVATE "/fsanitize=address")

target_compile_definitions(${PROJECT_NAME}
PUBLIC
$<$<COMPILE_LANG_AND_ID:CXX,MSVC>:
_DISABLE_VECTOR_ANNOTATION
_DISABLE_STRING_ANNOTATION
>
)
endif ()

target_include_directories(${PROJECT_NAME} PRIVATE ../../../include)

if (DXFCXX_ENABLE_ASAN_UBSAN)
target_compile_options(${PROJECT_NAME} PRIVATE "-fsanitize=address" "-fsanitize=undefined" "-fPIC")
target_link_options(${PROJECT_NAME} PRIVATE "-fsanitize=address" "-fsanitize=undefined" "-fPIC")

target_link_libraries(${PROJECT_NAME} PRIVATE dxfcxx)
target_compile_definitions(${PROJECT_NAME} PRIVATE DXFCPP_USE_DLLS)

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:dxfcxx::graal>
${CMAKE_CURRENT_SOURCE_DIR}/ConvertTapeFile.in
$<TARGET_FILE:dxfcxx>
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
else ()
target_link_libraries(${PROJECT_NAME} PRIVATE dxfcxx::static)

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:dxfcxx::graal>
${CMAKE_CURRENT_SOURCE_DIR}/ConvertTapeFile.in
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
endif ()

if (DXFCXX_INSTALL AND DXFCXX_INSTALL_SAMPLES)
install(TARGETS ${PROJECT_NAME})
install(FILES "ConvertTapeFile.in" DESTINATION ${CMAKE_INSTALL_BINDIR})

if (WIN32)
install(FILES $<TARGET_FILE:dxfcxx::graal> DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()
endif ()
23 changes: 23 additions & 0 deletions samples/cpp/ConvertTapeFile/ConvertTapeFile.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
==DXP3 type=tape version=QDS-3.315 time=field +STREAM_DATA
==STREAM_DATA
=Quote EventSymbol EventTime BidTime BidExchangeCode BidPrice BidSize AskTime AskExchangeCode AskPrice AskSize
Quote AAPL 20230216-180402.785+0300 20230216-180402+0300 Q 153.94 2 20230216-180402+0300 Q 153.95 1
Quote AAPL 20230216-180402.805+0300 20230216-180402+0300 Q 153.94 2 20230216-180402+0300 Z 153.96 2
Quote AAPL 20230216-180402.871+0300 20230216-180402+0300 Q 153.94 2 20230216-180402+0300 Q 153.95 1
Quote AAPL 20230216-180402.878+0300 20230216-180402+0300 Q 153.94 2 20230216-180402+0300 Q 153.96 3
Quote AAPL 20230216-180402.922+0300 20230216-180402+0300 P 153.94 2 20230216-180402+0300 Q 153.96 4
Quote AAPL 20230216-180402.945+0300 20230216-180402+0300 P 153.94 2 20230216-180402+0300 Q 153.96 3
Quote AAPL 20230216-180402.954+0300 20230216-180402+0300 P 153.94 2 20230216-180402+0300 Q 153.96 2
Quote AAPL 20230216-180402.969+0300 20230216-180402+0300 P 153.94 2 20230216-180402+0300 Q 153.96 3
Quote AAPL 20230216-180402.989+0300 20230216-180402+0300 P 153.94 2 20230216-180402+0300 Q 153.95 1
Quote AAPL 20230216-180403.005+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.96 3
Quote AAPL 20230216-180403.010+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.96 4
Quote AAPL 20230216-180403.121+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.96 3
Quote AAPL 20230216-180403.149+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.95 1
Quote AAPL 20230216-180403.182+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.96 3
Quote AAPL 20230216-180403.245+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.96 4
Quote AAPL 20230216-180403.309+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.95 1
Quote AAPL 20230216-180403.353+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Z 153.96 3
Quote AAPL 20230216-180403.507+0300 20230216-180403+0300 Q 153.94 2 20230216-180403+0300 Q 153.96 3
Quote AAPL 20230216-180403.516+0300 20230216-180403+0300 Q 153.95 1 20230216-180403+0300 Z 153.96 1
Quote AAPL 20230216-180403.523+0300 20230216-180403+0300 Q 153.95 1 20230216-180403+0300 Q 153.96 1
72 changes: 72 additions & 0 deletions samples/cpp/ConvertTapeFile/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2023 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#include <dxfeed_graal_cpp_api/api.hpp>

using namespace dxfcpp;
using namespace dxfcpp::literals;
using namespace std::literals;

/**
* Converts one tape file into another tape file with optional intermediate processing or filtering.
* This project contains sample tape file ConvertTapeFile.in.
*/
int main(int argc, char *argv[]) {
// Determine input and output tapes and specify appropriate configuration parameters.
std::string inputAddress = argc > 1 ? argv[0] : "file:ConvertTapeFile.in[readAs=stream_data,speed=max]";
std::string outputAddress = argc > 2 ? argv[1] : "tape:ConvertTapeFile.out[saveAs=stream_data,format=text]";

// Create input endpoint configured for tape reading.
auto inputEndpoint =
DXEndpoint::newBuilder()
->withRole(DXEndpoint::Role::STREAM_FEED) // Prevents event conflation and loss due to buffer overflow.
->withProperty(DXEndpoint::DXFEED_WILDCARD_ENABLE_PROPERTY, "true") // Enables wildcard subscription.
->withProperty(DXEndpoint::DXENDPOINT_EVENT_TIME_PROPERTY, "true") // Use provided event times.
->build();

// Create output endpoint configured for tape writing.
auto outputEndpoint =
DXEndpoint::newBuilder()
->withRole(DXEndpoint::Role::STREAM_PUBLISHER) // Prevents event conflation and loss due to buffer overflow.
->withProperty(DXEndpoint::DXFEED_WILDCARD_ENABLE_PROPERTY, "true") // Enables wildcard subscription.
->withProperty(DXEndpoint::DXENDPOINT_EVENT_TIME_PROPERTY, "true") // Use provided event times.
->build();

// Create and link event processor for all types of events.
// Note: Set of processed event types could be limited if needed.
auto sub = inputEndpoint->getFeed()->createSubscription(EventTypeEnum::ALL);

sub->addEventListener([outputEndpoint](auto events) {
// Here event processing occurs. Events could be modified, removed, or new events added.
// For example, the below code adds 1 hour to event times:
// for (auto &e : events) {
// if (auto event = e->template sharedAs<EventType>(); event) {
// event->setEventTime(event->getEventTime() + 3600'000);
// }
// }

// Publish processed events
auto publisher = outputEndpoint->getPublisher();

publisher->publishEvents(events);
});

// Subscribe to all symbols.
// Note: Set of processed symbols could be limited if needed.
sub->addSymbols(WildcardSymbol::ALL);

// Connect output endpoint and start output tape writing BEFORE starting input tape reading.
outputEndpoint->connect(outputAddress);
// Connect input endpoint and start input tape reading AFTER starting output tape writing.
inputEndpoint->connect(inputAddress);

// Wait until all data is read and processed, and then gracefully close input endpoint.
inputEndpoint->awaitNotConnected();
inputEndpoint->closeAndAwaitTermination();

// Wait until all data is processed and written, and then gracefully close output endpoint.
outputEndpoint->awaitProcessed();
outputEndpoint->closeAndAwaitTermination();

return 0;
}
20 changes: 20 additions & 0 deletions src/event/EventTypeEnum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,24 @@ const EventTypeEnum EventTypeEnum::SPREAD_ORDER{DXFG_EVENT_SPREAD_ORDER, "SPREAD
const EventTypeEnum EventTypeEnum::SERIES{DXFG_EVENT_SERIES, "SERIES", false, true};
const EventTypeEnum EventTypeEnum::OPTION_SALE{DXFG_EVENT_OPTION_SALE, "OPTION_SALE", false, true};

const std::vector<std::reference_wrapper<const EventTypeEnum>> EventTypeEnum::ALL{
std::cref(QUOTE),
std::cref(PROFILE),
std::cref(SUMMARY),
std::cref(GREEKS),
std::cref(CANDLE),
std::cref(UNDERLYING),
std::cref(THEO_PRICE),
std::cref(TRADE),
std::cref(TRADE_ETH),
//std::cref(CONFIGURATION),
//std::cref(MESSAGE),
std::cref(TIME_AND_SALE),
std::cref(ORDER),
std::cref(ANALYTIC_ORDER),
std::cref(SPREAD_ORDER),
std::cref(SERIES),
std::cref(OPTION_SALE),
};

} // namespace dxfcpp

0 comments on commit c289f02

Please sign in to comment.