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

com.caucho.v5.amp.stub.StubContainerBase Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
/*
 * Copyright (c) 1998-2015 Caucho Technology -- all rights reserved
 *
 * This file is part of Baratine(TM)
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * Baratine is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Baratine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
 * of NON-INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Baratine; if not, write to the
 *
 *   Free Software Foundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307  USA
 *
 * @author Scott Ferguson
 */

package com.caucho.v5.amp.stub;

import io.baratine.service.Result;
import io.baratine.service.ServiceRef;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;

import com.caucho.v5.amp.ServiceRefAmp;
import com.caucho.v5.amp.spi.StubContainerAmp;
import com.caucho.v5.util.LruCache;
import com.caucho.v5.util.LruCache.Entry;

/**
 * Baratine actor container for children.
 */
public class StubContainerBase implements StubContainerAmp
{
  private static final Logger log
    = Logger.getLogger(StubContainerBase.class.getName());
  
  private static final int SAVE_MAX = 8 * 1024;
  private static final int MAX = 64 * 1024;
  
  private String _path;
  
  private LruCache _lruCache;
  
  private final ArrayList _modifiedList = new ArrayList<>();
  private final ArrayList _modifiedWorkList = new ArrayList<>();
  
  private boolean _isActive;
  private AtomicBoolean _isSaveRequested = new AtomicBoolean();
  
  public StubContainerBase(String path)
  {
    _path = path;
    
    _lruCache = new LruCache(64);
  }
  
  @Override
  public boolean isJournalReplay()
  {
    return false;
  }

  @Override
  public String getChildPath(String path)
  {
    if (_path != null) {
      return path.substring(_path.length());
    }
    else {
      return path;
    }
  }
  
  @Override
  public void onActive()
  {
    _isActive = true;
    
    ArrayList children = new ArrayList<>(_modifiedList);
    //_modifiedList.clear();
    
    ServiceRefAmp serviceRef = (ServiceRefAmp) ServiceRef.current();
    
    for (StubAmp actor : children) {
      actor.loadState().onActive(actor, serviceRef.inbox());
    }
  }
  
  @Override
  public ServiceRef addService(String path, ServiceRef serviceRef)
  {
    synchronized (this) {
      LruCache lruCache = getLruCache();
      
      return lruCache.putIfNew(path, serviceRef);
    }
  }
  
  private LruCache getLruCache()
  {
    LruCache lruCache = _lruCache;
    
    if (lruCache.getCapacity() < MAX && _lruCache.size() >= 32) {
      LruCache lruCacheNew = new LruCache<>(MAX);
      
      Iterator> iter = lruCache.iterator();
      while (iter.hasNext()) {
        Entry entry = iter.next();
        
        lruCacheNew.put(entry.getKey(), entry.getValue());
      }

      lruCache = _lruCache = lruCacheNew;
    }
    
    return lruCache;
  }

  @Override
  public ServiceRef getService(String path)
  {
    synchronized (_lruCache) {
      return _lruCache.get(path);
    }
  }
  
  @Override
  public void addModifiedChild(StubAmp actor)
  {
    Objects.requireNonNull(actor);
    
    _modifiedList.add(actor);
    
    if (_isActive
        && _modifiedList.size() > SAVE_MAX
        && _isSaveRequested.compareAndSet(false, true)) {
      ServiceRef serviceRef = ServiceRef.current();
      
      serviceRef.save(Result.ignore());
    }
  }
  
  @Override
  public boolean isModifiedChild(StubAmp actor)
  {
    Objects.requireNonNull(actor);
    
    return _modifiedList.contains(actor);
  }
  
  @Override
  public void afterBatch(StubAmp actor)
  {
    onSave(null);
  }
  
  protected boolean isSaveRequired()
  {
    return _modifiedList.size() > 0;
  }

  @Override
  public void onSave(SaveResult saveResult)
  {
    _isSaveRequested.compareAndSet(true, false);
    
    if (_modifiedList.size() == 0) {
      return;
    }

    _modifiedWorkList.clear();
    _modifiedWorkList.addAll(_modifiedList);
    _modifiedList.clear();
      
    for (StubAmp actor : _modifiedWorkList) {
      if (saveResult != null) {
        actor.onSave(saveResult.addBean());
      }
      else {
        actor.onSave(Result.ignore());
      }
    }
  }

  @Override
  public void onLruModified(ServiceRefAmp serviceRef)
  {
    if (_isSaveRequested.compareAndSet(false, true)) {
      ServiceRefAmp parentRef = serviceRef.inbox().serviceRef();
      
      parentRef.save(Result.ignore());
    }
  }
  
  @Override
  public String toString()
  {
    return getClass().getSimpleName() + "[]";
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy