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

io.github.bucket4j.grid.coherence.CoherenceProcessor Maven / Gradle / Ivy

There is a newer version: 8.10.1
Show newest version
/*-
 * ========================LICENSE_START=================================
 * Bucket4j
 * %%
 * Copyright (C) 2015 - 2020 Vladimir Bukhtoyarov
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =========================LICENSE_END==================================
 */
package io.github.bucket4j.grid.coherence;

import com.tangosol.util.InvocableMap;
import com.tangosol.util.processor.AbstractProcessor;
import io.github.bucket4j.TimeMeter;
import io.github.bucket4j.distributed.remote.AbstractBinaryTransaction;
import io.github.bucket4j.distributed.remote.CommandResult;
import io.github.bucket4j.distributed.remote.RemoteCommand;
import io.github.bucket4j.distributed.remote.Request;
import io.github.bucket4j.distributed.serialization.InternalSerializationHelper;
import io.github.bucket4j.util.ComparableByContent;

import java.util.Arrays;

import static io.github.bucket4j.distributed.serialization.InternalSerializationHelper.serializeResult;

public class CoherenceProcessor extends AbstractProcessor implements ComparableByContent {

    private static final long serialVersionUID = 1L;

    private final byte[] requestBytes;

    public CoherenceProcessor(Request request) {
        this.requestBytes = InternalSerializationHelper.serializeRequest(request);
    }

    public CoherenceProcessor(byte[] requestBytes) {
        this.requestBytes = requestBytes;
    }

    public byte[] getRequestBytes() {
        return requestBytes;
    }

    @Override
    public byte[] process(InvocableMap.Entry entry) {
        return new AbstractBinaryTransaction(requestBytes) {
            @Override
            public boolean exists() {
                return entry.getValue() != null;
            }

            @Override
            protected byte[] getRawState() {
                return entry.getValue();
            }

            @Override
            protected void setRawState(byte[] stateBytes) {
                entry.setValue(stateBytes);
            }
        }.execute();
    }


    @Override
    public boolean equalsByContent(ComparableByContent other) {
        CoherenceProcessor processor = (CoherenceProcessor) other;
        return Arrays.equals(requestBytes, processor.requestBytes);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy