<commit_before>/*
 * Copyright 2017 Spotify AB.
 *
 * 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.spotify.scio.avro.types

import java.util.Collections.{emptyList, emptyMap}

import com.google.protobuf.ByteString
import com.spotify.scio.avro.types.MacroUtil._
import org.apache.avro.{JsonProperties, Schema}
import org.apache.avro.Schema.Field

import scala.collection.JavaConverters._
import scala.reflect.runtime.universe._

private[types] object SchemaProvider {

  def schemaOf[T: TypeTag]: Schema = {
    val tpe = typeOf[T]

    if (!isCaseClass(tpe.erasure)) {
      throw new RuntimeException(s"Unsupported type $tpe.erasure")
    }

    toSchema(tpe)._1
  }<commit_msg>fix #1025, cache schema in Avro SchemaProvider (#1359)<commit_after>/*
 * Copyright 2017 Spotify AB.
 *
 * 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.spotify.scio.avro.types

import java.util.Collections.{emptyList, emptyMap}
import java.util.concurrent.ConcurrentHashMap
import java.util.function

import com.google.protobuf.ByteString
import com.spotify.scio.avro.types.MacroUtil._
import org.apache.avro.{JsonProperties, Schema}
import org.apache.avro.Schema.Field

import scala.collection.JavaConverters._
import scala.reflect.runtime.universe
import scala.reflect.runtime.universe._

private[types] object SchemaProvider {

  private[this] val m: ConcurrentHashMap[Type, Schema] = new ConcurrentHashMap[Type, Schema]()

  def schemaOf[T: TypeTag]: Schema = {
    val tpe = typeOf[T]

    if (!isCaseClass(tpe.erasure)) {
      throw new RuntimeException(s"Unsupported type $tpe.erasure")
    }

    m.computeIfAbsent(tpe, new function.Function[Type, Schema] {
      override def apply(t: universe.Type): Schema = toSchema(tpe)._1
    })
  }
{'fraction_non_alphanumeric': 0.09525521207764198, 'fraction_numerical': 0.009345794392523364, 'mean_word_length': 4.125230202578269, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4825764', 'n_tokens_mistral': 834, 'n_tokens_neox': 814, 'n_words': 268}