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

com.alachisoft.ncache.client.internal.command.SearchCommand Maven / Gradle / Ivy

/*
 * Command.java
 *
 * Created on September 8, 2006, 11:45 AM
 *
 * Copyright 2005 Alachisoft, Inc. All rights reserved.
 * ALACHISOFT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.alachisoft.ncache.client.internal.command;

import com.alachisoft.ncache.common.protobuf.CommandProtocol;
import com.alachisoft.ncache.common.protobuf.KeyValueProtocol;
import com.alachisoft.ncache.common.protobuf.SearchCommandProtocol;
import com.alachisoft.ncache.common.protobuf.ValueWithTypeProtocol;
import com.alachisoft.ncache.runtime.exceptions.CommandException;
import com.alachisoft.ncache.runtime.util.HelperFxn;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author Administrator
 * @version 1.0
 */
public final class SearchCommand extends Command {

    private static char Delimitor = '|';
    private String query;
    private HashMap values;
    private boolean isSearchEntries;
    private int _commandVersion;
    private int _methodOverload = 0;
    protected com.alachisoft.ncache.common.protobuf.SearchCommandProtocol.SearchCommand _commandInstance;

    /**
     * Creates a new instance of SearchCommand
     *
     * @param query
     * @param isSearchEntriesCommand
     * @param async
     * @throws java.io.UnsupportedEncodingException
     * @throws java.io.IOException
     */
    public SearchCommand(String query, HashMap values,
                         boolean isSearchEntries, boolean async) {
//        name = isSearchEntries ? "SEARCHENTERIES " : "SEARCH ";
        this.query = query;
        this.values = values;
        this.isAsync = async;
        this.isSearchEntries = isSearchEntries;
    }

    public void createCommand() throws CommandException {

        SearchCommandProtocol.SearchCommand.Builder builder =
                SearchCommandProtocol.SearchCommand.newBuilder();

        builder = builder.setRequestId(this.getRequestId()).setQuery(this.query).setSearchEntries(this.isSearchEntries);

        int totalValuesCount = 0;

        Set keySet = values.keySet();
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            try {
                String typeKey = (String) iterator.next();

                KeyValueProtocol.KeyValue.Builder keyValPair =
                        KeyValueProtocol.KeyValue.newBuilder();
                keyValPair = keyValPair.setKey(typeKey);

                if (values.get(typeKey) instanceof ArrayList) {
                    ArrayList list = (ArrayList) values.get(typeKey);
                    for (int i = 0; i < list.size(); i++) {
                        Object typeValue = list.get(i);
                        keyValPair = keyValPair.addValue(ValueWithTypeProtocol.ValueWithType.newBuilder().setValue(getValueString(typeValue)).setType(typeValue.getClass().getName()).build());
                    }
                } else {
                    Object typeValue = values.get(typeKey);
                    keyValPair = keyValPair.addValue(ValueWithTypeProtocol.ValueWithType.newBuilder().setValue(getValueString(typeValue)).setType(typeValue.getClass().getName()).build());
                }
                builder = builder.addValues(keyValPair);
            } catch (CommandException cmE) {
                throw cmE;
            } catch (Exception e) {
                throw new CommandException("Error parsing values.");
            }
        }

//        String startTag = String.format(name + "\"" + requestId + "\"" + query
//                + "\"" + totalValuesCount + "\"");
        if (builder.getSearchEntries())
        {
            _commandVersion = 2;
        }
        else
        {
            _commandVersion = 3;
        }
        _commandInstance =builder
                .setClientLastViewId(getClientLastViewId())
                .setCommandVersion(_commandVersion)
                .setMethodOverload(_methodOverload)
                .build();
    }

    /**
     * @return
     */

    private String getValueString(Object value) throws Exception {
        /*String retVal = "";
        if (value instanceof String) {
        retVal = "'" + value.toString() + "'";
        } else {
        retVal = value.toString();
        }
        return retVal;*/
//        String retVal = "";

        if (value == null) {
            throw new CommandException("NCache query does not support null values");
        }

        if (value instanceof String) {
            value = (Object) (value.toString()).toLowerCase();
        }
        if (value instanceof Date) {
            value = HelperFxn.getTicks((Date) value);
        }
//        String typeStr = value.getClass().getName();
//        String valStr = value.toString();
//        retVal = valStr + Delimitor + typeStr;
        return value.toString();
    }

    public CommandType getCommandType() {
        return CommandType.SEARCH;
    }

    @Override
    public RequestType getCommandRequestType() {
        return RequestType.NonKeyBulkRead;
    }

    @Override
    protected  void serializeCommandInternal(ByteArrayOutputStream stream) throws IOException
    {
        _commandInstance.writeTo(stream);
    }
    @Override
    protected short getCommandHandle()
    {
        return (short)CommandProtocol.Command.Type.SEARCH.getNumber();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy