<commit_before>package io.cattle.iaas.labels.service.impl;

import static io.cattle.platform.core.model.Tables.*;

import io.cattle.iaas.labels.service.LabelsService;
import io.cattle.platform.core.constants.CommonStatesConstants;
import io.cattle.platform.core.constants.LabelConstants;
import io.cattle.platform.core.dao.GenericResourceDao;
import io.cattle.platform.core.model.HostLabelMap;
import io.cattle.platform.core.model.InstanceLabelMap;
import io.cattle.platform.core.model.Label;
import io.cattle.platform.object.ObjectManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

public class LabelsServiceImpl implements LabelsService {

    @Inject
    GenericResourceDao resourceDao;

    @Inject
    ObjectManager objectManager;

    @Override
    public Label getOrCreateLabel(long accountId, String key, String value,
            String type) {
        // best effort for not duplicating
        List<Label> labels = objectManager.find(Label.class,
                LABEL.KEY, key,
                LABEL.VALUE, value,
                LABEL.ACCOUNT_ID, accountId,
                LABEL.REMOVED, null);

        // DB query ignores leading/trailing whitesapce, so we need to check that keys and values are truly equal
        Label label = null;
        for (Label l : labels) {
            if (StringUtils.equals(key, l.getKey()) && StringUtils.equals(value, l.getValue())) {
                label = l;
                break;
            }
        }

        if (label == null) {
            Map<Object, Object> labelData = new HashMap<>();
            labelData.put(LABEL.STATE, CommonStatesConstants.CREATED);
            labelData.put(LABEL.KEY, truncate(StringUtils.trim(key), 1024));
            labelData.put(LABEL.VALUE, truncate(StringUtils.trim(value), 4096));
            labelData.put(LABEL.ACCOUNT_ID, accountId);

            labelData.put(LABEL.TYPE, type);<commit_msg>Trim label key and value during comparison<commit_after>package io.cattle.iaas.labels.service.impl;

import static io.cattle.platform.core.model.Tables.*;

import io.cattle.iaas.labels.service.LabelsService;
import io.cattle.platform.core.constants.CommonStatesConstants;
import io.cattle.platform.core.constants.LabelConstants;
import io.cattle.platform.core.dao.GenericResourceDao;
import io.cattle.platform.core.model.HostLabelMap;
import io.cattle.platform.core.model.InstanceLabelMap;
import io.cattle.platform.core.model.Label;
import io.cattle.platform.object.ObjectManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

public class LabelsServiceImpl implements LabelsService {

    @Inject
    GenericResourceDao resourceDao;

    @Inject
    ObjectManager objectManager;

    @Override
    public Label getOrCreateLabel(long accountId, String key, String value,
            String type) {
        // best effort for not duplicating
        List<Label> labels = objectManager.find(Label.class,
                LABEL.KEY, key,
                LABEL.VALUE, value,
                LABEL.ACCOUNT_ID, accountId,
                LABEL.REMOVED, null);

        // DB query ignores leading/trailing whitesapce, so we need to check that keys and values are truly equal
        Label label = null;
        for (Label l : labels) {
            if (StringUtils.equals(StringUtils.trim(key), l.getKey()) && StringUtils.equals(StringUtils.trim(value), l.getValue())) {
                label = l;
                break;
            }
        }

        if (label == null) {
            Map<Object, Object> labelData = new HashMap<>();
            labelData.put(LABEL.STATE, CommonStatesConstants.CREATED);
            labelData.put(LABEL.KEY, truncate(StringUtils.trim(key), 1024));
            labelData.put(LABEL.VALUE, truncate(StringUtils.trim(value), 4096));
            labelData.put(LABEL.ACCOUNT_ID, accountId);

            labelData.put(LABEL.TYPE, type);
{'fraction_non_alphanumeric': 0.10303179689425684, 'fraction_numerical': 0.004436775942814888, 'mean_word_length': 2.8537511870845202, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7594261', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1164, 'n_words': 247}