Author: glandais Date: 2007-12-10 15:20:06 +0000 (Mon, 10 Dec 2007) New Revision: 59 Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/Database.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java Log: Database ne traite plus que les metadata Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/Database.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/Database.java 2007-12-10 15:19:28 UTC (rev 58) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/Database.java 2007-12-10 15:20:06 UTC (rev 59) @@ -1,16 +1,16 @@ package fr.cemagref.simexplorer.is.storage.database; +import java.io.Reader; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; -import fr.cemagref.simexplorer.is.storage.entities.Entity; -import fr.cemagref.simexplorer.is.storage.entities.LoggableElement; -import fr.cemagref.simexplorer.is.storage.entities.Version; +import fr.cemagref.simexplorer.is.storage.entities.metadata.MetaDataEntity; +import fr.cemagref.simexplorer.is.storage.entities.metadata.Version; /** - * Interface of a dabase able to manage elements + * Interface of a database able to manage metadata elements * * @author landais * @@ -40,8 +40,20 @@ * Element to insert * @throws Exception */ - public abstract void insertElement(LoggableElement element) throws Exception; + public abstract void insertElement(MetaDataEntity element) throws Exception; + /** + * Index content for an element + * + * @param entity + * Related entity + * @param field + * Related field + * @param is + * Content + */ + public abstract void indexAdd(MetaDataEntity entity, List<Reader> readers) + throws Exception; // Read @@ -55,7 +67,7 @@ * @return Fund element, null if not fund * @throws Exception */ - public abstract LoggableElement getElement(String uuid, Version version) + public abstract MetaDataEntity getElement(String uuid, Version version) throws Exception; /** @@ -77,9 +89,8 @@ * exists * @throws Exception */ - public LoggableElement getElementLatestVersion(String uuid) - throws Exception { - LoggableElement result = null; + public MetaDataEntity getElementLatestVersion(String uuid) throws Exception { + MetaDataEntity result = null; // Get all versions List<Version> versions = getVersions(uuid); @@ -109,7 +120,7 @@ * @return Element list * @throws Exception */ - public abstract Set<LoggableElement> findElementsById(String uuid) + public abstract Set<MetaDataEntity> findElementsById(String uuid) throws Exception; /** @@ -120,24 +131,10 @@ * @return Element list * @throws Exception */ - public abstract Set<LoggableElement> findElementsByProperties( + public abstract Set<MetaDataEntity> findElementsByProperties( Map<String, String> properties) throws Exception; /** - * Retrieve elements of a class with specific properties - * - * @param properties - * Matching properties needed - * @param entityClass - * Class searched - * @return Element list - * @throws Exception - */ - public abstract Set<LoggableElement> findElementsByProperties( - Map<String, String> properties, Class<? extends Entity> entityClass) - throws Exception; - - /** * Retrieve elements by content search * * @param searchedText @@ -145,7 +142,7 @@ * @return Element list * @throws Exception */ - public abstract Set<LoggableElement> findElementsByContentSearch( + public abstract Set<MetaDataEntity> findElementsByContentSearch( String queryText) throws Exception; // Delete @@ -157,7 +154,7 @@ * Element to delete * @throws Exception */ - public void deleteElement(LoggableElement element) throws Exception { + public void deleteElement(MetaDataEntity element) throws Exception { deleteElement(element.getUuid(), element.getVersion()); } Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java 2007-12-10 15:19:28 UTC (rev 58) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java 2007-12-10 15:20:06 UTC (rev 59) @@ -9,10 +9,6 @@ public interface DatabaseConstants { /** - * Field name for class name - */ - public static final String KEY_CLASSNAME = "simexplorer.classname"; - /** * Field name for id */ public static final String KEY_UUID = "simexplorer.uuid"; @@ -21,9 +17,9 @@ */ public static final String KEY_NAME = "simexplorer.name"; /** - * Field name for hash + * Field name for type */ - public static final String KEY_HASH = "simexplorer.hash"; + public static final String KEY_TYPE = "simexplorer.type"; /** * Field name for description */ @@ -33,20 +29,36 @@ */ public static final String KEY_VERSION = "simexplorer.version"; /** - * Field name for type - */ - public static final String KEY_TYPE = "simexplorer.type"; - /** * Field name for creation date */ public static final String KEY_CREATIONDATE = "simexplorer.creationdate"; /** + * Field name for hash + */ + public static final String KEY_HASH = "simexplorer.hash"; + /** + * Field name for descriptor + */ + public static final String KEY_DESCRIPTOR = "simexplorer.descriptor"; + /** * Field name for id */ + public static final String KEY_PARENTDATA_UUID = "simexplorer.parentdata.uuid"; + /** + * Field name for id + */ + public static final String KEY_PARENTDATA_VERSION = "simexplorer.parentdata.version"; + /** + * Field name for id + */ public static final String KEY_PARENTVERSION_UUID = "simexplorer.parentversion.uuid"; /** * Field name for id */ public static final String KEY_PARENTVERSION_VERSION = "simexplorer.parentversion.version"; + /** + * Field name for searchable content + */ + public static final String KEY_SEARCHABLE_CONTENT = "simexplorer.searchablecontent"; } Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java 2007-12-10 15:19:28 UTC (rev 58) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java 2007-12-10 15:20:06 UTC (rev 59) @@ -1,8 +1,9 @@ package fr.cemagref.simexplorer.is.storage.database.lucene; -import java.io.ByteArrayInputStream; -import java.io.InputStream; +import java.io.Reader; +import java.io.StringReader; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -15,6 +16,7 @@ import org.apache.lucene.analysis.SimpleAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; +import org.apache.lucene.document.Fieldable; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; @@ -27,14 +29,10 @@ import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; -import org.w3c.dom.Element; import fr.cemagref.simexplorer.is.storage.database.Database; -import fr.cemagref.simexplorer.is.storage.entities.Entity; -import fr.cemagref.simexplorer.is.storage.entities.LoggableElement; -import fr.cemagref.simexplorer.is.storage.entities.Version; -import fr.cemagref.simexplorer.is.storage.factories.EntityFactory; -import fr.cemagref.simexplorer.is.storage.factories.LoggableElementFactory; +import fr.cemagref.simexplorer.is.storage.entities.metadata.MetaDataEntity; +import fr.cemagref.simexplorer.is.storage.entities.metadata.Version; /** * Lucene implementation @@ -96,23 +94,12 @@ } @Override - public void insertElement(LoggableElement element) throws Exception { - // Add all children to database - Set<LoggableElement> children = element.getChildren(); - for (LoggableElement loggableElement : children) { - insertElement(loggableElement); - } - - // Retrieve element factory - Class<? extends LoggableElement> clazz = element.getClass(); - LoggableElementFactory<LoggableElement> elementFactory = (LoggableElementFactory<LoggableElement>) LoggableElementFactory - .getFactory(clazz); - + public void insertElement(MetaDataEntity element) throws Exception { // Save element to a Lucene document - Document document = saveLuceneElement(elementFactory, element); + Document document = saveLuceneElement(element); // Retrieve existing element with same id/version - LoggableElement oldElement = getElement(element.getUuid(), element + MetaDataEntity oldElement = getElement(element.getUuid(), element .getVersion()); // Delete element if exists, for update if (oldElement != null) { @@ -120,7 +107,6 @@ deleteElement(element); } - // FIXME thread safe // add document to index writer.addDocument(document); // write index @@ -128,20 +114,14 @@ } @Override - public LoggableElement getElement(String uuid, Version version) + public MetaDataEntity getElement(String uuid, Version version) throws Exception { - LoggableElement result = null; + MetaDataEntity result = null; - // Retrieve element with id/version - Map<String, String> properties = new HashMap<String, String>(); - properties.put(KEY_UUID, uuid); - properties.put(KEY_VERSION, version.toString()); - - // Search elements - LuceneHits hits = findHits(properties); + LuceneHits hits = getHitsByIdVersion(uuid, version); if (hits.getHits().length() != 0) { // convert first document to element - result = convertDocumentToElement(hits.getHits().doc(0)); + result = loadLuceneElement(hits.getHits().doc(0)); } hits.closeSearcher(); @@ -149,6 +129,24 @@ return result; } + /** + * @param uuid + * @param version + * @return + * @throws Exception + */ + private LuceneHits getHitsByIdVersion(String uuid, Version version) + throws Exception { + // Retrieve element with id/version + Map<String, String> properties = new HashMap<String, String>(); + properties.put(KEY_UUID, uuid); + properties.put(KEY_VERSION, version.toString()); + + // Search elements + LuceneHits hits = findHits(properties); + return hits; + } + @Override public List<Version> getVersions(String uuid) throws Exception { // Retrieve all document corresponding to id @@ -165,9 +163,9 @@ return versions; } - + @Override - public Set<LoggableElement> findElementsById(String uuid) throws Exception { + public Set<MetaDataEntity> findElementsById(String uuid) throws Exception { // Create hash map with id Map<String, String> properties = new HashMap<String, String>(); properties.put(KEY_UUID, uuid); @@ -176,43 +174,31 @@ } @Override - public Set<LoggableElement> findElementsByProperties( + public Set<MetaDataEntity> findElementsByProperties( Map<String, String> properties) throws Exception { // Return all elements parsed from documents fund return convertHitsToElements(findHits(properties)); } @Override - public Set<LoggableElement> findElementsByProperties( - Map<String, String> properties, Class<? extends Entity> entityClass) - throws Exception { - // Create hash map with properties and entity class name - Map<String, String> props = new HashMap<String, String>(); - props.putAll(properties); - props.put(KEY_CLASSNAME, entityClass.getName()); - // Return all elements parsed from documents fund - return convertHitsToElements(findHits(properties)); - } - - @Override - public Set<LoggableElement> findElementsByContentSearch(String queryText) - throws Exception { + public Set<MetaDataEntity> findElementsByContentSearch(String queryText) throws Exception { // Create a searcher Searcher searcher = new IndexSearcher(indexDirectory); Analyzer analyzer = new SimpleAnalyzer(); - QueryParser parser = new QueryParser("__all__", analyzer); + QueryParser parser = new QueryParser(KEY_SEARCHABLE_CONTENT, analyzer); Query luceneQuery = parser.parse(queryText); - + // Create a bean with hits and searcher - LuceneHits luceneHits = new LuceneHits(searcher.search(luceneQuery), searcher); - + LuceneHits luceneHits = new LuceneHits(searcher.search(luceneQuery), + searcher); + // Convert hits to elements - Set<LoggableElement> result = convertHitsToElements(luceneHits); - - return result; + Set<MetaDataEntity> result = convertHitsToElements(luceneHits); + + return result; } - + @Override public void deleteElements(String uuid) throws Exception { // Delete all documents by term search @@ -223,7 +209,7 @@ @Override public void deleteElement(String uuid, Version version) throws Exception { // Delete element in db with term search - Term[] terms = new Term[3]; + Term[] terms = new Term[2]; terms[0] = new Term(KEY_UUID, uuid); terms[1] = new Term(KEY_VERSION, version.toString()); writer.deleteDocuments(terms); @@ -232,7 +218,9 @@ /** * Find all documents matching properties - * @param properties criteria + * + * @param properties + * criteria * @return Documents and search handle * @throws Exception */ @@ -254,35 +242,21 @@ } /** - * Convert a Lucene Document to a real entity - * @param document Storage - * @return Element - * @throws Exception - */ - private LoggableElement convertDocumentToElement(Document document) - throws Exception { - // Retrieve element factory via class name - String className = document.get(KEY_CLASSNAME); - LoggableElementFactory<LoggableElement> factory = (LoggableElementFactory<LoggableElement>) LoggableElementFactory - .getFactory(className); - // Rebuild element - return loadLuceneElement(factory, document); - } - - /** * Convert all Lucene documents to real entities - * @param hits Document collection + * + * @param hits + * Document collection * @return Entities list * @throws Exception */ - private Set<LoggableElement> convertHitsToElements(LuceneHits hits) + private Set<MetaDataEntity> convertHitsToElements(LuceneHits hits) throws Exception { // For all document - Set<LoggableElement> elements = new HashSet<LoggableElement>(); + Set<MetaDataEntity> elements = new HashSet<MetaDataEntity>(); for (int i = 0; i < hits.getHits().length(); i++) { Document document = hits.getHits().doc(i); // Convert to element - elements.add(convertDocumentToElement(document)); + elements.add(loadLuceneElement(document)); } // Close handle hits.closeSearcher(); @@ -291,47 +265,143 @@ /** * Transform a Lucene document into a LoggableElement thanks to a factory - * @param factory The element factory - * @param document The document to transform + * + * @param factory + * The element factory + * @param document + * The document to transform * @return Instance of element * @throws Exception */ - private LoggableElement loadLuceneElement( - LoggableElementFactory<LoggableElement> factory, Document document) - throws Exception { - LoggableElement element = factory.createInstance(); + private MetaDataEntity loadLuceneElement(Document document) + throws Exception { + MetaDataEntity element = new MetaDataEntity(); + element.setUuid(document.get(KEY_UUID)); element.setName(document.get(KEY_NAME)); - // FIXME toutes les metadonnées - + element.setType(document.get(KEY_TYPE)); + element.setDescription(document.get(KEY_DESCRIPTION)); + element.setVersion(document.get(KEY_VERSION)); + String creationDate = document.get(KEY_CREATIONDATE); + long creationDateLong = new Long(creationDate); + Date date = new Date(creationDateLong); + element.setCreationDate(date); + element.setHash(document.get(KEY_HASH)); + + Map<String, String> descriptors = new HashMap<String, String>(); + List<Fieldable> fields = document.getFields(); + for (Fieldable fieldable : fields) { + if (fieldable.name().startsWith(KEY_DESCRIPTOR)) { + String field = fieldable.name().replace(KEY_DESCRIPTOR + ".", + ""); + String value = fieldable.stringValue(); + descriptors.put(field, value); + } + } + element.setDescriptors(descriptors); + + String parentId = document.get(KEY_PARENTDATA_UUID); + String parentVersion = document.get(KEY_PARENTDATA_VERSION); + if (parentId != null && parentVersion != null) { + MetaDataEntity parentData = getElement(parentId, new Version( + parentVersion)); + element.setParentData(parentData); + } + + parentId = document.get(KEY_PARENTVERSION_UUID); + parentVersion = document.get(KEY_PARENTVERSION_VERSION); + if (parentId != null && parentVersion != null) { + MetaDataEntity parentByVersion = getElement(document + .get(KEY_PARENTVERSION_UUID), new Version(document + .get(KEY_PARENTVERSION_VERSION))); + element.setParentVersion(parentByVersion); + } + return element; } + private Field simpleField(String name, String value) { + return new Field(name, value, Field.Store.YES, Field.Index.UN_TOKENIZED); + } + /** * Transform an element to a Lucene document thanks to a factory + * * @param factory * @param element * @return * @throws Exception */ - private Document saveLuceneElement( - LoggableElementFactory<LoggableElement> factory, - LoggableElement element) throws Exception { + private Document saveLuceneElement(MetaDataEntity element) throws Exception { Document document = new Document(); - // FIXME save searchable/metadata fields - document.add(new Field(KEY_UUID, element.getUuid(), Field.Store.YES, - Field.Index.UN_TOKENIZED)); - document.add(new Field(KEY_DESCRIPTION, element.getDescription(), - Field.Store.YES, Field.Index.UN_TOKENIZED)); - document.add(new Field(KEY_CLASSNAME, element.getClass().getName(), - Field.Store.YES, Field.Index.UN_TOKENIZED)); - document.add(new Field(KEY_VERSION, element.getVersion().toString() - .toString(), Field.Store.YES, Field.Index.UN_TOKENIZED)); + document.add(simpleField(KEY_UUID, element.getUuid())); + document.add(simpleField(KEY_NAME, element.getName())); + document.add(simpleField(KEY_TYPE, element.getType())); + document.add(simpleField(KEY_DESCRIPTION, element.getDescription())); + document.add(simpleField(KEY_VERSION, element.getVersion().toString())); + document.add(simpleField(KEY_CREATIONDATE, Long.toString(element + .getCreationDate().getTime()))); + document.add(simpleField(KEY_HASH, element.getHash())); - // FIXME ne pas serializer - // Serialize element into document + Map<String, String> descriptors = element.getDescriptors(); + for (Map.Entry<String, String> entry : descriptors.entrySet()) { + String key = KEY_DESCRIPTOR + "." + entry.getKey(); + String value = entry.getValue(); + document.add(simpleField(key, value)); + } + if (element.getParentData() != null) { + document.add(simpleField(KEY_PARENTDATA_UUID, element + .getParentData().getUuid())); + document.add(simpleField(KEY_PARENTDATA_VERSION, element + .getParentData().getVersion().toString())); + } + if (element.getParentVersion() != null) { + document.add(simpleField(KEY_PARENTVERSION_UUID, element + .getParentVersion().getUuid())); + document.add(simpleField(KEY_PARENTVERSION_VERSION, element + .getParentVersion().getVersion().toString())); + } + return document; } + @Override + public void indexAdd(MetaDataEntity entity, List<Reader> readers) + throws Exception { + + // search associated document + LuceneHits hits = getHitsByIdVersion(entity.getUuid(), entity + .getVersion()); + + if (hits.getHits().length() > 0) { + + // retrieve document + Document document = hits.getHits().doc(0); + + StringBuffer buf = new StringBuffer(); + for (Reader reader : readers) { + for(int c = reader.read(); c != -1; c = reader.read()) { + buf.append((char)c); + } + } + + Reader reader = new StringReader(buf.toString()); + document.add(new Field(KEY_SEARCHABLE_CONTENT, reader)); + + hits.closeSearcher(); + + // update document + // delete old document + deleteElement(document.get(KEY_UUID), new Version(document + .get(KEY_VERSION))); + // add document to index + writer.addDocument(document); + // write index + writer.flush(); + + } + + } + }
participants (1)
-
glandais@users.labs.libre-entreprise.org