//(c):Benja Fallenstein
package gzz.storm;
import gzz.util.HexUtil;
import java.io.*;
import java.util.*;
/** An IndexType for indexing pointers.
*/
public abstract class PointerIndexType implements IndexedPool.IndexType {
/** The URI identifying the pointer application of Storm indexing.
*/
public static final String indexTypeURI =
"urn:urn-5:Y15xnzrC4wWfm-YWnziE8aH3d3pR";
public static final String contentType = "application/x-gzigzag-ptr";
/** The interface createIndex()
returns instances of.
* Usually this is not used directly, but through
* the IndexedPool.getPointer()
convenience method.
*/
public interface Index {
/**
* Mustn't return null even if there is no pointer block
* corresponding to this pointer yet (otherwise,
* we could not create new pointers in the first place).
*/
Pointer getPointer(String uri);
}
abstract public Object getIndex(IndexedPool pool, IndexedPool.DB db)
throws IOException;
public final String getIndexTypeURI() {
return indexTypeURI;
}
/** The default implementation of getting the mapping
* for a pointer block. Subclasses which override this
* must provide exactly the same functionality;
* the only reason to override would be if the subclass
* had a more efficient way to parse pointer blocks.
*/
public Set getMappings(Block b) throws IOException {
String blockContentType =
b.getHeader().get("Content-Type").toLowerCase();
if(blockContentType.equals(contentType)) {
BufferedReader br = new BufferedReader(
new InputStreamReader(b.getInputStream(), "ISO8859_1"));
String s = br.readLine();
if(!s.equals("GZZPTR0"))
// Unknown pointer block format-- ignore
return Collections.EMPTY_SET;
byte[] key = br.readLine().getBytes("US-ASCII");
List items = new ArrayList(3);
int len = 0;
String hex;
while((hex = br.readLine()) != null) {
byte[] arr = HexUtil.hexToByteArr(hex);
if(arr.length > 127)
throw new IOException("Storm id is too long: "+hex);
items.add(arr);
len += arr.length + 1;
}
br.close();
byte[] value = new byte[len];
int pos = 0;
for(Iterator i=items.iterator(); i.hasNext();) {
byte[] arr = (byte[])i.next();
value[pos] = (byte)arr.length;
pos++;
System.arraycopy(arr, 0, value, pos, arr.length);
pos += arr.length;
}
IndexedPool.Mapping m =
new IndexedPool.Mapping(b.getId(), key, value);
return Collections.singleton(m);
} else {
return Collections.EMPTY_SET;
}
}
}