Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
attempted fix for persistant index lock issue and other bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Winger committed May 21, 2015
1 parent eadf934 commit ecc58be
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 71 deletions.
3 changes: 0 additions & 3 deletions .gitignore

This file was deleted.

60 changes: 0 additions & 60 deletions AIS.iml

This file was deleted.

4 changes: 4 additions & 0 deletions src/ca/dracode/ais/alarm/AutoStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.preference.PreferenceManager;

import ca.dracode.ais.service.FileListener;
import ca.dracode.ais.service.IndexService;

public class AutoStart extends BroadcastReceiver {

Expand All @@ -41,6 +42,9 @@ public void onReceive(Context context, Intent intent) {
Alarm.SetAlarm(context);
Intent serviceIntent = new Intent(context, FileListener.class);
context.startService(serviceIntent);
serviceIntent = new Intent(context, IndexService.class);
serviceIntent.putExtra("crawl", true);
context.startService(serviceIntent);
}
}
}
13 changes: 10 additions & 3 deletions src/ca/dracode/ais/indexer/FileIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* TODO - Evaluate the usefulness of ForceMerging as it increases total indexing time by about 17%
*/

import android.content.Context;
import android.os.Environment;
import android.util.Log;

Expand Down Expand Up @@ -54,11 +55,18 @@ public class FileIndexer {
private IndexWriter writer;
private FileSearcher searcher;

public FileIndexer() {
public FileIndexer(Context c) {
super();
this.searcher = new FileSearcher();
this.searcher = new FileSearcher(c);
Directory dir;
try {
File lock = new File(FileIndexer.getRootStorageDir() + "/write.lock");
if(lock.exists()) {
lock.delete();
Log.e(TAG, "Lucene write lock exists when indexer isn't running, removing\n" +
"WARNING, Index may be corrupted");
if(lock.exists()) Log.e(TAG, "What?? Lock didn't delete");
}
dir = FSDirectory.open(new File(FileIndexer.getRootStorageDir()));
Analyzer analyzer = new SimpleAnalyzer(Version.LUCENE_47);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_47,
Expand Down Expand Up @@ -222,7 +230,6 @@ public int buildIndex(String filename, int pages) {
if(pages != -1) {
doc.add(new IntField("pages", pages, Field.Store.YES));
}
Log.w(TAG, "" + writer);
if(writer.getConfig().getOpenMode() == OpenMode.CREATE) {
writer.addDocument(doc);
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/ca/dracode/ais/indexer/FileSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package ca.dracode.ais.indexer;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import org.apache.lucene.analysis.core.SimpleAnalyzer;
Expand Down Expand Up @@ -54,6 +56,7 @@
import java.util.LinkedHashMap;
import java.util.List;

import ca.dracode.ais.alarm.AutoStart;
import ca.dracode.ais.indexdata.SearchResult;

/**
Expand Down Expand Up @@ -85,7 +88,7 @@ public class FileSearcher {
private IndexSearcher indexSearcher;
private int interrupt = -1;

public FileSearcher() {
public FileSearcher(Context c) {
IndexReader indexReader;
IndexSearcher indexSearcher = null;
try {
Expand Down
6 changes: 5 additions & 1 deletion src/ca/dracode/ais/service/IndexService.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void onCreate() {
return;
}
Log.i(TAG, "Creating Indexer");
this.indexer = new FileIndexer();
this.indexer = new FileIndexer(getApplicationContext());
Log.i(TAG, "Created Indexer");
this.pIndexes = new LinkedList<Indexable>();
this.doneCrawling = true;
Expand Down Expand Up @@ -493,4 +493,8 @@ IndexService getService() {
return IndexService.this;
}
}

public boolean isIndexing(){
return tasks != 0;
}
}
23 changes: 22 additions & 1 deletion src/ca/dracode/ais/service/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;

import ca.dracode.ais.indexdata.SearchResult;
import ca.dracode.ais.indexer.FileIndexer;
import ca.dracode.ais.indexer.FileSearcher;

/**
Expand Down Expand Up @@ -114,6 +115,25 @@ public void onCreate() {
this.sm = new SearchManager();
this.data = new HashMap<String, SearchData>();
this.builtIndexes = new HashMap<File, Integer>();
verifyIndex();
}

private void verifyIndex(){
File dir = new File(FileIndexer.getRootStorageDir());
if(!dir.exists()) {
dir.mkdirs();
if(mBoundService != null) mBoundService.crawl();
Log.i(TAG, "Index folder doesn't exist, creating...");
}
if(mBoundService != null && !mBoundService.isIndexing()){
File lock = new File(FileIndexer.getRootStorageDir() + "/write.lock");
if(lock.exists()) {
lock.delete();
Log.e(TAG, "Lucene write lock exists when indexer isn't running, removing\n" +
"WARNING, Index may be corrupted");
if(lock.exists()) Log.e(TAG, "What?? Lock didn't delete");
}
}
}

private void doBindService() {
Expand Down Expand Up @@ -167,6 +187,7 @@ public int buildIndex(int id, String filePath) {
* -1 if there was an error
*/
public int load(final String filePath) {
this.verifyIndex();
if(this.data.containsKey(filePath)) {
return 1;
}
Expand Down Expand Up @@ -214,7 +235,7 @@ private class SearchManager {
private FileSearcher searcher;

public SearchManager() {
this.searcher = new FileSearcher();
this.searcher = new FileSearcher(getApplicationContext());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/ca/dracode/ais/ui/AISPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {

@Override
public void onDestroy(){
super.onDestroy();
if(indexInfo != null)
indexInfo.close(this);
indexInfo.close(this);
super.onDestroy();
}
}

0 comments on commit ecc58be

Please sign in to comment.