<commit_before>package com.fizzbuzz.vroom.core.service.datastore;

/*
 * Copyright (c) 2014 Fizz Buzz LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.fizzbuzz.vroom.core.domain.DomainCollection;
import com.fizzbuzz.vroom.core.domain.IEntityObject;
import com.googlecode.objectify.Key;

import java.util.ArrayList;
import java.util.List;

import static com.fizzbuzz.vroom.core.service.datastore.OfyManager.ofy;

/**
 * Base class for entity collections.
 */
public abstract class EntityCollection<
        EO extends IEntityObject,
        DAO extends VroomDao<EO>,
        E extends VroomEntity<EO, DAO>>
        implements IEntityCollection<EO> {

    private final E mElementEntity;

    protected EntityCollection(final E elementEntity) {
        mElementEntity = elementEntity;
    }

    @Override
    public DomainCollection<EO> getElements() {
        List<DAO> daos = ofy().load().type(mElementEntity.getDaoClass()).list();
        return toDomainCollection(daos);
    }

    @Override
    public void addElement(final EO entityObject) {
        mElementEntity.create(entityObject);
    }

    @Override
    public void deleteAll() {
        Iterable<Key<DAO>> keys = ofy().load().type(getElementDaoClass()).keys();
        deleteEntitiesByKey(keys);
    }

    @Override
    public void delete(List<EO> entityObjectCollection) {
        List<Key<DAO>> keys = new ArrayList<>();
        for (EO entityObjectObject : entityObjectCollection) {
            keys.add(Key.create(getElementDaoClass(), entityObjectObject.getKey().toString()));
        }
        deleteEntitiesByKey(keys);
    }

    protected DomainCollection<EO> toDomainCollection(List<DAO> daoCollection) {
        DomainCollection<EO> domainCollection = new DomainCollection<>();
        for (DAO dao : daoCollection) {
            domainCollection.add(dao.toDomainObject());
        }
        return domainCollection;
    }

    protected Class<DAO> getElementDaoClass() {
        return mElementEntity.getDaoClass();
    }

    private void deleteEntitiesByKey(Iterable<Key<DAO>> keys) {
        ofy().delete().keys(keys);
    }

    public E getElementEntity() {
        return mElementEntity;
    }<commit_msg>Rewrite function to fully migrate data<commit_after>package com.fizzbuzz.vroom.core.service.datastore;

/*
 * Copyright (c) 2014 Fizz Buzz LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.fizzbuzz.vroom.core.domain.DomainCollection;
import com.fizzbuzz.vroom.core.domain.IEntityObject;
import com.googlecode.objectify.Key;

import java.util.ArrayList;
import java.util.List;

import static com.fizzbuzz.vroom.core.service.datastore.OfyManager.ofy;

/**
 * Base class for entity collections.
 */
public abstract class EntityCollection<
        EO extends IEntityObject,
        DAO extends VroomDao<EO>,
        E extends VroomEntity<EO, DAO>>
        implements IEntityCollection<EO> {

    private final E mElementEntity;

    protected EntityCollection(final E elementEntity) {
        mElementEntity = elementEntity;
    }

    @Override
    public DomainCollection<EO> getElements() {
        List<DAO> daos = ofy().load().type(mElementEntity.getDaoClass()).list();
        return toDomainCollection(daos);
    }

    @Override
    public void addElement(final EO entityObject) {
        mElementEntity.create(entityObject);
    }

    @Override
    public void deleteAll() {
        Iterable<Key<DAO>> keys = ofy().load().type(getElementDaoClass()).keys();
        deleteEntitiesByKey(keys);
    }

    @Override
    public void delete(List<EO> entityObjectCollection) {
        List<Key<DAO>> keys = new ArrayList<>();
        for (EO entityObjectObject : entityObjectCollection) {
            keys.add(Key.create(getElementDaoClass(), entityObjectObject.getKey().toString()));
        }
        deleteEntitiesByKey(keys);
    }

    protected DomainCollection<EO> toDomainCollection(List<DAO> daoCollection) {
        DomainCollection<EO> domainCollection = new DomainCollection<>();
        for (DAO dao : daoCollection) {
            domainCollection.add(dao.toDomainObject());
        }
        return domainCollection;
    }

    protected Class<DAO> getElementDaoClass() {
        return mElementEntity.getDaoClass();
    }

    private void deleteEntitiesByKey(Iterable<Key<DAO>> keys) {
        ofy().delete().keys(keys);
    }

    public E getElementEntity() {
        return mElementEntity;
    }

    public void rewriteAll() {
        List<DAO> daos = ofy().load().type(mElementEntity.getDaoClass()).list();
        ofy().save().entities(daos).now();
    }
{'fraction_non_alphanumeric': 0.10319585788252097, 'fraction_numerical': 0.0028566327441528296, 'mean_word_length': 3.3426356589147286, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5016599', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1578, 'n_words': 412}