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

nstream.reflect.agent.MetaCellAgent Maven / Gradle / Ivy

The 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.agent;

import nstream.reflect.MetaCell;
import nstream.reflect.model.DataStats;
import swim.api.lane.DemandLane;
import swim.api.lane.function.OnCue;
import swim.api.warp.WarpUplink;
import swim.uri.Uri;

public abstract class MetaCellAgent extends MetaAgent {
  protected final DataStats dataTotal = new DataStats();
  protected final DataStats dataDelta = new DataStats();

  volatile DemandLane dataStats;

  public MetaCellAgent() {
    // nop
  }

  @Override
  public abstract MetaCell meta();

  @Override
  protected void openLanes() {
    super.openLanes();

    final DemandLane dataStats = demandLane()
        .valueForm(DataStats.form())
        .observe(new MetaDataStatsController(this));
    openLane(DATA_STATS_URI, dataStats);
    this.dataStats = dataStats;
  }

  public DataStats dataStats() {
    return this.dataTotal.get();
  }

  public void dataDidLoadPage(long pageSize) {
    this.dataTotal.didLoadPage(pageSize);
    this.dataDelta.didLoadPage(pageSize);
    didUpdateStats();
  }

  public void dataDidChange() {
    didUpdateStats();
  }

  public void setDataStats(DataStats stats) {
    stats.supersede(this.dataTotal, this.dataDelta);
    didUpdateStats();
  }

  public void accumulateDataStats(DataStats stats) {
    this.dataTotal.accumulate(stats);
    this.dataDelta.accumulate(stats);
    didUpdateStats();
  }

  protected void bubbleDataStats() {
    final MetaCell metaParent = meta().metaParent();
    if (metaParent != null) {
      final MetaCellAgent parentAgent = metaParent.metaAgent();
      parentAgent.accumulateDataStats(this.dataDelta.getAndReset());
    }
  }

  protected void cueDataStats() {
    final DemandLane dataStats = this.dataStats;
    if (dataStats != null) {
      dataStats.cue();
    }
  }

  @Override
  public void bubbleStats() {
    super.bubbleStats();
    bubbleDataStats();
  }

  @Override
  public void cueStats() {
    super.cueStats();
    cueDataStats();
  }

  static final Uri DATA_STATS_URI = Uri.parse("dataStats");
}

final class MetaDataStatsController implements OnCue {
  final MetaCellAgent agent;

  MetaDataStatsController(MetaCellAgent agent) {
    this.agent = agent;
  }

  @Override
  public DataStats onCue(WarpUplink uplink) {
    return this.agent.dataStats();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy