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

com.caucho.v5.ramp.compute.ComputeServiceImpl 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.ramp.compute;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Spliterator;
import java.util.function.Supplier;

import com.caucho.v5.amp.AmpSystem;
import com.caucho.v5.amp.ServiceRefAmp;
import com.caucho.v5.amp.message.ResultStreamJoin;
import com.caucho.v5.lifecycle.Lifecycle;
import com.caucho.v5.util.L10N;

import io.baratine.function.RunnableSync;
import io.baratine.service.OnLookup;
import io.baratine.service.Result;
import io.baratine.service.Service;
import io.baratine.stream.ResultStream;

/**
 * Timer service.
 */
@Service
public class ComputeServiceImpl
{
  private static final L10N L = new L10N(ComputeServiceImpl.class);
  
  private final Lifecycle _lifecycle = new Lifecycle();
  private TaskManager _taskManager;
  
  public ComputeServiceImpl()
  {
  }
  
  private TaskManager getTaskManager()
  {
    if (_taskManager == null) {
      AmpSystem ampSystem = AmpSystem.getCurrent();
      
      if (ampSystem != null) {
        //_taskManager = ampSystem.getTaskManager();
        throw new UnsupportedOperationException();
      }
      
      if (_taskManager == null) {
        throw new UnsupportedOperationException(L.l("Task manager is not available in this context"));
      }
    }
    
    return _taskManager;
  }
  
  @OnLookup
  public ComputeServiceImpl onLookup(String path)
  {
    return this;
  }
  
  public void run(RunnableSync task, Result result)
  {
    getTaskManager().run(task, result);
  }
  
  public  void execute(Supplier task, Result result)
  {
    getTaskManager().execute(task, result);
  }
  
  /*
  public  void spliterator(Spliterator iter, ResultStream result)
  {
    spliterator(s.spliterator(), result);
  }
  */
  
  public  void spliterator(Spliterator s, ResultStream result)
  {
    ArrayList> splits = new ArrayList<>();
    
    splits.add(s);
    
    int count = 2 * getTaskManager().getWorkers();
    split(splits, count);
    
    int size = splits.size();
    
    if (size == 1) {
      getTaskManager().run(new ComputeTask(splits.get(0), result));
      // _workers.compute(splits.get(0), new ResultSinkWrapper(result));
      return;
    }

    /*
    if (true) {
      throw new UnsupportedOperationException(L.l("fork unsupported for {0}", result));
    }
    */
    
    ServiceRefAmp serviceRef = ServiceRefAmp.current();
    
    //ResultStreamAmp resultAmp = (ResultStreamAmp) result;
    
    ResultStreamJoin join = new ResultStreamJoin(result, serviceRef.inbox());
    for (int i = 0; i < size; i++) {
      //resultSinkFork[i].begin(0);
      //_workers.compute(splits.get(i), new ResultSinkWrapper(resultSinkFork[i]));
      
      getTaskManager().run(new ComputeTask(splits.get(i), join.fork()));
    }
    
    join.ok();
  }
  
  private  void split(ArrayList> splits, int max)
  {
    while (splits.size() < max) {
      Collections.sort(splits, (x,y)->Long.signum(y.estimateSize() - x.estimateSize()));

      Spliterator first = splits.get(0);
      Spliterator next = first.trySplit();
      
      if (next == null) {
        return;
      }
      
      splits.add(next);
    }
  }

  @Override
  public String toString()
  {
    return getClass().getSimpleName() + "[]";
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy