Package com.almworks.sqlite4java

Examples of com.almworks.sqlite4java.SQLiteStatement


            db.openReadonly();
        } else {
            db.open(true);
        }
        // set 1GB cache_size:
        final SQLiteStatement stmt = db.prepare("PRAGMA page_size;");
        if (stmt.step()) {
            long cacheSize = stmt.columnLong(0);
            db.exec("PRAGMA cache_size = " + (1024l*1024l*1024l / cacheSize) + ";");
        }
        stmt.dispose();
        db.exec("PRAGMA synchronous = OFF;");
        db.exec("PRAGMA journal_mode = OFF;");
//        db.exec("PRAGMA locking_mode = EXCLUSIVE;");
        db.exec("PRAGMA case_sensitive_like = true;");
        db.exec("PRAGMA encoding = 'UTF-8';");
View Full Code Here


        }
       
        @Override
        public void run() {
            // NOTE: need to normalise every page title!!
            SQLiteStatement stmt = null;
            try {
                // list of pages in each category:
                do {
                    println("  creating category lists");
                    stmt = db.prepare("SELECT cat.title, page.title FROM categories as categories " +
                            "INNER JOIN pages AS cat ON categories.category == cat.id " +
                            "INNER JOIN pages AS page ON categories.page == page.id;");
                    String category0 = null;
                    List<String> catPageList = new ArrayList<String>(1000);
                    while (stmt.step()) {
                        final String stmtCat0 = stmt.columnString(0);
                        final String stmtPage0 = stmt.columnString(1);
                        final String stmtPage = wikiModel.normalisePageTitle(stmtPage0);
                        if (category0 == null) {
                            category0 = stmtCat0;
                            catPageList.add(stmtPage);
                        } else if (category0.equals(stmtCat0)) {
                            catPageList.add(stmtPage);
                        } else {
                            // write old, accumulate new
                            writeCatToScalaris(category0, catPageList);
                            category0 = stmtCat0;
                            catPageList.add(stmtPage);
                        }
                    }
                    if (category0 != null && !catPageList.isEmpty()) {
                        writeCatToScalaris(category0, catPageList);
                    }
                    stmt.dispose();
                } while (false);

                // list of pages a template is used in:
                do {
                    println("  creating template lists");
                    stmt = db.prepare("SELECT tpl.title, page.title FROM templates as templates " +
                            "INNER JOIN pages AS tpl ON templates.template == tpl.id " +
                            "INNER JOIN pages AS page ON templates.page == page.id;");
                    String template0 = null;
                    List<String> tplPageList = new ArrayList<String>(1000);
                    while (stmt.step()) {
                        final String stmtTpl0 = stmt.columnString(0);
                        final String stmtPage0 = stmt.columnString(1);
                        final String stmtPage = wikiModel.normalisePageTitle(stmtPage0);
                        if (template0 == null) {
                            template0 = stmtTpl0;
                            tplPageList.add(stmtPage);
                        } else if (template0.equals(stmtTpl0)) {
                            tplPageList.add(stmtPage);
                        } else {
                            // write old, accumulate new
                            writeTplToScalaris(template0, tplPageList);
                            template0 = stmtTpl0;
                            tplPageList.add(stmtPage);
                        }
                    }
                    if (template0 != null && !tplPageList.isEmpty()) {
                        writeTplToScalaris(template0, tplPageList);
                    }
                    stmt.dispose();
                } while (false);

                // list of pages linking to other pages:
                do {
                    println("  creating backlink lists");
                    stmt = db.prepare("SELECT lnkDest.title, lnkSrc.title FROM links as links " +
                            "INNER JOIN pages AS lnkDest ON links.lnkDest == lnkDest.id " +
                            "INNER JOIN pages AS lnkSrc ON links.lnkSrc == lnkSrc.id;");
                    String linkDest0 = null;
                    List<String> backLinksPageList = new ArrayList<String>(1000);
                    while (stmt.step()) {
                        final String stmtLnkDest0 = stmt.columnString(0);
                        final String stmtLnkSrc0 = stmt.columnString(1);
                        final String stmtLnkSrc = wikiModel.normalisePageTitle(stmtLnkSrc0);
                        if (linkDest0 == null) {
                            linkDest0 = stmtLnkDest0;
                            backLinksPageList.add(stmtLnkSrc);
                        } else if (linkDest0.equals(stmtLnkDest0)) {
                            backLinksPageList.add(stmtLnkSrc);
                        } else {
                            // write old, accumulate new
                            writeLnkToScalaris(linkDest0, backLinksPageList);
                            linkDest0 = stmtLnkDest0;
                            backLinksPageList.add(stmtLnkSrc);
                        }
                    }
                    if (linkDest0 != null && !backLinksPageList.isEmpty()) {
                        writeLnkToScalaris(linkDest0, backLinksPageList);
                    }
                    stmt.dispose();
                } while (false);
            } catch (SQLiteException e) {
                System.err.println("sqlite error: " + e.toString());
                throw new RuntimeException(e);
            } finally {
                if (stmt != null) {
                    stmt.dispose();
                }
            }
        }
View Full Code Here

     */
    public void writeToScalaris() {
        println("Importing key/value pairs to Scalaris...");
        try {
            importStart();
            SQLiteStatement st = db.prepare("SELECT scalaris_key FROM objects;");
            while (st.step()) {
                String key = st.columnString(0);
                writeToScalaris(key, WikiDumpPrepareSQLiteForScalarisHandler.readObject(stRead, key));
            }
            st.dispose();
            // some requests may be left over
            Runnable worker = new WikiDumpToScalarisHandler.MyScalarisSingleRunnable(requests, scalaris_single, "");
            executor.execute(worker);
            requests = new TransactionSingleOp.RequestList();
            executor.shutdown();
View Full Code Here

            this.siteInfo = siteInfo;
        }
       
        @Override
        public void run() {
            SQLiteStatement stmt = null;
            try {
                stmt = db.prepare("REPLACE INTO properties (key, value) VALUES (?, ?);");
                WikiDumpPrepareSQLiteForScalarisHandler.writeObject(stmt, "siteinfo", siteInfo);
            } catch (SQLiteException e) {
                throw new RuntimeException(e);
            } finally {
                if (stmt != null) {
                    stmt.dispose();
                }
            }
        }
View Full Code Here

            throws RuntimeException {
        addSQLiteJob(new SQLiteWriteSiteInfoJob(siteInfo));
    }

    static SiteInfo readSiteInfo(SQLiteConnection db) throws RuntimeException {
        SQLiteStatement stmt = null;
        try {
            stmt = db.prepare("SELECT value FROM properties WHERE key == ?");
            return WikiDumpPrepareSQLiteForScalarisHandler.readObject(stmt, "siteinfo");
        } catch (SQLiteException e) {
            throw new RuntimeException(e);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            if (stmt != null) {
                stmt.dispose();
            }
        }
    }
View Full Code Here

            Map<String, Set<String>> templateTree,
            Map<String, Set<String>> includeTree,
            Map<String, Set<String>> referenceTree)
            throws RuntimeException {
        SQLiteConnection db = null;
        SQLiteStatement stmt = null;
        try {
            db = WikiDumpPrepareSQLiteForScalarisHandler.openDB(dbFileName, true);
            SiteInfo siteInfo = readSiteInfo(db);
            MyParsingWikiModel wikiModel = new MyParsingWikiModel("", "", new MyNamespace(siteInfo));
            stmt = db
                    .prepare("SELECT page.title, tpl.title FROM " +
                            "templates INNER JOIN pages AS page ON templates.title == page.id " +
                            "INNER JOIN pages AS tpl ON templates.template == tpl.id " +
                            "WHERE page.title LIKE '" + wikiModel.normalisePageTitle(wikiModel.getTemplateNamespace() + ":") + "%';");
            while (stmt.step()) {
                String pageTitle = stmt.columnString(0);
                String template = stmt.columnString(1);
                updateMap(templateTree, pageTitle, template);
            }
            stmt.dispose();
            stmt = db
                    .prepare("SELECT page.title, incl.title FROM " +
                            "includes INNER JOIN pages AS page ON includes.title == page.id " +
                            "INNER JOIN pages AS incl ON includes.include == incl.id;");
            while (stmt.step()) {
                String pageTitle = stmt.columnString(0);
                String include = stmt.columnString(1);
                updateMap(includeTree, pageTitle, include);
            }
            stmt.dispose();
            stmt = db
                    .prepare("SELECT page.title, redir.title FROM " +
                            "redirects INNER JOIN pages AS page ON redirects.title == page.id " +
                            "INNER JOIN pages AS redir ON redirects.redirect == redir.id;");
            while (stmt.step()) {
                String pageTitle = stmt.columnString(0);
                String redirect = stmt.columnString(1);
                updateMap(referenceTree, redirect, pageTitle);
            }
        } catch (SQLiteException e) {
            System.err.println("read of category tree failed (sqlite error: " + e.toString() + ")");
            throw new RuntimeException(e);
        } finally {
            if (stmt != null) {
                stmt.dispose();
            }
            if (db != null) {
                db.dispose();
            }
        }
View Full Code Here

        Set<String> allowedCatsFull = new HashSet<String>();
        Set<String> currentPages = new HashSet<String>();
        Set<String> newPages = new HashSet<String>();
       
        if (!allowedCats.isEmpty()) {
            SQLiteStatement stmt = null;
            try {
                // need to extend the category set by all sub-categories:
                currentPages.addAll(allowedCats);

                println(msgOut, " determining sub-categories of " + allowedCats0.toString() + "");
                do {
                    stmt = db.prepare("INSERT INTO currentPages (id) SELECT pages.id FROM pages WHERE pages.title == ?;");
                    for (String pageTitle : currentPages) {
                        addToPages(allowedCatsFull, newPages, pageTitle, includeTree, referenceTree);
                        // beware: add pageTitle to allowedCatsFull _AFTER_ adding its dependencies
                        // (otherwise the dependencies won't be added)
                        allowedCatsFull.add(pageTitle);
                        stmt.bind(1, pageTitle).stepThrough().reset();
                    }
                    stmt.dispose();

                    println(msgOut, "  adding sub-categories of " + currentPages.size() + " categories or templates");
                    // add all categories the page belongs to
                    stmt = db
                            .prepare("SELECT page.title FROM categories " +
                                    "INNER JOIN currentPages AS cp ON categories.category == cp.id " +
                                    "INNER JOIN pages AS page ON categories.title == page.id " +
                                    // "INNER JOIN pages AS cat ON categories.category == cat.id" +
                                    "WHERE page.title LIKE '" + MyWikiModel.normalisePageTitle(nsObject.getCategory() + ":", nsObject) + "%';");
                    while (stmt.step()) {
                        String pageCategory = stmt.columnString(0);
                        addToPages(allowedCatsFull, newPages, pageCategory, includeTree, referenceTree);
                    }
                    stmt.dispose();
                    println(msgOut, "  adding sub-templates or sub-categories of " + currentPages.size() + " categories or templates");
                    // add all templates (and their requirements) of the pages
                    stmt = db
                            .prepare("SELECT page.title FROM templates " +
                                    "INNER JOIN currentPages AS cp ON templates.template == cp.id " +
                                    "INNER JOIN pages AS page ON templates.title == page.id " +
                                    // "INNER JOIN pages AS tpl ON templates.template == tpl.id" +
                                    "WHERE page.title LIKE '" + MyWikiModel.normalisePageTitle(nsObject.getCategory() + ":", nsObject) + "%' OR "
                                    + "page.title LIKE '" + MyWikiModel.normalisePageTitle(nsObject.getTemplate() + ":", nsObject) + "%';");
                    while (stmt.step()) {
                        String pageTemplate = stmt.columnString(0);
                        Set<String> tplChildren = WikiDumpGetCategoryTreeHandler.getAllChildren(templateTree, pageTemplate);
                        addToPages(allowedCatsFull, newPages, tplChildren, includeTree, referenceTree);
                    }
                    stmt.dispose();
                    db.exec("DELETE FROM currentPages;");
                    if (newPages.isEmpty()) {
                        break;
                    } else {
                        println(msgOut, " adding " + newPages.size() + " dependencies");
                        currentPages = newPages;
                        newPages = new HashSet<String>();
                    }
                } while (true);
            } finally {
                if (stmt != null) {
                    stmt.dispose();
                }
            }
        }
        return allowedCatsFull;
    }
View Full Code Here

            SQLiteConnection db) throws SQLiteException {
        Set<String> currentPages = new HashSet<String>();

        // note: allowedCatsFull can contain categories or templates
        if (!allowedCats.isEmpty()) {
            SQLiteStatement stmt = null;
            try {
                // select all pages belonging to any of the allowed categories:
                stmt = db
                        .prepare("SELECT page.title, cat.title FROM " +
                                "categories INNER JOIN pages AS page ON categories.title == page.id " +
                                "INNER JOIN pages AS cat ON categories.category == cat.id;");
                while (stmt.step()) {
                    String pageTitle = stmt.columnString(0);
                    String pageCategory = stmt.columnString(1);
                    if (allowedCats.contains(pageCategory)) {
                        currentPages.add(pageTitle);
                    }
                }
                stmt.dispose();
                // select all pages belonging to any of the allowed templates:
                stmt = db
                        .prepare("SELECT page.title, tpl.title FROM " +
                                "templates INNER JOIN pages AS page ON templates.title == page.id " +
                                "INNER JOIN pages AS tpl ON templates.template == tpl.id;");
                while (stmt.step()) {
                    String pageTitle = stmt.columnString(0);
                    String pageTemplate = stmt.columnString(1);
                    if (allowedCats.contains(pageTemplate)) {
                        currentPages.add(pageTitle);
                    }
                }
            } finally {
                if (stmt != null) {
                    stmt.dispose();
                }
            }
        }
        return currentPages;
    }
View Full Code Here

            PrintStream msgOut)
            throws SQLiteException {
        Set<String> allPages = new HashSet<String>();
        Set<String> newPages = new HashSet<String>();
        Set<String> pageLinks = new HashSet<String>();
        SQLiteStatement stmt = null;
        try {
            while(depth >= 0) {
                println(msgOut, "recursion level: " + depth);
                println(msgOut, " adding " + currentPages.size() + " pages");
                do {
                    stmt = db.prepare("INSERT INTO currentPages (id) SELECT pages.id FROM pages WHERE pages.title == ?;");
                    for (String pageTitle : currentPages) {
                        addToPages(allPages, newPages, pageTitle, includeTree, referenceTree);
                        // beware: add pageTitle to pages _AFTER_ adding its dependencies
                        // (otherwise the dependencies won't be added)
                        allPages.add(pageTitle);
                        stmt.bind(1, pageTitle).stepThrough().reset();
                    }
                    stmt.dispose();

                    println(msgOut, "  adding categories of " + currentPages.size() + " pages");
                    // add all categories the page belongs to
                    stmt = db
                            .prepare("SELECT cat.title FROM categories " +
                                    "INNER JOIN currentPages AS cp ON categories.title == cp.id " +
                                    // "INNER JOIN pages AS page ON categories.title == page.id " +
                                    "INNER JOIN pages AS cat ON categories.category == cat.id;");
                    while (stmt.step()) {
                        String pageCategory = stmt.columnString(0);
                        addToPages(allPages, newPages, pageCategory, includeTree, referenceTree);
                    }
                    stmt.dispose();
                    println(msgOut, "  adding templates of " + currentPages.size() + " pages");
                    // add all templates (and their requirements) of the pages
                    stmt = db
                            .prepare("SELECT tpl.title FROM templates " +
                                    "INNER JOIN currentPages AS cp ON templates.title == cp.id " +
                                    // "INNER JOIN pages AS page ON templates.title == page.id " +
                                    "INNER JOIN pages AS tpl ON templates.template == tpl.id;");
                    while (stmt.step()) {
                        String pageTemplate = stmt.columnString(0);
                        Set<String> tplChildren = WikiDumpGetCategoryTreeHandler.getAllChildren(templateTree, pageTemplate);
                        addToPages(allPages, newPages, tplChildren, includeTree, referenceTree);
                    }
                    stmt.dispose();
                    println(msgOut, "  adding links of " + currentPages.size() + " pages");
                    // add all links of the pages for further processing
                    stmt = db
                            .prepare("SELECT lnk.title FROM links " +
                                    "INNER JOIN currentPages AS cp ON links.title == cp.id " +
                                    // "INNER JOIN pages AS page ON links.title == page.id " +
                                    "INNER JOIN pages AS lnk ON links.link == lnk.id;");
                    while (stmt.step()) {
                        String pageLink = stmt.columnString(0);
                        if (!pageLink.isEmpty()) { // there may be empty links
                            pageLinks.add(pageLink);
                        }
                    }
                    stmt.dispose();
                    db.exec("DELETE FROM currentPages;");
                    if (newPages.isEmpty()) {
                        break;
                    } else {
                        println(msgOut, " adding " + newPages.size() + " dependencies");
                        currentPages = newPages;
                        newPages = new HashSet<String>();
                    }
                } while (true);
                // for the next recursion:
                currentPages = pageLinks;
                pageLinks = new HashSet<String>();
                --depth;
            }
        } finally {
            if (stmt != null) {
                stmt.dispose();
            }
        }
        return allPages;
    }
View Full Code Here

TOP

Related Classes of com.almworks.sqlite4java.SQLiteStatement

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.