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

com.alachisoft.ncache.client.internal.caching.SearchServiceImpl Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
package com.alachisoft.ncache.client.internal.caching;

import Alachisoft.NCache.Caching.TagComparisonType;

import com.alachisoft.ncache.client.CacheReader;
import com.alachisoft.ncache.client.QueryCommand;
import com.alachisoft.ncache.client.internal.util.ConversionUtil;
import com.alachisoft.ncache.client.services.SearchService;
import com.alachisoft.ncache.runtime.exceptions.CacheException;

import java.io.IOException;
import java.util.*;

import static tangible.DotNetToJavaStringHelper.isNullOrEmpty;
import static tangible.DotNetToJavaStringHelper.isNull;

class SearchServiceImpl implements SearchService {

    protected CacheImpl cacheContainer;

    SearchServiceImpl(CacheImpl cache) {
        cacheContainer = cache;
    }

    CacheImpl getCacheContainer() {
        return cacheContainer;
    }

    void setCacheContainer(CacheImpl value) {
        cacheContainer = value;
    }

    //region Execute Reader Operations
    @Override
    public int executeNonQuery(QueryCommand queryCommand) throws CacheException {
        if (queryCommand == null) {
            throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: queryCommand");
        }

        if (queryCommand.getQuery()==null) {
            throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: query");
        }
        if (queryCommand.getQuery()=="") {
            throw new IllegalArgumentException("Value cannot be empty.\nParameter name: query");
        }

        return cacheContainer.executeNonQuery(queryCommand.getQuery(), queryCommand.getParameters());

    }

    @Override
    public CacheReader executeReader(QueryCommand queryCommand) throws CacheException {
        return executeReader(queryCommand, true, -1);
    }

    @Override
    public CacheReader executeReader(QueryCommand queryCommand, boolean getData, int chunkSize) throws CacheException {
        if (queryCommand == null) {
            throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: queryCommand");
        }

        if (queryCommand.getQuery()==null) {
            throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: query");
        }
        if (queryCommand.getQuery()=="") {
            throw new IllegalArgumentException("Value cannot be empty.\nParameter name: query");
        }

        return cacheContainer.executeReader(queryCommand.getQuery(), queryCommand.getParameters(), getData, chunkSize);
    }

    //endregion

    //region Tag Based Operations

    @Override
    public  T executeScalar(QueryCommand queryCommand, Class cls) throws CacheException {
        if (queryCommand == null) {
            throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: queryCommand");
        }

        if (queryCommand.getQuery()==null) {
            throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: query");
        }
        if (queryCommand.getQuery()=="") {
            throw new IllegalArgumentException("Value cannot be empty.\nParameter name: query");
        }

        Object scalarValue = null;
        CacheReader reader = null;

        try {
            reader = cacheContainer.executeReader(queryCommand.getQuery(), queryCommand.getParameters(), true, -1);

            if (reader != null) {
                if (reader.read()) {
                    if (reader.getFieldCount() > 1) {
                        scalarValue = reader.getValue(1, cls);
                    } else {
                        scalarValue = reader.getValue(0, cls);
                    }
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            throw new CacheException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    throw new CacheException(e);
                }
            }
        }
        return (T) scalarValue;

    }




    @Override
    public void removeGroupData(String group) throws CacheException {
        if (isNull(group)) {
            throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: group");
        }
        cacheContainer.removeGroupData(group);
    }

    @Override
    public Collection getGroupKeys(String group) throws CacheException {
        if (isNull(group)) {
            throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: group");
        }
        ArrayList groupKeys = cacheContainer.getGroupKeys(group);
        return getGenericCollection(groupKeys);
    }

    //endregion

    //region Internal Methods

    @Override
    public  Map getGroupData(String group) throws CacheException {
        if (isNull(group)) {
            throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: group");
        }
        return cacheContainer.getGroupData(group);
    }



    Collection getGenericCollection(Collection col) {
        Collection keyList = new ArrayList();
        for (Object item : col) {
            keyList.add((String) item);
        }
        return keyList;
    }

    //endregion
}