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

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

There is a newer version: 5.3.0
Show newest version
/*
 * 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 Alachisoft.NCache.Common.BitSet;
import Alachisoft.NCache.Common.BitSetConstants;
import Alachisoft.NCache.Common.Util.DependencyHelper;

import com.alachisoft.ncache.client.internal.caching.CacheHelper;
import com.alachisoft.ncache.client.internal.caching.CacheImpl;
import com.alachisoft.ncache.client.CacheSyncDependency;
import com.alachisoft.ncache.common.protobuf.AddCommandProtocol;
import com.alachisoft.ncache.common.protobuf.CommandProtocol;
import com.alachisoft.ncache.common.protobuf.ObjectQueryInfoProtocol;
import com.alachisoft.ncache.common.protobuf.SyncDependencyProtocol;
import com.alachisoft.ncache.runtime.CacheItemPriority;
import com.alachisoft.ncache.runtime.dependencies.CacheDependency;
import com.alachisoft.ncache.runtime.events.EventDataFilter;
import com.alachisoft.ncache.runtime.exceptions.CommandException;
import com.alachisoft.ncache.runtime.util.HelperFxn;
import com.alachisoft.ncache.runtime.util.NCDateTime;
import com.alachisoft.ncache.runtime.util.TimeSpan;
import Alachisoft.NCache.Common.Caching.UserBinaryObject;
import com.google.protobuf.ByteString;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 */

public final class AddCommand extends Command {

    private int _methodOverload;
    private String groupType;
    private String group;
    private String subGroup;
    private Date absoluteExpiration;
    private TimeSpan slidingExpiration;
    private boolean isResyncExpiredItems;
    private CacheItemPriority priority;
    private CacheDependency dependency;
    private int remCallbackID;
    private int upCallbackID;
    private CacheSyncDependency syncDependency;
    private HashMap queryInfo;
    private int flagMap;
    //+ Asad:20110330 ReadThru/WriteThru provider support in JAVA
    private String providerName;
    private String resyncProviderName;
    private int datasourceItemAddedCallbackID;
    private boolean encryption;
    private String cacheId;
    private int updateCallbackFilter;
    private int removeCallbackFilter;
    private short _updateCallbackId;
    private short _removeCallbackId;

    private String _clientId;

    protected com.alachisoft.ncache.common.protobuf.AddCommandProtocol.AddCommand _commandInstance;

    //- Asad:20110330

    /**
     * Creates a new instance of AddCommand
     *
     * @param key
     * @param value
     * @param dependency
     * @param absoluteExpiration
     * @param slidingExpiration
     * @param priority
     * @param isResyncExpiredItems
     * @param group
     * @param subGroup
     * @param async
     * @throws java.io.UnsupportedEncodingException
     * @throws java.io.IOException
     */
    public AddCommand(String key, byte[] value, CacheDependency dependency,
                      CacheSyncDependency syncDependency, Date absoluteExpiration,
                      TimeSpan slidingExpiration, CacheItemPriority priority, short remId,
                      short upId, int dsItemAddedCallback, boolean isResyncExpiredItems,
                      String group, short itemAdded ,boolean async, HashMap queryInfo,
                      BitSet flagMap, String providerName,
                      boolean encryption, String cacheId,
                      EventDataFilter updateDataFilter, EventDataFilter removeDataFilter,int methodOverload,
                      String clientId, String typeName) {
        super.name = "AddCommand";
        super.asyncCallbackSpecified = this.isAsync && itemAdded != -1;

        this.key = key;
        this.value = value;
        this.dependency = dependency;
        this.syncDependency = syncDependency;
        this.absoluteExpiration = absoluteExpiration;
        this.slidingExpiration = slidingExpiration;
        this.priority = priority;
        this.remCallbackID = remId;
        this.upCallbackID = upId;
        this.datasourceItemAddedCallbackID = dsItemAddedCallback;
        this.isResyncExpiredItems = isResyncExpiredItems;
        this.group = group;
        this.subGroup = subGroup;
        super.asyncCallbackId = itemAdded;
        this.isAsync = async;
        this.queryInfo = queryInfo;
        this.flagMap = BitSetConstants.getBitSetData(flagMap);
        this.providerName = providerName;
        this.encryption = encryption;
        this.cacheId = cacheId;
        this.updateCallbackFilter = updateDataFilter.getValue();
        this.removeCallbackFilter = removeDataFilter.getValue();
        this._methodOverload = methodOverload;
        this._clientId = clientId;
        this.groupType = typeName;


    }

    public void createCommand() throws CommandException {
        long sldExp = 0, absExp = 0;
        try {
            if (absoluteExpiration.equals(NCDateTime.getUTCDate(CacheImpl.defaultAbsolute))) {
                absExp = 1;
            }
            else if (absoluteExpiration.equals(NCDateTime.getUTCDate(CacheImpl.defaultAbsoluteLonger))) {
                absExp = 2;
            }
            else if (absoluteExpiration != CacheImpl.NoAbsoluteExpiration) {
                absExp = HelperFxn.getTicks(absoluteExpiration);
            }
            if (slidingExpiration.equals(CacheImpl.DefaultSliding)) {
                sldExp = 1;
            }
            else if (slidingExpiration.equals(CacheImpl.DefaultSlidingLonger)) {
                sldExp = 2;
            }
            else if (slidingExpiration != CacheImpl.NoSlidingExpiration) {
                sldExp = slidingExpiration.getTotalTicks();
            }
        } catch (ParseException e) {
            //ignore
        }

        UserBinaryObject userBin = UserBinaryObject.createUserBinaryObject(this.value);
        List dataList = userBin.getDataList();

        ///Copy the chunks to protobuf list
        List dataChunks = new ArrayList();
        for (int i = 0; i < dataList.size(); i++) {
            dataChunks.add(ByteString.copyFrom(dataList.get(i)));
        }

        AddCommandProtocol.AddCommand.Builder builder =
                AddCommandProtocol.AddCommand.newBuilder()
                        .setKey(this.key)
                        .addAllData(dataChunks)

                        .setRequestId(this.getRequestId())
                        .setUpdateCallbackId(this.upCallbackID)
                        .setRemoveCallbackId(this.remCallbackID)
                        .setDatasourceItemAddedCallbackId(this.datasourceItemAddedCallbackID)
                        .setIsAsync(this.isAsync)
                        .setPriority(this.priority.value())
                        .setIsResync(this.isResyncExpiredItems)
                        .setAbsExpiration(absExp)
                        .setSldExpiration(sldExp);


        if (this.group != null) {
            builder = builder.setGroup(this.group);
            if (this.subGroup != null) {
                builder = builder.setSubGroup(subGroup);
            }
            if (this.groupType != null)
                builder = builder.setGroupType(this.groupType);
        }
        builder.setFlag(this.flagMap);
        //working
        if (this.providerName != null) {
            builder = builder.setProviderName(this.providerName);
        }

        if (this.resyncProviderName != null) {
            builder = builder.setResyncProviderName(this.resyncProviderName);
        }
        if (this._clientId != null) {
            builder = builder.setClientID(this._clientId);
        }

        builder
                .setUpdateDataFilter(this.updateCallbackFilter)
                .setRemoveDataFilter(this.removeCallbackFilter);



        _commandInstance =builder.setVersion(VERSION).build();


    }

    /**
     * @return
     */
    @Override
    public CommandType getCommandType() {
        return CommandType.ADD;
    }

    public int AsycItemAddedOpComplete() {
        return super.asyncCallbackId;
    }

    @Override
    public boolean getIsSafe() {
        return false;
    }

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


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy