//(c):Benja Fallenstein package gzz.storm; import java.io.IOException; import java.util.*; /** For testing purposes: An index of blocks by content-type */ public class ContentTypeIndexType implements IndexedPool.IndexType { public static final String contentTypeIndexTypeURI = "urn:urn-5:JQLckWcNz56B6GSQZyvA6HkEzAI5:1"; public static class Index { protected IndexedPool pool; protected IndexedPool.DB db; public Index(IndexedPool pool, IndexedPool.DB db) { this.pool = pool; this.db = db; } /** Get all blocks matching a given Content-Type. * XXX For real use, this should not block. */ public Set getBlocks(String contentType) throws IOException { byte[] key = contentType.getBytes("US-ASCII"); Collection mappings = db.get(key).block(); Set result = new HashSet(mappings.size()); for(Iterator i=mappings.iterator(); i.hasNext();) { IndexedPool.Mapping m = (IndexedPool.Mapping)i.next(); if(!java.util.Arrays.equals(key, m.key)) throw new Error("Key does not match"); if(m.value.length != 0) throw new Error("Value does not match"); result.add(m.block); } return result; } } public Set getMappings(Block block) throws IOException { String ct = block.getHeader().get("Content-Type"); byte[] key = ct.getBytes("US-ASCII"); byte[] value = new byte[0]; IndexedPool.Mapping mapping = new IndexedPool.Mapping(block.getId(), key, value); Set result = Collections.singleton(mapping); return result; } public Object createIndex(IndexedPool pool, IndexedPool.DB db) { return new Index(pool, db); } public String getIndexTypeURI() { return contentTypeIndexTypeURI; } }