@Test public void Node() throws UnsupportedEncodingException, NoSuchAlgorithmException { Map<Long, NODE> seen = new HashMap<Long, NODE>(); Set<NODE> nodes = new HashSet<NODE>(); nodes.addAll(Nodes.all); nodes.add(new BID(RDF.type.getValue())); nodes.add(new BID("1")); nodes.add(new LIT(RDF.type.getValue())); nodes.add(new LIT("1", Locale.ENGLISH)); nodes.add(new LIT("1", XSD.stringType)); nodes.add(new LIT("1", XSD.integerType)); nodes.add(new LIT("true", XSD.booleanType)); nodes.add(new LIT("false", XSD.booleanType)); nodes.add(new LIT("1" + Locale.ENGLISH)); nodes.add(new LIT("1" + XSD.stringType)); for (int i = -128; i < 128; i++) { String str = String.valueOf(i); nodes.add(new LIT(str, XSD.byteType)); nodes.add(new LIT(str, XSD.intType)); nodes.add(new LIT(str, XSD.longType)); nodes.add(new LIT(str + ".0", XSD.doubleType)); nodes.add(new LIT(str + ".0", XSD.floatType)); } for (int i = 0; i < 10000; i++) { String str = String.valueOf(i); nodes.add(new LIT(str, XSD.intType)); nodes.add(new LIT("-" + str, XSD.intType)); nodes.add(new LIT(str, XSD.longType)); nodes.add(new LIT("-" + str, XSD.longType)); for (int j = 0; j < 10; j++) { nodes.add(new LIT(str + "." + j, XSD.doubleType)); nodes.add(new LIT("-" + str + "." + j, XSD.doubleType)); nodes.add(new LIT(str + "." + j, XSD.floatType)); nodes.add(new LIT("-" + str + "." + j, XSD.floatType)); } } long start = System.currentTimeMillis(); for (NODE node : nodes) { Long id = idFactory.getId(node); if (seen.containsKey(id)) { throw new IllegalStateException("Clash : " + node + " and " + seen.get(id)); } seen.put(id, node); } System.out.println(System.currentTimeMillis() - start); }
@Override public Long getId(NODE node) { int mask; String value; if (node.isLiteral()) { LIT literal = node.asLiteral(); if (literal.getLang() != null) { mask = 0; value = literal.getValue() + literal.getLang(); } else { mask = 1; value = literal.getValue() + getReadableURI(literal.getDatatype()); } } else if (node.isBNode()) { mask = 2; value = node.getValue(); } else { mask = 3; value = getReadableURI(node.asURI()); } try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(value.getBytes("UTF-8")); byte[] hash = digest.digest(); byte[] longBytes = new byte[8]; System.arraycopy(hash, 0, longBytes, 0, longBytes.length); longBytes[0] = (byte) (mask(longBytes[0], mask)); return new BigInteger(longBytes).longValue(); } catch (NoSuchAlgorithmException e) { throw new RepositoryException(e); } catch (UnsupportedEncodingException e) { throw new RepositoryException(e); } }
MD5IdFactory implements IdFactory { @Override public Long getId(NODE node) { int mask; String value; if (node.isLiteral()) { LIT literal = node.asLiteral(); if (literal.getLang() != null) { mask = 0; value = literal.getValue() + literal.getLang(); } else { mask = 1; value = literal.getValue() + getReadableURI(literal.getDatatype()); } } else if (node.isBNode()) { mask = 2; value = node.getValue(); } else { mask = 3; value = getReadableURI(node.asURI()); } try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(value.getBytes("UTF-8")); byte[] hash = digest.digest(); byte[] longBytes = new byte[8]; System.arraycopy(hash, 0, longBytes, 0, longBytes.length); longBytes[0] = (byte) (mask(longBytes[0], mask)); return new BigInteger(longBytes).longValue(); } catch (NoSuchAlgorithmException e) { throw new RepositoryException(e); } catch (UnsupportedEncodingException e) { throw new RepositoryException(e); } } }
MD5IdFactory implements IdFactory { @Override public Long getId(NODE node) { int mask; String value; if (node.isLiteral()) { LIT literal = node.asLiteral(); if (literal.getLang() != null) { mask = 0; value = literal.getValue() + literal.getLang(); } else { mask = 1; value = literal.getValue() + getReadableURI(literal.getDatatype()); } } else if (node.isBNode()) { mask = 2; value = node.getValue(); } else { mask = 3; value = getReadableURI(node.asURI()); } try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(value.getBytes("UTF-8")); byte[] hash = digest.digest(); byte[] longBytes = new byte[8]; System.arraycopy(hash, 0, longBytes, 0, longBytes.length); longBytes[0] = (byte) (mask(longBytes[0], mask)); return new BigInteger(longBytes).longValue(); } catch (NoSuchAlgorithmException e) { throw new RepositoryException(e); } catch (UnsupportedEncodingException e) { throw new RepositoryException(e); } } MD5IdFactory(); }
MD5IdFactory implements IdFactory { @Override public Long getId(NODE node) { int mask; String value; if (node.isLiteral()) { LIT literal = node.asLiteral(); if (literal.getLang() != null) { mask = 0; value = literal.getValue() + literal.getLang(); } else { mask = 1; value = literal.getValue() + getReadableURI(literal.getDatatype()); } } else if (node.isBNode()) { mask = 2; value = node.getValue(); } else { mask = 3; value = getReadableURI(node.asURI()); } try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(value.getBytes("UTF-8")); byte[] hash = digest.digest(); byte[] longBytes = new byte[8]; System.arraycopy(hash, 0, longBytes, 0, longBytes.length); longBytes[0] = (byte) (mask(longBytes[0], mask)); return new BigInteger(longBytes).longValue(); } catch (NoSuchAlgorithmException e) { throw new RepositoryException(e); } catch (UnsupportedEncodingException e) { throw new RepositoryException(e); } } MD5IdFactory(); @Override Long getId(NODE node); @Override Integer getId(Locale locale); }
MD5IdFactory implements IdFactory { @Override public Long getId(NODE node) { int mask; String value; if (node.isLiteral()) { LIT literal = node.asLiteral(); if (literal.getLang() != null) { mask = 0; value = literal.getValue() + literal.getLang(); } else { mask = 1; value = literal.getValue() + getReadableURI(literal.getDatatype()); } } else if (node.isBNode()) { mask = 2; value = node.getValue(); } else { mask = 3; value = getReadableURI(node.asURI()); } try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(value.getBytes("UTF-8")); byte[] hash = digest.digest(); byte[] longBytes = new byte[8]; System.arraycopy(hash, 0, longBytes, 0, longBytes.length); longBytes[0] = (byte) (mask(longBytes[0], mask)); return new BigInteger(longBytes).longValue(); } catch (NoSuchAlgorithmException e) { throw new RepositoryException(e); } catch (UnsupportedEncodingException e) { throw new RepositoryException(e); } } MD5IdFactory(); @Override Long getId(NODE node); @Override Integer getId(Locale locale);  }