Fixing merge error Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/4cb2eca9 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/4cb2eca9 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/4cb2eca9
Branch: refs/heads/develop Commit: 4cb2eca9eae4445a00963c1b3440622c38f43c5e Parents: a1a3b21 Author: Jakob Frank <[email protected]> Authored: Wed Nov 27 13:33:55 2013 +0100 Committer: Jakob Frank <[email protected]> Committed: Wed Nov 27 13:33:55 2013 +0100 ---------------------------------------------------------------------- .../kiwi/persistence/KiWiConnection.java | 132 +++++++++++-------- 1 file changed, 74 insertions(+), 58 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/4cb2eca9/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java index 857e879..fc4fa9e 100644 --- a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java +++ b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java @@ -308,7 +308,7 @@ public class KiWiConnection { */ public void storeNamespace(KiWiNamespace namespace) throws SQLException { // TODO: add unique constraints to table - if(namespace.getId() != null) { + if(namespace.getId() >= 0) { log.warn("trying to store namespace which is already persisted: {}",namespace); return; } @@ -335,7 +335,7 @@ public class KiWiConnection { * @throws SQLException in case a database error occurred */ public void deleteNamespace(KiWiNamespace namespace) throws SQLException { - if(namespace.getId() == null) { + if(namespace.getId() < 0) { log.warn("trying to remove namespace which is not persisted: {}",namespace); return; } @@ -377,7 +377,7 @@ public class KiWiConnection { * @throws SQLException */ public long getSize(KiWiResource context) throws SQLException { - if(context.getId() == null) { + if(context.getId() < 0) { return 0; }; @@ -605,7 +605,7 @@ public class KiWiConnection { requireJDBCConnection(); // ltype not persisted - if(ltype != null && ltype.getId() == null) { + if(ltype != null && ltype.getId() < 0) { return null; } @@ -671,7 +671,7 @@ public class KiWiConnection { KiWiUriResource ltype = loadUriResource(Namespaces.NS_XSD + "dateTime"); - if(ltype == null || ltype.getId() == null) { + if(ltype == null || ltype.getId() < 0) { return null; } @@ -726,7 +726,7 @@ public class KiWiConnection { KiWiUriResource ltype = loadUriResource(Namespaces.NS_XSD + "integer"); // ltype not persisted - if(ltype == null || ltype.getId() == null) { + if(ltype == null || ltype.getId() < 0) { return null; } @@ -781,7 +781,7 @@ public class KiWiConnection { KiWiUriResource ltype = loadUriResource(Namespaces.NS_XSD + "double"); // ltype not persisted - if(ltype == null || ltype.getId() == null) { + if(ltype == null || ltype.getId() < 0) { return null; } @@ -836,7 +836,7 @@ public class KiWiConnection { KiWiUriResource ltype = loadUriResource(Namespaces.NS_XSD + "boolean"); // ltype not persisted - if(ltype == null || ltype.getId() == null) { + if(ltype == null || ltype.getId() < 0) { return null; } @@ -883,7 +883,7 @@ public class KiWiConnection { // ensure the data type of a literal is persisted first if(node instanceof KiWiLiteral) { KiWiLiteral literal = (KiWiLiteral)node; - if(literal.getType() != null && literal.getType().getId() == null) { + if(literal.getType() != null && literal.getType().getId() < 0) { storeNode(literal.getType(), batch); } } @@ -891,7 +891,7 @@ public class KiWiConnection { requireJDBCConnection(); // retrieve a new node id and set it in the node object - if(node.getId() == null) { + if(node.getId() < 0) { node.setId(getNextSequence("seq.nodes")); } @@ -999,20 +999,43 @@ public class KiWiConnection { } else if(node instanceof KiWiStringLiteral) { KiWiStringLiteral stringLiteral = (KiWiStringLiteral)node; + + Double dbl_value = null; + Long lng_value = null; + if(stringLiteral.getContent().length() < 64 && NumberUtils.isNumber(stringLiteral.getContent())) + try { + dbl_value = Double.parseDouble(stringLiteral.getContent()); + lng_value = Long.parseLong(stringLiteral.getContent()); + } catch (NumberFormatException ex) { + // ignore, keep NaN + } + + PreparedStatement insertNode = getPreparedStatement("store.sliteral"); insertNode.setLong(1,node.getId()); insertNode.setString(2, stringLiteral.getContent()); - if(stringLiteral.getLocale() != null) { - insertNode.setString(3,stringLiteral.getLocale().getLanguage()); + if(dbl_value != null) { + insertNode.setDouble(3, dbl_value); } else { insertNode.setObject(3, null); } - if(stringLiteral.getType() != null) { - insertNode.setLong(4,stringLiteral.getType().getId()); + if(lng_value != null) { + insertNode.setLong(4, lng_value); } else { insertNode.setObject(4, null); } - insertNode.setTimestamp(5, new Timestamp(stringLiteral.getCreated().getTime())); + + if(stringLiteral.getLocale() != null) { + insertNode.setString(5, stringLiteral.getLocale().getLanguage()); + } else { + insertNode.setObject(5, null); + } + if(stringLiteral.getType() != null) { + insertNode.setLong(6,stringLiteral.getType().getId()); + } else { + insertNode.setObject(6, null); + } + insertNode.setTimestamp(7, new Timestamp(stringLiteral.getCreated().getTime())); if(batch) { insertNode.addBatch(); @@ -1040,7 +1063,7 @@ public class KiWiConnection { requireJDBCConnection(); - boolean hasId = triple.getId() != null; + boolean hasId = triple.getId() >= 0; if(hasId && deletedStatementsLog.contains(triple.getId())) { // this is a hack for a concurrency problem that may occur in case the triple is removed in the @@ -1053,7 +1076,7 @@ public class KiWiConnection { return true; } else { // retrieve a new triple ID and set it in the object - if(triple.getId() == null) { + if(triple.getId() < 0) { triple.setId(getNextSequence("seq.triples")); } @@ -1127,7 +1150,7 @@ public class KiWiConnection { * @param inferred * @return */ - public synchronized Long getTripleId(final KiWiResource subject, final KiWiUriResource predicate, final KiWiNode object, final KiWiResource context, final boolean inferred) throws SQLException { + public synchronized long getTripleId(final KiWiResource subject, final KiWiUriResource predicate, final KiWiNode object, final KiWiResource context, final boolean inferred) throws SQLException { if(tripleBatch != null && tripleBatch.size() > 0) { Collection<KiWiTriple> batched = tripleBatch.listTriples(subject,predicate,object,context, false); if(batched.size() > 0) { @@ -1151,7 +1174,7 @@ public class KiWiConnection { if(result.next()) { return result.getLong(1); } else { - return null; + return -1L; } } finally { @@ -1171,7 +1194,7 @@ public class KiWiConnection { public void deleteTriple(final KiWiTriple triple) throws SQLException { requireJDBCConnection(); - RetryExecution execution = new RetryExecution("DELETE"); + RetryExecution<Void> execution = new RetryExecution<>("DELETE"); execution.setUseSavepoint(true); execution.execute(connection, new RetryCommand<Void>() { @Override @@ -1183,7 +1206,7 @@ public class KiWiConnection { triple.setDeleted(true); triple.setDeletedAt(new Date()); - if (triple.getId() == null) { + if (triple.getId() < 0) { log.warn("attempting to remove non-persistent triple: {}", triple); removeCachedTriple(triple); } else { @@ -1236,7 +1259,7 @@ public class KiWiConnection { * @param triple */ public void undeleteTriple(KiWiTriple triple) throws SQLException { - if(triple.getId() == null) { + if(triple.getId() < 0) { log.warn("attempting to undelete non-persistent triple: {}",triple); return; } @@ -1335,7 +1358,7 @@ public class KiWiConnection { requireJDBCConnection(); PreparedStatement queryContexts = getPreparedStatement("query.resources_prefix"); - queryContexts.setString(1, prefix+"%"); + queryContexts.setString(1, prefix + "%"); final ResultSet result = queryContexts.executeQuery(); @@ -1440,16 +1463,16 @@ public class KiWiConnection { */ private CloseableIteration<Statement, SQLException> listTriplesInternal(KiWiResource subject, KiWiUriResource predicate, KiWiNode object, KiWiResource context, boolean inferred, final boolean wildcardContext) throws SQLException { // if one of the database ids is null, there will not be any database results, so we can return an empty result - if(subject != null && subject.getId() == null) { + if(subject != null && subject.getId() < 0) { return new EmptyIteration<Statement, SQLException>(); } - if(predicate != null && predicate.getId() == null) { + if(predicate != null && predicate.getId() < 0) { return new EmptyIteration<Statement, SQLException>(); } - if(object != null && object.getId() == null) { + if(object != null && object.getId() < 0) { return new EmptyIteration<Statement, SQLException>(); } - if(context != null && context.getId() == null) { + if(context != null && context.getId() < 0) { return new EmptyIteration<Statement, SQLException>(); } @@ -1546,7 +1569,7 @@ public class KiWiConnection { */ protected KiWiNode constructNodeFromDatabase(ResultSet row) throws SQLException { - Long id = row.getLong("id"); + long id = row.getLong("id"); Element cached = nodeCache.get(id); @@ -1557,23 +1580,20 @@ public class KiWiConnection { String ntype = row.getString("ntype"); if("uri".equals(ntype)) { - KiWiUriResource result = new KiWiUriResource(row.getString("svalue")); + KiWiUriResource result = new KiWiUriResource(row.getString("svalue"),new Date(row.getTimestamp("createdAt").getTime())); result.setId(id); - result.setCreated(new Date(row.getTimestamp("createdAt").getTime())); cacheNode(result); return result; } else if("bnode".equals(ntype)) { - KiWiAnonResource result = new KiWiAnonResource(row.getString("svalue")); - result.setId(row.getLong("id")); - result.setCreated(new Date(row.getTimestamp("createdAt").getTime())); + KiWiAnonResource result = new KiWiAnonResource(row.getString("svalue"), new Date(row.getTimestamp("createdAt").getTime())); + result.setId(id); cacheNode(result); return result; } else if("string".equals(ntype)) { - final KiWiStringLiteral result = new KiWiStringLiteral(row.getString("svalue")); - result.setId(row.getLong("id")); - result.setCreated(new Date(row.getTimestamp("createdAt").getTime())); + final KiWiStringLiteral result = new KiWiStringLiteral(row.getString("svalue"), new Date(row.getTimestamp("createdAt").getTime())); + result.setId(id); if(row.getString("lang") != null) { result.setLocale(getLocale(row.getString("lang"))); @@ -1585,10 +1605,8 @@ public class KiWiConnection { cacheNode(result); return result; } else if("int".equals(ntype)) { - KiWiIntLiteral result = new KiWiIntLiteral(); - result.setId(row.getLong("id")); - result.setCreated(new Date(row.getTimestamp("createdAt").getTime())); - result.setIntContent(row.getLong("ivalue")); + KiWiIntLiteral result = new KiWiIntLiteral(row.getLong("ivalue"), null, new Date(row.getTimestamp("createdAt").getTime())); + result.setId(id); if(row.getLong("ltype") != 0) { result.setType((KiWiUriResource) loadNodeById(row.getLong("ltype"))); } @@ -1596,10 +1614,8 @@ public class KiWiConnection { cacheNode(result); return result; } else if("double".equals(ntype)) { - KiWiDoubleLiteral result = new KiWiDoubleLiteral(); - result.setId(row.getLong("id")); - result.setCreated(new Date(row.getTimestamp("createdAt").getTime())); - result.setDoubleContent(row.getDouble("dvalue")); + KiWiDoubleLiteral result = new KiWiDoubleLiteral(row.getDouble("dvalue"), null, new Date(row.getTimestamp("createdAt").getTime())); + result.setId(id); if(row.getLong("ltype") != 0) { result.setType((KiWiUriResource) loadNodeById(row.getLong("ltype"))); } @@ -1607,10 +1623,8 @@ public class KiWiConnection { cacheNode(result); return result; } else if("boolean".equals(ntype)) { - KiWiBooleanLiteral result = new KiWiBooleanLiteral(); - result.setId(row.getLong("id")); - result.setCreated(new Date(row.getTimestamp("createdAt").getTime())); - result.setValue(row.getBoolean("bvalue")); + KiWiBooleanLiteral result = new KiWiBooleanLiteral(row.getBoolean("bvalue"),null,new Date(row.getTimestamp("createdAt").getTime())); + result.setId(id); if(row.getLong("ltype") != 0) { result.setType((KiWiUriResource) loadNodeById(row.getLong("ltype"))); @@ -1619,10 +1633,8 @@ public class KiWiConnection { cacheNode(result); return result; } else if("date".equals(ntype)) { - KiWiDateLiteral result = new KiWiDateLiteral(); - result.setId(row.getLong("id")); - result.setCreated(new Date(row.getTimestamp("createdAt").getTime())); - result.setDateContent(new Date(row.getTimestamp("tvalue").getTime())); + KiWiDateLiteral result = new KiWiDateLiteral(new Date(row.getTimestamp("tvalue").getTime()), null, new Date(row.getTimestamp("createdAt").getTime())); + result.setId(id); if(row.getLong("ltype") != 0) { result.setType((KiWiUriResource) loadNodeById(row.getLong("ltype"))); @@ -1656,6 +1668,10 @@ public class KiWiConnection { * @return a KiWiTriple representation of the database result */ protected KiWiTriple constructTripleFromDatabase(ResultSet row) throws SQLException { + if(row.isClosed()) { + throw new ResultInterruptedException("retrieving results has been interrupted"); + } + Long id = row.getLong("id"); Element cached = tripleCache.get(id); @@ -1694,7 +1710,7 @@ public class KiWiConnection { protected static Locale getLocale(String language) { Locale locale = localeMap.get(language); - if(locale == null && language != null) { + if(locale == null && language != null && !language.isEmpty()) { try { Locale.Builder builder = new Locale.Builder(); builder.setLanguageTag(language); @@ -1768,7 +1784,7 @@ public class KiWiConnection { private void cacheNode(KiWiNode node) { - if(node.getId() != null) { + if(node.getId() >= 0) { nodeCache.put(new Element(node.getId(), node)); } if(node instanceof KiWiUriResource) { @@ -1781,13 +1797,13 @@ public class KiWiConnection { } private void cacheTriple(KiWiTriple triple) { - if(triple.getId() != null) { + if(triple.getId() >= 0) { tripleCache.put(new Element(triple.getId(),triple)); } } private void removeCachedTriple(KiWiTriple triple) { - if(triple.getId() != null) { + if(triple.getId() >= 0) { tripleCache.remove(triple.getId()); } } @@ -2051,7 +2067,7 @@ public class KiWiConnection { if(tripleBatch != null && tripleBatch.size() > 0) { synchronized (tripleBatch) { for(KiWiTriple triple : tripleBatch) { - triple.setId(null); + triple.setId(-1L); } tripleBatch.clear(); } @@ -2150,7 +2166,7 @@ public class KiWiConnection { } // retrieve a new triple ID and set it in the object - if(triple.getId() == null) { + if(triple.getId() < 0) { triple.setId(getNextSequence("seq.triples")); }
