<commit_before>package won.node.service.persistence;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import won.node.service.linkeddata.lookup.SocketLookup;
import won.protocol.exception.NoAtomForSocketFoundException;
import won.protocol.exception.NoDefaultSocketException;
import won.protocol.exception.NoSuchAtomException;
import won.protocol.exception.NoSuchSocketException;
import won.protocol.model.Socket;
import won.protocol.model.SocketDefinition;
import won.protocol.repository.AtomRepository;
import won.protocol.repository.SocketRepository;

/**
 * Service that is informed of a state change of a connection and performs data
 * derivation work, changing the data of the atom owning the connection.
 */
@Component
public class SocketService {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    AtomRepository atomRepository;
    @Autowired
    SocketRepository socketRepository;
    Map<URI, SocketDefinition> knownSockets = new HashMap<>();
    @Autowired
    SocketLookup socketLookup;

    public SocketService() {
    }

    public Optional<Socket> getDefaultSocket(URI atomUri) throws NoSuchAtomException {
        List<Socket> sockets = socketRepository.findByAtomURI(atomUri);
        for (Socket socket : sockets) {
            if (socket.isDefaultSocket())
                return Optional.of(socket);
        }
        return sockets.stream().findFirst();
    }

    public Socket getSocket(URI atomUri, Optional<URI> socketUri) throws IllegalArgumentException, NoSuchAtomException {
        if (socketUri.isPresent()) {
            return socketRepository.findOneBySocketURI(socketUri.get())
                            .orElseThrow(() -> new NoSuchSocketException(socketUri.get()));
        }
        return getDefaultSocket(atomUri)
                        .orElseThrow(() -> new NoDefaultSocketException(atomUri));
    }

    public Optional<URI> getAtomOfSocket(URI socketURI) {
        if (socketURI == null) {
            return Optional.empty();
        }
        String uri = socketURI.toString();
        String fragment = socketURI.getRawFragment();
        if (fragment == null) {
            return Optional.empty();
        }
        return Optional.of(URI.create(uri.substring(0, uri.length() - fragment.length() - 1)));
    }
<commit_msg>Remove obsolete default socket handling<commit_after>package won.node.service.persistence;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import won.node.service.linkeddata.lookup.SocketLookup;
import won.protocol.exception.NoAtomForSocketFoundException;
import won.protocol.exception.NoSuchAtomException;
import won.protocol.exception.NoSuchSocketException;
import won.protocol.model.Socket;
import won.protocol.model.SocketDefinition;
import won.protocol.repository.AtomRepository;
import won.protocol.repository.SocketRepository;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * Service that is informed of a state change of a connection and performs data
 * derivation work, changing the data of the atom owning the connection.
 */
@Component
public class SocketService {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    AtomRepository atomRepository;
    @Autowired
    SocketRepository socketRepository;
    Map<URI, SocketDefinition> knownSockets = new HashMap<>();
    @Autowired
    SocketLookup socketLookup;

    public SocketService() {
    }

    public Socket getSocket(URI atomUri, Optional<URI> socketUri) throws IllegalArgumentException, NoSuchAtomException {
        if (socketUri.isPresent()) {
            return socketRepository.findOneBySocketURI(socketUri.get())
                            .orElseThrow(() -> new NoSuchSocketException(socketUri.get()));
        }
        return null;
    }

    public Optional<URI> getAtomOfSocket(URI socketURI) {
        if (socketURI == null) {
            return Optional.empty();
        }
        String uri = socketURI.toString();
        String fragment = socketURI.getRawFragment();
        if (fragment == null) {
            return Optional.empty();
        }
        return Optional.of(URI.create(uri.substring(0, uri.length() - fragment.length() - 1)));
    }

{'fraction_non_alphanumeric': 0.09694323144104804, 'fraction_numerical': 0.0017467248908296944, 'mean_word_length': 3.562749003984064, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, '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': '7154164', 'n_tokens_mistral': 1210, 'n_tokens_neox': 1205, 'n_words': 280}