[ 
https://issues.apache.org/jira/browse/BEAM-4453?focusedWorklogId=171240&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-171240
 ]

ASF GitHub Bot logged work on BEAM-4453:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 30/Nov/18 23:42
            Start Date: 30/Nov/18 23:42
    Worklog Time Spent: 10m 
      Work Description: apilloud commented on a change in pull request #7147: 
[BEAM-4453] Use constructors to generate schema POJOs
URL: https://github.com/apache/beam/pull/7147#discussion_r238030223
 
 

 ##########
 File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/CachingFactory.java
 ##########
 @@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.beam.sdk.schemas;
+
+import java.util.concurrent.ConcurrentHashMap;
+import javax.annotation.Nullable;
+
+/**
+ * A wrapper around a {@link Factory} that assumes the schema parameter never 
changes.
+ *
+ * <p>{@link Factory} objects take the schema as a parameter, as often the 
returned type varies by
+ * schema (e.g. sometimes the returned-type is a list that must be in 
schema-field order). However
+ * in many cases it's known by the caller that the schema parameter is always 
the same across all
+ * calls to create. In these cases we want to save the cost of Schema 
comparison (which can be
+ * significant for larger schemas) on each lookup. This wrapper caches the 
value returned by the
+ * inner factory, so the schema comparison only need happen on the first 
lookup.
+ */
+class CachingFactory<CreatedT> implements Factory<CreatedT> {
+  @Nullable private transient ConcurrentHashMap<Class, CreatedT> cache = null;
+
+  private final Factory<CreatedT> innerFactory;
+
+  public CachingFactory(Factory<CreatedT> innerFactory) {
+    this.innerFactory = innerFactory;
+  }
+
+  @Override
+  public CreatedT create(Class<?> clazz, Schema schema) {
+    if (cache == null) {
 
 Review comment:
   This check is not thread safe. You should create the ConcurrentHashMap in 
the constructor.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 171240)
    Time Spent: 11h 10m  (was: 11h)

> Provide automatic schema registration for POJOs
> -----------------------------------------------
>
>                 Key: BEAM-4453
>                 URL: https://issues.apache.org/jira/browse/BEAM-4453
>             Project: Beam
>          Issue Type: Sub-task
>          Components: sdk-java-core
>            Reporter: Reuven Lax
>            Assignee: Reuven Lax
>            Priority: Major
>             Fix For: 2.6.0
>
>          Time Spent: 11h 10m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to