org.piax.gtrans.ov.sg.MSkipGraph Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of piax-compat Show documentation
Show all versions of piax-compat Show documentation
A backward compatibility package for PIAX
The newest version!
/*
* MSkipGraph.java - Multi-Key implementation of Skip Graph.
*
* Copyright (c) 2015 PIAX development team
*
* You can redistribute it and/or modify it under either the terms of
* the AGPLv3 or PIAX binary code license. See the file COPYING
* included in the PIAX package for more in detail.
*
* $Id: MSkipGraph.java 1176 2015-05-23 05:56:40Z teranisi $
*/
package org.piax.gtrans.ov.sg;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.piax.common.ComparableKey;
import org.piax.common.ComparableKey.SpecialKey;
import org.piax.common.Destination;
import org.piax.common.Endpoint;
import org.piax.common.ObjectId;
import org.piax.common.PeerId;
import org.piax.common.TransportId;
import org.piax.common.subspace.KeyRange;
import org.piax.common.subspace.KeyRanges;
import org.piax.common.subspace.LowerUpper;
import org.piax.gtrans.ChannelTransport;
import org.piax.gtrans.FutureQueue;
import org.piax.gtrans.IdConflictException;
import org.piax.gtrans.Peer;
import org.piax.gtrans.ProtocolUnsupportedException;
import org.piax.gtrans.RemoteValue;
import org.piax.gtrans.RequestTransportListener;
import org.piax.gtrans.ReturnValue;
import org.piax.gtrans.TransOptions;
import org.piax.gtrans.TransportListener;
import org.piax.gtrans.impl.NestedMessage;
import org.piax.gtrans.ov.Link;
import org.piax.gtrans.ov.Overlay;
import org.piax.gtrans.ov.OverlayListener;
import org.piax.gtrans.ov.OverlayReceivedMessage;
import org.piax.gtrans.ov.RoutingTableAccessor;
import org.piax.gtrans.ov.impl.OverlayImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Multi-key Skip Graph implementation for GTRANS.
*/
public class MSkipGraph>
extends OverlayImpl implements RoutingTableAccessor, SGExecQueryCallback {
/*--- logger ---*/
private static final Logger logger = LoggerFactory
.getLogger(MSkipGraph.class);
public static TransportId DEFAULT_TRANSPORT_ID = new TransportId("sg");
SkipGraph ddllSG;
public MSkipGraph() throws IdConflictException, IOException {
this(Overlay.DEFAULT_ENDPOINT.value());
}
public MSkipGraph(String spec) throws IdConflictException, IOException {
this(DEFAULT_TRANSPORT_ID,
Peer.getInstance(PeerId.newId()).newBaseChannelTransport(Endpoint.newEndpoint(spec)));
}
public MSkipGraph(ChannelTransport> lowerTrans) throws IdConflictException,
IOException {
this(DEFAULT_TRANSPORT_ID, lowerTrans);
}
@SuppressWarnings("unchecked")
public MSkipGraph(TransportId transId, ChannelTransport> lowerTrans)
throws IdConflictException, IOException {
super(lowerTrans.getPeer(), transId, lowerTrans);
peer.registerBaseOverlay(transIdPath);
ddllSG = new SkipGraph(new TransportId(transId + "x"),
(ChannelTransport) lowerTrans, this);
}
@Override
public synchronized void fin() {
ddllSG.fin();
super.fin();
}
public Endpoint getEndpoint() {
return peerId;
}
public FutureQueue> request1(ObjectId sender, ObjectId receiver,
K dst, Object msg, TransOptions opts)
throws ProtocolUnsupportedException, IOException {
return request3(sender, receiver, new KeyRanges(dst), msg, opts);
}
public FutureQueue> request2(ObjectId sender, ObjectId receiver,
KeyRange dst, Object msg, TransOptions opts)
throws ProtocolUnsupportedException, IOException {
return request3(sender, receiver, new KeyRanges(dst), msg, opts);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public FutureQueue> request3(ObjectId sender, ObjectId receiver,
KeyRanges dst, Object msg, TransOptions opts)
throws ProtocolUnsupportedException, IOException {
Collection> ranges = dst.getRanges();
if (msg != null && msg instanceof NestedMessage
&& ((NestedMessage) msg).passthrough == SpecialKey.WILDCARD) {
ranges.add(new KeyRange(SpecialKey.WILDCARD));
}
NestedMessage nmsg = new NestedMessage(sender, receiver, null, peerId,
msg);
return ddllSG.scalableRangeQuery(ranges, nmsg, opts);
}
@Override @SuppressWarnings("unchecked")
public FutureQueue> request(ObjectId sender, ObjectId receiver,
Destination dst, Object msg, TransOptions opts)
throws ProtocolUnsupportedException, IOException {
logger.trace("ENTRY:");
logger.debug("peer:{} dst:{} msg:{}", peerId, dst, msg);
if (dst instanceof KeyRanges) {
return request3(sender, receiver, (KeyRanges) dst, msg, opts);
} else if (dst instanceof KeyRange) {
return request2(sender, receiver, (KeyRange) dst, msg, opts);
} else if (dst instanceof ComparableKey) {
return request1(sender, receiver, (K) dst, msg, opts);
} else if (dst instanceof LowerUpper) {
return forwardQueryToMaxLessThan(sender, receiver, (LowerUpper) dst, msg);
} else {
throw new ProtocolUnsupportedException(
"skip graph only supports ranges");
}
}
/*
* for MaxLessEq key query
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public FutureQueue> forwardQueryToMaxLessThan(ObjectId sender,
ObjectId receiver, LowerUpper lu, Object msg)
throws IllegalStateException {
logger.trace("ENTRY:");
NestedMessage nmsg = new NestedMessage(sender, receiver, null, peerId,
msg);
Collection> ret = ddllSG.forwardQuery(lu.isPlusDir(),
lu.getRange(), lu.getMaxNum(), nmsg);
List> ret2 = new ArrayList>();
if (ret != null) {
// ddllSG.forwardQuery がerrによって、nullを返すことがあるため
for (RemoteValue> rv : ret) {
if (rv != null && rv.getValue() instanceof MVal) {
MVal mval = (MVal) rv.getValue();
for (ReturnValue