Custom decoder

By default, OCMAM uses the protopack library to decode the requested layer data.

To decode layer data using a custom decoder, use the schema_decoder field of the DataStoreServerSettings structure.

In this section, learn how to:

Use a custom decoder for a specific layer

To decode a specific layer using a custom decoder:

  1. Create the custom decoder for the layer.

    Example:

     struct RoadAttributeLayerDecoder
     {
         bool
         operator( )( const void* data,
                      const int size,
                      clientmap::decoder::LayerConfigurationPtr configuration,
                      clientmap::decoder::ClientMapTile& tile ) const
         {
             // Set the layer configuration.
             tile.road_attribute_layer_configuration
                 = std::move( configuration );
    
             // The layer data may not be present (for example, if the users
             // request road attributes for a tile that does not contain any
             // roads). In this case, do not set the layer data and return.
             if ( data == nullptr || size <= 0 )
             {
                 return true;
             }
    
             // Decode layer data.
             if ( !tile.arena )
             {
                 tile.arena = std::make_shared<
                     clientmap::decoder::ClientMapTile::ArenaType >( );
             }
    
             auto* const road_attribute_layer = google::protobuf::Arena::Create<
                 clientmap::decoder::RoadAttributeLayer >( tile.arena.get( ) );
    
             // Fill the `road_attribute_layer` instance with data.
             ...
    
             tile.road_attribute_layer = clientmap::decoder::LayerPtr<
                 const clientmap::decoder::RoadAttributeLayer >(
                 road_attribute_layer, tile.arena );
             return true;
         }
     };
    

    Note: operator( ) must be of the clientmap::decoder::AbstractSchemaDecoder::LayerDecoderType type.

  2. Create a DefaultSchemaDecoder instance and configure it to use the custom decoder for the specific layer.

    Note: The layers for which custom decoders are not set are decoded using the protopack library by default.

    Example:

     auto decoder
         = std::make_shared< clientmap::decoder::DefaultSchemaDecoder >( );
    
     decoder->SetDecoder( clientmap::rendering::kRoadAttributeLayerName,
                          RoadAttributeLayerDecoder( ) );
    
  3. Create a DataStoreServer instance that uses the DefaultSchemaDecoder instance.

    Example:

     olp::clientmap::datastore::DataStoreServerSettings server_settings;
     server_settings.schema_decoder = decoder;
     ...
    
     auto server
         = std::make_shared< olp::clientmap::datastore::DataStoreServer >(
             server_settings );
    

Use a custom decoder for all layers

To decode all layers using a custom decoder:

  1. Create the custom decoder for all layers.

    Example:

     struct CustomLayerDecoder
     {
         bool
         operator( )( const void* data,
                      const int size,
                      clientmap::decoder::LayerConfigurationPtr configuration,
                      clientmap::decoder::ClientMapTile& tile ) const
         {
             ...
         }
     };
    

    Notes:

    • operator( ) must be of the clientmap::decoder::AbstractSchemaDecoder::LayerDecoderType type.
    • For each layer, operator( ) must set the correct layer data and layer configuration fields of the tile instance.
  2. Create a DefaultSchemaDecoder instance and configure it to use the custom decoder for all layers.

    Example:

     auto decoder
         = std::make_shared< clientmap::decoder::DefaultSchemaDecoder >( );
    
     decoder->SetDecoder( CustomLayerDecoder( ) );
    
  3. Create a DataStoreServer instance that uses the DefaultSchemaDecoder instance.

    Example:

     olp::clientmap::datastore::DataStoreServerSettings server_settings;
     server_settings.schema_decoder = decoder;
     ...
    
     auto server
         = std::make_shared< olp::clientmap::datastore::DataStoreServer >(
             server_settings );
    

Use a custom schema

To decode all layers using a custom schema:

  1. Inherit the custom schema decoder from the clientmap::decoder::AbstractSchemaDecoder abstract class.

    Example:

     class CustomSchemaDecoder : public AbstractSchemaDecoder
     {
     public:
         ...
     };
    
  2. Create a DataStoreServer instance that uses the custom schema decoder instance.

    Example:

     olp::clientmap::datastore::DataStoreServerSettings server_settings;
     server_settings.schema_decoder
         = std::make_shared< CustomSchemaDecoder >( );
     ...
    
     auto server
         = std::make_shared< olp::clientmap::datastore::DataStoreServer >(
             server_settings );
    

results matching ""

    No results matching ""