Package com.leansoft.bigqueue

Examples of com.leansoft.bigqueue.IBigQueue


    QueueResponse resp = new QueueResponse();
    if (topic == null) {
      resp.setResultCode(ResultCode.FAILURE);
      return resp;
    }
    IBigQueue bigQueue = queueMap.get(topic);
    if (bigQueue == null) {
      synchronized(lock) {
        bigQueue = queueMap.get(topic);
        if (bigQueue == null) {
          try {
            bigQueue = new BigQueueImpl(queueDir, topic);
            queueMap.put(topic, bigQueue);
          } catch (IOException e) {
            resp.setResultCode(ResultCode.FAILURE);
            return resp;
          }
        }
      }
    }
   
    if (req.getData() != null && req.getData().length > 0) {
      try {
        bigQueue.enqueue(req.getData());
      } catch (IOException e) {
        resp.setResultCode(ResultCode.FAILURE);
        return resp;
      }
      resp.setResultCode(ResultCode.SUCCESS);
View Full Code Here


  @Override
  public QueueResponse dequeue(String topic) throws TException {
    QueueResponse resp = new QueueResponse();
    byte[] data = null;
    IBigQueue bigQueue = queueMap.get(topic);
    if (bigQueue != null) {
      try {
        data = bigQueue.dequeue();
      } catch (IOException e) {
        resp.setResultCode(ResultCode.FAILURE);
        return resp;
      }
    }
View Full Code Here

  @Override
  public QueueResponse peek(String topic) throws TException {
    QueueResponse resp = new QueueResponse();
    byte[] data = null;
    IBigQueue bigQueue = queueMap.get(topic);
    if (bigQueue != null) {
      try {
        data = bigQueue.peek();
      } catch (IOException e) {
        resp.setResultCode(ResultCode.FAILURE);
        return resp;
      }
    }
View Full Code Here

    return resp;
  }

  @Override
  public long getSize(String topic) throws TException {
    IBigQueue bigQueue = queueMap.get(topic);
    if (bigQueue != null) return bigQueue.size();
    return 0L;
  }
View Full Code Here

    return 0L;
  }

  @Override
  public boolean isEmpty(String topic) throws TException {
    IBigQueue bigQueue = queueMap.get(topic);
    if (bigQueue != null) return bigQueue.isEmpty();
    return true;
  }
View Full Code Here

    MergeSortHelper.SAMPLE_DIR = MergeSortHelper.SAMPLE_DIR + "multi_thread";
   
    MergeSortHelper.output("Multi threads sort begin ...");
     
    MergeSortHelper.output("Generating random big queue ...");
        IBigQueue srcBigQueue = new BigQueueImpl(MergeSortHelper.SAMPLE_DIR, "srcq");
       
        Populator[] populators = new Populator[threadNum];
        for(int i = 0; i < threadNum; i++) {
          populators[i] = new Populator(srcBigQueue, maxNumOfItems, itemSize);
          populators[i].start();
        }
        for(int i = 0; i < threadNum; i++) {
          try {
        populators[i].join();
      } catch (InterruptedException e) {
        // ignore
      }
        }
       
        long start = System.currentTimeMillis();
        MergeSortHelper.output("Making queue of sorted queues ...");
      Queue<IBigQueue> queueOfSortedQueues = new LinkedBlockingQueue<IBigQueue>();
        SortedQueueMaker[] sortedQueueMakers = new SortedQueueMaker[threadNum];
        for(int i = 0; i < threadNum; i++) {
          sortedQueueMakers[i] = new SortedQueueMaker(srcBigQueue, maxInMemSortNumOfItems, queueOfSortedQueues);
          sortedQueueMakers[i].start();
        }
        for(int i = 0; i < threadNum; i++) {
          try {
            sortedQueueMakers[i].join();
      } catch (InterruptedException e) {
        // ignore
      }
        }
        srcBigQueue.removeAll();
        srcBigQueue.close();
       
    MergeSortHelper.output("Merging and sorting the queues ...");
        MergeSorter[] mergeSorters = new MergeSorter[threadNum];
        for(int i = 0; i < threadNum; i++) {
          mergeSorters[i] = new MergeSorter(queueOfSortedQueues, maxMergeSortWays);
          mergeSorters[i].start();
        }
        for(int i = 0; i < threadNum; i++) {
          try {
            mergeSorters[i].join();
      } catch (InterruptedException e) {
        // ignore
      }
        }
        long end = System.currentTimeMillis();
        MergeSortHelper.output("Mergesort finished.");
   
    MergeSortHelper.output("Time used to sort " + maxNumOfItems + " string items is " + (end - start) + "ms");
        MergeSortHelper.output("Item size each is " + itemSize + " bytes");
        MergeSortHelper.output("Total size sorted " + (long)(maxNumOfItems * itemSize) / (1024 * 1024) + "MB");
        MergeSortHelper.output("Thread num " + threadNum);
   
    IBigQueue targetSortedQueue = queueOfSortedQueues.poll(); // last and only one is the target sorted queue
   
    MergeSortHelper.output("Validation begin ....");
    long targetSize = targetSortedQueue.size();
    if(targetSize != maxNumOfItems) {
        System.err.println("target queue size is not correct!, target queue size is " + targetSize + " expected queue size is " + maxNumOfItems);
    }
   
    // first sorted item
    String previousItem = new String(targetSortedQueue.dequeue());
   
    // validate the sorted queue
    for(int i = 1; i < targetSize; i++) {
      String item = new String(targetSortedQueue.dequeue());
      if (item.compareTo(previousItem) < 0) {
        System.err.println("target queue is not in sorted order!");
      }
      previousItem = item;
    }
    MergeSortHelper.output("Validation finished.");
   
    // have done with target sorted queue, empty it and delete back data files to save disk space
    targetSortedQueue.removeAll();
    targetSortedQueue.close();
  }
View Full Code Here

        String[] stringArray = list.toArray(new String[0]);
        if (stringArray.length == 0) break;
        // sort in memory
        inMemSort(stringArray);
        String newQueueName = getNextTempQueueName();
        IBigQueue newBigQueue = new BigQueueImpl(SAMPLE_DIR, newQueueName);
        // make sorted sub-queue
        for(String item : stringArray) {
          newBigQueue.enqueue(item.getBytes());
        }
        // put the sub-queue into output queue
        queueOfSortedQueues.offer(newBigQueue);
        newBigQueue.close();
       
        if (data == null) break;
        list.clear();
        long end = System.currentTimeMillis();
        output("Time used to make one sorted queue " + (end - begin) + " ms, maxInMemSortNumOfItems = " + maxInMemSortNumOfItems);
View Full Code Here

   * @return target sorted queue
   * @throws IOException exception thrown if there is IO error during the operation
   */
  public static IBigQueue nWayMergeSort(List<IBigQueue> listOfSortedQueues) throws IOException {
    String newQueueName = getNextTempQueueName();
    IBigQueue targetBigQueue = new BigQueueImpl(SAMPLE_DIR, newQueueName); // target queue
   
    int ways = listOfSortedQueues.size();
    long begin = System.currentTimeMillis();
    while(true) {
      IBigQueue queueWithLowestItem = null;
      String lowestItem = null;
      // find the lowest item in all n way queues
      for(IBigQueue bigQueue : listOfSortedQueues) {
        if (!bigQueue.isEmpty()) {
          if (queueWithLowestItem == null) {
            queueWithLowestItem = bigQueue;
            lowestItem = new String(bigQueue.peek());
          } else {
            String item = new String(bigQueue.peek());
            if (item.compareTo(lowestItem) < 0) {
              queueWithLowestItem = bigQueue;
              lowestItem = item;
            }
          }
        }
      }
      if (queueWithLowestItem == null) break; // all queues are empty
      // extract and put the lowest item into the target queue
      byte[] data = queueWithLowestItem.dequeue();
      targetBigQueue.enqueue(data);
    }
   
    // release the source queues since we have done with them
    for(IBigQueue bigQueue : listOfSortedQueues) {
View Full Code Here

    // repeat until there is only one left in queueOfSortedQueue
    while(queueOfSortedQueues.size() > 1) {
      listOfSortedQueues.clear();
      int count = 0;
      while(!queueOfSortedQueues.isEmpty() && count < maxWays) {
        IBigQueue sortedQueue = queueOfSortedQueues.poll();
        if (sortedQueue != null) { // null only happen in multi-threads case
          listOfSortedQueues.add(sortedQueue);
          count++;
        }
      }
     
      if (listOfSortedQueues.size() > 1) { // grabbed enough to do n way mergesort
        // n way merge sort
        IBigQueue targetSortedQueue = nWayMergeSort(listOfSortedQueues);
        // return the result queue into queueOfSortedQueues
        queueOfSortedQueues.offer(targetSortedQueue);
      } else if (listOfSortedQueues.size() == 1) { // 1 only happen in multi-threads case
        // grabbed one, but can't do n way meragesort, so just return and try again
        queueOfSortedQueues.offer(listOfSortedQueues.remove(0));
View Full Code Here

  }
 
  static void mergeSort() throws IOException {
    MergeSortHelper.output("Single thread sort begin ...");
    MergeSortHelper.output("Generating random big queue ...");
    IBigQueue srcBigQueue = new BigQueueImpl(MergeSortHelper.SAMPLE_DIR, "srcq");
    MergeSortHelper.populateBigQueue(srcBigQueue, maxNumOfItems, itemSize);
   
    long start = System.currentTimeMillis();
    MergeSortHelper.output("Making queue of sorted queues ...");
    Queue<IBigQueue> queueOfSortedQueues = new LinkedList<IBigQueue>();
    MergeSortHelper.makeQueueOfSortedQueues(srcBigQueue, maxInMemSortNumOfItems, queueOfSortedQueues);
    // have done with source queue, empty it and delete back data files to save disk space
    srcBigQueue.removeAll();
    srcBigQueue.close();
   
    MergeSortHelper.output("Merging and sorting the queues ...");
    MergeSortHelper.mergeSort(queueOfSortedQueues, maxMergeSortWays);
    long end = System.currentTimeMillis();
    MergeSortHelper.output("Mergesort finished.");
   
    MergeSortHelper.output("Time used to sort " + maxNumOfItems + " string items is " + (end - start) + "ms");
    MergeSortHelper.output("Item size each is " + itemSize + " bytes");
    MergeSortHelper.output("Total size sorted " + (long)(maxNumOfItems * itemSize) / (1024 * 1024) + "MB");
   
    IBigQueue targetSortedQueue = queueOfSortedQueues.poll(); // last and only one is the target sorted queue
   
    MergeSortHelper.output("Validation begin ....");
    long targetSize = targetSortedQueue.size();
    if(targetSize != maxNumOfItems) {
        System.err.println("target queue size is not correct!, target queue size is " + targetSize + " expected queue size is " + maxNumOfItems);
    }
   
    // first sorted item
    String previousItem = new String(targetSortedQueue.dequeue());
   
    // put sorted items in a big array so we can binary search it later
    // validate the sorted queue at the same time
    IBigArray bigArray = new BigArrayImpl(MergeSortHelper.SAMPLE_DIR, "sample_array");
    bigArray.append(previousItem.getBytes());
    for(int i = 1; i < targetSize; i++) {
      String item = new String(targetSortedQueue.dequeue());
      if (item.compareTo(previousItem) < 0) {
        System.err.println("target queue is not in sorted order!");
      }
      bigArray.append(item.getBytes());
      previousItem = item;
    }
    MergeSortHelper.output("Validation finished.");
   
    // have done with target sorted queue, empty it and delete back data files to save disk space
    targetSortedQueue.removeAll();
    targetSortedQueue.close();
   
    bigArray.close();
  }
View Full Code Here

TOP

Related Classes of com.leansoft.bigqueue.IBigQueue

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.