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

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

package com.alachisoft.ncache.client.internal.command;

import Alachisoft.NCache.Common.Caching.UserBinaryObject;
import Alachisoft.NCache.Common.DataTypes.List.ListGetItemOperation;
import Alachisoft.NCache.Common.DataTypes.List.ListInsertWithIndexOperation;
import Alachisoft.NCache.Common.JSON.ExtendedJsonValueBase;
import com.alachisoft.ncache.common.protobuf.CollectionItemProtocol;
import com.alachisoft.ncache.common.protobuf.ListCommandProtocol;
import com.alachisoft.ncache.common.protobuf.ListInsertWithIndexProtocol;
import com.google.protobuf.ByteString;

import java.nio.charset.StandardCharsets;

public class ListInsertWithIndexCommand extends ListCommandBase {
    private int _index;
    private ListInsertWithIndexOperation.OpType _operationType;
    private ExtendedJsonValueBase _value;
    private ListInsertWithIndexProtocol.ListInsertWithIndex.Builder _listInsertWithIndex;

    public ListInsertWithIndexCommand(ListInsertWithIndexOperation operation)
    {
        super.name = "ListInsertWithIndex";
        super.key = operation.getName();
        _writeThruOption = operation.getWriteThruOptions();
        _lockId = (String)operation.getLockId();
        _lockTimeout = operation.getLockTimeout();
        _index = operation.getIndex();
        _operationType = operation.getOperationType();
        _value = operation.getItem();
    }

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

    @Override
    public CommandType getCommandType()
    {
        return CommandType.DATA_TYPE;
    }

    @Override
    protected void createCommand()
    {
        _listInsertWithIndex = ListInsertWithIndexProtocol.ListInsertWithIndex.newBuilder();
        _listInsertWithIndex.setIndex(_index);
        UserBinaryObject ubObject = UserBinaryObject.createUserBinaryObject(_value.toJson().getBytes(StandardCharsets.UTF_8));

        CollectionItemProtocol.CollectionItem.Builder item = CollectionItemProtocol.CollectionItem.newBuilder();
        for(byte[] bytes : ubObject.getDataList())
            item.addData(ByteString.copyFrom(bytes));

        _listInsertWithIndex.setValue(item);
        switch (_operationType)
        {
            case Add:
                _listInsertWithIndex.setMethod(ListInsertWithIndexProtocol.ListInsertWithIndex.Method.INSERT);
                break;

            case Update:
                _listInsertWithIndex.setMethod(ListInsertWithIndexProtocol.ListInsertWithIndex.Method.LIST_SETTER);
                break;
        }

        super.createCommand();

        _listCommandBuilder.setType(ListCommandProtocol.ListCommand.Type.LIST_INSERT_WITH_INDEX);
        _listCommandBuilder.setListInsertWithIndex(_listInsertWithIndex);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy