Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// Copyright 2015-2024 Nstream, 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.system.lane;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import swim.api.LaneException;
import swim.api.Link;
import swim.api.data.MapData;
import swim.api.downlink.ValueDownlink;
import swim.collections.FingerTrieSeq;
import swim.collections.HashTrieMap;
import swim.concurrent.Cont;
import swim.concurrent.Stage;
import swim.structure.Attr;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
import swim.system.LaneModel;
import swim.system.LaneRelay;
import swim.system.LaneView;
import swim.system.Push;
import swim.system.WarpBinding;
import swim.system.warp.WarpLaneModel;
import swim.uri.Uri;
import swim.warp.CommandMessage;
public class JoinValueLaneModel extends WarpLaneModel, JoinValueLaneUplink> {
protected int flags;
protected MapData data;
protected volatile HashTrieMap> downlinks;
JoinValueLaneModel(int flags) {
this.flags = flags;
this.data = null;
this.downlinks = HashTrieMap.empty();
}
public JoinValueLaneModel() {
this(0);
}
@Override
public String laneType() {
return "map";
}
@Override
protected JoinValueLaneUplink createWarpUplink(WarpBinding link) {
return new JoinValueLaneUplink(this, link, this.createUplinkAddress(link));
}
protected void openDownlinks() {
for (Map.Entry entry : this.data) {
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 JoinValueLaneDownlink(this.laneContext, stage(), this, key,
this.laneContext.meshUri(), this.laneContext.hostUri(),
nodeUri, laneUri, prio, rate, body,
Form.forValue()).openDownlink();
}
}
protected void downlink(Value key, JoinValueLaneDownlink> downlink) {
Value value = this.data.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.create(2).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 = Attr.of("downlink", header).concat(value);
}
this.data.put(key, value);
}
new JoinValueLaneRelayDownlink(this, key, downlink).run();
}
protected void openDownlink(Value key, JoinValueLaneDownlink> downlink) {
downlink.openDownlink(); // Open before CAS
do {
final HashTrieMap> oldDownlinks = JoinValueLaneModel.DOWNLINKS.get(this);
final HashTrieMap> newDownlinks = oldDownlinks.updated(key, downlink);
if (oldDownlinks != newDownlinks) {
if (JoinValueLaneModel.DOWNLINKS.compareAndSet(this, oldDownlinks, newDownlinks)) {
final JoinValueLaneDownlink> oldDownlink = oldDownlinks.get(key);
if (oldDownlink != null) {
try {
oldDownlink.close();
} catch (Exception swallow) {
}
}
break;
}
} else {
break;
}
} while (true);
}
protected void closeDownlinks() {
do {
final HashTrieMap> oldDownlinks = JoinValueLaneModel.DOWNLINKS.get(this);
if (!oldDownlinks.isEmpty()) {
final HashTrieMap> newDownlinks = HashTrieMap.empty();
if (JoinValueLaneModel.DOWNLINKS.compareAndSet(this, oldDownlinks, newDownlinks)) {
for (JoinValueLaneDownlink> downlink : oldDownlinks.values()) {
try {
downlink.close();
} catch (Exception swallow) {
}
}
break;
}
} else {
break;
}
} while (true);
}
protected void closeDownlinkKey(Value key) {
do {
final HashTrieMap> oldDownlinks = JoinValueLaneModel.DOWNLINKS.get(this);
final HashTrieMap> newDownlinks = oldDownlinks.removed(key);
if (oldDownlinks != newDownlinks) {
if (JoinValueLaneModel.DOWNLINKS.compareAndSet(this, oldDownlinks, newDownlinks)) {
final JoinValueLaneDownlink> downlink = oldDownlinks.get(key);
try {
downlink.close();
} catch (Exception swallow) {
}
break;
}
} else {
break;
}
} while (true);
}
@Override
protected void didOpenLaneView(JoinValueLaneView, ?> view) {
view.setLaneBinding(this);
}
@Override
public void onCommand(Push push) {
this.awaitStart();
final CommandMessage message = push.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 Item head = this.data.get(key).head();
final Value newValue = payload.body();
new JoinValueLaneRelayUpdate(this, message, push.cont(), key, newValue).run();
} else if ("remove".equals(tag)) {
final Value header = payload.header("remove");
final Value key = header.get("key");
new JoinValueLaneRelayRemove(this, message, push.cont(), key).run();
} else if ("clear".equals(tag)) {
new JoinValueLaneRelayClear(this, message, push.cont()).run();
} else {
push.trap(new LaneException("unknown subcommand: " + payload));
}
}
@SuppressWarnings("unchecked")
protected void cueDownKey(Value key) {
FingerTrieSeq uplinks;
do {
uplinks = (FingerTrieSeq) LaneModel.UPLINKS.get(this);
for (int i = 0, n = uplinks.size(); i < n; i += 1) {
uplinks.get(i).cueDownKey(key);
}
} while (uplinks != LaneModel.UPLINKS.get(this));
}
public final boolean isResident() {
return (this.flags & JoinValueLaneModel.RESIDENT) != 0;
}
public JoinValueLaneModel isResident(boolean isResident) {
if (this.data != null) {
this.data.isResident(isResident);
}
if (isResident) {
this.flags |= JoinValueLaneModel.RESIDENT;
} else {
this.flags &= ~JoinValueLaneModel.RESIDENT;
}
final Object views = LaneModel.VIEWS.get(this);
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 & JoinValueLaneModel.TRANSIENT) != 0;
}
public JoinValueLaneModel isTransient(boolean isTransient) {
if (this.data != null) {
this.data.isTransient(isTransient);
}
if (isTransient) {
this.flags |= JoinValueLaneModel.TRANSIENT;
} else {
this.flags &= ~JoinValueLaneModel.TRANSIENT;
}
final Object views = LaneModel.VIEWS.get(this);
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 Value get(Object key) {
if (key != null) {
Value value = this.data.get(key);
if ("downlink".equals(value.tag())) {
value = value.body();
}
return value;
} else {
return Value.absent();
}
}
public JoinValueLaneDownlink> getDownlink(Object key) {
return JoinValueLaneModel.DOWNLINKS.get(this).get(key);
}
public void put(JoinValueLaneDownlink> downlink, Value key, Value newValue) {
final JoinValueLaneRelayUpdate relay = new JoinValueLaneRelayUpdate(this, downlink, key, newValue);
relay.run();
}
@SuppressWarnings("unchecked")
public V put(JoinValueLaneView 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 JoinValueLaneRelayUpdate relay = new JoinValueLaneRelayUpdate(this, this.stage(), key, newValue);
relay.keyForm = (Form