package com.tzld.piaoquan.recommend.server.service.score4recall.model4recall;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.tuple.Pair;

public class Model4RecallKeyValue extends AbstractModel {
    private static final Logger LOGGER = LoggerFactory.getLogger(Model4RecallKeyValue.class);
    public Map<String, List<Pair<Long, Double>>> kv;
    public Model4RecallKeyValue() {
        //配置不同环境的hdfs conf
        this.kv = new HashMap<>();
    }


    @Override
    public boolean loadFromStream(InputStreamReader in) throws IOException {
        BufferedReader input = new BufferedReader(in);
        String line = null;
        while ((line = input.readLine()) != null) {
            String[] items = line.split("\t");
            if (items.length < 3) {
                continue;
            }
            String key = items[0].trim();
            String [] l1 = items[1].trim().split(",");
            String [] l2 = items[2].trim().split(",");
            if (l1.length != l2.length){
                continue;
            }
            List<Pair<Long, Double>> lists = new ArrayList<>(l1.length);
            for (int i=0; i<l1.length; ++i){
                String id = l1[i];
                String score = l2[i];
                try{
                    Pair<Long, Double> pair = Pair.of(Long.valueOf(id), Double.valueOf(score));
                    lists.add(pair);
                }catch (Exception e){
                    LOGGER.error(String.format("something is wrong with parse pair %s %s: ", id, score), e);
                }
            }
            this.kv.put(key, lists);
        }
        input.close();
        in.close();
        return true;
    }


}
