All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.landawn.abacus.util.Neo4jExecutor Maven / Gradle / Ivy

There is a newer version: 1.10.1
Show newest version
/*
 * Copyright (C) 2016 HaiYang Li
 *
 * 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.
 */

package com.landawn.abacus.util;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import org.neo4j.ogm.cypher.Filter;
import org.neo4j.ogm.cypher.Filters;
import org.neo4j.ogm.cypher.query.Pagination;
import org.neo4j.ogm.cypher.query.SortOrder;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;

import com.landawn.abacus.util.stream.Stream;

/**
 * It's a simple wrapper of Neo4j Java client.
 * Refer to: http://neo4j.com/docs/ogm/java/stable/ 
 * 
 * @since 0.8
 * 
 * @author HaiYang Li
 */
public final class Neo4jExecutor {
    private final LinkedBlockingQueue sessionPool = new LinkedBlockingQueue(8192);
    private final SessionFactory sessionFactory;

    public Neo4jExecutor(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public SessionFactory sessionFactory() {
        return sessionFactory;
    }

    public Session openSession() {
        return sessionFactory.openSession();
    }

    public  T load(Class targetClass, Long id) {
        final Session session = getSession();

        try {
            return session.load(targetClass, id);
        } finally {
            closeSession(session);
        }
    }

    public  T load(Class targetClass, Long id, int depth) {
        final Session session = getSession();

        try {
            return session.load(targetClass, id, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Collection ids) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, ids);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Collection ids, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, ids, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Collection ids, SortOrder sortOrder) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, ids, sortOrder);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Collection ids, SortOrder sortOrder, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, ids, sortOrder, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Collection ids, Pagination pagination) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, ids, pagination);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Collection ids, Pagination pagination, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, ids, pagination, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Collection ids, SortOrder sortOrder, Pagination pagination) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, ids, sortOrder, pagination);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Collection ids, SortOrder sortOrder, Pagination pagination, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, ids, sortOrder, pagination, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Collection objects) {
        final Session session = getSession();

        try {
            return session.loadAll(objects);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Collection objects, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(objects, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Collection objects, SortOrder sortOrder) {
        final Session session = getSession();

        try {
            return session.loadAll(objects, sortOrder);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Collection objects, SortOrder sortOrder, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(objects, sortOrder, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Collection objects, Pagination pagination) {
        final Session session = getSession();

        try {
            return session.loadAll(objects, pagination);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Collection objects, Pagination pagination, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(objects, pagination, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Collection objects, SortOrder sortOrder, Pagination pagination) {
        final Session session = getSession();

        try {
            return session.loadAll(objects, sortOrder, pagination);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Collection objects, SortOrder sortOrder, Pagination pagination, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(objects, sortOrder, pagination, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, SortOrder sortOrder) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, sortOrder);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, SortOrder sortOrder, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, sortOrder, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Pagination pagination) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, pagination);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Pagination pagination, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, pagination, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, SortOrder sortOrder, Pagination pagination) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, sortOrder, pagination);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, SortOrder sortOrder, Pagination pagination, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, sortOrder, pagination, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filter filter) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filter);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filter filter, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filter, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filter filter, SortOrder sortOrder) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filter, sortOrder);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filter filter, SortOrder sortOrder, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filter, sortOrder, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filter filter, Pagination pagination) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filter, pagination);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filter filter, Pagination pagination, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filter, pagination, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filter filter, SortOrder sortOrder, Pagination pagination) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filter, sortOrder, pagination);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filter filter, SortOrder sortOrder, Pagination pagination, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filter, sortOrder, pagination, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filters filters) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filters);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filters filters, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filters, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filters filters, SortOrder sortOrder) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filters, sortOrder);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filters filters, SortOrder sortOrder, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filters, sortOrder, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filters filters, Pagination pagination) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filters, pagination);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filters filters, Pagination pagination, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filters, pagination, depth);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filters filters, SortOrder sortOrder, Pagination pagination) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filters, sortOrder, pagination);
        } finally {
            closeSession(session);
        }
    }

    public  Collection loadAll(Class targetClass, Filters filters, SortOrder sortOrder, Pagination pagination, int depth) {
        final Session session = getSession();

        try {
            return session.loadAll(targetClass, filters, sortOrder, pagination, depth);
        } finally {
            closeSession(session);
        }
    }

    public  void save(T object) {
        final Session session = getSession();

        try {
            session.save(object);
        } finally {
            closeSession(session);
        }
    }

    public  void save(T object, int depth) {
        final Session session = getSession();

        try {
            session.save(object, depth);
        } finally {
            closeSession(session);
        }
    }

    public  void delete(T object) {
        final Session session = getSession();

        try {
            session.delete(object);
        } finally {
            closeSession(session);
        }
    }

    public  void deleteAll(Class targetClass) {
        final Session session = getSession();

        try {
            session.deleteAll(targetClass);
        } finally {
            closeSession(session);
        }

    }

    public  T queryForObject(Class objectType, String cypher, Map parameters) {
        final Session session = getSession();

        try {
            return session.queryForObject(objectType, cypher, parameters);
        } finally {
            closeSession(session);
        }
    }

    public Stream> query(String cypher, Map parameters) {
        final Session session = getSession();

        return Stream.of(session.query(cypher, parameters).iterator()).onClose(newCloseHandle(session));
    }

    public Stream> query(String cypher, Map parameters, boolean readOnly) {
        final Session session = getSession();

        return Stream.of(session.query(cypher, parameters, readOnly).iterator()).onClose(newCloseHandle(session));
    }

    public  Stream query(Class objectType, String cypher, Map parameters) {
        final Session session = getSession();

        return Stream.of(session.query(objectType, cypher, parameters).iterator()).onClose(newCloseHandle(session));
    }

    private Runnable newCloseHandle(final Session session) {
        return new Runnable() {
            @Override
            public void run() {
                closeSession(session);
            }
        };
    }

    public long countEntitiesOfType(Class entity) {
        final Session session = getSession();

        try {
            return session.countEntitiesOfType(entity);
        } finally {
            closeSession(session);
        }
    }

    public Long resolveGraphIdFor(Object possibleEntity) {
        final Session session = getSession();

        try {
            return session.resolveGraphIdFor(possibleEntity);
        } finally {
            closeSession(session);
        }
    }

    private Session getSession() {
        Session session = null;

        try {
            session = sessionPool.poll(100, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            // ignore.
        }

        if (session == null) {
            session = openSession();
        }

        return session;
    }

    private void closeSession(Session session) {
        if (session != null) {
            try {
                sessionPool.offer(session, 100, TimeUnit.MILLISECONDS);
            } catch (Exception e) {
                // ignore.
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy