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

nstream.reflect.MetaNode Maven / Gradle / Ivy

There is a newer version: 4.13.21
Show newest version
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://redis.com/legal/rsalv2-agreement/
//
// 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 nstream.reflect;

import java.util.Iterator;
import nstream.reflect.agent.MetaNodeAgent;
import nstream.reflect.model.DataStats;
import nstream.reflect.model.LinkStats;
import nstream.reflect.model.LogEntry;
import nstream.reflect.model.ProcessStats;
import nstream.reflect.model.StoreStats;
import nstream.reflect.model.SystemStats;
import swim.api.Downlink;
import swim.store.DataBinding;
import swim.system.LaneAddress;
import swim.system.LaneBinding;
import swim.system.LinkBinding;
import swim.system.NodeBinding;
import swim.system.NodeProxy;
import swim.system.Push;
import swim.system.WarpBinding;
import swim.uri.Uri;
import swim.uri.UriPath;
import swim.warp.Envelope;

public class MetaNode extends NodeProxy implements MetaRouter {
  final MetaHost parent;
  final MetaNodeAgent agent;

  public MetaNode(MetaHost parent, NodeBinding nodeBinding) {
    super(nodeBinding);
    this.parent = parent;
    this.agent = new MetaNodeAgent(this);
  }

  @Override
  public MetaHost metaParent() {
    return this.parent;
  }

  @Override
  public MetaNodeAgent metaAgent() {
    return this.agent;
  }

  @Override
  public LinkStats linkStats() {
    return this.agent.linkStats();
  }

  @Override
  public DataStats dataStats() {
    final DataStats stats = new DataStats();
    final Iterator dataBindings = store().dataBindings();
    while (dataBindings.hasNext()) {
      final DataBinding data = dataBindings.next();
      stats.dataSize += data.dataSize();
      stats.treeCount += 1L;
      //stats.leafCount += data.leafCount();
    }
    return stats;
  }

  @Override
  public StoreStats storeStats() {
    final StoreStats stats = new StoreStats();
    //stats.dataSize = getDataSize();
    //stats.storeSize = getStoreSize();
    //stats.treeCount = getTreeCount();
    //stats.leafCount = getLeafCount();
    return stats;
  }

  @Override
  public ProcessStats processStats() {
    return this.parent.processStats();
  }

  @Override
  public SystemStats systemStats() {
    return this.parent.systemStats();
  }

  @Override
  public LaneBinding injectLane(LaneAddress laneAddress, LaneBinding lane) {
    return new MetaLane(this, super.injectLane(laneAddress, lane));
  }

  @Override
  public LinkBinding bindDownlink(Downlink downlink) {
    final LinkBinding link = super.bindDownlink(downlink);
    if (link instanceof WarpBinding) {
      final MetaWarpDownlink metaLink = link.linkWrapper().unwrapLink(MetaWarpDownlink.class);
      if (metaLink != null) {
        metaLink.parent = this;
      }
    }
    return link;
  }

  @Override
  public void openDownlink(LinkBinding link) {
    if (link instanceof WarpBinding) {
      if (link.linkWrapper().unwrapLink(MetaWarpDownlink.class) == null) {
        link = new MetaWarpDownlink(this, (WarpBinding) link);
      }
    }
    super.openDownlink(link);
  }

  @Override
  public void pushDown(Push push) {
    super.pushDown(push);
    final Object message = push.message();
    if (message instanceof Envelope) {
      this.agent.didPushDown((Envelope) message);
    }
  }

  @Override
  public void trace(Object message) {
    this.agent.didLogTrace(LogEntry.trace(nodeUri(), message));
  }

  @Override
  public void debug(Object message) {
    this.agent.didLogDebug(LogEntry.debug(nodeUri(), message));
  }

  @Override
  public void info(Object message) {
    this.agent.didLogInfo(LogEntry.info(nodeUri(), message));
  }

  @Override
  public void warn(Object message) {
    this.agent.didLogWarn(LogEntry.warn(nodeUri(), message));
  }

  @Override
  public void error(Object message) {
    this.agent.didLogError(LogEntry.error(nodeUri(), message));
  }

  @Override
  public void didFail(Throwable error) {
    try {
      this.agent.didLogFail(LogEntry.fail(nodeUri(), error));
    } finally {
      super.didFail(error);
    }
  }

  //@Override
  //public void didCommit(long commitSize) {
  //  super.didCommit(commitSize);
  //  this.agent.storeDidCommit(commitSize);
  //}

  //@Override
  //public void didCompact() {
  //  super.didCompact();
  //  this.agent.storeDidCompact();
  //}

  @Override
  public void didOpen() {
    this.agent.didOpenNode();
  }

  @Override
  public void willLoad() {
    super.willLoad();
    this.agent.load();
  }

  @Override
  public void didClose() {
    this.agent.didCloseNode();
    this.agent.close();
    super.didClose();
  }

  public NodeBinding resolveMetaNode(UriPath nodePath) {
    if (nodePath.isEmpty()) {
      return this.agent;
    } else if ("lane".equals(nodePath.head())) {
      nodePath = nodePath.tail(); // drop lane
      if (!nodePath.isEmpty()) {
        nodePath = nodePath.tail(); // drop /
      }
      return resolveMetaLane(nodePath);
    }
    return null;
  }

  public NodeBinding resolveMetaLane(UriPath nodePath) {
    if (nodePath.isAbsolute()) {
      return null;
    }
    final Uri laneUri = nodePath.isEmpty() ? Uri.empty() : Uri.parse(nodePath.head());
    final LaneBinding laneBinding = getLane(laneUri);
    if (laneBinding != null) {
      final MetaLane metaLane = laneBinding.laneWrapper().unwrapLane(MetaLane.class);
      if (metaLane != null) {
        if (!nodePath.isEmpty()) {
          nodePath = nodePath.tail(); // drop laneUri
          if (!nodePath.isEmpty()) {
            nodePath = nodePath.tail(); // drop /
          }
        }
        return metaLane.resolveMetaLane(nodePath);
      }
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy