Add a build full-text indexing feature in settings

This commit is contained in:
xingxing
2018-06-04 23:32:56 +08:00
parent 9ed4bba184
commit a91ec5e9f4
8 changed files with 132 additions and 11 deletions

View File

@@ -38,17 +38,54 @@ public class NoteDataStore {
}
public static void updateFTSNoteByLocalId(Long localId) {
// DatabaseWrapper databaseWrapper = FlowManager.getWritableDatabase(AppDataBase.class);
// String query = "INSERT INTO fts_note(fts_note) VALUES('rebuild')";//This can be slow
// databaseWrapper.execSQL(query);
Note note = getByLocalId(localId);
DatabaseWrapper databaseWrapper = FlowManager.getWritableDatabase(AppDataBase.class);
String query = "UPDATE fts_note SET content = '" + note.getContent() + "' where rowid = " + localId;
databaseWrapper.execSQL(query);
}
public static boolean isExistsTableFTSNote() {
boolean result = false;
DatabaseWrapper databaseWrapper = FlowManager.getWritableDatabase(AppDataBase.class);
String query = "select count(*) as c from sqlite_master where type ='table' and name ='fts_note'";
Cursor cursor = databaseWrapper.rawQuery(query, null);
if(cursor.moveToNext()){
int count = cursor.getInt(0);
if(count > 0){
result = true;
}
}
return result;
}
public static void createTableFTSNote() {
DatabaseWrapper databaseWrapper = FlowManager.getWritableDatabase(AppDataBase.class);
String query = "CREATE VIRTUAL TABLE fts_note USING fts4 (content='note', content)";
databaseWrapper.execSQL(query);
}
public static void FTSNoteRebuild() {
if (!isExistsTableFTSNote()) {
createTableFTSNote();
}
FTSNoteRebuildInternal();
}
public static void FTSNoteRebuildInternal() {
DatabaseWrapper databaseWrapper = FlowManager.getWritableDatabase(AppDataBase.class);
String query = "INSERT INTO fts_note(fts_note) VALUES('rebuild')";//This can be slow
databaseWrapper.execSQL(query);
}
public static List<Note> searchByKeyword(String keyword) {
if (!isExistsTableFTSNote()) {
createTableFTSNote();
return searchByTitle(keyword);
} else {
return searchByFullTextSearch(keyword);
}
}
public static List<Note> searchByFullTextSearch(String keyword) {
Set<Long> set = new LinkedHashSet<>();
DatabaseWrapper databaseWrapper = FlowManager.getWritableDatabase(AppDataBase.class);

View File

@@ -61,7 +61,7 @@ public class NoteService {
}
}
public static void buildFTSNote() {
if (!SharedPreferenceUtils.read(SharedPreferenceUtils.CONFIG, SP_HAS_FTS_FULL_BUILD, false)) {
if (!SharedPreferenceUtils.read(SharedPreferenceUtils.CONFIG, SP_HAS_FTS_FULL_BUILD, false)) {//存在缺馅对于多账户用户而言每个账户第一次全量同步都会触发rebuild
NoteDataStore.FTSNoteRebuild();
SharedPreferenceUtils.write(SharedPreferenceUtils.CONFIG, SP_HAS_FTS_FULL_BUILD, true);
SharedPreferenceUtils.write(SharedPreferenceUtils.CONFIG, SP_FTS_INCREASE_BUILD_KES, "");

View File

@@ -163,7 +163,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
notes = NoteDataStore.getByTagText(mode.tagText, Account.getCurrent().getUserId());
break;
case SEARCH:
notes = NoteDataStore.searchByFullTextSearch(mode.keywords);
notes = NoteDataStore.searchByKeyword(mode.keywords);
mNoteList.setHighlight(mode.keywords);
break;
default:

View File

@@ -2,6 +2,7 @@ package org.houxg.leamonax.ui;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
@@ -22,11 +23,10 @@ import org.houxg.leamonax.database.NoteTagDataStore;
import org.houxg.leamonax.database.NotebookDataStore;
import org.houxg.leamonax.model.Account;
import org.houxg.leamonax.model.BaseResponse;
import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.model.Notebook;
import org.houxg.leamonax.model.RelationshipOfNoteTag;
import org.houxg.leamonax.model.Tag;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.service.NoteService;
import org.houxg.leamonax.utils.SharedPreferenceUtils;
import org.houxg.leamonax.utils.ToastUtils;
import java.util.List;
@@ -41,6 +41,8 @@ import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import static org.houxg.leamonax.service.NoteService.SP_HAS_FTS_FULL_BUILD;
public class SettingsActivity extends BaseActivity {
private final String[] mEditors = new String[]{"RichText", "Markdown"};
@@ -59,6 +61,9 @@ public class SettingsActivity extends BaseActivity {
TextView mHostTv;
@BindView(R.id.ll_clear)
View mClearDataView;
@BindView(R.id.ll_fts_rebuild)
View mFtsRebuildLayout;
private ProgressDialog mDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -146,8 +151,8 @@ public class SettingsActivity extends BaseActivity {
@OnClick(R.id.ll_change_password)
void clickedPassword() {
View view = LayoutInflater.from(this).inflate(R.layout.dialog_change_passowrd, null);
final EditText mOldPasswordEt = (EditText) view.findViewById(R.id.et_old_password);
final EditText mNewPasswordEt = (EditText) view.findViewById(R.id.et_new_password);
final EditText mOldPasswordEt = view.findViewById(R.id.et_old_password);
final EditText mNewPasswordEt = view.findViewById(R.id.et_new_password);
new AlertDialog.Builder(this)
.setTitle(R.string.change_password)
.setView(view)
@@ -183,6 +188,52 @@ public class SettingsActivity extends BaseActivity {
.show();
}
private void showProgressDialog() {
if (mDialog == null) {
mDialog = new ProgressDialog(this);
mDialog.setTitle(R.string.progress_dialog_loading_msg);
mDialog.setCancelable(false);
mDialog.show();
}
}
private void hideProgressDialog() {
if (mDialog != null) {
mDialog.dismiss();
mDialog = null;
}
}
@OnClick(R.id.ll_fts_rebuild)
void rebuildIndex() {
new AlertDialog.Builder(this)
.setTitle(R.string.full_text_search_index_rebuild)
.setMessage(R.string.full_text_search_rebuild_index_msg)
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showProgressDialog();
SharedPreferenceUtils.write(SharedPreferenceUtils.CONFIG, SP_HAS_FTS_FULL_BUILD, false);
NoteService.buildFTSNote();
dialog.dismiss();
mFtsRebuildLayout.postDelayed(new Runnable() {
@Override
public void run() {
hideProgressDialog();
ToastUtils.show(SettingsActivity.this, R.string.full_text_search_index_rebuild_success);
}
}, 1000 * 15);
}
})
.show();
}
private void clearData() {
Observable.create(
new Observable.OnSubscribe<Void>() {

View File

@@ -5,10 +5,11 @@ import android.content.Context;
import android.content.SharedPreferences;
import org.houxg.leamonax.Leamonax;
import org.houxg.leamonax.model.Account;
public class SharedPreferenceUtils {
public static final String CONFIG = "CONFIG";
public static final String CONFIG = "CONFIG_" + Account.getCurrent().getUserId();
public static SharedPreferences getSharedPreferences(String name) {
return Leamonax.getContext().getSharedPreferences(name, Context.MODE_PRIVATE);

View File

@@ -139,6 +139,28 @@
<include layout="@layout/divider" />
<LinearLayout
android:id="@+id/ll_fts_rebuild"
style="@style/SettingsPanel"
android:orientation="vertical">
<TextView
style="@style/SettingsSecondaryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/full_text_search" />
<TextView
android:id="@+id/tv_rebuild"
style="@style/SettingsStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/full_text_search_index_rebuild" />
</LinearLayout>
<include layout="@layout/divider" />
<LinearLayout
android:id="@+id/ll_image_size"
style="@style/SettingsPanel"

View File

@@ -122,4 +122,9 @@
<string name="lea_api_error_need_upgrade_account">需要升级蚂蚁笔记账户</string>
<string name="webview_select_picture">选择图片</string>
<string name="note_not_load_completed">笔记未加载完,不能保存</string>
<string name="full_text_search">全文搜索</string>
<string name="full_text_search_index_rebuild">重建索引</string>
<string name="full_text_search_index_rebuild_success">索引构建成功</string>
<string name="full_text_search_rebuild_index_msg">这是一个很耗时的操作,你确定要重新构建索引么(默认情况下我们不推荐用户使用此功能)?</string>
<string name="progress_dialog_loading_msg">Processing&#8230;</string>
</resources>

View File

@@ -124,4 +124,9 @@
<string name="lea_api_error_need_upgrade_account">need upgrade Leanote account</string>
<string name="webview_select_picture">Select Picture</string>
<string name="note_not_load_completed">Notes are not loaded and cannot be saved</string>
<string name="full_text_search">Full text search</string>
<string name="full_text_search_index_rebuild">rebuild fts index</string>
<string name="full_text_search_index_rebuild_success">Index build succeeded</string>
<string name="full_text_search_rebuild_index_msg">This is a time-consuming operation. Are you sure you want to rebuild the index(We don\'t recommend users to use this feature by default)?</string>
<string name="progress_dialog_loading_msg">Loading&#8230;</string>
</resources>