Ejemplo n.º 1
0
  private CollisionMap(int initialCapacity, float loadFactor, CharBlockArray labelRepository) {
    this.labelRepository = labelRepository;
    this.loadFactor = loadFactor;
    this.capacity = CompactLabelToOrdinal.determineCapacity(2, initialCapacity);

    this.entries = new Entry[this.capacity];
    this.threshold = (int) (this.capacity * this.loadFactor);
  }
  @Test
  public void testL2O() throws Exception {
    LabelToOrdinal map = new LabelToOrdinalMap();

    CompactLabelToOrdinal compact = new CompactLabelToOrdinal(2000000, 0.15f, 3);

    final int n = atLeast(10 * 1000);
    final int numUniqueValues = 50 * 1000;

    String[] uniqueValues = new String[numUniqueValues];
    byte[] buffer = new byte[50];

    Random random = random();
    for (int i = 0; i < numUniqueValues; ) {
      random.nextBytes(buffer);
      int size = 1 + random.nextInt(buffer.length);

      // This test is turning random bytes into a string,
      // this is asking for trouble.
      CharsetDecoder decoder =
          IOUtils.CHARSET_UTF_8
              .newDecoder()
              .onUnmappableCharacter(CodingErrorAction.REPLACE)
              .onMalformedInput(CodingErrorAction.REPLACE);
      uniqueValues[i] = decoder.decode(ByteBuffer.wrap(buffer, 0, size)).toString();
      // we cannot have empty path components, so eliminate all prefix as well
      // as middle consecutive delimiter chars.
      uniqueValues[i] = uniqueValues[i].replaceAll("/+", "/");
      if (uniqueValues[i].startsWith("/")) {
        uniqueValues[i] = uniqueValues[i].substring(1);
      }
      if (uniqueValues[i].indexOf(CompactLabelToOrdinal.TERMINATOR_CHAR) == -1) {
        i++;
      }
    }

    File tmpDir = TestUtil.getTempDir("testLableToOrdinal");
    File f = new File(tmpDir, "CompactLabelToOrdinalTest.tmp");
    int flushInterval = 10;

    for (int i = 0; i < n; i++) {
      if (i > 0 && i % flushInterval == 0) {
        compact.flush(f);
        compact = CompactLabelToOrdinal.open(f, 0.15f, 3);
        assertTrue(f.delete());
        if (flushInterval < (n / 10)) {
          flushInterval *= 10;
        }
      }

      int index = random.nextInt(numUniqueValues);
      FacetLabel label;
      String s = uniqueValues[index];
      if (s.length() == 0) {
        label = new FacetLabel();
      } else {
        label = new FacetLabel(s.split("/"));
      }

      int ord1 = map.getOrdinal(label);
      int ord2 = compact.getOrdinal(label);

      assertEquals(ord1, ord2);

      if (ord1 == LabelToOrdinal.INVALID_ORDINAL) {
        ord1 = compact.getNextOrdinal();
        map.addLabel(label, ord1);
        compact.addLabel(label, ord1);
      }
    }

    for (int i = 0; i < numUniqueValues; i++) {
      FacetLabel label;
      String s = uniqueValues[i];
      if (s.length() == 0) {
        label = new FacetLabel();
      } else {
        label = new FacetLabel(s.split("/"));
      }
      int ord1 = map.getOrdinal(label);
      int ord2 = compact.getOrdinal(label);
      assertEquals(ord1, ord2);
    }
  }