<commit_before>package ro.infoiasi.fiiadmis.db.dao;

import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import org.apache.commons.lang.RandomStringUtils;
import ro.infoiasi.fiiadmis.db.Table;
import ro.infoiasi.fiiadmis.model.Entity;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.List;
import java.util.Scanner;

public class EntityDAOImpl<E extends Entity> implements EntityDAO<E> {

    private final Table<E> table;
    private final Object toSync = new Object();


    public EntityDAOImpl(Table<E> table) {
        this.table = table;
    }

    private E getSingleItem(Predicate<E> filter) throws IOException {
        try (BufferedReader reader = Files.newBufferedReader(table.getTablePath(), Charset.defaultCharset())) {
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (line.isEmpty())
                    continue; // skipping empty lines

                E currentItem = table.getFormatter().read(line);
                if (filter.apply(currentItem)) {
                    return currentItem;
                }
            }

            return null; // no items with this id was found
        }
    }

    private List<E> getMultipleItems(Predicate<E> filter) throws IOException {
        try (BufferedReader reader = Files.newBufferedReader(table.getTablePath(), Charset.defaultCharset())) {
            String line = null;
            final List<E> results = Lists.newArrayList();
            while ((line = reader.readLine()) != null) {

                if (line.isEmpty())
                    continue; // skipping empty lines
                E currentItem = table.getFormatter().read(line);
                if (filter.apply(currentItem)) {
                    results.add(currentItem);
                }
            }

            return results;
        }
    }

    @Override
    public String addItem(E item) throws IOException {

        String newId = RandomStringUtils.randomAlphanumeric(4);
        while (getItemById(newId) != null) {
            newId = RandomStringUtils.randomAlphanumeric(4);   // ensuring that the id is unique
        }
        synchronized (toSync) {
            item.setId(newId);
            try (BufferedWriter writer = Files.newBufferedWriter(table.getTablePath(), Charset.defaultCharset(), StandardOpenOption.APPEND)) {
                writer.write(table.getFormatter().write(item));
                writer.write(System.lineSeparator());
            }
            return newId;
        }
    }

    @Override
    public E getItemById(final String entityId) throws IOException {<commit_msg>a bit of synchronization and validation by id<commit_after>package ro.infoiasi.fiiadmis.db.dao;

import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import org.apache.commons.lang.RandomStringUtils;
import ro.infoiasi.fiiadmis.db.Table;
import ro.infoiasi.fiiadmis.model.Entity;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.List;
import java.util.Scanner;

public class EntityDAOImpl<E extends Entity> implements EntityDAO<E> {

    private final Table<E> table;
    private final Object toSync = new Object();


    public EntityDAOImpl(Table<E> table) {
        this.table = table;
    }

    private E getSingleItem(Predicate<E> filter) throws IOException {
        try (BufferedReader reader = Files.newBufferedReader(table.getTablePath(), Charset.defaultCharset())) {
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (line.isEmpty())
                    continue; // skipping empty lines

                E currentItem = table.getFormatter().read(line);
                if (filter.apply(currentItem)) {
                    return currentItem;
                }
            }

            return null; // no items with this id was found
        }
    }

    private List<E> getMultipleItems(Predicate<E> filter) throws IOException {
        try (BufferedReader reader = Files.newBufferedReader(table.getTablePath(), Charset.defaultCharset())) {
            String line = null;
            final List<E> results = Lists.newArrayList();
            while ((line = reader.readLine()) != null) {

                if (line.isEmpty())
                    continue; // skipping empty lines
                E currentItem = table.getFormatter().read(line);
                if (filter.apply(currentItem)) {
                    results.add(currentItem);
                }
            }

            return results;
        }
    }

    @Override
    public String addItem(E item) throws IOException {

        synchronized (toSync) {
            String newId = RandomStringUtils.randomAlphanumeric(4);
            while (getItemById(newId) != null) {
                newId = RandomStringUtils.randomAlphanumeric(4);   // ensuring that the id is unique
            }

            item.setId(newId);
            try (BufferedWriter writer = Files.newBufferedWriter(table.getTablePath(), Charset.defaultCharset(), StandardOpenOption.APPEND)) {
                writer.write(table.getFormatter().write(item));
                writer.write(System.lineSeparator());
            }
            return newId;
        }
    }

    @Override
    public E getItemById(final String entityId) throws IOException {
{'fraction_non_alphanumeric': 0.1025004433410179, 'fraction_numerical': 0.0007093456286575634, 'mean_word_length': 2.2469775474956823, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '1569217', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1499, 'n_words': 343}