Package org.apache.marmotta.kiwi.persistence

Examples of org.apache.marmotta.kiwi.persistence.KiWiConnection


            if(result != null) {
                return result;
            } else {
                final KiWiUriResource rtype = type==null?null:(KiWiUriResource)createURI(type);

                final KiWiConnection connection = aqcuireConnection();
                try {

                    try {
                        // differentiate between the different types of the value
                        if (type == null) {
                            // FIXME: MARMOTTA-39 (this is to avoid a NullPointerException in the following if-clauses)
                            result = connection.loadLiteral(value.toString(), lang, rtype);

                            if(result == null) {
                                result = new KiWiStringLiteral(value.toString(), locale, rtype);
                            }
                        } else if(value instanceof Date || type.equals(Namespaces.NS_XSD+"dateTime")) {
                            // parse if necessary
                            final Date dvalue;
                            if(value instanceof Date) {
                                dvalue = (Date)value;
                            } else {
                                dvalue = DateUtils.parseDate(value.toString());
                            }

                            result = connection.loadLiteral(dvalue);

                            if(result == null) {
                                result= new KiWiDateLiteral(dvalue, rtype);
                            }
                        } else if(Integer.class.equals(value.getClass()) || int.class.equals(value.getClass())  ||
                                Long.class.equals(value.getClass())    || long.class.equals(value.getClass()) ||
                                type.equals(Namespaces.NS_XSD+"integer") || type.equals(Namespaces.NS_XSD+"long")) {
                            long ivalue = 0;
                            if(Integer.class.equals(value.getClass()) || int.class.equals(value.getClass())) {
                                ivalue = (Integer)value;
                            } else if(Long.class.equals(value.getClass()) || long.class.equals(value.getClass())) {
                                ivalue = (Long)value;
                            } else {
                                ivalue = Long.parseLong(value.toString());
                            }


                            result = connection.loadLiteral(ivalue);

                            if(result == null) {
                                result= new KiWiIntLiteral(ivalue, rtype);
                            }
                        } else if(Double.class.equals(value.getClass())   || double.class.equals(value.getClass())  ||
                                Float.class.equals(value.getClass())    || float.class.equals(value.getClass()) ||
                                type.equals(Namespaces.NS_XSD+"double") || type.equals(Namespaces.NS_XSD+"float")) {
                            double dvalue = 0.0;
                            if(Float.class.equals(value.getClass()) || float.class.equals(value.getClass())) {
                                dvalue = (Float)value;
                            } else if(Double.class.equals(value.getClass()) || double.class.equals(value.getClass())) {
                                dvalue = (Double)value;
                            } else {
                                dvalue = Double.parseDouble(value.toString());
                            }


                            result = connection.loadLiteral(dvalue);

                            if(result == null) {
                                result= new KiWiDoubleLiteral(dvalue, rtype);
                            }
                        } else if(Boolean.class.equals(value.getClass())   || boolean.class.equals(value.getClass())  ||
                                type.equals(Namespaces.NS_XSD+"boolean")) {
                            boolean bvalue = false;
                            if(Boolean.class.equals(value.getClass())   || boolean.class.equals(value.getClass())) {
                                bvalue = (Boolean)value;
                            } else {
                                bvalue = Boolean.parseBoolean(value.toString());
                            }


                            result = connection.loadLiteral(bvalue);

                            if(result == null) {
                                result= new KiWiBooleanLiteral(bvalue, rtype);
                            }
                        } else {
                            result = connection.loadLiteral(value.toString(), lang, rtype);

                            if(result == null) {
                                result = new KiWiStringLiteral(value.toString(), locale, rtype);
                            }
                        }
                    } catch(IllegalArgumentException ex) {
                        // malformed number or date
                        log.warn("malformed argument for typed literal of type {}: {}", rtype.stringValue(), value);
                        KiWiUriResource mytype = (KiWiUriResource)createURI(Namespaces.NS_XSD+"string");

                        result = connection.loadLiteral(value.toString(), lang, mytype);

                        if(result == null) {
                            result = new KiWiStringLiteral(value.toString(), locale, mytype);
                        }

                    }

                    if(result.getId() == null) {
                        if(batchCommit) {
                            result.setId(connection.getNodeId());
                            batchLiteralLookup.put(key, result);

                            nodeBatch.add(result);
                        } else {
                            connection.storeNode(result, false);
                        }
                    }

                    return result;
View Full Code Here


    /**
     * Immediately flush the batch to the database using the value factory's connection. The method expects the
     * underlying connection to start and commit the node batch.
     */
    public void flushBatch() throws SQLException {
        KiWiConnection con = aqcuireConnection();
        try {
            flushBatch(con);
        } finally {
            releaseConnection(con);
        }
View Full Code Here


    @Test
    public void testTablesCreateDrop() throws Exception {
        // test if database exists and has a version
        KiWiConnection connection = rpersistence.getConnection();
        try {
            Assert.assertThat(connection.getDatabaseTables(), hasItems(
                    "reasoner_programs", "reasoner_program_namespaces", "reasoner_program_rules",
                    "reasoner_rules", "reasoner_justifications", "reasoner_just_supp_triples", "reasoner_just_supp_rules"));
            Assert.assertEquals(3, connection.getDatabaseVersion());

            connection.commit();
        } finally {
            connection.close();
        }
    }
View Full Code Here


    private void prepareFulltext(KiWiConfiguration configuration) {
        try {
            if(configuration.isFulltextEnabled()) {
                KiWiConnection connection = parent.getPersistence().getConnection();
                try {
                    if(configuration.getDialect() instanceof PostgreSQLDialect) {

                        // for postgres, we need to create
                        // - a stored procedure for mapping ISO language codes to PostgreSQL fulltext configuration names
                        // - if languages are not null, for each configured language as well as for the generic configuration
                        //   an index over nodes.svalue

                        ScriptRunner runner = new ScriptRunner(connection.getJDBCConnection(), false, false);
                        if(connection.getMetadata("ft.lookup") == null) {
                            log.info("PostgreSQL: creating language configuration lookup function");
                            StringBuilder script = new StringBuilder();
                            for(String line : IOUtils.readLines(PostgreSQLDialect.class.getResourceAsStream("create_fulltext_langlookup.sql"))) {
                                if(!line.startsWith("--")) {
                                    script.append(line);
                                    script.append(" ");
                                }
                            }
                            log.debug("PostgreSQL: running SQL script '{}'", script.toString());
                            runner.runScript(new StringReader(script.toString()));
                        }

                        // language specific indexes
                        if(configuration.getFulltextLanguages() != null) {
                            StringBuilder script = new StringBuilder();
                            for(String line : IOUtils.readLines(PostgreSQLDialect.class.getResourceAsStream("create_fulltext_index.sql"))) {
                                if(!line.startsWith("--")) {
                                    script.append(line);
                                    script.append(" ");
                                }
                            }
                            for(String lang : configuration.getFulltextLanguages()) {
                                if(connection.getMetadata("ft.idx."+lang) == null) {
                                    String pg_configuration = POSTGRES_LANG_MAPPINGS.get(lang);
                                    if(pg_configuration != null) {
                                        log.info("PostgreSQL: creating fulltext index for language {}", lang);
                                        String script_lang = script.toString().replaceAll("@LANGUAGE@", lang).replaceAll("@CONFIGURATION@",pg_configuration);
                                        log.debug("PostgreSQL: running SQL script '{}'", script_lang);
                                        runner.runScript(new StringReader(script_lang));
                                    }
                                }
                            }
                        }

                        // generic index
                        if(configuration.getFulltextLanguages() != null) {
                            if(connection.getMetadata("ft.idx.generic") == null) {
                                StringBuilder script = new StringBuilder();
                                for(String line : IOUtils.readLines(PostgreSQLDialect.class.getResourceAsStream("create_fulltext_index_generic.sql"))) {
                                    if(!line.startsWith("--")) {
                                        script.append(line);
                                        script.append(" ");
                                    }
                                }
                                log.info("PostgreSQL: creating generic fulltext index ");
                                log.debug("PostgreSQL: running SQL script '{}'", script.toString());
                                runner.runScript(new StringReader(script.toString()));
                            }
                        }

                        /*
                    } else if(configuration.getDialect() instanceof MySQLDialect) {

                        // for MySQL, just create a fulltext index (no language support)
                        if(connection.getMetadata("ft.idx") == null) {
                            ScriptRunner runner = new ScriptRunner(connection.getJDBCConnection(), false, false);
                            String script = IOUtils.toString(MySQLDialect.class.getResourceAsStream("create_fulltext_index.sql"));
                            log.info("MySQL: creating generic fulltext index ");
                            log.debug("MySQL: running SQL script '{}'", script.toString());
                            runner.runScript(new StringReader(script));
                        }
                        /*
                    } else if(configuration.getDialect() instanceof H2Dialect) {

                        // for H2, just create a fulltext index (no language support)
                        if(connection.getMetadata("fulltext.index") == null) {
                            ScriptRunner runner = new ScriptRunner(connection.getJDBCConnection(), false, false);
                            String script = IOUtils.toString(H2Dialect.class.getResourceAsStream("create_fulltext_index.sql"));
                            runner.runScript(new StringReader(script));
                        }
                        */
                    }
                } finally {
                    connection.close();
                }
            }
        } catch (IOException | SQLException ex) {
            log.error("error while preparing fulltext support",ex);
        }
View Full Code Here

            LoggerFactory.getLogger(this.getClass());

    @Test
    public void testTablesCreateDrop() throws Exception {
        // test if database exists and has a version
        KiWiConnection connection = vpersistence.getConnection();
        try {
            Assert.assertThat(connection.getDatabaseTables(), hasItems("versions", "versions_added", "versions_removed"));
            Assert.assertEquals(3, connection.getDatabaseVersion());

            connection.commit();
        } finally {
            connection.close();
        }
    }
View Full Code Here



    @Test
    public void testWriteNodes() throws IOException, SQLException {
        KiWiConnection con = store.getPersistence().getConnection();

        PGCopyOutputStream out = new PGCopyOutputStream(PGCopyUtil.getWrappedConnection(con.getJDBCConnection()), "COPY nodes FROM STDIN (FORMAT csv)");

        long start = System.currentTimeMillis();

        List<KiWiNode> nodes = new ArrayList<>(10000);

        nodes.add(TYPE_INT);
        nodes.add(TYPE_DBL);
        nodes.add(TYPE_BOOL);
        nodes.add(TYPE_DATE);
        nodes.add(EMPTY);

        // randomly create 10000 nodes
        for(int i=0; i<10000; i++) {
            nodes.add(randomObject());
        }

        // flush out nodes
        PGCopyUtil.flushNodes(nodes, out);

        out.close();

        long imported = System.currentTimeMillis();

        log.info("imported {} nodes in {} ms", nodes.size(), imported-start);

        // check if database contains the nodes (based on ID)

        PreparedStatement stmt = con.getJDBCConnection().prepareStatement("SELECT * FROM nodes WHERE id = ?");
        for(int i=0; i<nodes.size(); i++) {
            stmt.setLong(1, (long)i);
            ResultSet dbResult = stmt.executeQuery();
            Assert.assertTrue(dbResult.next());
            Assert.assertEquals(nodes.get(i).stringValue(),dbResult.getString("svalue"));
View Full Code Here

        this.store = store;
    }

    @Override
    public void registerKey(IntArray key, long transactionId, long tripleId) {
        KiWiConnection con = aqcuireConnection();
        try {
            PreparedStatement stmt = con.getPreparedStatement("registry.register");
            synchronized (stmt) {
                stmt.setLong(1, key.longHashCode());
                stmt.setLong(2, tripleId);
                stmt.setLong(3, transactionId);
                stmt.executeUpdate();
View Full Code Here



    @Override
    public long lookupKey(IntArray key) {
        KiWiConnection con = aqcuireConnection();
        try {
            PreparedStatement stmt = con.getPreparedStatement("registry.lookup");
            synchronized (stmt) {
                stmt.setLong(1, key.longHashCode());

                try(ResultSet r = stmt.executeQuery()) {
                    if(r.next()) {
View Full Code Here

    }


    @Override
    public void releaseTransaction(long transactionId) {
        KiWiConnection con = aqcuireConnection();
        try {
            PreparedStatement stmt = con.getPreparedStatement("registry.release");
            synchronized (stmt) {
                stmt.setLong(1, transactionId);
                stmt.executeUpdate();
            }
        } catch (SQLException e) {
View Full Code Here

    }


    @Override
    public void deleteKey(IntArray key) {
        KiWiConnection con = aqcuireConnection();
        try {
            PreparedStatement stmt = con.getPreparedStatement("registry.delete");
            synchronized (stmt) {
                stmt.setLong(1, key.longHashCode());
                stmt.executeUpdate();
            }
        } catch (SQLException e) {
View Full Code Here

TOP

Related Classes of org.apache.marmotta.kiwi.persistence.KiWiConnection

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.