All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.cloud.spanner.spi.v1.SpannerMetadataProvider Maven / Gradle / Ivy

There is a newer version: 6.81.1
Show newest version
/*
 * Copyright 2017 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.cloud.spanner.spi.v1;

import com.google.common.collect.ImmutableMap;
import io.grpc.Metadata;
import io.grpc.Metadata.Key;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/** For internal use only. */
class SpannerMetadataProvider {
  private final Map, String> headers;
  private final String resourceHeaderKey;
  private static final String ROUTE_TO_LEADER_HEADER_KEY = "x-goog-spanner-route-to-leader";
  private static final Pattern[] RESOURCE_TOKEN_PATTERNS = {
    Pattern.compile("^(?projects/[^/]*/instances/[^/]*/databases/[^/]*)(.*)?"),
    Pattern.compile("^(?projects/[^/]*/instances/[^/]*)(.*)?")
  };

  private static final Map> ROUTE_TO_LEADER_HEADER_MAP =
      ImmutableMap.of(ROUTE_TO_LEADER_HEADER_KEY, Collections.singletonList("true"));

  private SpannerMetadataProvider(Map headers, String resourceHeaderKey) {
    this.resourceHeaderKey = resourceHeaderKey;
    this.headers = constructHeadersAsMetadata(headers);
  }

  static SpannerMetadataProvider create(Map headers, String resourceHeaderKey) {
    return new SpannerMetadataProvider(headers, resourceHeaderKey);
  }

  Metadata newMetadata(String resourceTokenTemplate, String defaultResourceToken) {
    Metadata metadata = new Metadata();
    for (Map.Entry, String> header : headers.entrySet()) {
      metadata.put(header.getKey(), header.getValue());
    }

    metadata.put(
        Key.of(resourceHeaderKey, Metadata.ASCII_STRING_MARSHALLER),
        getResourceHeaderValue(resourceTokenTemplate, defaultResourceToken));

    return metadata;
  }

  Map> newExtraHeaders(
      String resourceTokenTemplate, String defaultResourceToken) {
    return ImmutableMap.>builder()
        .put(
            resourceHeaderKey,
            Collections.singletonList(
                getResourceHeaderValue(resourceTokenTemplate, defaultResourceToken)))
        .build();
  }

  Map> newRouteToLeaderHeader() {
    return ROUTE_TO_LEADER_HEADER_MAP;
  }

  private Map, String> constructHeadersAsMetadata(
      Map headers) {
    ImmutableMap.Builder, String> headersAsMetadataBuilder =
        ImmutableMap.builder();
    for (Map.Entry entry : headers.entrySet()) {
      headersAsMetadataBuilder.put(
          Metadata.Key.of(entry.getKey(), Metadata.ASCII_STRING_MARSHALLER), entry.getValue());
    }
    return headersAsMetadataBuilder.build();
  }

  private String getResourceHeaderValue(String resourceTokenTemplate, String defaultResourceToken) {
    String resourceToken = defaultResourceToken;
    if (resourceTokenTemplate != null) {
      for (Pattern pattern : RESOURCE_TOKEN_PATTERNS) {
        Matcher m = pattern.matcher(resourceTokenTemplate);
        if (m.matches()) {
          resourceToken = m.group("headerValue");
          break;
        }
      }
    }

    return resourceToken;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy