Author: glandais Date: 2007-12-05 15:23:47 +0000 (Wed, 05 Dec 2007) New Revision: 41 Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/DatabaseConstants.java trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneHits.java Removed: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneConstants.java Modified: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/Database.java trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneDatabase.java Log: Refactoring database Modified: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/Database.java =================================================================== --- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/Database.java 2007-12-05 15:23:08 UTC (rev 40) +++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/Database.java 2007-12-05 15:23:47 UTC (rev 41) @@ -5,44 +5,44 @@ import org.cemagref.simexplorer.si.storage.entities.LoggableElement; -public abstract class Database { +public abstract class Database implements DatabaseConstants { - public Database() throws Throwable { + public Database() throws Exception { super(); init(); } - protected abstract void init() throws Throwable; + protected abstract void init() throws Exception; // Create / Update - public abstract void pushElement(LoggableElement element) throws Throwable; + public abstract void pushElement(LoggableElement element) throws Exception; // Read public abstract LoggableElement findElement(String uuid, Integer majorVersion, - Integer minorVersion) throws Throwable; + Integer minorVersion) throws Exception; public abstract LoggableElement getElementLatestVersion(String uuid) - throws Throwable; + throws Exception; - public abstract Set<LoggableElement> findElementsById(String uuid) throws Throwable; + public abstract Set<LoggableElement> findElementsById(String uuid) throws Exception; - public abstract Set<LoggableElement> findElementsByProperties(Map<String, String> properties) throws Throwable; + public abstract Set<LoggableElement> findElementsByProperties(Map<String, String> properties) throws Exception; - public abstract Set<LoggableElement> findElementsByProperties(Map<String, String> properties, Class entityClass) throws Throwable; + public abstract Set<LoggableElement> findElementsByProperties(Map<String, String> properties, Class entityClass) throws Exception; // Delete - public void deleteElement(LoggableElement element) throws Throwable { + public void deleteElement(LoggableElement element) throws Exception { deleteElement(element.getUuid(), element.getMajorVersion(), element .getMinorVersion()); } - public abstract void deleteElement(String uuid) throws Throwable; + public abstract void deleteElements(String uuid) throws Exception; public abstract void deleteElement(String uuid, Integer majorVersion, - Integer minorVersion) throws Throwable; + Integer minorVersion) throws Exception; } Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/DatabaseConstants.java =================================================================== --- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/DatabaseConstants.java (rev 0) +++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/DatabaseConstants.java 2007-12-05 15:23:47 UTC (rev 41) @@ -0,0 +1,16 @@ +package org.cemagref.simexplorer.si.storage.database; + +public interface DatabaseConstants { + + public static final String KEY_CLASSNAME = "simexplorer.classname"; + public static final String KEY_UUID = "simexplorer.uuid"; + public static final String KEY_NAME = "simexplorer.name"; + public static final String KEY_HASH = "simexplorer.hash"; + public static final String KEY_DESCRIPTION = "simexplorer.description"; + public static final String KEY_MAJORVERSION = "simexplorer.version.major"; + public static final String KEY_MINORVERSION = "simexplorer.version.minor"; + public static final String KEY_TYPE = "simexplorer.type"; + public static final String KEY_CREATIONDATE = "simexplorer.creationdate"; + public static final String KEY_XML = "simexplorer.xml"; + +} Deleted: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneConstants.java =================================================================== --- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneConstants.java 2007-12-05 15:23:08 UTC (rev 40) +++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneConstants.java 2007-12-05 15:23:47 UTC (rev 41) @@ -1,11 +0,0 @@ -package org.cemagref.simexplorer.si.storage.database.lucene; - -public interface LuceneConstants { - - public static final String KEY_CLASSNAME = "simexplorer.classname"; - public static final String KEY_UUID = "simexplorer.uuid"; - public static final String KEY_DESCRIPTION = "simexplorer.description"; - public static final String KEY_MAJORVERSION = "simexplorer.version.major"; - public static final String KEY_MINORVERSION = "simexplorer.version.minor"; - -} Modified: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneDatabase.java =================================================================== --- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneDatabase.java 2007-12-05 15:23:08 UTC (rev 40) +++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneDatabase.java 2007-12-05 15:23:47 UTC (rev 41) @@ -1,7 +1,6 @@ package org.cemagref.simexplorer.si.storage.database.lucene; import java.io.ByteArrayInputStream; -import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.util.ArrayList; @@ -23,58 +22,56 @@ import org.apache.lucene.analysis.SimpleAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; -import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; -import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Searcher; import org.apache.lucene.search.TermQuery; -import org.cemagref.simexplorer.si.storage.dao.ElementDAOFactory; -import org.cemagref.simexplorer.si.storage.dao.factories.LoggableElementFactory; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FSDirectory; import org.cemagref.simexplorer.si.storage.database.Database; import org.cemagref.simexplorer.si.storage.entities.LoggableElement; +import org.cemagref.simexplorer.si.storage.factories.LoggableElementFactory; import org.cemagref.simexplorer.si.storage.tools.Version; import org.w3c.dom.Element; -public class LuceneDatabase extends Database implements LuceneConstants {// implements - // LuceneDatabaseInterface - // { +public class LuceneDatabase extends Database { - private static Analyzer analyser; - private static IndexWriter writer; - private static Searcher searcher; - private static boolean initok = false; + private Analyzer analyser; + private String indexDirectory; + private IndexWriter writer; + private boolean initok = false; - public LuceneDatabase() throws Throwable { + public LuceneDatabase() throws Exception { super(); } - protected void init() throws Throwable { + protected void init() throws Exception { if (!initok) { analyser = new PerFieldAnalyzerWrapper(new SimpleAnalyzer()); // analyser.addAnalyzer("firstname", new KeywordAnalyzer()); // analyser.addAnalyzer("lastname", new KeywordAnalyzer()); - String directory = "./index/"; + indexDirectory = "./index/"; boolean create = false; - if (!IndexReader.indexExists(directory)) { - // si l'index n'existe pas, on force la creation + if (!IndexReader.indexExists(indexDirectory)) { create = true; } - writer = new IndexWriter(directory, analyser, create); - searcher = new IndexSearcher(directory); + + Directory dir = FSDirectory.getDirectory(indexDirectory); + + writer = new IndexWriter(dir, true, analyser, create); initok = true; } } @Override - public void pushElement(LoggableElement element) throws Throwable { - LoggableElementFactory elementFactory = (LoggableElementFactory) ElementDAOFactory + public void pushElement(LoggableElement element) throws Exception { + LoggableElementFactory elementFactory = (LoggableElementFactory) LoggableElementFactory .getFactory(element.getClass()); Document document = saveLuceneElement(elementFactory, element); @@ -86,57 +83,62 @@ } writer.addDocument(document); + writer.flush(); } @Override public LoggableElement findElement(String uuid, Integer majorVersion, - Integer minorVersion) throws Throwable { + Integer minorVersion) throws Exception { + LoggableElement result = null; Map<String, String> properties = new HashMap<String, String>(); properties.put(KEY_UUID, uuid); properties.put(KEY_MAJORVERSION, majorVersion.toString()); properties.put(KEY_MINORVERSION, minorVersion.toString()); - Hits hits = findHits(properties); - if (hits.length() == 0) { - return null; - } else { - return convertDocumentToElement(hits.doc(0)); + LuceneHits hits = findHits(properties); + if (hits.getHits().length() != 0) { + result = convertDocumentToElement(hits.getHits().doc(0)); } + hits.closeSearcher(); + + return result; } @Override public LoggableElement getElementLatestVersion(String uuid) - throws Throwable { + throws Exception { LoggableElement element = null; Map<String, String> properties = new HashMap<String, String>(); properties.put(KEY_UUID, uuid); - Hits hits = findHits(properties); - if (hits.length() > 0) { + LuceneHits hits = findHits(properties); + if (hits.getHits().length() > 0) { List<Version> versions = new ArrayList<Version>(); - for (int i = 0; i < hits.length(); i++) { - Document doc = hits.doc(i); + for (int i = 0; i < hits.getHits().length(); i++) { + Document doc = hits.getHits().doc(i); Version version = new Version(); version.setMajorVersion(new Integer(doc.get(KEY_MAJORVERSION))); version.setMinorVersion(new Integer(doc.get(KEY_MINORVERSION))); version.setVersionnedObject(doc); + versions.add(version); } Collections.sort(versions); Collections.reverse(versions); Document latestDoc = (Document) versions.get(0) .getVersionnedObject(); - LoggableElementFactory elementFactory = (LoggableElementFactory) ElementDAOFactory + LoggableElementFactory elementFactory = (LoggableElementFactory) LoggableElementFactory .getFactory(latestDoc.get(KEY_CLASSNAME)); element = loadLuceneElement(elementFactory, latestDoc); } + hits.closeSearcher(); return element; } @Override - public Set<LoggableElement> findElementsById(String uuid) throws Throwable { + public Set<LoggableElement> findElementsById(String uuid) throws Exception { Map<String, String> properties = new HashMap<String, String>(); properties.put(KEY_UUID, uuid); return convertHitsToElements(findHits(properties)); @@ -144,107 +146,122 @@ @Override public Set<LoggableElement> findElementsByProperties( - Map<String, String> properties) throws Throwable { + Map<String, String> properties) throws Exception { return convertHitsToElements(findHits(properties)); } @Override public Set<LoggableElement> findElementsByProperties( - Map<String, String> properties, Class entityClass) throws Throwable { + Map<String, String> properties, Class entityClass) throws Exception { Map<String, String> props = new HashMap<String, String>(); props.putAll(properties); props.put(KEY_CLASSNAME, entityClass.getName()); return convertHitsToElements(findHits(properties)); } - public void deleteElement(String uuid) throws Throwable { + public void deleteElements(String uuid) throws Exception { writer.deleteDocuments(new Term(KEY_UUID, uuid)); + writer.flush(); } public void deleteElement(String uuid, Integer majorVersion, - Integer minorVersion) throws Throwable { + Integer minorVersion) throws Exception { Term[] terms = new Term[3]; terms[0] = new Term(KEY_UUID, uuid); terms[1] = new Term(KEY_MAJORVERSION, uuid); terms[2] = new Term(KEY_MINORVERSION, uuid); writer.deleteDocuments(terms); + writer.flush(); } - private Hits findHits(Map<String, String> properties) throws Throwable { + private LuceneHits findHits(Map<String, String> properties) + throws Exception { + Searcher searcher = new IndexSearcher(indexDirectory); + // FIXME disablecoords? BooleanQuery query = new BooleanQuery(false); for (Entry<String, String> kv : properties.entrySet()) { query.add(new TermQuery(new Term(kv.getKey(), kv.getValue())), BooleanClause.Occur.MUST); } - return searcher.search(query); + + LuceneHits luceneHits = new LuceneHits(searcher.search(query), searcher); + return luceneHits; } private LoggableElement convertDocumentToElement(Document document) - throws Throwable { + throws Exception { String className = document.get(KEY_CLASSNAME); - LoggableElementFactory factory = (LoggableElementFactory) ElementDAOFactory + LoggableElementFactory factory = (LoggableElementFactory) LoggableElementFactory .getFactory(className); return loadLuceneElement(factory, document); } - private Set<LoggableElement> convertHitsToElements(Hits hits) - throws Throwable { + private Set<LoggableElement> convertHitsToElements(LuceneHits hits) + throws Exception { Set<LoggableElement> elements = new HashSet<LoggableElement>(); - for (int i = 0; i < hits.length(); i++) { - Document document = hits.doc(i); + for (int i = 0; i < hits.getHits().length(); i++) { + Document document = hits.getHits().doc(i); elements.add(convertDocumentToElement(document)); } + hits.closeSearcher(); return elements; } - private LoggableElement loadLuceneElement(LoggableElementFactory factory, Document document) throws Throwable { - String strXMLElement = document.get("xmlElement"); + private LoggableElement loadLuceneElement(LoggableElementFactory factory, + Document document) throws Exception { + String strXMLElement = document.get(KEY_XML); InputStream is = new ByteArrayInputStream(strXMLElement.getBytes()); + org.w3c.dom.Document xmlDocument = factory.getBuilder().parse(is); - - LoggableElement element = factory.loadXMLElement((Element) xmlDocument.getFirstChild()); - + + LoggableElement element = factory.loadXMLElement((Element) xmlDocument + .getFirstChild()); + return element; } - - private Document saveLuceneElement(LoggableElementFactory factory, LoggableElement element) throws Throwable { + + private Document saveLuceneElement(LoggableElementFactory factory, + LoggableElement element) throws Exception { Document document = new Document(); - + // FIXME save searchable/metadata fields - document.add(new Field(KEY_UUID, element.getUuid().getBytes(), - Field.Store.YES)); - document.add(new Field(KEY_DESCRIPTION, element.getDescription().getBytes(), - Field.Store.YES)); - document.add(new Field(KEY_CLASSNAME, element.getClass().getName().getBytes(), - Field.Store.YES)); - document.add(new Field(KEY_MINORVERSION, element.getMinorVersion().toString().getBytes(), - Field.Store.YES)); - document.add(new Field(KEY_MAJORVERSION, element.getMajorVersion().toString().getBytes(), - Field.Store.YES)); - - org.w3c.dom.Document xmlDocument = factory.getBuilder().newDocument(); - // Propriétés du DOM - xmlDocument.setXmlVersion("1.0"); - xmlDocument.setXmlStandalone(true); - // Création de l'arborescence du DOM - Element racine = xmlDocument.createElement(element.getClass().getName()); - factory.saveXMLElement(xmlDocument, racine, element); - xmlDocument.appendChild(racine); - DOMSource domSource = new DOMSource(xmlDocument); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - String stringResult = writer.toString(); - document.add(new Field("xmlElement", stringResult, Field.Store.COMPRESS, Field.Index.NO)); - + 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_MINORVERSION, element.getMinorVersion() + .toString(), Field.Store.YES, Field.Index.UN_TOKENIZED)); + document.add(new Field(KEY_MAJORVERSION, element.getMajorVersion() + .toString(), Field.Store.YES, Field.Index.UN_TOKENIZED)); + + org.w3c.dom.Document xmlDocument = factory.getBuilder().newDocument(); + // Propriétés du DOM + xmlDocument.setXmlVersion("1.0"); + xmlDocument.setXmlStandalone(true); + // Création de l'arborescence du DOM + Element racine = xmlDocument.createElement(element.getClass() + .getSimpleName()); + factory.saveXMLElement(xmlDocument, racine, element); + xmlDocument.appendChild(racine); + DOMSource domSource = new DOMSource(xmlDocument); + StringWriter writer = new StringWriter(); + StreamResult result = new StreamResult(writer); + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer transformer = tf.newTransformer(); + transformer.transform(domSource, result); + String stringResult = writer.toString(); + document.add(new Field(KEY_XML, stringResult, + Field.Store.COMPRESS, Field.Index.NO)); + return document; } - public void shutDown() throws Throwable { + public void shutDown() throws Exception { writer.close(); - } + } + } Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneHits.java =================================================================== --- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneHits.java (rev 0) +++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/database/lucene/LuceneHits.java 2007-12-05 15:23:47 UTC (rev 41) @@ -0,0 +1,37 @@ +package org.cemagref.simexplorer.si.storage.database.lucene; + +import org.apache.lucene.search.Hits; +import org.apache.lucene.search.Searcher; + +public class LuceneHits { + + private Hits hits; + private Searcher searcher; + + public LuceneHits(Hits hits, Searcher searcher) { + super(); + this.hits = hits; + this.searcher = searcher; + } + + public void closeSearcher() throws Exception { + searcher.close(); + } + + public Hits getHits() { + return hits; + } + + public void setHits(Hits hits) { + this.hits = hits; + } + + public Searcher getSearcher() { + return searcher; + } + + public void setSearcher(Searcher searcher) { + this.searcher = searcher; + } + +}
participants (1)
-
glandais@users.labs.libre-entreprise.org