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

com.alachisoft.ncache.client.internal.command.InsertCommand 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.Locking.LockAccessType;
import Alachisoft.NCache.Common.Util.DependencyHelper;

import com.alachisoft.ncache.client.internal.caching.CacheImpl;
import com.alachisoft.ncache.client.CacheSyncDependency;
import com.alachisoft.ncache.common.protobuf.CommandProtocol;
import com.alachisoft.ncache.common.protobuf.InsertCommandProtocol;
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.events.ListenerType;
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 InsertCommand extends Command {

    private int _methodOverload=0;
    private final ListenerType _listenerType;
    private String group;
    private String subGroup;
    private Date absoluteExpiration;
    private TimeSpan slidingExpiration;
    private boolean isResyncExpiredItems;
    private CacheItemPriority priority;
    private int remCallbackID;
    private int upCallbackID;
    private int dsItemUpdateCallbackID;
    private CacheSyncDependency syncDependency;
    private CacheDependency dependency;
    private HashMap queryInfo;
    private int flagMap;
    private String _lockId;
    private LockAccessType _accessType;
    private long _version;
    private String providerName;
    private String resyncProviderName;
    private boolean encryption;
    private String cacheId;
    private int updateCallbackFilter;
    private int removeCallbackFilter;
    private final String groupType;
    private String _clientId;
    protected com.alachisoft.ncache.common.protobuf.InsertCommandProtocol.InsertCommand _commandInstance;

    /**
     * Creates a new instance of AddCommand
     *
     * @param key
     * @param value
     * @param dependency
     * @param absoluteExpiration
     * @param slidingExpiration
     * @param priority
     * @param isResyncExpiredItems
     * @param group
     * @param subGroup
     * @throws java.io.UnsupportedEncodingException
     * @throws java.io.IOException
     */
    public InsertCommand(String key, byte[] value, CacheDependency dependency, CacheSyncDependency syncDependency, java.util.Date absoluteExpiration,
                         TimeSpan slidingExpiration, CacheItemPriority priority, short removeCallback, short updateCallback,
                         boolean isResyncExpiredItems, String group, short itemUpdated, boolean isAsync, java.util.HashMap queryInfo,
                         BitSet flagMap, String lockId, long version, LockAccessType accessType, String providerName,  boolean encryption,
                         String cacheId, EventDataFilter updateCallbackFilter, EventDataFilter removeCallabackFilter, int methodOverload, String clientId, String typeName,
                         ListenerType listenerType) {

        super.name = "InsertCommand";
        super.asyncCallbackSpecified = this.isAsync && itemUpdated != -1;

        this.key = key;
        this.value = value;
        this.dependency = dependency;
        this.syncDependency = syncDependency;
        this.absoluteExpiration = absoluteExpiration;
        this.slidingExpiration = slidingExpiration;
        this.priority = priority;
        this.remCallbackID = removeCallback;
        this.upCallbackID = updateCallback;
        this.group = group;
        this.subGroup = subGroup;
        this.asyncCallbackId = itemUpdated;
        this.isAsync = isAsync;
        this.queryInfo = queryInfo;
        this.flagMap = BitSetConstants.getBitSetData(flagMap);
        this._lockId =  lockId;
        this._version = version;
        this._accessType = accessType;
        this.providerName = providerName;
        this.resyncProviderName = resyncProviderName;
        this.encryption = encryption;
        this.cacheId = cacheId;
        this.updateCallbackFilter = updateCallbackFilter.getValue();
        this.removeCallbackFilter = removeCallabackFilter.getValue();
        this._methodOverload = methodOverload;
        this._clientId = clientId;
        this.groupType = typeName;
        this._listenerType = listenerType;
    }

    public void createCommand() throws CommandException {
        if (key == null) {
            throw new NullPointerException("key");
        }
        if (value == null) {
            throw new NullPointerException("values");
        }

        if (key.equals("")) {
            throw new IllegalArgumentException("key");
        }

        if (group == null) {
            if (subGroup != null) {
                throw new NullPointerException("group must be specified for sub group");
            }
        }

        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 = new NCDateTime(absoluteExpiration).getTicks();
            }
            else {
                absExp = 0;
            }
            if (slidingExpiration.equals(CacheImpl.DefaultSliding)) {
                sldExp = 1;
            }
            else if (slidingExpiration.equals(CacheImpl.DefaultSlidingLonger)) {
                sldExp = 2;
            }
            else if (slidingExpiration != CacheImpl.NoSlidingExpiration) {
                sldExp = slidingExpiration.getTotalTicks();
            }
            else {
                sldExp = 0;
            }
        } catch (ParseException ignored) {
        }

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

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


        InsertCommandProtocol.InsertCommand.Builder builder =
                InsertCommandProtocol.InsertCommand.newBuilder()
                        .setKey(key)
                        .addAllData(dataChunks)
                        .setRequestId(super.getRequestId())
                        .setRemoveCallbackId(remCallbackID)
                        .setUpdateCallbackId(upCallbackID)
                        .setUpdateDataFilter(this.updateCallbackFilter)
                        .setRemoveDataFilter(this.removeCallbackFilter)
                        .setDatasourceUpdatedCallbackId(dsItemUpdateCallbackID)
                        .setIsAsync(isAsync)
                        .setPriority(priority.value())
                        .setFlag(flagMap)
                        .setItemVersion(this._version)
                        .setLockAccessType(this._accessType.getValue())
                        .setCallbackType(_listenerType.getValue())
                        .setAbsExpiration(absExp)
                        .setSldExpiration(sldExp)
                        .setIsResync(isResyncExpiredItems);


        if(_lockId != null){
            builder.setLockId(this._lockId);
        }

        if (group != null) {
            builder = builder.setGroup(group);
            if (subGroup != null) {
                builder = builder.setSubGroup(subGroup);
            }
            if (this.groupType != null)
                builder = builder.setGroupType(this.groupType);
        }

        if (providerName != null) {
            builder = builder.setProviderName(providerName);
        }

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




        _commandInstance =builder
                .setCommandID(getCommandID())
                .setVersion(VERSION)
                .setMethodOverload(_methodOverload)
                .build();


    }

    /**
     * @return
     */


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


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

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

    @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.INSERT.getNumber();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy