Package org.xtreemfs.babudb.index.reader

Examples of org.xtreemfs.babudb.index.reader.DiskIndex


        // write the map to a disk index
        DiskIndexWriter index = new DiskIndexWriter(PATH1, MAX_BLOCK_ENTRIES, COMPRESSED, MAX_BLOCK_FILE_SIZE);
        index.writeIndex(getBufferIterator(map.entrySet().iterator()));
       
        // read the disk index
        DiskIndex diskIndex = new DiskIndex(PATH1, DefaultByteRangeComparator.getInstance(), COMPRESSED,
            MMAPED);
       
        {
            // look up the complete list of elements
            Iterator<Entry<byte[], byte[]>> mapIt = map.entrySet().iterator();
            Iterator<Entry<byte[], byte[]>> indexIt = diskIndex.rangeLookup(null, null, true);
           
            while (indexIt.hasNext() || mapIt.hasNext()) {
               
                Entry<byte[], byte[]> next = indexIt.next();
                String indexKey = new String(next.getKey());
                String indexValue = new String(next.getValue());
               
                Entry<byte[], byte[]> next2 = mapIt.next();
                String mapKey = new String(next2.getKey());
                String mapValue = new String(next2.getValue());
               
                assertEquals(mapKey, indexKey);
                assertEquals(mapValue, indexValue);
            }
        }
       
        {
            // look up a range of elements
            byte[] from = "e".getBytes();
            byte[] to = "f".getBytes();
           
            Iterator<Entry<byte[], byte[]>> mapIt = map.subMap(from, to).entrySet().iterator();
            Iterator<Entry<byte[], byte[]>> indexIt = diskIndex.rangeLookup(from, to, true);
           
            while (indexIt.hasNext() || mapIt.hasNext()) {
               
                Entry<byte[], byte[]> next = indexIt.next();
                String indexKey = new String(next.getKey());
                String indexValue = new String(next.getValue());
               
                Entry<byte[], byte[]> next2 = mapIt.next();
                String mapKey = new String(next2.getKey());
                String mapValue = new String(next2.getValue());
               
                assertEquals(mapKey, indexKey);
                assertEquals(mapValue, indexValue);
            }
        }
       
        {
            // look up another range of elements
            byte[] from = "fd".getBytes();
            byte[] to = "sa".getBytes();
           
            Iterator<Entry<byte[], byte[]>> mapIt = map.subMap(from, to).entrySet().iterator();
            Iterator<Entry<byte[], byte[]>> indexIt = diskIndex.rangeLookup(from, to, true);
           
            while (indexIt.hasNext() || mapIt.hasNext()) {
               
                Entry<byte[], byte[]> next = indexIt.next();
                String indexKey = new String(next.getKey());
                String indexValue = new String(next.getValue());
               
                Entry<byte[], byte[]> next2 = mapIt.next();
                String mapKey = new String(next2.getKey());
                String mapValue = new String(next2.getValue());
               
                assertEquals(mapKey, indexKey);
                assertEquals(mapValue, indexValue);
            }
        }
       
        diskIndex.destroy();
       
    }
View Full Code Here


        // write the map to a disk index
        DiskIndexWriter index = new DiskIndexWriter(PATH1, MAX_BLOCK_ENTRIES, COMPRESSED, MAX_BLOCK_FILE_SIZE);
        index.writeIndex(getBufferIterator(map.entrySet().iterator()));
       
        // read the disk index
        DiskIndex diskIndex = new DiskIndex(PATH1, DefaultByteRangeComparator.getInstance(), COMPRESSED,
            MMAPED);
       
        {
            // look up the complete list of elements
            Iterator<Entry<byte[], byte[]>> mapIt = map.descendingMap().entrySet().iterator();
            Iterator<Entry<byte[], byte[]>> indexIt = diskIndex.rangeLookup(null, null, false);
           
            while (indexIt.hasNext() || mapIt.hasNext()) {
               
                Entry<byte[], byte[]> next = indexIt.next();
                String indexKey = new String(next.getKey());
                String indexValue = new String(next.getValue());
               
                Entry<byte[], byte[]> next2 = mapIt.next();
                String mapKey = new String(next2.getKey());
                String mapValue = new String(next2.getValue());
               
                assertEquals(mapKey, indexKey);
                assertEquals(mapValue, indexValue);
            }
        }
       
        {
            // look up a range of elements
            byte[] from = "e".getBytes();
            byte[] to = "f".getBytes();
           
            Iterator<Entry<byte[], byte[]>> mapIt = map.descendingMap().subMap(to, from).entrySet()
                    .iterator();
            Iterator<Entry<byte[], byte[]>> indexIt = diskIndex.rangeLookup(from, to, false);
           
            while (mapIt.hasNext())
                System.out.println(new String(mapIt.next().getKey()));
           
            System.out.println(" --------- ");
           
            while (indexIt.hasNext())
                System.out.println(new String(indexIt.next().getKey()));
           
            while (indexIt.hasNext() || mapIt.hasNext()) {
               
                assertTrue((indexIt.hasNext() && mapIt.hasNext()) || (!indexIt.hasNext() && !mapIt.hasNext()));
               
                Entry<byte[], byte[]> next = indexIt.next();
                String indexKey = new String(next.getKey());
                String indexValue = new String(next.getValue());
               
                Entry<byte[], byte[]> next2 = mapIt.next();
                String mapKey = new String(next2.getKey());
                String mapValue = new String(next2.getValue());
               
                assertEquals(mapKey, indexKey);
                assertEquals(mapValue, indexValue);
            }
        }
       
        {
            // look up another range of elements
            byte[] from = "fd".getBytes();
            byte[] to = "sa".getBytes();
           
            Iterator<Entry<byte[], byte[]>> mapIt = map.descendingMap().subMap(to, from).entrySet()
                    .iterator();
            Iterator<Entry<byte[], byte[]>> indexIt = diskIndex.rangeLookup(from, to, false);
           
            while (indexIt.hasNext() || mapIt.hasNext()) {
               
                Entry<byte[], byte[]> next = indexIt.next();
                String indexKey = new String(next.getKey());
                String indexValue = new String(next.getValue());
               
                Entry<byte[], byte[]> next2 = mapIt.next();
                String mapKey = new String(next2.getKey());
                String mapValue = new String(next2.getValue());
               
                assertEquals(mapKey, indexKey);
                assertEquals(mapValue, indexValue);
            }
        }
       
        diskIndex.destroy();
       
    }
View Full Code Here

            } else {
              index.writeIndex(DataGenerator.randomIterator(lookupHits, size, hitrate, minStrLen, maxStrLen, minChar, maxChar));
            }
        } else {
          //   populate the lookup-hits table
          DiskIndex diskIndexTmp = new DiskIndex(path, new DefaultByteRangeComparator(), compress, mmap);
          Iterator<Entry<byte[], byte[]>> itTmp = diskIndexTmp.rangeLookup(null, null, true);
          while(itTmp.hasNext()) {
                if(generator.nextInt() % hitrate == 0)
                  lookupHits.add(itTmp.next().getKey());
          }
          diskIndexTmp.destroy();
        }
       
        // do a warm-up phase to trick the JVM
        int warmups = 5;
       
        while(warmups-- > 0) {
          int readEntries = 10000;         
          //   read the disk index
          DiskIndex diskIndex = new DiskIndex(path, new DefaultByteRangeComparator(), compress, mmap);
          Iterator<Entry<byte[], byte[]>> it = diskIndex.rangeLookup(null, null, true);
          while(it.hasNext() && readEntries-- > 0) it.next();
          diskIndex.destroy();
        }

      // clear caches...
      Runtime.getRuntime().exec("/bin/sync");
      Runtime.getRuntime().exec("/bin/echo 3 > /proc/sys/vm/drop_caches");
   
      // run garbage collection to remove any existing mmap:ed pages
      Runtime.getRuntime().gc();
     
      //   read the disk index
      DiskIndex diskIndex = new DiskIndex(path, new DefaultByteRangeComparator(), compress, mmap);
      Iterator<Entry<byte[], byte[]>> it = diskIndex.rangeLookup(null, null, true);
   
      /* iterate over all data in the disk index to measure the prefix lookup throughput */
      long iterStart = System.currentTimeMillis();
      while(it.hasNext()) it.next();
      long iterTime = System.currentTimeMillis() - iterStart;

        // Iterator<Entry<ReusableBuffer, ReusableBuffer>> it =
        // diskIndex.rangeLookup(null, null);
        // while (it.hasNext())
        // System.out.println(new String(it.next().getKey().array()));
       
        if(verbose)
          System.out.println("performing " + lookups + " random lookups ..." + " hits size: " + lookupHits.size());
       
        // look up each element
        int hits = 0;
        long sumLookups = 0;
       
        Collections.shuffle(lookupHits);
       
        /* random lookups, this should put random blocks into memory */
        for (int i = 0; i < lookups; i++) {
            byte[] key;
           
            /* pick a random element that is in the index according to the given hitrate */
          if(generator.nextInt() % hitrate == 0) {
            key = lookupHits.get(Math.abs(generator.nextInt()) % lookupHits.size());
          } else {
            key = DataGenerator.createRandomString(minChar, maxChar, maxStrLen+1, maxStrLen*2).getBytes();
          }
         
            long t0 = System.currentTimeMillis();
            byte[] result = diskIndex.lookup(key);
            sumLookups += System.currentTimeMillis() - t0;
            if (result != null)
                hits++;
           
            //if (i % 100000 == 0 && verbose)
            //    System.out.println(i);
        }
       
        /* random scans */
        long scanTotal = 0;
        for(int i=0; i < scans; i++) {
          byte[] from;
          byte[] to;
          int firstIndex = Math.abs(generator.nextInt()) % lookupHits.size();
         
          from = lookupHits.get(firstIndex);
          to = lookupHits.get(firstIndex + (Math.abs(generator.nextInt()) % (lookupHits.size() - firstIndex)));
         
            Iterator<Entry<byte[], byte[]>> tmpIt = diskIndex.rangeLookup(from, to, true);
           
            /* iterate over all data returned by the range scan */
            long scanStart = System.currentTimeMillis();
            while(tmpIt.hasNext()) tmpIt.next();
            scanTotal += System.currentTimeMillis() - scanStart;
        }
       
       
        System.out.print(path + ", ");
        System.out.print(size + ", ");
        System.out.print(lookups + ", ");
        System.out.print(hits + ", ");
        System.out.print(sumLookups + ", ");
        /* number of scans */
        System.out.print(scans + ", ");
        /* total time for scans */
        System.out.print(scanTotal + ", ");
        // lookups/s (lookup throughput)
        System.out.print((int) Math.ceil(((double) lookups / (((double) sumLookups) / 1000.0))) + ", ");
        System.out.print((int) Math.ceil((double) iterTime) + ", ");
        // entries/s (scan throughput)
        System.out.println((int) Math.ceil(((double) size / (((double) iterTime) / 1000.0))));
       
        diskIndex.destroy();
    }       
View Full Code Here

TOP

Related Classes of org.xtreemfs.babudb.index.reader.DiskIndex

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.