📄 cmcache.java
字号:
/*
* Copyright (c) 2001 Sun Microsystems, Inc. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more
* information on Project JXTA, please see
* <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache Foundation.
*
* $Id: CmCache.java,v 1.2 2002/03/04 21:38:13 echtcherbina Exp $
*/
package net.jxta.impl.cm;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;
//PDA requirements 19.02.2002
//java.util.Iterator -> java.util.Enumeration
//import java.util.Iterator;
import java.util.Enumeration;
//PDA requirements 19.02.2002
import java.util.Vector;
import net.jxta.document.Element;
import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.document.StructuredTextDocument;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
/**
* A simple cache to be used by the Cm. It is designed to be memory efficient,
* and it is limited to a small number of fields to cache/index
*/
public class CmCache implements Runnable {
private final static Category LOG = Category.getInstance(CmCache.class.getName());
private Hashtable caches = new Hashtable();
private File directory;
private String[] keys;
/**
* Constructor for the CmCache object
*
*@param keys
*@param dir
*/
public CmCache(String[] keys, File dir) {
for (int i = 0; i < keys.length; i++) {
Hashtable cache = new Hashtable();
if (LOG.isDebugEnabled()) {
LOG.debug(" Created Table [" + keys[i] + "]");
}
caches.put(keys[i], cache);
}
this.directory = dir;
this.keys = keys;
}
/**
* add a cache entry
*
*@param attribute Attribute String to query on
*@param value value of the attribute string
*@param path in a specific path, if null specified search in all
* paths
*/
public synchronized void add(String attribute, String value, String path) {
Hashtable tbl = (Hashtable) caches.get(attribute);
value = value.toUpperCase();
if (!tbl.containsKey(value)) {
Vector vec = new Vector();
//PDA requirements 19.02.2002
//java.util.Vector.add(Object obj) -> java.util.Vector.addElement(Object obj)
//vec.add(path);
vec.addElement(path);
//PDA requirements 19.02.2002
tbl.put(value, vec);
} else {
Vector vec = (Vector) tbl.get(value);
if ( ! vec.contains(path) ) {
//PDA requirements 19.02.2002
//java.util.Vector.add(Object obj) -> java.util.Vector.addElement(Object obj)
//vec.add(path);
vec.addElement(path);
//PDA requirements 19.02.2002
}
}
}
/**
* Determines whether this object is cachine a particular key
*
*@param key
*@return true if the object is caching key
*/
public boolean containsKey(String key) {
return caches.containsKey(key);
}
/**
* remove a file entry from cache
*
*@param path relative path
*/
public synchronized void remove(String path) {
Enumeration caTbl = caches.elements();
while (caTbl.hasMoreElements()) {
Hashtable tbl = (Hashtable) caTbl.nextElement();
// remove all entries that contain value of "path"
//PDA requirements 19.02.2002
//java.util.Iterator -> java.util.Enumeration
//Iterator values = tbl.values().iterator();
Enumeration values = tbl.elements();
//while (values.hasNext()) {
while (values.hasMoreElements()) {
//Vector vec = (Vector) values.next();
Vector vec = (Vector) values.nextElement();
for (int i = 0; i < vec.size(); i++) {
if (((String) vec.elementAt(i)).equals(path)) {
//java.util.Vector.remove(int index) -> java.util.Vector.removeElementAt(int index)
//vec.remove(i);
vec.removeElementAt(i);
// shift to the left
i--;
}
}
}
//PDA requirements 19.02.2002
}
}
/**
* Query the cache
*
*@param attribute Attribute String to query on
*@param value value of the attribute string
*@return an enumeration of canonical paths
*/
public synchronized Enumeration query(String attribute, String value) {
boolean endswith = false;
boolean startswith = false;
if (value.charAt(0) == '*') {
endswith = true;
value = value.substring(1, value.length());
}
if (value.charAt(value.length() - 1) == '*') {
startswith = true;
value = value.substring(0, value.indexOf("*"));
}
// '*' by itself not allowed
if (value.length() == 0) {
return null;
}
Hashtable tbl = (Hashtable) caches.get(attribute);
if (!endswith && !startswith) {
Vector res = (Vector) tbl.get(value.toUpperCase());
if (res != null) {
if (LOG.isDebugEnabled()) {
LOG.debug(attribute + " Found " + res.size());
}
return res.elements();
}
} else {
Vector result = new Vector();
Enumeration keys = tbl.keys();
while (keys.hasMoreElements()) {
String val = (String) keys.nextElement();
String uVal = ((String) val).toUpperCase();
String uValue = ((String) value).toUpperCase();
if (startswith && !endswith) {
if (uVal.startsWith(uValue)) {
addTo(result, (Vector) tbl.get(val));
}
} else if (endswith && !startswith) {
if (uVal.endsWith(uValue)) {
addTo(result, (Vector) tbl.get(val));
}
} else if (startswith && endswith) {
if (uVal.indexOf(uValue) >= 0) {
addTo(result, (Vector) tbl.get(val));
}
}
}
return result.elements();
}
return null;
}
/**
* Adds content of one vector to another
*
*@param to vector where elements to be copied into
*@param from vector where elements to be copied from
*/
private void addTo(Vector to, Vector from) {
Enumeration enum = from.elements();
while (enum.hasMoreElements()) {
//PDA requirements 19.02.2002
//java.util.Vector.add(Object obj) -> java.util.Vector.addElement(Object obj)
//to.add(enum.nextElement());
to.addElement(enum.nextElement());
//PDA requirements 19.02.2002
}
}
/**
* a private method to parse a file
*
*@param file
*@return
*/
private StructuredDocument restoreFile(File file) {
InputStream ip = null;
StructuredDocument doc = null;
try {
ip = new FileInputStream(file);
doc = StructuredDocumentFactory.newStructuredDocument
(new MimeMediaType("text/xml"), ip);
} catch (IOException e) {
} catch (RuntimeException re) {
// thrown by LiteXMLDocument, we should not fail here
// move on
} finally {
if (ip != null) {
try {
ip.close();
} catch (IOException e) {
if (LOG.isDebugEnabled() ) {
LOG.debug("error closing file",e);
}
}
}
}
return doc;
}
/**
* Index a document
*
*@param path
*@param doc
*/
public synchronized void add(String path, StructuredDocument doc) {
for (int i = 0; i < keys.length; i++) {
Enumeration enum = doc.getChildren(keys[i]);
while (enum.hasMoreElements()) {
add(keys[i], (String) ((Element) enum.nextElement()).getValue(), path);
}
}
}
/**
* adds a file to the index
*
*@param path
*/
public synchronized void add(String path) {
File adv = new File(directory, path);
StructuredDocument doc = restoreFile(adv);
add(path, doc);
}
/**
* run method for the CmCache , this builds the index for the given
* directory
*/
public void run() {
long startTime;
long endTime;
long totalTime;
StructuredDocument doc;
File adv;
if (LOG.isDebugEnabled()) {
LOG.debug("Caching thread started");
}
String[] files = directory.list();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (LOG.isDebugEnabled()) {
LOG.debug("Indexing " + files[i]);
}
adv = new File(directory, files[i]);
startTime = System.currentTimeMillis();
doc = restoreFile(adv);
if ( doc != null ) {
add(files[i], doc);
}
if (LOG.isDebugEnabled()) {
endTime = System.currentTimeMillis();
totalTime = endTime - startTime;
LOG.debug("Total time to parse and index " + totalTime);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -