📄 invertedlistmanager.java
字号:
package invertedList;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
* @author Administrator a core class that deal with index management. including
* creation and searching
*/
public class InvertedListManager implements IFileManagement, IQueryResult {
/**
* indexer to handle indexing process
*/
private AbstractIndexer indexer;
/**
* retain the collections of files
*/
private Set<FileRecord> fileList;
/**
* handle the saving and loading procedure of disk
*/
// private AbstractSavorAndLoader savorAndLoader;
// private Map<String,Set<String>> vagueInvertedList;
/**
* the form that saved in system as an index
*/
private Map<String, Map<FileRecord, SortedSet<Integer>>> allInvertedList;
private volatile static InvertedListManager uniqueInstance;
private InvertedListManager() {
fileList = new HashSet<FileRecord>();
// vagueInvertedList = new HashMap<String,Set<String>>();
allInvertedList = new HashMap<String, Map<FileRecord, SortedSet<Integer>>>();
// savorAndLoader = new DefaultSavorAndLoader(allInvertedList,fileList);
indexer = new DefaultIndexer(allInvertedList);
}
public static InvertedListManager getInstance() {
if (uniqueInstance == null) {
synchronized (InvertedListManager.class) {
if (uniqueInstance == null) {
uniqueInstance = new InvertedListManager();
}
}
}
return uniqueInstance;
}
/**
* @see invertedList.IFileManagement#addFile(java.io.File)
*
* after we add the file to our fileList, we index that file
*/
public void addFile(File file) {
FileRecord record = new FileRecord(file);
fileList.add(record);
index(file);
}
public void addFiles(File[] files) {
for (int i = 0; i < files.length; i++) {
if(files[i].isFile()){
addFile(files[i]);
}
else if(files[i].isDirectory()){
addFiles(files[i].listFiles());
}
}
}
/**
* @see invertedList.IFileManagement#reindex() reindex all the files in
* filelist, I don't recommend you to use this method frequently it is
* very time-consuming
*/
public void reindex() {
Iterator<FileRecord> iter = fileList.iterator();
while (iter.hasNext()) {
index(iter.next().getFile());
}
}
/**
* remove a file from the system, including index and file.
*/
public void removeFile(File file) {
fileList.remove(file);
}
/*
* (non-Javadoc)
*
* @see invertedList.IQueryResult#getQueryResult(java.lang.String)
*
* the query will simply be one word.
*/
/*
* public Set<String> getVagueQueryResult(String query) { Set<String>
* result = vagueInvertedList.get(query); if(result == null){ result = new
* HashSet<String>(); } return result; }
*/
/**
* get the result of a simple query
*/
public Map<FileRecord, SortedSet<Integer>> getAllQueryResult(String query) {
Map<FileRecord, SortedSet<Integer>> result = allInvertedList.get(query);
if (result == null) {
result = new HashMap<FileRecord, SortedSet<Integer>>();
}
return result;
}
/**
* index the file, add the specific file to the list
*
* @param file
*/
public void index(File file) {
indexer.doindex(file);
}
public Set<FileRecord> notQueryResult(Set<FileRecord> notList) {
Set<FileRecord> result = new HashSet<FileRecord>();
Iterator<FileRecord> iter = fileList.iterator();
while (iter.hasNext()) {
FileRecord currentRecord = iter.next();
if (!notList.contains(currentRecord)) {
result.add(currentRecord);
}
}
return result;
}
public void loadIndexFile(File dir, boolean force) {
if (force) {
loadFileList(dir);
// loadVagueIndex(dir);
loadInvertedList(dir);
} else {
if (fileList.isEmpty()) {
loadFileList(dir);
}
if (allInvertedList.isEmpty()) {
loadInvertedList(dir);
}
}
}
public void saveIndexFile(File directory) {
if (!directory.exists()) {
directory.mkdir();
}
saveInvertedList(directory);
saveFileList(directory);
}
/*
* private void saveVagueIndex(String dir){ try { ObjectOutputStream oos =
* new ObjectOutputStream(new FileOutputStream(new File(dir +
* "/VaugeIndex.xbq"))); oos.writeObject(vagueInvertedList); oos.flush();
* oos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated
* catch block System.out.println("Saving Failed, the vague index file can
* not be created."); } catch (IOException e) { // TODO Auto-generated catch
* block System.out.println("Saving Failed, the vague index file can not be
* written."); } }
*/
/*
* @SuppressWarnings("unchecked") private void loadVagueIndex(String dir){
* try { ObjectInputStream ois = new ObjectInputStream(new
* FileInputStream(new File(dir + "/VaugeIndex.xbq"))); vagueInvertedList =
* (Map<String,Set<String>>)ois.readObject(); ois.close(); } catch
* (FileNotFoundException e) { // TODO Auto-generated catch block
* System.out.println("Loading Failed. File can not be found."); } catch
* (IOException e) { // TODO Auto-generated catch block
* System.out.println("Loading Failed. File can not be read."); } catch
* (ClassNotFoundException e) { // TODO Auto-generated catch block
* e.printStackTrace(); } }
*/
@SuppressWarnings("unchecked")
protected void loadInvertedList(File dir) {
try {
if(!dir.exists()){
dir.mkdir();
}
SAXBuilder sb = new SAXBuilder();
Document myDocument = sb.build(dir.getPath() + "/AllIndex.xml");
Element root = myDocument.getRootElement();
List<Element> children = root.getChildren();
Iterator<Element> iter = children.iterator();
while(iter.hasNext()){
Element currentTerm = iter.next();
String term = currentTerm.getAttributeValue("name");
Iterator<Element> files = currentTerm.getChildren().iterator();
Map<FileRecord,SortedSet<Integer>> tempMap = new HashMap<FileRecord,SortedSet<Integer>>();
while(files.hasNext()){
Element currentFile = files.next();
String filename = currentFile.getAttributeValue("name");
long filesize = Long.parseLong(currentFile.getAttributeValue("size"));
long lastModified = Long.parseLong(currentFile.getAttributeValue("lastModified"));
FileRecord currentRecord = new FileRecord(filename,filesize,lastModified);
SortedSet<Integer> positions = new TreeSet<Integer>();
Iterator<Element> pos = currentFile.getChildren().iterator();
while(pos.hasNext()){
positions.add(Integer.parseInt(pos.next().getAttributeValue("pos")));
}
tempMap.put(currentRecord, positions);
}
allInvertedList.put(term, tempMap);
}
// ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
// new File(dir.getPath() + "/AllIndex.xbq")));
// allInvertedList = (Map<String, Map<FileRecord, SortedSet<Integer>>>) ois
// .readObject();
// ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Loading Failed. File can not be found.");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Loading Failed. File can not be read.");
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void saveInvertedList(File dir) {
try {
if(!dir.exists()){
dir.mkdir();
}
Element root = new Element("terms");
Iterator<String> terms = allInvertedList.keySet().iterator();
while(terms.hasNext()){
Element term = new Element("term");
String currentTerm = terms.next();
Attribute name = new Attribute("name",currentTerm);
term.setAttribute(name);
Map<FileRecord,SortedSet<Integer>> currentMap = allInvertedList.get(currentTerm);
Iterator<FileRecord> files = currentMap.keySet().iterator();
while(files.hasNext()){
Element file = new Element("file");
FileRecord record = files.next();
Attribute fileName = new Attribute("name", record.getName());
Attribute fileSize = new Attribute("size", Long.toString(record.getSize()));
Attribute lastModified = new Attribute("lastModified", Long.toString(record.getLastModified()));
file.setAttribute(fileName);
file.setAttribute(fileSize);
file.setAttribute(lastModified);
SortedSet<Integer> positions = currentMap.get(record);
Iterator<Integer> pos = positions.iterator();
while(pos.hasNext()){
Element position = new Element("position");
position.setAttribute(new Attribute("pos",Integer.toString(pos.next())));
file.addContent(position);
}
term.addContent(file);
}
root.addContent(term);
}
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
xmlOut.output(root,new FileOutputStream(new File(dir.getPath() + "/AllIndex.xml")));
//xmlOut.output(rootElement.getChild("name"),System.out);
//String outString = xmlOut.outputString(myDocument);
// ObjectOutputStream oos = new ObjectOutputStream(
// new FileOutputStream(new File(dir.getPath() + "/AllIndex.xbq")));
// oos.writeObject(allInvertedList);
// oos.flush();
// oos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out
.println("Saving Failed, the all index file can not be created.");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out
.println("Saving Failed, the all index file can not be written.");
}
}
@SuppressWarnings("unchecked")
protected void loadFileList(File dir) {
try {
if(!dir.exists()){
dir.mkdir();
}
SAXBuilder sb = new SAXBuilder();
Document myDocument = sb.build(dir.getPath() + "/fileIndex.xml");
Element root = myDocument.getRootElement();
List<Element> children = root.getChildren();
Iterator<Element> iter = children.iterator();
while(iter.hasNext()){
Element currentFile = iter.next();
String filename = currentFile.getAttributeValue("name");
long filesize = Long.parseLong(currentFile.getAttributeValue("size"));
long lastModified = Long.parseLong(currentFile.getAttributeValue("lastModified"));
fileList.add(new FileRecord(filename,filesize,lastModified));
}
// ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
// new File(dir.getPath() + "/fileIndex.xbq")));
// fileList = (Set<FileRecord>) ois.readObject();
//
// ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Loading Failed. File can not be found.");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Loading Failed. File can not be read.");
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void saveFileList(File dir) {
try {
if(!dir.exists()){
dir.mkdir();
}
Element root = new Element("files");
Iterator<FileRecord> files = fileList.iterator();
while(files.hasNext()){
FileRecord record = files.next();
Element currentFile = new Element("file");
Attribute fileName = new Attribute("name",record.getName());
Attribute fileSize = new Attribute("size",Long.toString(record.getSize()));
Attribute lastModified = new Attribute("lastModified",Long.toString(record.getLastModified()));
currentFile.setAttribute(fileName);
currentFile.setAttribute(fileSize);
currentFile.setAttribute(lastModified);
root.addContent(currentFile);
}
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
xmlOut.output(root,new FileOutputStream(new File(dir.getPath() + "/fileIndex.xml")));
// ObjectOutputStream oos = new ObjectOutputStream(
// new FileOutputStream(new File(dir.getPath() + "/fileIndex.xml")));
// oos.writeObject(fileList);
// oos.flush();
// oos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Saving Failed, file can not be created.");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Saving Failed, file can not be written.");
}
}
public Iterator<FileRecord> getAllFileRecords(){
return fileList.iterator();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -