swim.runtime.lane.JoinMapLaneModel Maven / Gradle / Ivy
// Copyright 2015-2019 SWIM.AI inc.
//
// 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.
package swim.runtime.lane;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import swim.api.Link;
import swim.api.data.MapData;
import swim.api.downlink.MapDownlink;
import swim.collections.FingerTrieSeq;
import swim.collections.HashTrieMap;
import swim.runtime.LaneContext;
import swim.runtime.LinkBinding;
import swim.structure.Form;
import swim.structure.Record;
import swim.structure.Value;
import swim.uri.Uri;
import swim.warp.CommandMessage;
public class JoinMapLaneModel extends LaneModel, JoinMapLaneUplink> {
protected int flags;
protected MapData data;
protected MapData linkData;
protected volatile HashTrieMap> downlinks;
JoinMapLaneModel(int flags) {
this.flags = flags;
this.downlinks = HashTrieMap.empty();
}
public JoinMapLaneModel() {
this(0);
}
@Override
protected JoinMapLaneUplink createUplink(LinkBinding link) {
return new JoinMapLaneUplink(this, link);
}
@Override
public void setLaneContext(LaneContext laneContext) {
super.setLaneContext(laneContext);
openStore();
}
protected void openStore() {
this.data = this.laneContext.data().mapData(laneUri().toString())
.isResident(isResident())
.isTransient(isTransient());
this.linkData = this.laneContext.data().mapData(Record.create(1).attr("join", laneUri().toString()))
.isResident(isResident())
.isTransient(isTransient());
}
protected void openDownlinks() {
for (Map.Entry entry : this.linkData) {
final Value key = entry.getKey();
final Value value = entry.getValue();
final Value header = value.header("downlink");
final Uri nodeUri = header.get("node").coerce(Uri.form());
final Uri laneUri = header.get("lane").coerce(Uri.form());
final float prio = header.get("prio").floatValue(0.0f);
final float rate = header.get("rate").floatValue(0.0f);
final Value body = header.get("body");
new JoinMapLaneDownlink(this.laneContext, stage(), this, key,
this.laneContext.meshUri(), this.laneContext.hostUri(), nodeUri, laneUri,
prio, rate, body, Form.forValue(), Form.forValue()).openDownlink();
}
}
protected void downlink(Value key, JoinMapLaneDownlink, ?> downlink) {
Value value = this.linkData.get(key);
Record header = value.headers("downlink");
if (header == null
|| !header.get("node").coerce(Uri.form()).equals(downlink.nodeUri())
|| !header.get("lane").coerce(Uri.form()).equals(downlink.laneUri())
|| header.get("prio").floatValue(0.0f) != downlink.prio()
|| header.get("rate").floatValue(0.0f) != downlink.rate()
|| !header.get("body").equals(downlink.body())) {
header = Record.of()
.slot("node", downlink.nodeUri().toString())
.slot("lane", downlink.laneUri().toString());
if (downlink.prio() != 0.0f) {
header.slot("prio", downlink.prio());
}
if (downlink.rate() != 0.0f) {
header.slot("rate", downlink.rate());
}
if (downlink.body().isDefined()) {
header.slot("body", downlink.body());
}
if ("downlink".equals(value.tag())) {
value = value.updatedAttr("downlink", header);
} else {
value = Record.of().attr("downlink", header).concat(value);
}
this.linkData.put(key, value);
}
new JoinMapLaneRelayDownlink(this, key, downlink).run();
}
protected void openDownlink(Value key, JoinMapLaneDownlink, ?> downlink) {
downlink.openDownlink(); // Open before CAS
HashTrieMap> oldDownlinks;
HashTrieMap> newDownlinks;
do {
oldDownlinks = this.downlinks;
newDownlinks = oldDownlinks.updated(key, downlink);
} while (oldDownlinks != newDownlinks && !DOWNLINKS.compareAndSet(this, oldDownlinks, newDownlinks));
if (oldDownlinks != newDownlinks) {
final JoinMapLaneDownlink, ?> oldDownlink = oldDownlinks.get(key);
if (oldDownlink != null) {
try {
oldDownlink.close();
} catch (Exception swallow) { }
}
}
}
protected void closeDownlinks() {
HashTrieMap> oldDownlinks;
final HashTrieMap> newDownlinks = HashTrieMap.empty();
do {
oldDownlinks = this.downlinks;
} while (oldDownlinks != newDownlinks && !DOWNLINKS.compareAndSet(this, oldDownlinks, newDownlinks));
if (!oldDownlinks.isEmpty()) {
for (JoinMapLaneDownlink, ?> downlink : oldDownlinks.values()) {
try {
downlink.close();
} catch (Exception swallow) { }
}
}
}
protected void closeDownlinkKey(Value key) {
HashTrieMap> oldDownlinks;
HashTrieMap> newDownlinks;
do {
oldDownlinks = this.downlinks;
newDownlinks = oldDownlinks.removed(key);
} while (oldDownlinks != newDownlinks && !DOWNLINKS.compareAndSet(this, oldDownlinks, newDownlinks));
if (oldDownlinks != newDownlinks) {
final JoinMapLaneDownlink, ?> downlink = oldDownlinks.get(key);
try {
downlink.close();
} catch (Exception swallow) { }
}
}
@Override
protected void didOpenLaneView(JoinMapLaneView, ?, ?> view) {
view.setLaneBinding(this);
}
@Override
public void onCommand(CommandMessage message) {
final Value payload = message.body();
final String tag = payload.tag();
if ("update".equals(tag)) {
final Value header = payload.header("update");
final Value key = header.get("key");
final Value value = payload.body();
new JoinMapLaneRelayUpdate(this, null, message, key, value).run();
} else if ("remove".equals(tag)) {
final Value header = payload.header("remove");
final Value key = header.get("key");
new JoinMapLaneRelayRemove(this, null, message, key).run();
} else if ("clear".equals(tag)) {
new JoinMapLaneRelayClear(this, null, message).run();
}
}
protected void cueDownKey(Value key) {
FingerTrieSeq uplinks;
do {
uplinks = this.uplinks;
for (int i = 0, n = uplinks.size(); i < n; i += 1) {
uplinks.get(i).cueDownKey(key);
}
} while (uplinks != this.uplinks);
}
public final boolean isResident() {
return (this.flags & RESIDENT) != 0;
}
public JoinMapLaneModel isResident(boolean isResident) {
if (this.data != null) {
this.data.isResident(isResident);
}
if (this.linkData != null) {
this.linkData.isResident(isResident);
}
if (isResident) {
this.flags |= RESIDENT;
} else {
this.flags &= ~RESIDENT;
}
final Object views = this.views;
if (views instanceof ValueLaneView>) {
((ValueLaneView>) views).didSetResident(isResident);
} else if (views instanceof LaneView[]) {
final LaneView[] viewArray = (LaneView[]) views;
for (int i = 0, n = viewArray.length; i < n; i += 1) {
((ValueLaneView>) viewArray[i]).didSetResident(isResident);
}
}
return this;
}
public final boolean isTransient() {
return (this.flags & TRANSIENT) != 0;
}
public JoinMapLaneModel isTransient(boolean isTransient) {
if (this.data != null) {
this.data.isTransient(isTransient);
}
if (this.linkData != null) {
this.linkData.isTransient(isTransient);
}
if (isTransient) {
this.flags |= TRANSIENT;
} else {
this.flags &= ~TRANSIENT;
}
final Object views = this.views;
if (views instanceof ValueLaneView>) {
((ValueLaneView>) views).didSetTransient(isTransient);
} else if (views instanceof LaneView[]) {
final LaneView[] viewArray = (LaneView[]) views;
for (int i = 0, n = viewArray.length; i < n; i += 1) {
((ValueLaneView>) viewArray[i]).didSetTransient(isTransient);
}
}
return this;
}
public final boolean isSigned() {
return (this.flags & SIGNED) != 0;
}
public JoinMapLaneModel isSigned(boolean isSigned) {
if (isSigned) {
this.flags |= SIGNED;
} else {
this.flags &= ~SIGNED;
}
final Object views = this.views;
if (views instanceof JoinMapLaneView, ?, ?>) {
((JoinMapLaneView, ?, ?>) views).didSetSigned(isSigned);
} else if (views instanceof LaneView[]) {
final LaneView[] viewArray = (LaneView[]) views;
for (int i = 0, n = viewArray.length; i < n; i += 1) {
((JoinMapLaneView, ?, ?>) viewArray[i]).didSetSigned(isSigned);
}
}
return this;
}
public Value get(Object key) {
if (key != null) {
return this.data.get(key);
} else {
return Value.absent();
}
}
public JoinMapLaneDownlink, ?> getDownlink(Object key) {
return this.downlinks.get(key);
}
public void put(JoinMapLaneDownlink, ?> downlink, Value key, Value newValue) {
final JoinMapLaneRelayUpdate relay = new JoinMapLaneRelayUpdate(this, downlink, key, newValue);
//relay.stage = stage();
relay.run();
}
@SuppressWarnings("unchecked")
public V put(JoinMapLaneView, K, V> view, K keyObject, V newObject) {
final Form keyForm = view.keyForm;
final Form valueForm = view.valueForm;
final Value key = keyForm.mold(keyObject).toValue();
final Value newValue = valueForm.mold(newObject).toValue();
final JoinMapLaneRelayUpdate relay = new JoinMapLaneRelayUpdate(this, null, key, newValue);
relay.keyForm = (Form
© 2015 - 2025 Weber Informatics LLC | Privacy Policy