/*
* Copyright 2010 Bizosys Technologies Limited
*
* Licensed to the Bizosys Technologies Limited (Bizosys) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The Bizosys licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bizosys.hsearch.dictionary;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.bizosys.oneline.ApplicationFault;
import com.bizosys.oneline.SystemFault;
import com.bizosys.oneline.services.batch.BatchTask;
/**
* Loads the dictionry terms to memory in intervals.
* It helps for fuzzy search and regex search.
* @author karan
*/
public class DictionaryRefresh implements BatchTask {
/**
* The job name
*/
private String jobName = "DictionaryRefresh";
public String getJobName() {
return this.jobName;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
public Object process() throws ApplicationFault, SystemFault {
DictionaryManager manager = DictionaryManager.getInstance();
Map<String, Dictionary> dicts = manager.getCachedTenants();
int extraCached = dicts.size() - manager.maxCacheSize;
if ( extraCached < 0) return true;
List<Dictionary> lstDicts = new ArrayList<Dictionary>();
Collections.sort(lstDicts, new DictionaryUsageComparator());
/**
* Unload the unused pieces which have been loaded for some time.
*/
for ( int i=0; i< extraCached; i++) {
Dictionary dict = lstDicts.get(i);
dicts.remove(dict.tenant);
dict.clean();
}
/**
* Refresh the rests
*/
for ( Dictionary aDict: dicts.values()) {
aDict.buildTerms();
}
return true;
}
}