#include "proto_helpers.h" #include #include #include #include #include #include #include #include #include #include namespace NYT { using ::google::protobuf::Message; using ::google::protobuf::Descriptor; using ::google::protobuf::DescriptorPool; using ::google::protobuf::io::CodedInputStream; //////////////////////////////////////////////////////////////////////////////// namespace { TVector GetJobDescriptors(const TString& fileName) { TVector descriptors; if (!TFsPath(fileName).Exists()) { ythrow TIOException() << "Cannot load '" << fileName << "' file"; } TIFStream input(fileName); TString line; while (input.ReadLine(line)) { const auto* pool = DescriptorPool::generated_pool(); const auto* descriptor = pool->FindMessageTypeByName(line); descriptors.push_back(descriptor); } return descriptors; } } // namespace //////////////////////////////////////////////////////////////////////////////// TVector GetJobInputDescriptors() { return GetJobDescriptors("proto_input"); } TVector GetJobOutputDescriptors() { return GetJobDescriptors("proto_output"); } void ValidateProtoDescriptor( const Message& row, size_t tableIndex, const TVector& descriptors, bool isRead) { const char* direction = isRead ? "input" : "output"; if (tableIndex >= descriptors.size()) { ythrow TIOException() << "Table index " << tableIndex << " is out of range [0, " << descriptors.size() << ") in " << direction; } if (row.GetDescriptor() != descriptors[tableIndex]) { ythrow TIOException() << "Invalid row of type " << row.GetDescriptor()->full_name() << " at index " << tableIndex << ", row of type " << descriptors[tableIndex]->full_name() << " expected in " << direction; } } void ParseFromArcadiaStream(IInputStream* stream, Message& row, ui32 length) { TLengthLimitedInput input(stream, length); TProtobufInputStreamAdaptor adaptor(&input); CodedInputStream codedStream(&adaptor); codedStream.SetTotalBytesLimit(length + 1); bool parsedOk = row.ParseFromCodedStream(&codedStream); Y_ENSURE(parsedOk, "Failed to parse protobuf message"); Y_ENSURE(input.Left() == 0); } //////////////////////////////////////////////////////////////////////////////// } // namespace NYT