package com.yahoo.searchdefinition;

import com.yahoo.config.application.api.FileRegistry;
import com.yahoo.vespa.model.AbstractService;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class LargeRankExpressions {
    private final Map<String, RankExpressionBody> expressions = new HashMap<>();
    private final FileRegistry fileRegistry;

    public LargeRankExpressions(FileRegistry fileRegistry) {
        this.fileRegistry = fileRegistry;
    }

    public void add(RankExpressionBody expression) {
        expression.validate();
        expression.register(fileRegistry);
        String name = expression.getName();
        if (expressions.containsKey(name)) {
            if ( ! expressions.get(name).getBlob().equals(expression.getBlob())) {
                throw new IllegalArgumentException("Rank expression '" + name +
                        "' defined twice. Previous blob with " + expressions.get(name).getBlob().remaining() +
                        " bytes, while current has " + expression.getBlob().remaining() + " bytes");
            }
        } else {
            expressions.put(name, expression);
        }
    }

    /** Returns the ranking constant with the given name, or null if not present */
    public RankExpressionBody get(String name) {
        return expressions.get(name);
    }

    /** Returns a read-only map of the ranking constants in this indexed by name */
    public Map<String, RankExpressionBody> asMap() {
        return Collections.unmodifiableMap(expressions);
    }

    /** Initiate sending of these constants to some services over file distribution */
    public void sendTo(Collection<? extends AbstractService> services) {
        expressions.values().forEach(constant -> constant.sendTo(services));
    }
}

@@ -207,20 +207,16 @@
 va.util.
-Hash
 Map;%0Aimp
@@ -225,24 +225,49 @@
 t java.util.
+concurrent.ConcurrentHash
 Map;%0A%0Apublic
@@ -364,16 +364,26 @@
 s = new 
+Concurrent
 HashMap%3C
@@ -595,32 +595,46 @@
 ssion) %7B%0A       
+ String name =
  expression.vali
@@ -633,15 +633,14 @@
 ion.
-validat
+getNam
 e();
@@ -640,33 +640,37 @@
 Name();%0A        
-e
+RankE
 xpression.regist
@@ -666,54 +666,102 @@
 sion
-.register(fileRegistry);%0A        String name =
+Body prev = expressions.putIfAbsent(name, expression);%0A        if (prev == null) %7B%0A           
  exp
@@ -760,38 +760,39 @@
      expression.
-getNam
+validat
 e();%0A        if 
@@ -788,20 +788,20 @@
         
+ 
-if (
+   
 expressi
@@ -806,28 +806,47 @@
 sion
-s.containsKey(name))
+.register(fileRegistry);%0A        %7D else
  %7B%0A 
@@ -863,37 +863,20 @@
  if ( ! 
-ex
 pre
-ssions.get(name)
+v
 .getBlob
@@ -1055,37 +1055,20 @@
 ith %22 + 
-ex
 pre
-ssions.get(name)
+v
 .getBlob
@@ -1212,250 +1212,8 @@
    %7D
- else %7B%0A            expressions.put(name, expression);%0A        %7D%0A    %7D%0A%0A    /** Returns the ranking constant with the given name, or null if not present */%0A    public RankExpressionBody get(String name) %7B%0A        return expressions.get(name);
 %0A   

Make LargeRankExpressions thread safe.