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

io.jsync.spi.cluster.impl.HazelcastAsyncMultiMap Maven / Gradle / Ivy

There is a newer version: 1.10.13
Show newest version
/*
 * Copyright (c) 2011-2013 The original author or authors
 * ------------------------------------------------------
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *     The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 *
 *     The Apache License v2.0 is available at
 *     http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 */

package io.jsync.spi.cluster.impl;

import io.jsync.AsyncResult;
import io.jsync.Handler;
import io.jsync.spi.AsyncSPI;
import io.jsync.spi.cluster.AsyncMultiMap;
import io.jsync.spi.cluster.ChoosableIterable;

import java.util.Collection;
import java.util.Map;

/**
 * @author Tim Fox
 */
public class HazelcastAsyncMultiMap implements AsyncMultiMap {

    private final AsyncSPI async;
    private final com.hazelcast.core.MultiMap map;

    public HazelcastAsyncMultiMap(AsyncSPI async, com.hazelcast.core.MultiMap map) {
        this.async = async;
        this.map = map;
    }

    @Override
    public void removeAllForValue(final V val, final Handler> completionHandler) {
        V wrappedVal = HazelcastServerID.convertServerID(val);
        for (Map.Entry entry : map.entrySet()) {
            V v = entry.getValue();
            if (wrappedVal.equals(v)) {
                map.remove(entry.getKey(), v);
            }
        }
        if (completionHandler != null) {
            completionHandler.handle(new AsyncResult() {
                @Override
                public Void result() {
                    return null;
                }

                @Override
                public Throwable cause() {
                    return null;
                }

                @Override
                public boolean succeeded() {
                    return true;
                }

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

    @Override
    public void add(final K k, final V v, final Handler> completionHandler) {
        map.put(k, HazelcastServerID.convertServerID(v));
        if (completionHandler != null) {
            completionHandler.handle(new AsyncResult() {
                @Override
                public Void result() {
                    return null;
                }

                @Override
                public Throwable cause() {
                    return null;
                }

                @Override
                public boolean succeeded() {
                    return true;
                }

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

    @Override
    public void get(final K k, final Handler>> resultHandler) {
        Collection iteratable = map.get(k);
        ChoosableSet choosable = new ChoosableSet<>(iteratable.size());
        for (V obj : iteratable) {
            choosable.add(obj);
        }
        if (resultHandler != null) {
            resultHandler.handle(new AsyncResult>() {
                @Override
                public ChoosableIterable result() {
                    return choosable;
                }

                @Override
                public Throwable cause() {
                    return null;
                }

                @Override
                public boolean succeeded() {
                    return true;
                }

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

    @Override
    public void remove(final K k, final V v, final Handler> completionHandler) {
        map.remove(k, HazelcastServerID.convertServerID(v));
        if (completionHandler != null) {
            completionHandler.handle(new AsyncResult() {
                @Override
                public Void result() {
                    return null;
                }

                @Override
                public Throwable cause() {
                    return null;
                }

                @Override
                public boolean succeeded() {
                    return true;
                }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy