2011-12-01 3 views
4

protobuf 바이너리 파일 io에서 이상한 동작이 나타납니다. protobuf 중간 파일에 텍스트 코퍼스를 사전 처리하고 있습니다. 다음과 같이 내 직렬화 클래스는 같습니다프로토콜 버퍼 문제가 발생했습니다. 이진 파일에 여러 개의 직렬화가 발생했습니다.

0:0:8 
132:1:8 
227:2:6 
303:3:6 
381:4:19 
849:5:9 
1028:6:2 
1048:7:18 
1333:8:28 
2473:9:24 

첫 번째 필드는 직렬화가 정상적으로 진행되고 있음을 보여줍니다처럼

class pb_session_printer 
    { 
    public: 
    pb_session_printer(std::string & filename) 
     : out(filename.c_str(), std::fstream::out | std::fstream::trunc | 
           std::fstream|binary) 
     {} 

    void print_batch(std::vector<session> & pb_sv) 
    { 
     boost::lock_guard<boost::mutex> lock(m); 

     BOOST_FOREACH(session & s, pb_sv) 
     { 
     std::cout << out.tellg() << ":"; 
     s.SerializeToOstream(&out); 
     out.flush(); 
     std::cout << s.session_id() << ":" << s.action_size() << std::endl; 
     } 
     exit(0); 
    } 

    std::fstream out; 
    boost::mutex m; 
    }; 

출력의 조각이 보인다. 내 부하 프로그램을 실행하면

:

int main() 
{ 
    std::fstream in_file("out_file", std::fstream::in | std::ios::binary); 
    session s; 

    std::cout << in_file.tellg() << std::endl; 
    s.ParseFromIstream(&in_file); 
    std::cout << in_file.tellg() << std::endl; 
    std::cout << s.session_id() << std::endl; 

    s.ParseFromIstream(&in_file); 
} 

를 내가 얻을 :

0 
-1 
111 
libprotobuf ERROR google/protobuf/message_lite.cc:123] Can't parse message of type 
"session" because it is missing required fields: session_id 

SESSION_ID : (111)은 스트림의 끝으로 항목, 내가 명확의 의미를 이해하지 못하는 라이브러리의 바이너리 io 기능. 도와주세요.

답변

4

하나의 파일에 여러 protobuffers를 작성하는 경우 protobuf + protobuffer의 크기를 쓰고 별도로 읽어야합니다 (Cat Plus Plus가 언급 된대로 ParseFromIstream없이). protobuffer를 읽을 때 ParseFromArray으로 구문 분석 할 수 있습니다.

파일의 크기가이 (공백은 단지 가독성을위한) 보일 것이다

크기 protobuf 크기 protobuf 크기 protobuf 등

3

Message::ParseFromIstreamis documented 전체 입력을 소모합니다. 동일한 유형의 메시지 시퀀스를 직렬화하기 때문에 repeated 필드를 사용하여 새 메시지를 만들고 그 필드로 작업 할 수 있습니다.

+0

예를, 나뿐만 아니라 궁금 날 잡았어, 읽어 보시기 바랍니다. 텍스트 코퍼스는 17gigs이고 3 억 4 천만 줄입니다. (당신이 제안하는 것은 전체 코퍼스가 한번에 메모리에 있어야한다는 뜻입니까? –

+0

@HassanSyed : 아마 하나를 소비하는 구문 분석 기능을 구현하는 또 다른 옵션이 있습니다. –

+0

재미있는 점은 스트림의 모든 "세션"을 먼저 탐색하고 끝에있는 세션 111을 반환한다는 것입니다. 첫 번째 세션을 읽었을 때 파일 헤드를 지나치는 것으로 생각했을 것입니다. end. –

관련 문제