SlideShare a Scribd company logo
1 of 27
GOOGLE PROTOCOL
BUFFERS
Плюсы и минусы использования в
реальных проектах
Как все это работает
• Создайте .proto файл в котором будет информация о
 том что вы будете сериализовать



• Сгенерируйте файлы с учетом нужного языка
 программирования используя утилиту protoc.exe



• Добавьте код для вызова функций сериализации
Поддерживаемые языки


• С++




• Java




• Python
Other Languages
• Action Script
•C
• C#
• Haskell
• Javascript
• Objective C
• Perl
• PHP
• Ruby
• Scala
• Visual Basic
Proto файл
message Person {
 required string name = 1;
 required int32 id = 2;
 optional string email = 3;

    enum PhoneType {
      MOBILE = 0;
      HOME = 1;
      WORK = 2;
    }

    message PhoneNumber {
      required string number = 1;
      optional PhoneType type = 2 [default = HOME];
    }

    repeated PhoneNumber phone = 4;
}
С чего начинается .proto
                    Message




message SearchRequest
{
optional int32 page_number = 1;
}
Message Field Types
• double,    float, int32, int64, uint32, uint64, sint32, sint64,
    fixed32, fixed64, sfixed32, sfixed64, bool, string, bytes



• enum Smth
{
     UNIVERSAL = 0;
     WEB = 1;
     IMAGES = 2;
}



• Other Messages
Field Rules
В сообщении могут быть поля следующих типов

• required: 1


• optional: 0 или 1


• repeated: > 0
Required Field
required int32 foo = 1;

• bool has_foo() const
• int32 foo() const
• void set_foo(int32 value)
• void clear_foo()
Optional Field
optional Int32 foo = 1;

• bool has_foo() const
• int32 foo() const
• void set_foo(int32 value)
• void clear_foo()


IsInitialized = true;
foo вернет дефолтное значение
String Field
optional string foo = 1;
required string foo = 1;

• bool has_foo() const
• const string& foo() const
• void set_foo(const string& value)
• void set_foo(const char* value)
• void set_foo(const char* value, int size)
• string* mutable_foo()
• void clear_foo()
• string* release_foo()
Enum Fields
enum Bar { BAR_VALUE = 1; }
optional Bar foo = 1;
required Bar foo = 1;



• bool has_foo() const
• Bar foo() const
• void set_foo(Bar value)
• void clear_foo()
Message Fields
message Bar {}
optional Bar foo = 1;
required Bar foo = 1;

• bool has_foo() const
• const Bar& foo() const
• Bar* mutable_foo()
• void clear_foo()
• Bar* release_foo()
Repeated Numeric Fields
repeated int32 foo = 1;

• int foo_size() const
• int32 foo(int index) const
• void set_foo(int index, int32 value)
• void add_foo(int32 value)
• void clear_foo()
• const RepeatedField<int32>& foo() const
• RepeatedField<int32>* mutable_foo()
Repeated String Fields
repeated string foo = 1;
repeated bytes foo = 1;

•   int foo_size() const
•   const string& foo(int index) const
•   void set_foo(int index, const string& value)
•   void set_foo(int index, const char* value)
•   void set_foo(int index, const char* value, int size)
•   string* mutable_foo(int index)
•   void add_foo(const string& value)
•   void add_foo(const char* value)
•   void add_foo(const char* value, int size)
•   string* add_foo()
•   void clear_foo()
•   const RepeatedPtrField<string>& foo() const
•   RepeatedPtrField<string>* mutable_foo()
Repeated Enum Fields
enum Bar { BAR_VALUE = 1; }
repeated Bar foo = 1;

• int foo_size() const
• Bar foo(int index) const
• void set_foo(int index, Bar value)
• void add_foo(Bar value)
• void clear_foo()
• const RepeatedField<int>& foo() const
• RepeatedField<int>* mutable_foo()
Repeated Embedded Message Fields
message Bar {}
repeated Bar foo = 1;

• int int foo_size() const
• const Bar& foo(int index) const
• Bar* mutable_foo(int index)
• Bar* add_foo()
• void clear_foo()
• const RepeatedPtrField<Bar>& foo() const
• RepeatedPtrField<Bar>* mutable_foo()
Enumerations
enum Foo {
  VALUE_A = 1;
  VALUE_B = 5;
}



• const const EnumDescriptor* Foo_descriptor()
• bool Foo_IsValid(int value)
Arrays



• String


• Bytes
String




         String => std::string
         Bytes => std::string
Convert

std::wstring ConvertToWstring(const std::string& source)
{
   if (source.empty())
       return std::wstring();

    return std::wstring((const wchar_t *)source.c_str(),
         source.size() / sizeof(wchar_t));
}
Generate File




      protoc --cpp_out=. test.proto
Serializing

SearchRequest pack;
pack.set_opt("test");
pack.set_req("test");
std::vector<char> vec(pack.ByteSize());

pack.SerializeToArray(&vec.at(0), vec.size());
Deserializing


SearchRequest unpack;

unpack.ParseFromArray(&vec.at(0), vec.size());
Pack Functions
Bool SerializeToCodedStream(io::CodedOutputStream * output)
const

Bool SerializeToZeroCopyStream(io::ZeroCopyOutputStream *
output) const

Bool SerializeToString(string * output) const

Bool SerializeToArray(void * data, int size) const
String SerializeAsString() const

Bool AppendToString(string * output) const
Cookies
• Поддержка extensions
    message Foo { // ... extensions 100 to 199; }
    extend Foo { optional int32 bar = 126; }
• Packages
    package foo.bar;
    message Open { ... }
• Options
    option optimize_for = CODE_SIZE;
    option optimize_for = SPEED;
Questions?

More Related Content

Viewers also liked

RProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for RRProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for RRomain Francois
 
Melbourne Microservices Meetup: Agenda for a new Architecture
Melbourne Microservices Meetup: Agenda for a new ArchitectureMelbourne Microservices Meetup: Agenda for a new Architecture
Melbourne Microservices Meetup: Agenda for a new ArchitectureSaul Caganoff
 
Redis for the Everyday Developer
Redis for the Everyday DeveloperRedis for the Everyday Developer
Redis for the Everyday DeveloperRoss Tuck
 
Mongodb, our Swiss Army Knife Database
Mongodb, our Swiss Army Knife DatabaseMongodb, our Swiss Army Knife Database
Mongodb, our Swiss Army Knife DatabaseMathieu Poumeyrol
 
Microservices At Gilt - NYC Microservices Meetup
Microservices At Gilt - NYC Microservices MeetupMicroservices At Gilt - NYC Microservices Meetup
Microservices At Gilt - NYC Microservices MeetupMichael Bryzek
 
Building and deploying microservices with event sourcing, CQRS and Docker (Me...
Building and deploying microservices with event sourcing, CQRS and Docker (Me...Building and deploying microservices with event sourcing, CQRS and Docker (Me...
Building and deploying microservices with event sourcing, CQRS and Docker (Me...Chris Richardson
 
Google Protocol buffer
Google Protocol bufferGoogle Protocol buffer
Google Protocol bufferknight1128
 
JavaOne 2009 - TS-5276 - RESTful Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful  Protocol BuffersJavaOne 2009 - TS-5276 - RESTful  Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful Protocol BuffersMatt O'Keefe
 
Getting Started with Redis
Getting Started with RedisGetting Started with Redis
Getting Started with RedisFaisal Akber
 
Redis SoCraTes 2014
Redis SoCraTes 2014Redis SoCraTes 2014
Redis SoCraTes 2014steffenbauer
 
Speed up your Symfony2 application and build awesome features with Redis
Speed up your Symfony2 application and build awesome features with RedisSpeed up your Symfony2 application and build awesome features with Redis
Speed up your Symfony2 application and build awesome features with RedisRicard Clau
 
Redis Developers Day 2014 - Redis Labs Talks
Redis Developers Day 2014 - Redis Labs TalksRedis Developers Day 2014 - Redis Labs Talks
Redis Developers Day 2014 - Redis Labs TalksRedis Labs
 
Introduction to MicroServices (Oakjug)
Introduction to MicroServices (Oakjug)Introduction to MicroServices (Oakjug)
Introduction to MicroServices (Oakjug)Chris Richardson
 
REDIS intro and how to use redis
REDIS intro and how to use redisREDIS intro and how to use redis
REDIS intro and how to use redisKris Jeong
 
Think Small To Go Big - Introduction To Microservices
Think Small To Go Big - Introduction To MicroservicesThink Small To Go Big - Introduction To Microservices
Think Small To Go Big - Introduction To MicroservicesRyan Baxter
 
The 2014 Decision Makers Guide to Java Web Frameworks
The 2014 Decision Makers Guide to Java Web FrameworksThe 2014 Decision Makers Guide to Java Web Frameworks
The 2014 Decision Makers Guide to Java Web FrameworksKunal Ashar
 
Everything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to askEverything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to askCarlos Abalde
 

Viewers also liked (20)

RProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for RRProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for R
 
Melbourne Microservices Meetup: Agenda for a new Architecture
Melbourne Microservices Meetup: Agenda for a new ArchitectureMelbourne Microservices Meetup: Agenda for a new Architecture
Melbourne Microservices Meetup: Agenda for a new Architecture
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
Redis for the Everyday Developer
Redis for the Everyday DeveloperRedis for the Everyday Developer
Redis for the Everyday Developer
 
Mongodb, our Swiss Army Knife Database
Mongodb, our Swiss Army Knife DatabaseMongodb, our Swiss Army Knife Database
Mongodb, our Swiss Army Knife Database
 
Microservices At Gilt - NYC Microservices Meetup
Microservices At Gilt - NYC Microservices MeetupMicroservices At Gilt - NYC Microservices Meetup
Microservices At Gilt - NYC Microservices Meetup
 
About Microservices
About MicroservicesAbout Microservices
About Microservices
 
Building and deploying microservices with event sourcing, CQRS and Docker (Me...
Building and deploying microservices with event sourcing, CQRS and Docker (Me...Building and deploying microservices with event sourcing, CQRS and Docker (Me...
Building and deploying microservices with event sourcing, CQRS and Docker (Me...
 
Google Protocol buffer
Google Protocol bufferGoogle Protocol buffer
Google Protocol buffer
 
JavaOne 2009 - TS-5276 - RESTful Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful  Protocol BuffersJavaOne 2009 - TS-5276 - RESTful  Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful Protocol Buffers
 
Getting Started with Redis
Getting Started with RedisGetting Started with Redis
Getting Started with Redis
 
Redis SoCraTes 2014
Redis SoCraTes 2014Redis SoCraTes 2014
Redis SoCraTes 2014
 
Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to Microservices
 
Speed up your Symfony2 application and build awesome features with Redis
Speed up your Symfony2 application and build awesome features with RedisSpeed up your Symfony2 application and build awesome features with Redis
Speed up your Symfony2 application and build awesome features with Redis
 
Redis Developers Day 2014 - Redis Labs Talks
Redis Developers Day 2014 - Redis Labs TalksRedis Developers Day 2014 - Redis Labs Talks
Redis Developers Day 2014 - Redis Labs Talks
 
Introduction to MicroServices (Oakjug)
Introduction to MicroServices (Oakjug)Introduction to MicroServices (Oakjug)
Introduction to MicroServices (Oakjug)
 
REDIS intro and how to use redis
REDIS intro and how to use redisREDIS intro and how to use redis
REDIS intro and how to use redis
 
Think Small To Go Big - Introduction To Microservices
Think Small To Go Big - Introduction To MicroservicesThink Small To Go Big - Introduction To Microservices
Think Small To Go Big - Introduction To Microservices
 
The 2014 Decision Makers Guide to Java Web Frameworks
The 2014 Decision Makers Guide to Java Web FrameworksThe 2014 Decision Makers Guide to Java Web Frameworks
The 2014 Decision Makers Guide to Java Web Frameworks
 
Everything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to askEverything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to ask
 

More from Ciklum Ukraine

"How keep normal blood pressure using TDD" By Roman Loparev
"How keep normal blood pressure using TDD" By Roman Loparev"How keep normal blood pressure using TDD" By Roman Loparev
"How keep normal blood pressure using TDD" By Roman LoparevCiklum Ukraine
 
"Through the three circles of the it hell" by Roman Liashenko
"Through the three circles of the it hell" by Roman Liashenko"Through the three circles of the it hell" by Roman Liashenko
"Through the three circles of the it hell" by Roman LiashenkoCiklum Ukraine
 
Alex Pazhyn: Google_Material_Design
Alex Pazhyn: Google_Material_DesignAlex Pazhyn: Google_Material_Design
Alex Pazhyn: Google_Material_DesignCiklum Ukraine
 
Introduction to amazon web services for developers
Introduction to amazon web services for developersIntroduction to amazon web services for developers
Introduction to amazon web services for developersCiklum Ukraine
 
Your 1st Apple watch Application
Your 1st Apple watch ApplicationYour 1st Apple watch Application
Your 1st Apple watch ApplicationCiklum Ukraine
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentCiklum Ukraine
 
Back to the future: ux trends 2015
Back to the future: ux trends 2015Back to the future: ux trends 2015
Back to the future: ux trends 2015Ciklum Ukraine
 
Developing high load systems using C++
Developing high load systems using C++Developing high load systems using C++
Developing high load systems using C++Ciklum Ukraine
 
Collection view layout
Collection view layoutCollection view layout
Collection view layoutCiklum Ukraine
 
Introduction to auto layout
Introduction to auto layoutIntroduction to auto layout
Introduction to auto layoutCiklum Ukraine
 
Unit Testing: Special Cases
Unit Testing: Special CasesUnit Testing: Special Cases
Unit Testing: Special CasesCiklum Ukraine
 
Model-View-Controller: Tips&Tricks
Model-View-Controller: Tips&TricksModel-View-Controller: Tips&Tricks
Model-View-Controller: Tips&TricksCiklum Ukraine
 
Future of Outsourcing report published in The Times featuring Ciklum's CEO To...
Future of Outsourcing report published in The Times featuring Ciklum's CEO To...Future of Outsourcing report published in The Times featuring Ciklum's CEO To...
Future of Outsourcing report published in The Times featuring Ciklum's CEO To...Ciklum Ukraine
 
Михаил Попчук "Cкрытые резервы команд или 1+1=3"
Михаил Попчук "Cкрытые резервы команд или 1+1=3"Михаил Попчук "Cкрытые резервы команд или 1+1=3"
Михаил Попчук "Cкрытые резервы команд или 1+1=3"Ciklum Ukraine
 

More from Ciklum Ukraine (20)

"How keep normal blood pressure using TDD" By Roman Loparev
"How keep normal blood pressure using TDD" By Roman Loparev"How keep normal blood pressure using TDD" By Roman Loparev
"How keep normal blood pressure using TDD" By Roman Loparev
 
"Through the three circles of the it hell" by Roman Liashenko
"Through the three circles of the it hell" by Roman Liashenko"Through the three circles of the it hell" by Roman Liashenko
"Through the three circles of the it hell" by Roman Liashenko
 
Alex Pazhyn: Google_Material_Design
Alex Pazhyn: Google_Material_DesignAlex Pazhyn: Google_Material_Design
Alex Pazhyn: Google_Material_Design
 
Introduction to amazon web services for developers
Introduction to amazon web services for developersIntroduction to amazon web services for developers
Introduction to amazon web services for developers
 
Your 1st Apple watch Application
Your 1st Apple watch ApplicationYour 1st Apple watch Application
Your 1st Apple watch Application
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Back to the future: ux trends 2015
Back to the future: ux trends 2015Back to the future: ux trends 2015
Back to the future: ux trends 2015
 
Developing high load systems using C++
Developing high load systems using C++Developing high load systems using C++
Developing high load systems using C++
 
Collection view layout
Collection view layoutCollection view layout
Collection view layout
 
Introduction to auto layout
Introduction to auto layoutIntroduction to auto layout
Introduction to auto layout
 
Groovy on Android
Groovy on AndroidGroovy on Android
Groovy on Android
 
Unit Testing: Special Cases
Unit Testing: Special CasesUnit Testing: Special Cases
Unit Testing: Special Cases
 
Material design
Material designMaterial design
Material design
 
Kanban development
Kanban developmentKanban development
Kanban development
 
Mobile sketching
Mobile sketching Mobile sketching
Mobile sketching
 
More UX in our life
More UX in our lifeMore UX in our life
More UX in our life
 
Model-View-Controller: Tips&Tricks
Model-View-Controller: Tips&TricksModel-View-Controller: Tips&Tricks
Model-View-Controller: Tips&Tricks
 
Unit Tesing in iOS
Unit Tesing in iOSUnit Tesing in iOS
Unit Tesing in iOS
 
Future of Outsourcing report published in The Times featuring Ciklum's CEO To...
Future of Outsourcing report published in The Times featuring Ciklum's CEO To...Future of Outsourcing report published in The Times featuring Ciklum's CEO To...
Future of Outsourcing report published in The Times featuring Ciklum's CEO To...
 
Михаил Попчук "Cкрытые резервы команд или 1+1=3"
Михаил Попчук "Cкрытые резервы команд или 1+1=3"Михаил Попчук "Cкрытые резервы команд или 1+1=3"
Михаил Попчук "Cкрытые резервы команд или 1+1=3"
 

CiklumCPPSat24032012:SergeyBratus-GoogleProtocolBuffers

  • 1. GOOGLE PROTOCOL BUFFERS Плюсы и минусы использования в реальных проектах
  • 2. Как все это работает • Создайте .proto файл в котором будет информация о том что вы будете сериализовать • Сгенерируйте файлы с учетом нужного языка программирования используя утилиту protoc.exe • Добавьте код для вызова функций сериализации
  • 4. Other Languages • Action Script •C • C# • Haskell • Javascript • Objective C • Perl • PHP • Ruby • Scala • Visual Basic
  • 5. Proto файл message Person { required string name = 1; required int32 id = 2; optional string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phone = 4; }
  • 6. С чего начинается .proto Message message SearchRequest { optional int32 page_number = 1; }
  • 7. Message Field Types • double, float, int32, int64, uint32, uint64, sint32, sint64, fixed32, fixed64, sfixed32, sfixed64, bool, string, bytes • enum Smth { UNIVERSAL = 0; WEB = 1; IMAGES = 2; } • Other Messages
  • 8. Field Rules В сообщении могут быть поля следующих типов • required: 1 • optional: 0 или 1 • repeated: > 0
  • 9. Required Field required int32 foo = 1; • bool has_foo() const • int32 foo() const • void set_foo(int32 value) • void clear_foo()
  • 10. Optional Field optional Int32 foo = 1; • bool has_foo() const • int32 foo() const • void set_foo(int32 value) • void clear_foo() IsInitialized = true; foo вернет дефолтное значение
  • 11. String Field optional string foo = 1; required string foo = 1; • bool has_foo() const • const string& foo() const • void set_foo(const string& value) • void set_foo(const char* value) • void set_foo(const char* value, int size) • string* mutable_foo() • void clear_foo() • string* release_foo()
  • 12. Enum Fields enum Bar { BAR_VALUE = 1; } optional Bar foo = 1; required Bar foo = 1; • bool has_foo() const • Bar foo() const • void set_foo(Bar value) • void clear_foo()
  • 13. Message Fields message Bar {} optional Bar foo = 1; required Bar foo = 1; • bool has_foo() const • const Bar& foo() const • Bar* mutable_foo() • void clear_foo() • Bar* release_foo()
  • 14. Repeated Numeric Fields repeated int32 foo = 1; • int foo_size() const • int32 foo(int index) const • void set_foo(int index, int32 value) • void add_foo(int32 value) • void clear_foo() • const RepeatedField<int32>& foo() const • RepeatedField<int32>* mutable_foo()
  • 15. Repeated String Fields repeated string foo = 1; repeated bytes foo = 1; • int foo_size() const • const string& foo(int index) const • void set_foo(int index, const string& value) • void set_foo(int index, const char* value) • void set_foo(int index, const char* value, int size) • string* mutable_foo(int index) • void add_foo(const string& value) • void add_foo(const char* value) • void add_foo(const char* value, int size) • string* add_foo() • void clear_foo() • const RepeatedPtrField<string>& foo() const • RepeatedPtrField<string>* mutable_foo()
  • 16. Repeated Enum Fields enum Bar { BAR_VALUE = 1; } repeated Bar foo = 1; • int foo_size() const • Bar foo(int index) const • void set_foo(int index, Bar value) • void add_foo(Bar value) • void clear_foo() • const RepeatedField<int>& foo() const • RepeatedField<int>* mutable_foo()
  • 17. Repeated Embedded Message Fields message Bar {} repeated Bar foo = 1; • int int foo_size() const • const Bar& foo(int index) const • Bar* mutable_foo(int index) • Bar* add_foo() • void clear_foo() • const RepeatedPtrField<Bar>& foo() const • RepeatedPtrField<Bar>* mutable_foo()
  • 18. Enumerations enum Foo { VALUE_A = 1; VALUE_B = 5; } • const const EnumDescriptor* Foo_descriptor() • bool Foo_IsValid(int value)
  • 20. String String => std::string Bytes => std::string
  • 21. Convert std::wstring ConvertToWstring(const std::string& source) { if (source.empty()) return std::wstring(); return std::wstring((const wchar_t *)source.c_str(), source.size() / sizeof(wchar_t)); }
  • 22. Generate File protoc --cpp_out=. test.proto
  • 25. Pack Functions Bool SerializeToCodedStream(io::CodedOutputStream * output) const Bool SerializeToZeroCopyStream(io::ZeroCopyOutputStream * output) const Bool SerializeToString(string * output) const Bool SerializeToArray(void * data, int size) const String SerializeAsString() const Bool AppendToString(string * output) const
  • 26. Cookies • Поддержка extensions message Foo { // ... extensions 100 to 199; } extend Foo { optional int32 bar = 126; } • Packages package foo.bar; message Open { ... } • Options option optimize_for = CODE_SIZE; option optimize_for = SPEED;