Skip to content

Commit 08b3aa8

Browse files
committed
FIX: Fix encoding metadata changing version
1 parent 3a8b1c1 commit 08b3aa8

File tree

7 files changed

+46
-14
lines changed

7 files changed

+46
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
`Metadata` is represented in other languages and removed the boolean
1414
`has_mixed_stype_in` and `has_mixed_schema` fields
1515

16+
### Bug fixes
17+
- Fixed behavior where encoding metadata could lower the `version`
18+
1619
## 0.36.0 - 2025-05-27
1720

1821
This version marks the release of DBN version 3 (DBNv3), which is the new default.

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,17 @@ Here is a simple program that fetches 10 seconds of trades for all ES mini futur
9595
#include <databento/live.hpp>
9696
#include <databento/symbol_map.hpp>
9797
#include <iostream>
98-
#include <string>
9998
#include <thread>
10099

101100
using namespace databento;
102101

103102
int main() {
104103
PitSymbolMap symbol_mappings;
105104

106-
auto client =
107-
LiveBuilder{}.SetKeyFromEnv().SetDataset("GLBX.MDP3").BuildThreaded();
105+
auto client = LiveBuilder{}
106+
.SetKeyFromEnv()
107+
.SetDataset(Dataset::GlbxMdp3)
108+
.BuildThreaded();
108109

109110
auto handler = [&symbol_mappings](const Record& rec) {
110111
symbol_mappings.OnRecord(rec);

examples/live/readme.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
#include <databento/live.hpp>
66
#include <databento/symbol_map.hpp>
77
#include <iostream>
8-
#include <string>
98
#include <thread>
109

1110
using namespace databento;
1211

1312
int main() {
1413
PitSymbolMap symbol_mappings;
1514

16-
auto client =
17-
LiveBuilder{}.SetKeyFromEnv().SetDataset("GLBX.MDP3").BuildThreaded();
15+
auto client = LiveBuilder{}
16+
.SetKeyFromEnv()
17+
.SetDataset(Dataset::GlbxMdp3)
18+
.BuildThreaded();
1819

1920
auto handler = [&symbol_mappings](const Record& rec) {
2021
symbol_mappings.OnRecord(rec);

examples/live/simple.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ int main() {
2323
.SetLogReceiver(log_receiver.get())
2424
.SetSendTsOut(true)
2525
.SetKeyFromEnv()
26-
.SetDataset(databento::dataset::kGlbxMdp3)
26+
.SetDataset(databento::Dataset::GlbxMdp3)
2727
.BuildThreaded();
2828

2929
// Set up signal handler for Ctrl+C
3030
std::signal(SIGINT, [](int signal) { gSignal = signal; });
3131

32-
std::vector<std::string> symbols{"ESZ4", "ESZ4 C4200", "ESZ4 P4100"};
32+
std::vector<std::string> symbols{"ESZ5", "ESZ5 C6200", "ESZ5 P5500"};
3333
client.Subscribe(symbols, databento::Schema::Definition,
3434
databento::SType::RawSymbol);
3535
client.Subscribe(symbols, databento::Schema::Mbo,

src/dbn_encoder.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,15 @@ DbnEncoder::DbnEncoder(const Metadata& metadata, IWritable* output)
8787
}
8888

8989
void DbnEncoder::EncodeMetadata(const Metadata& metadata, IWritable* output) {
90-
const auto version = std::min<std::uint8_t>(
91-
std::max<std::uint8_t>(1, metadata.version), kDbnVersion);
90+
if (metadata.version > kDbnVersion) {
91+
throw databento::InvalidArgumentError{
92+
"EncodeMetadata", "metadata",
93+
"Can't encode Metadata with version " +
94+
std::to_string(+metadata.version) +
95+
" which is greater than the maximum supported version " +
96+
std::to_string(+kDbnVersion)};
97+
}
98+
const auto version = std::max<std::uint8_t>(1, metadata.version);
9299
EncodeChars(kDbnPrefix, kMagicSize - 1, output);
93100
EncodeAsBytes(version, output);
94101
const auto [length, end_padding] = CalcLength(metadata);

tests/include/temp_file.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <gtest/gtest.h> // EXPECT_EQ
44

55
#include <cassert> // assert
6-
#include <cstdio> // remove
76
#include <filesystem>
87
#include <fstream> // ifstream
98
#include <string>
@@ -29,9 +28,8 @@ class TempFile {
2928
TempFile(TempFile&&) = default;
3029
TempFile& operator=(TempFile&&) = default;
3130
~TempFile() {
32-
const int ret = std::remove(path_.c_str());
33-
EXPECT_EQ(ret, 0) << "TempFile couldn't remove file at " << path_ << ": "
34-
<< ::strerror(errno);
31+
const bool ret = std::filesystem::remove(path_);
32+
EXPECT_TRUE(ret) << "TempFile at " << path_ << " did not exist";
3533
}
3634

3735
const std::filesystem::path& Path() const { return path_; }

tests/src/dbn_encoder_tests.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "databento/dbn.hpp"
1010
#include "databento/dbn_decoder.hpp"
1111
#include "databento/dbn_encoder.hpp"
12+
#include "databento/exceptions.hpp"
1213
#include "databento/log.hpp"
1314
#include "mock/mock_io.hpp"
1415

@@ -41,4 +42,25 @@ TEST(DbnEncoderTests, TestEncodeDecodeMetadataIdentity) {
4142
const auto res = decoder.DecodeMetadata();
4243
ASSERT_EQ(res, metadata);
4344
}
45+
46+
TEST(DbnEncoderTests, TestEncodeNewerMetadataErrors) {
47+
auto logger = std::make_unique<NullLogReceiver>();
48+
const Metadata metadata{kDbnVersion + 1,
49+
dataset::kGlbxMdp3,
50+
Schema::Mbp10,
51+
{},
52+
UnixNanos{},
53+
0,
54+
SType::RawSymbol,
55+
SType::InstrumentId,
56+
true,
57+
kSymbolCstrLen,
58+
{},
59+
{},
60+
{},
61+
{}};
62+
mock::MockIo io{};
63+
ASSERT_THROW(DbnEncoder::EncodeMetadata(metadata, &io),
64+
databento::InvalidArgumentError);
65+
}
4466
} // namespace databento::tests

0 commit comments

Comments
 (0)