// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.derived;

import com.yahoo.config.application.api.FileRegistry;
import com.yahoo.searchdefinition.DistributableResource;
import com.yahoo.searchdefinition.RankProfile;
import com.yahoo.tensor.TensorType;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * Constant values for ranking/model execution tied to a rank profile,
 * to be distributed as files.
 *
 * @author bratseth
 */
public class FileDistributedConstants {

    private final FileRegistry fileRegistry;

    private final Map<String, DistributableConstant> constants = new LinkedHashMap<>();

    public FileDistributedConstants(FileRegistry fileRegistry, Collection<RankProfile.Constant> constants) {
        this.fileRegistry = fileRegistry;
        for (var constant : constants) {
            if (constant.valuePath().isPresent())
                add(new DistributableConstant(constant.name().simpleArgument().get(),
                                              constant.type(),
                                              constant.valuePath().get(),
                                              constant.pathType().get()));
        }
    }

    public void add(DistributableConstant constant) {
        constant.validate();
        constant.register(fileRegistry);
        String name = constant.getName();
        DistributableConstant prev = constants.putIfAbsent(name, constant);
        if ( prev != null )
            throw new IllegalArgumentException("Constant '" + name + "' defined twice");
    }

    public void putIfAbsent(DistributableConstant constant) {
        constant.validate();
        constant.register(fileRegistry);
        String name = constant.getName();
        constants.putIfAbsent(name, constant);
    }

    public void computeIfAbsent(String name, Function<? super String, ? extends DistributableConstant> createConstant) {
        constants.computeIfAbsent(name, key -> {
            DistributableConstant constant = createConstant.apply(key);
            constant.validate();
            constant.register(fileRegistry);
            return constant;
        });
    }

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

    public static class DistributableConstant extends DistributableResource {

        private TensorType tensorType;

        public DistributableConstant(String name, TensorType type, String fileName) {
            this(name, type, fileName, PathType.FILE);
        }

        public DistributableConstant(String name, TensorType type, String fileName, PathType pathType) {
            super(name, fileName, pathType);
            this.tensorType = type;
            validate();
        }

        public void setType(TensorType type) {
            this.tensorType = type;
        }

        public TensorType getTensorType() { return tensorType; }
        public String getType() { return tensorType.toString(); }

        public void validate() {
            super.validate();
            if (tensorType == null)
                throw new IllegalArgumentException("Ranking constant '" + getName() + "' must have a type.");
            if (tensorType.dimensions().stream().anyMatch(d -> d.isIndexed() && d.size().isEmpty()))
                throw new IllegalArgumentException("Illegal type in field " + getName() + " type " + tensorType +
                                                   ": Dense tensor dimensions must have a size");
        }

        @Override
        public String toString() {
            return super.toString() + "' of type '" + tensorType + "'";
        }

    }

}


@@ -666,54 +666,8 @@
  %7B%0A%0A
-    private final FileRegistry fileRegistry;%0A%0A
     
@@ -728,32 +728,8 @@
 ants
- = new LinkedHashMap%3C%3E()
 ;%0A%0A 
@@ -848,40 +848,89 @@
     
-this.fileRegistry = fileRegistry
+Map%3CString, DistributableConstant%3E distributableConstants = new LinkedHashMap%3C%3E()
 ;%0A  
@@ -984,16 +984,19 @@
     if (
+ ! 
 constant
@@ -1020,16 +1020,27 @@
 esent())
+ continue;%0A
 %0A       
@@ -1048,16 +1048,36 @@
     
-    add(
+var distributableConstant = 
 new 
@@ -1176,32 +1176,52 @@
                 
+      
+              
 constant.type(),
@@ -1217,24 +1217,44 @@
 ant.type(),%0A
+                    
             
@@ -1353,32 +1353,52 @@
                 
+                    
 constant.pathTyp
@@ -1407,17 +1407,16 @@
 ).get())
-)
 ;%0A      
@@ -1421,80 +1421,26 @@
     
-%7D%0A
     
-%7D%0A%0A    public void add(DistributableConstant constant) %7B%0A        c
+distributableC
 onst
@@ -1455,33 +1455,50 @@
 date();%0A        
-c
+    distributableC
 onstant.register
@@ -1525,828 +1525,181 @@
     
-String name = constant.getName();%0A        DistributableConstant prev = constants.putIfAbsent(name, constant);%0A        if ( prev != null )%0A            throw new IllegalArgumentException(%22Constant '%22 + name + %22' defined twice%22);%0A    %7D%0A%0A    public void putIfAbsent(DistributableConstant constant) %7B%0A        constant.validate();%0A        constant.register(fileRegistry);%0A        String name = constant.getName();%0A        constants.putIfAbsent(name, constant);%0A    %7D%0A%0A    public void computeIfAbsent(String name, Function%3C? super String, ? extends DistributableConstant%3E createConstant) %7B%0A        constants.computeIfAbsent(name, key -%3E %7B%0A            DistributableConstant constant = createConstant.apply(key);%0A            constant.validate();%0A            constant.register(fileRegistry);%0A            return constant;%0A        %7D
+    distributableConstants.put(distributableConstant.getName(), distributableConstant);%0A        %7D%0A        this.constants = Collections.unmodifiableMap(distributableConstants
 );%0A 
@@ -1840,52 +1840,16 @@
 () %7B
-%0A        return Collections.unmodifiableMap(
+ return 
 cons
@@ -1845,38 +1845,33 @@
 return constants
-);%0A   
+;
  %7D%0A%0A    public s
@@ -1953,16 +1953,22 @@
 private 
+final 
 TensorTy
@@ -2360,102 +2360,8 @@
  %7D%0A%0A
-        public void setType(TensorType type) %7B%0A            this.tensorType = type;%0A        %7D%0A%0A
     

Make immutable