dbsearchmanager.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,026 行 · 第 1/3 页

JAVA
1,026
字号
                        files[i].delete();                    }                }            }            catch (Exception e) {                e.printStackTrace();            }            // Make sure that the search dir pointed to by the Jive property            // actually exists. If it does, create a directory with it. If it            // doesn't, make a new search directory.            if (!searchDir.exists()) {                rebuildIndex();            }            else {                try {                    searchDirectory = FSDirectory.getDirectory(searchDir, false);                }                catch (IOException ioe) {                    ioe.printStackTrace();                }            }        }    }    public int getAutoIndexInterval() {        return autoIndexInterval;    }    public void setAutoIndexInterval(int minutes) {        this.autoIndexInterval = minutes;        JiveGlobals.setJiveProperty("search.autoIndexInterval",                "" + autoIndexInterval);        // Restart indexer with new schedule.        if (timerTask != null) {            timerTask.cancel();        }        timerTask = TaskEngine.scheduleTask(this,autoIndexInterval*JiveGlobals.MINUTE,                autoIndexInterval*JiveGlobals.MINUTE);    }    public Date getLastIndexedDate() {        return lastIndexed;    }    public boolean isSearchEnabled() {        return searchEnabled;    }    public void setSearchEnabled(boolean searchEnabled) {        this.searchEnabled = searchEnabled;        JiveGlobals.setJiveProperty("search.enabled", String.valueOf(searchEnabled));    }    public boolean isBusy() {        // The search manager is "busy" if any indexing operations are taking        // place.        return (indexLock.permits() == 0 || newIndexLock.permits() == 0);    }    public synchronized int getPercentComplete() {        if (!isBusy()) {            return -1;        }        // If we're busy but the total count hasn't been set yet, return 0.        if (totalCount == -1 || totalCount == 0) {            return 0;        }        else {            return (int)(((double)currentCount/(double)totalCount)*100.0);        }    }    public boolean isAutoIndexEnabled() {        return autoIndexEnabled;    }    public void setAutoIndexEnabled(boolean enabled) {        // If we are switching from off to on, enable indexer        if (!autoIndexEnabled && enabled) {            if (timerTask != null) {                timerTask.cancel();            }            timerTask = TaskEngine.scheduleTask(                    this,autoIndexInterval*JiveGlobals.MINUTE,                    autoIndexInterval*JiveGlobals.MINUTE);        }        // Otherwise, if switching from on to off, disable indexer.        else if (autoIndexEnabled && !enabled) {            if (timerTask != null) {                timerTask.cancel();            }        }        autoIndexEnabled = enabled;        JiveGlobals.setJiveProperty("search.autoIndexEnabled", "" + enabled);    }    public synchronized void addToIndex(ForumMessage message) {        if (!searchEnabled) {            return;        }        // Attempt to acquire lock on index operations.        try {            if (!indexLock.attempt(10000)) {                // We failed to acquire a lock after 10 seconds of waiting,                // so give up.                return;            }        }        catch (InterruptedException ie) {            return;        }        IndexWriter writer = null;        try {            writer = getWriter();            if (writer == null) {                return;            }            long messageID = message.getID();            long userID = -1;            if (!message.isAnonymous()) {                userID = message.getUser().getID();            }            long threadID = message.getForumThread().getID();            long forumID = message.getForumThread().getForum().getID();            String subject = message.getUnfilteredSubject();            String body = message.getUnfilteredBody();            addMessageToIndex(messageID, userID, threadID, forumID,                    subject, body, message.getCreationDate(), writer);        }        catch (IOException ioe) {            ioe.printStackTrace();        }        finally{            try { writer.close(); }            catch (Exception e) { }            // Release the indexing lock.            indexLock.release();        }    }    public synchronized void removeFromIndex(ForumMessage message) {        if (!searchEnabled) {            return;        }        // Attempt to acquire lock on index operations.        try {            if (!indexLock.attempt(10000)) {                // We failed to acquire a lock after 10 seconds of waiting,                // so give up.                return;            }        }        catch (InterruptedException ie) {            return;        }        IndexReader reader = null;        try {            reader = getReader();            long [] toDelete = new long [] { message.getID() };            deleteMessagesFromIndex(toDelete, reader);            // Close searcherReader since the index has changed.            closeSearcherReader();        }        catch (IOException ioe) {            ioe.printStackTrace();        }        finally {            try { reader.close(); }            catch (Exception e) { }            // Release the indexing lock.            indexLock.release();        }    }    public synchronized void removeFromIndex(ForumThread thread) {        if (!searchEnabled) {            return;        }        // Attempt to acquire lock on index operations.        try {            if (!indexLock.attempt(10000)) {                // We failed to acquire a lock after 10 seconds of waiting,                // so give up.                return;            }        }        catch (InterruptedException ie) {            return;        }        IndexReader reader = null;        try {            reader = getReader();            if (reader == null) {                // Reader will be null if the search index doesn't exist.                return;            }            Term term = new Term("threadID", Long.toString(thread.getID()));            reader.delete(term);            // Close searcherReader since the index has changed.            closeSearcherReader();        }        catch (IOException ioe) {            ioe.printStackTrace();        }        finally {            try { reader.close(); }            catch (Exception e) { }            // Release the indexing lock.            indexLock.release();        }    }    /**     * Deletes a forum from the index.     */    public synchronized void removeFromIndex(Forum forum) {        if (!searchEnabled) {            return;        }        // Attempt to acquire lock on index operations.        try {            if (!indexLock.attempt(10000)) {                // We failed to acquire a lock after 10 seconds of waiting,                // so give up.                return;            }        }        catch (InterruptedException ie) {            return;        }        IndexReader reader = null;        try {            reader = getReader();            if (reader == null) {                // Reader will be null if the search index doesn't exist.                return;            }            Term term = new Term("forumID", Long.toString(forum.getID()));            reader.delete(term);            // Close searcherReader since the index has changed.            closeSearcherReader();        }        catch (IOException ioe) {            ioe.printStackTrace();        }        finally {            try { reader.close(); }            catch (Exception e) { }            // Release the indexing lock.            indexLock.release();        }    }    public synchronized void updateIndex() {        if (!searchEnabled) {            return;        }        // Add a task to the task engine to update the index.        TaskEngine.addTask(TaskEngine.HIGH_PRIORITY, new IndexTask(false));    }    public synchronized void rebuildIndex() {        if (!searchEnabled) {            return;        }        // Add a task to the task engine to update the index.        TaskEngine.addTask(TaskEngine.HIGH_PRIORITY, new IndexTask(true));    }    public synchronized void optimize() {        if (!searchEnabled) {            return;        }        // Attempt to acquire lock on index operations.        try {            if (!indexLock.attempt(10000)) {                // We failed to acquire a lock after 10 seconds of waiting,                // so give up.                return;            }        }        catch (InterruptedException ie) {            return;        }        IndexWriter writer = null;        try {            writer = getWriter();            if (writer != null) {                writer.optimize();            }        }        catch (IOException ioe) {            ioe.printStackTrace();        }        finally {            try { writer.close(); }            catch (Exception e) { }            // Release the indexing lock.            indexLock.release();        }    }    //OTHER METHODS//    /**     * Auto-indexing logic. It will automatically be scheduled to run at the     * desired interval if auto-indexing is turned on.     */    public synchronized void run() {        // Do nothing if searching is disabled.        if (!searchEnabled) {            return;        }        // Add a task to the task engine to update the index.        TaskEngine.addTask(TaskEngine.LOW_PRIORITY, new IndexTask(false));    }    /**     * Indexes an indivual message. The writer is assumed to be open when     * passed in and will remain open after the method is done executing.     */    protected final void addMessageToIndex(long messageID, long userID,            long threadID, long forumID, String subject, String body,            java.util.Date creationDate, IndexWriter writer) throws IOException    {        if (writer == null) {            return;        }        Document doc = new Document();        doc.add(Field.Keyword("messageID",Long.toString(messageID)));        doc.add(new Field("userID", Long.toString(userID), false, true, false));        doc.add(new Field("threadID", Long.toString(threadID), false, true, false));        doc.add(new Field("forumID", Long.toString(forumID), false, true, false));        if (subject != null) {            doc.add(Field.UnStored("subject", subject));        }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?