Examples of IBigArray


Examples of com.leansoft.bigqueue.IBigArray

    // 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

Examples of com.leansoft.bigqueue.IBigArray

   
    bigArray.close();
  }
 
  static void binarySearch() throws IOException {
    IBigArray bigArray = new BigArrayImpl(MergeSortHelper.SAMPLE_DIR, "sample_array");
    // randomly get some items which can be found later
    int searchLoopLimit = 10000;
    List<String> randomKeys = new ArrayList<String>();
    Random random = new Random();
    for(int i = 0; i < searchLoopLimit / 2; i++) {
      long randomIndex = (long)(random.nextDouble() * maxNumOfItems);
      String randomKey = new String(bigArray.get(randomIndex));
      randomKeys.add(randomKey);
    }
    bigArray.close(); // close to release cache, otherwise the performance of binary search will not be real.
   
    bigArray = new BigArrayImpl(MergeSortHelper.SAMPLE_DIR, "sample_array");
   
    MergeSortHelper.output("Single thread binary search begin ...");
    int foundCount = 0;
    long start = System.currentTimeMillis();
    for(int i = 0; i < searchLoopLimit; i++) {
      String randomKey = null;
      if (i < searchLoopLimit / 2) {
        randomKey = randomKeys.get(i);
      } else {
        randomKey = MergeSortHelper.genRandomString(itemSize);
      }
      long result = BinarySearchHelper.binarySearch(bigArray, randomKey, 0, bigArray.size() - 1);
      if (result != BinarySearchHelper.NOT_FOUND) {
        foundCount++;
      }
    }
    long end = System.currentTimeMillis();
    MergeSortHelper.output("Binary search finished.");
    MergeSortHelper.output("Time to search " + searchLoopLimit + " items in the big array is " + (end - start) + " ms.");
    MergeSortHelper.output("Average search time is " + (double)(end - start) / searchLoopLimit + "ms.");
    MergeSortHelper.output("Found count is " + foundCount);
   
    // have done with the big array, empty it and delete back data files to save disk space
    bigArray.removeAll();
    bigArray.close();
  }
View Full Code Here

Examples of com.leansoft.bigqueue.IBigArray

*/
public class BigArrayTutorial {

  @Test
  public void demo() throws IOException {
    IBigArray bigArray = null;
    try {
      // create a new big array
      bigArray = new BigArrayImpl("d:/bigarray/tutorial", "demo");
      // ensure the new big array is empty
      assertNotNull(bigArray);
      assertTrue(bigArray.isEmpty());
      assertTrue(bigArray.size() == 0);
      assertTrue(bigArray.getHeadIndex() == 0);
      assertTrue(bigArray.getTailIndex() == 0);
     
      // append some items into the array
      for(int i = 0; i < 10; i++) {
        String item = String.valueOf(i);
        long index = bigArray.append(item.getBytes());
        assertTrue(i == index);
      }
      assertTrue(bigArray.size() == 10);
      assertTrue(bigArray.getHeadIndex() == 10);
      assertTrue(bigArray.getTailIndex() == 0);
     
      // randomly read items in the array
      String item0 = new String(bigArray.get(0));
      assertEquals(String.valueOf(0), item0);
     
      String item3 = new String(bigArray.get(3));
      assertEquals(String.valueOf(3), item3);
     
      String item9 = new String(bigArray.get(9));
      assertEquals(String.valueOf(9), item9);
     
      // empty the big array
      bigArray.removeAll();
      assertTrue(bigArray.isEmpty());
    } finally {
      bigArray.close();
    }
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.