Author: glandais Date: 2007-12-11 17:07:26 +0000 (Tue, 11 Dec 2007) New Revision: 63 Removed: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneHits.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/FSStorageEngine.java Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/handler/AttachmentHandler.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/handler/FileSystemAttachmentHandler.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/type/ContentTypeFactory.java 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 trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/Component.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/ExplorationApplication.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/ExplorationData.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/Library.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/LoggableElement.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/metadata/MetaDataEntity.java Log: Refactoring et optimisations Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/handler/AttachmentHandler.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/handler/AttachmentHandler.java 2007-12-10 15:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/handler/AttachmentHandler.java 2007-12-11 17:07:26 UTC (rev 63) @@ -24,7 +24,7 @@ * Content * @throws Exception */ - public abstract void storeData(Database database, MetaDataEntity entity, String field, + public abstract void storeData(MetaDataEntity entity, String field, InputStream is) throws Exception; /** @@ -49,7 +49,7 @@ * Unique field for content * @throws Exception */ - public abstract void deleteData(Database database, MetaDataEntity entity, String field) + public abstract void deleteData(MetaDataEntity entity, String field) throws Exception; } \ No newline at end of file Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/handler/FileSystemAttachmentHandler.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/handler/FileSystemAttachmentHandler.java 2007-12-10 15:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/handler/FileSystemAttachmentHandler.java 2007-12-11 17:07:26 UTC (rev 63) @@ -8,7 +8,6 @@ import java.io.InputStream; import fr.cemagref.simexplorer.is.storage.database.Database; -import fr.cemagref.simexplorer.is.storage.entities.data.LoggableElement; import fr.cemagref.simexplorer.is.storage.entities.metadata.MetaDataEntity; /** @@ -60,8 +59,8 @@ } @Override - public void storeData(Database database, MetaDataEntity entity, - String field, InputStream is) throws Exception { + public void storeData(MetaDataEntity entity, String field, InputStream is) + throws Exception { // Simple stream on file FileOutputStream fos = new FileOutputStream(getFile(entity, field)); @@ -82,8 +81,8 @@ } @Override - public void deleteData(Database database, MetaDataEntity entity, - String field) throws Exception { + public void deleteData(MetaDataEntity entity, String field) + throws Exception { // Simple delete on file getFile(entity, field).delete(); } Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/type/ContentTypeFactory.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/type/ContentTypeFactory.java 2007-12-10 15:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/type/ContentTypeFactory.java 2007-12-11 17:07:26 UTC (rev 63) @@ -16,28 +16,34 @@ */ private static Map<String, ContentType> contentTypes = null; + private static String PACKAGE_TYPES = "fr.cemagref.simexplorer.is.storage.attachment.type"; + /** * Retrieve a content type instance * - * @param contentTypeClass + * @param contentTypeClassSimpleName * Class required * @return Instance * @throws Exception */ public static ContentType getContentTypeInstance( - Class<? extends ContentType> contentTypeClass) throws Exception { + String contentTypeClassSimpleName) throws Exception { + // Create cache if doesn't exist if (contentTypes == null) { contentTypes = new HashMap<String, ContentType>(); } // Check cache - ContentType result = contentTypes.get(contentTypeClass.getSimpleName()); + ContentType result = contentTypes.get(contentTypeClassSimpleName); // Create instance if doesn't exist, and puts it in cache if (result == null) { - result = contentTypeClass.newInstance(); + Class<?> contentTypeClass = Class.forName(PACKAGE_TYPES + "." + + contentTypeClassSimpleName); + result = (ContentType) contentTypeClass.newInstance(); contentTypes.put(contentTypeClass.getSimpleName(), result); } return result; + } } 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:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/Database.java 2007-12-11 17:07:26 UTC (rev 63) @@ -31,6 +31,13 @@ */ public abstract void close() throws Exception; + /** + * Commit pending modifications + * + * @throws Exception + */ + public abstract void commit() throws Exception; + // Create / Update /** @@ -40,25 +47,18 @@ * Element to insert * @throws Exception */ - public abstract void insertElement(MetaDataEntity element) throws Exception; + public abstract void insertElement(MetaDataEntity element, List<Reader> readers, boolean forceCommit) 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; + public void insertElement(MetaDataEntity element, List<Reader> readers) + throws Exception { + insertElement(element, readers, true); + } // Read /** * Get an element from its id and its version + * null if not fund * * @param uuid * Id @@ -72,6 +72,7 @@ /** * Get all version of an element thanks to its id + * Empty list if no element with this id * * @param uuid * Id @@ -82,6 +83,7 @@ /** * Retrieve an element in its latest version + * null if not fund * * @param uuid * Id @@ -114,6 +116,7 @@ /** * Retrieve all elements by id + * Empty list if no element with this id * * @param uuid * Id @@ -125,6 +128,7 @@ /** * Retrieve elements with specific properties + * Empty list if no element * * @param properties * Matching properties needed @@ -136,6 +140,7 @@ /** * Retrieve elements by content search + * Empty list if no element * * @param searchedText * Searched text @@ -159,15 +164,6 @@ } /** - * Delete all elements by id - * - * @param uuid - * Id - * @throws Exception - */ - public abstract void deleteElements(String uuid) throws Exception; - - /** * Delete an element by id and version * * @param uuid 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:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java 2007-12-11 17:07:26 UTC (rev 63) @@ -41,6 +41,10 @@ */ public static final String KEY_DESCRIPTOR = "simexplorer.descriptor"; /** + * Field name for attachment + */ + public static final String KEY_ATTACHMENT = "simexplorer.attachment"; + /** * Field name for id */ public static final String KEY_PARENTDATA_UUID = "simexplorer.parentdata.uuid"; 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:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java 2007-12-11 17:07:26 UTC (rev 63) @@ -23,6 +23,7 @@ import org.apache.lucene.queryParser.QueryParser; 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.Query; import org.apache.lucene.search.Searcher; @@ -45,19 +46,23 @@ /** * Content analyzer */ - private Analyzer analyser; + private static Analyzer analyser; /** * Index location */ - private String indexDirectory; + private static String indexDirectory; /** * Database writer */ - private IndexWriter writer; + private static IndexWriter writer; /** + * Database searcher + */ + private static Searcher searcher; + /** * Init flag */ - private boolean initok = false; + private static boolean initok = false; @Override public void open() throws Exception { @@ -66,8 +71,8 @@ analyser = new PerFieldAnalyzerWrapper(new SimpleAnalyzer()); // Specific analyzers - // analyser.addAnalyzer("firstname", new KeywordAnalyzer()); - // analyser.addAnalyzer("lastname", new KeywordAnalyzer()); + // analyser.addAnalyzer(KEY_, new KeywordAnalyzer()); + // analyser.addAnalyzer(KEY_, new KeywordAnalyzer()); // FIXME index location indexDirectory = "./index/"; @@ -83,6 +88,12 @@ // Create Lucene indexer writer = new IndexWriter(dir, true, analyser, create); + + searcher = new IndexSearcher(indexDirectory); + + // FIXME + System.out.println("writer.docCount() : " + writer.docCount()); + initok = true; } } @@ -91,12 +102,21 @@ public void close() throws Exception { // Close writer writer.close(); + searcher.close(); + initok = false; } @Override - public void insertElement(MetaDataEntity element) throws Exception { + public void commit() throws Exception { + // write index + writer.flush(); + searcher = new IndexSearcher(indexDirectory); + } + + @Override + public void insertElement(MetaDataEntity element, List<Reader> readers, boolean forceCommit) throws Exception { // Save element to a Lucene document - Document document = saveLuceneElement(element); + Document document = saveLuceneElement(element, readers); // Retrieve existing element with same id/version MetaDataEntity oldElement = getElement(element.getUuid(), element @@ -109,8 +129,9 @@ // add document to index writer.addDocument(document); - // write index - writer.flush(); + if (forceCommit) { + commit(); + } } @Override @@ -118,14 +139,12 @@ throws Exception { MetaDataEntity result = null; - LuceneHits hits = getHitsByIdVersion(uuid, version); - if (hits.getHits().length() != 0) { + Hits hits = getHitsByIdVersion(uuid, version); + if (hits.length() != 0) { // convert first document to element - result = loadLuceneElement(hits.getHits().doc(0)); + result = loadLuceneElement(hits.doc(0)); } - hits.closeSearcher(); - return result; } @@ -135,7 +154,7 @@ * @return * @throws Exception */ - private LuceneHits getHitsByIdVersion(String uuid, Version version) + private Hits getHitsByIdVersion(String uuid, Version version) throws Exception { // Retrieve element with id/version Map<String, String> properties = new HashMap<String, String>(); @@ -143,7 +162,7 @@ properties.put(KEY_VERSION, version.toString()); // Search elements - LuceneHits hits = findHits(properties); + Hits hits = findHits(properties); return hits; } @@ -152,12 +171,12 @@ // Retrieve all document corresponding to id Map<String, String> properties = new HashMap<String, String>(); properties.put(KEY_UUID, uuid); - LuceneHits hits = findHits(properties); + Hits hits = findHits(properties); // Add all versions to a list List<Version> versions = new ArrayList<Version>(); - for (int i = 0; i < hits.getHits().length(); i++) { - Document doc = hits.getHits().doc(i); + for (int i = 0; i < hits.length(); i++) { + Document doc = hits.doc(i); versions.add(new Version(doc.get(KEY_VERSION))); } @@ -182,16 +201,12 @@ @Override public Set<MetaDataEntity> findElementsByContentSearch(String queryText) throws Exception { - // Create a searcher - Searcher searcher = new IndexSearcher(indexDirectory); - Analyzer analyzer = new SimpleAnalyzer(); 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); + Hits luceneHits = searcher.search(luceneQuery); // Convert hits to elements Set<MetaDataEntity> result = convertHitsToElements(luceneHits); @@ -200,20 +215,13 @@ } @Override - public void deleteElements(String uuid) throws Exception { - // Delete all documents by term search - writer.deleteDocuments(new Term(KEY_UUID, uuid)); - writer.flush(); - } - - @Override public void deleteElement(String uuid, Version version) throws Exception { // Delete element in db with term search Term[] terms = new Term[2]; terms[0] = new Term(KEY_UUID, uuid); terms[1] = new Term(KEY_VERSION, version.toString()); writer.deleteDocuments(terms); - writer.flush(); + commit(); } /** @@ -224,11 +232,8 @@ * @return Documents and search handle * @throws Exception */ - private LuceneHits findHits(Map<String, String> properties) + private Hits findHits(Map<String, String> properties) throws Exception { - // Create a searcher - Searcher searcher = new IndexSearcher(indexDirectory); - // Create a query with all parameters BooleanQuery query = new BooleanQuery(); for (Entry<String, String> kv : properties.entrySet()) { @@ -237,7 +242,7 @@ } // Create a bean with hits and searcher - LuceneHits luceneHits = new LuceneHits(searcher.search(query), searcher); + Hits luceneHits = searcher.search(query); return luceneHits; } @@ -249,17 +254,15 @@ * @return Entities list * @throws Exception */ - private Set<MetaDataEntity> convertHitsToElements(LuceneHits hits) + private Set<MetaDataEntity> convertHitsToElements(Hits hits) throws Exception { - // For all document Set<MetaDataEntity> elements = new HashSet<MetaDataEntity>(); - for (int i = 0; i < hits.getHits().length(); i++) { - Document document = hits.getHits().doc(i); + // For all documents + for (int i = 0; i < hits.length(); i++) { + Document document = hits.doc(i); // Convert to element elements.add(loadLuceneElement(document)); } - // Close handle - hits.closeSearcher(); return elements; } @@ -288,16 +291,28 @@ element.setHash(document.get(KEY_HASH)); Map<String, String> descriptors = new HashMap<String, String>(); + Map<String, String> attachments = 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); } + + if (fieldable.name().startsWith(KEY_ATTACHMENT)) { + String field = fieldable.name().replace(KEY_ATTACHMENT + ".", + ""); + String value = fieldable.stringValue(); + attachments.put(field, value); + } } + element.setDescriptors(descriptors); + element.setAttachments(attachments); String parentId = document.get(KEY_PARENTDATA_UUID); String parentVersion = document.get(KEY_PARENTDATA_VERSION); @@ -331,7 +346,7 @@ * @return * @throws Exception */ - private Document saveLuceneElement(MetaDataEntity element) throws Exception { + private Document saveLuceneElement(MetaDataEntity element, List<Reader> readers) throws Exception { Document document = new Document(); document.add(simpleField(KEY_UUID, element.getUuid())); @@ -350,6 +365,13 @@ document.add(simpleField(key, value)); } + Map<String, String> attachments = element.getAttachments(); + for (Map.Entry<String, String> entry : attachments.entrySet()) { + String key = KEY_ATTACHMENT + "." + entry.getKey(); + String value = entry.getValue(); + document.add(simpleField(key, value)); + } + if (element.getParentData() != null) { document.add(simpleField(KEY_PARENTDATA_UUID, element .getParentData().getUuid())); @@ -363,45 +385,17 @@ .getParentVersion().getVersion().toString())); } + 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)); + 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(); - - } - - } - } Deleted: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneHits.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneHits.java 2007-12-10 15:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneHits.java 2007-12-11 17:07:26 UTC (rev 63) @@ -1,74 +0,0 @@ -package fr.cemagref.simexplorer.is.storage.database.lucene; - -import org.apache.lucene.search.Hits; -import org.apache.lucene.search.Searcher; - -/** - * Bean handling hits collection and searcher handle - * - * @author landais - * - */ -public class LuceneHits { - - /** - * Hits collection - */ - private Hits hits; - /** - * Searcher handle - */ - private Searcher searcher; - - /** - * Default constructor - * - * @param hits - * @param searcher - */ - public LuceneHits(Hits hits, Searcher searcher) { - super(); - this.hits = hits; - this.searcher = searcher; - } - - /** - * Close handle - * - * @throws Exception - */ - public void closeSearcher() throws Exception { - searcher.close(); - } - - /** - * @return the hits - */ - public Hits getHits() { - return hits; - } - - /** - * @param hits - * the hits to set - */ - public void setHits(Hits hits) { - this.hits = hits; - } - - /** - * @return the searcher - */ - public Searcher getSearcher() { - return searcher; - } - - /** - * @param searcher - * the searcher to set - */ - public void setSearcher(Searcher searcher) { - this.searcher = searcher; - } - -} Deleted: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/FSStorageEngine.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/FSStorageEngine.java 2007-12-10 15:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/FSStorageEngine.java 2007-12-11 17:07:26 UTC (rev 63) @@ -1,239 +0,0 @@ -package fr.cemagref.simexplorer.is.storage.engine; - -import java.io.InputStream; -import java.io.OutputStream; -import java.util.Collections; -import java.util.List; - -import fr.cemagref.simexplorer.is.storage.attachment.handler.AttachmentHandler; -import fr.cemagref.simexplorer.is.storage.attachment.handler.FileSystemAttachmentHandler; -import fr.cemagref.simexplorer.is.storage.database.lucene.LuceneDatabase; -import fr.cemagref.simexplorer.is.storage.entities.data.LoggableElement; -import fr.cemagref.simexplorer.is.storage.entities.metadata.Version; -import fr.cemagref.simexplorer.is.storage.factories.DataEntityFactory; -import fr.cemagref.simexplorer.is.storage.factories.LoggableElementFactory; - -public class FSStorageEngine extends StorageEngine { - - @Override - public void close() throws Exception { - // TODO Auto-generated method stub - - } - - @Override - public void deleteData(LoggableElement entity, String field) - throws Exception { - // TODO Auto-generated method stub - - } - - @Override - public void exportElement(LoggableElement element, OutputStream os) - throws Exception { - // TODO Auto-generated method stub - - } - - @Override - public LoggableElement getElement(String uuid) throws Exception { - // TODO Auto-generated method stub - return null; - } - - @Override - public LoggableElement getElementVersion(String uuid, Version version) - throws Exception { - // TODO Auto-generated method stub - return null; - } - - @Override - public List<Version> getVersions(String uuid) throws Exception { - // TODO Auto-generated method stub - return null; - } - - @Override - public LoggableElement importElement(InputStream inputStream) - throws Exception { - // TODO Auto-generated method stub - return null; - } - - @Override - public LoggableElement newElement(LoggableElement element) throws Exception { - // TODO Auto-generated method stub - return null; - } - - @Override - public void open() throws Exception { - // TODO Auto-generated method stub - - } - - @Override - public InputStream retrieveData(LoggableElement entity, String field) - throws Exception { - // TODO Auto-generated method stub - return null; - } - - @Override - public LoggableElement saveElement(LoggableElement element) - throws Exception { - // TODO Auto-generated method stub - return null; - } - - @Override - public void storeData(LoggableElement entity, String field, InputStream is) - throws Exception { - // TODO Auto-generated method stub - - } - - @Override - public LoggableElement synchronizeElement(LoggableElement element) - throws Exception { - // TODO Auto-generated method stub - return null; - } - - /* - private static AttachmentHandler handler; - private static boolean handlerInit = false; - - public static AttachmentHandler getHandler() { - if (!handlerInit) { - handler = new FileSystemAttachmentHandler(); - } - return handler; - } - - @Override - public void close() throws Exception { - database.close(); - } - - @Override - public void storeData(LoggableElement entity, String field, InputStream is) - throws Exception { - getHandler().storeData(database, entity, field, is); - } - - - @Override - public void exportElement(LoggableElement element, OutputStream os) - throws Exception { - LoggableElement completeElement = database.getElement(element - .getUuid(), element.getVersion()); - LoggableElementFactory<LoggableElement> factory = (LoggableElementFactory<LoggableElement>) DataEntityFactory - .getFactory(completeElement.getClass()); - factory.getElementXML(element, os); - } - - @Override - public LoggableElement getElement(String uuid) throws Exception { - return database.getElementLatestVersion(uuid); - } - - @Override - public LoggableElement getElementVersion(String uuid, Version version) - throws Exception { - - - // FIXME - String strXMLElement = document.get(KEY_XML); - InputStream is = new ByteArrayInputStream(strXMLElement.getBytes()); - org.w3c.dom.Document xmlDocument = DataEntityFactory.getBuilder().parse(is); - LoggableElement element = factory.loadXMLElement((Element) xmlDocument - .getFirstChild()); - - - return database.getElement(uuid, version); - } - - @Override - public List<Version> getVersions(String uuid) throws Exception { - return database.getVersions(uuid); - } - - @Override - public LoggableElement importElement(InputStream inputStream) - throws Exception { - LoggableElement importedElement = null; - - database.insertElement(importedElement); - return importedElement; - } - - @Override - public LoggableElement newElement(LoggableElement element) throws Exception { - if (getElement(element.getUuid()) != null) { - throw new Exception(element.getUuid() + " already exist"); - } - element.setVersion("1"); - - // FIXME - //document.add(new Field(KEY_XML, factory.getElementXML(element), - // Field.Store.COMPRESS, Field.Index.NO)); - - - database.insertElement(element); - return getElement(element.getUuid()); - } - - @Override - public void open() throws Exception { - database = new LuceneDatabase(); - database.open(); - } - - @Override - public LoggableElement saveElement(LoggableElement element) - throws Exception { - Version version = null; - List<Version> versions = getVersions(element.getUuid()); - if (versions.size() > 0) { - Collections.sort(versions); - Collections.reverse(versions); - - version = versions.get(0); - version.incVersion(0); - } else { - version = new Version("1"); - } - - element.setVersion(version.toString()); - - // FIXME - //document.add(new Field(KEY_XML, factory.getElementXML(element), - // Field.Store.COMPRESS, Field.Index.NO)); - - - database.insertElement(element); - return getElement(element.getUuid()); - } - - @Override - public LoggableElement synchronizeElement(LoggableElement element) - throws Exception { - // TODO Auto-generated method stub - return null; - } - - @Override - public void deleteData(FileEntity entity, String field) throws Exception { - getHandler().deleteData(database, entity, field); - } - - @Override - public InputStream retrieveData(FileEntity entity, String field) - throws Exception { - return getHandler().retrieveData(entity, field); - } -*/ - -} Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java 2007-12-10 15:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java 2007-12-11 17:07:26 UTC (rev 63) @@ -2,79 +2,155 @@ import java.io.InputStream; import java.io.OutputStream; +import java.io.Reader; +import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.UUID; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import fr.cemagref.simexplorer.is.storage.attachment.handler.AttachmentHandler; +import fr.cemagref.simexplorer.is.storage.attachment.handler.FileSystemAttachmentHandler; +import fr.cemagref.simexplorer.is.storage.attachment.type.ContentType; +import fr.cemagref.simexplorer.is.storage.attachment.type.ContentTypeFactory; import fr.cemagref.simexplorer.is.storage.database.Database; -import fr.cemagref.simexplorer.is.storage.entities.data.LoggableElement; +import fr.cemagref.simexplorer.is.storage.database.lucene.LuceneDatabase; +import fr.cemagref.simexplorer.is.storage.entities.metadata.MetaDataEntity; import fr.cemagref.simexplorer.is.storage.entities.metadata.Version; -public abstract class StorageEngine { +public class StorageEngine { protected Database database; - public abstract void open() throws Exception; + protected AttachmentHandler attachmentHandler; - public abstract void close() throws Exception; + private MetaDataEntity mdTmp; - public abstract LoggableElement getElement(String uuid) throws Exception; + public StorageEngine() { + super(); + database = new LuceneDatabase(); + attachmentHandler = new FileSystemAttachmentHandler(); + mdTmp = new MetaDataEntity(); + mdTmp.setUuid(UUID.randomUUID().toString()); + mdTmp.setVersion("0"); + } - public abstract List<Version> getVersions(String uuid) throws Exception; + public void open() throws Exception { + database.open(); + } - public abstract LoggableElement getElementVersion(String uuid, - Version version) throws Exception; + public void close() throws Exception { + database.close(); + } - public abstract LoggableElement newElement(LoggableElement element) - throws Exception; + public void commit() throws Exception { + database.commit(); + } - public abstract LoggableElement saveElement(LoggableElement element) - throws Exception; + // Create / update - public abstract LoggableElement synchronizeElement(LoggableElement element) - throws Exception; + public void saveElement(MetaDataEntity element, + Map<String, InputStream> attachments) throws Exception { - public abstract void exportElement(LoggableElement element, OutputStream os) - throws Exception; + for (Map.Entry<String, InputStream> entry : attachments.entrySet()) { + attachmentHandler.storeData(element, entry.getKey(), entry + .getValue()); + } - public abstract LoggableElement importElement(InputStream inputStream) - throws Exception; + List<Reader> readers = new ArrayList<Reader>(); + for (Map.Entry<String, InputStream> entry : attachments.entrySet()) { + String field = entry.getKey(); + InputStream content = attachmentHandler + .retrieveData(element, field); + String type = element.getAttachments().get(field); + if (type != null) { + ContentType contentType = ContentTypeFactory + .getContentTypeInstance(type); + Reader reader = contentType.renderToText(content); + readers.add(reader); + } + } - /** - * Attatch content to an element FIXME move to storage engine - * - * @param entity - * Related entity - * @param field - * Target field - * @param is - * Content - * @throws Exception - */ - public abstract void storeData(LoggableElement entity, String field, - InputStream is) throws Exception; + database.insertElement(element, readers); + } - /** - * Retrieve content - * - * @param entity - * DataEntity related to content - * @param field - * Unique field for content - * @return Content - * @throws Exception - */ - public abstract InputStream retrieveData(LoggableElement entity, String field) - throws Exception; + // Read - /** - * Delete content - * - * @param entity - * DataEntity related to content - * @param field - * Unique field for content - * @throws Exception - */ - public abstract void deleteData(LoggableElement entity, String field) - throws Exception; + public MetaDataEntity getElement(String uuid) throws Exception { + MetaDataEntity mde = database.getElementLatestVersion(uuid); + return mde; + } + public List<Version> getVersions(String uuid) throws Exception { + return database.getVersions(uuid); + } + + public MetaDataEntity getElementVersion(String uuid, Version version) + throws Exception { + MetaDataEntity mde = database.getElement(uuid, version); + return mde; + } + + public InputStream retrieveData(MetaDataEntity entity, String field) + throws Exception { + InputStream result = attachmentHandler.retrieveData(entity, field); + return result; + } + + // Delete + + public void deleteElements(String uuid) throws Exception { + List<Version> versions = getVersions(uuid); + for (Version version : versions) { + deleteElement(uuid, version); + } + } + + public void deleteElement(String uuid, Version version) throws Exception { + MetaDataEntity element = getElementVersion(uuid, version); + Map<String, String> attachments = element.getAttachments(); + for (Map.Entry<String, String> entry : attachments.entrySet()) { + attachmentHandler.deleteData(element, entry.getKey()); + } + database.deleteElement(element); + } + + // Import/Export + + public MetaDataEntity importElement(InputStream inputStream) + throws Exception { + // TODO Auto-generated method stub + return null; + } + + public void exportElement(MetaDataEntity element, OutputStream os) + throws Exception { + // Retrieve element factory + /* + * Class<? extends LoggableElement> clazz = element.getClass(); + * LoggableElementFactory<LoggableElement> elementFactory = + * (LoggableElementFactory<LoggableElement>) LoggableElementFactory + * .getFactory(clazz); elementFactory.getElementXML(element, os); + */ + } + + // Tools + + public String storeTempData(InputStream stream) throws Exception { + String id = UUID.randomUUID().toString(); + attachmentHandler.storeData(mdTmp, id, stream); + return id; + } + + public InputStream retrieveTempData(String id) throws Exception { + InputStream is = attachmentHandler.retrieveData(mdTmp, id); + return is; + } + + public void deleteTempData(String id) throws Exception { + attachmentHandler.deleteData(mdTmp, id); + } + } Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/Component.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/Component.java 2007-12-10 15:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/Component.java 2007-12-11 17:07:26 UTC (rev 63) @@ -41,10 +41,4 @@ this.libraries = libraries; } - /* - * @Override public Set<LoggableElement> getChildren() { Set<LoggableElement> - * elements = new HashSet<LoggableElement>(); elements.addAll(libraries); - * return elements; } - */ - } Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/ExplorationApplication.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/ExplorationApplication.java 2007-12-10 15:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/ExplorationApplication.java 2007-12-11 17:07:26 UTC (rev 63) @@ -23,11 +23,4 @@ this.components = components; } - /* - * @Override public Set<LoggableElement> getChildren() { Set<LoggableElement> - * elements = new HashSet<LoggableElement>(); - * elements.addAll(explorations); elements.addAll(components); return - * elements; } - */ - } Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/ExplorationData.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/ExplorationData.java 2007-12-10 15:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/ExplorationData.java 2007-12-11 17:07:26 UTC (rev 63) @@ -1,6 +1,5 @@ package fr.cemagref.simexplorer.is.storage.entities.data; -import java.util.HashSet; import java.util.Set; public class ExplorationData extends LoggableElement { @@ -52,9 +51,4 @@ this.valuesMap = valuesMap; } - /* - * @Override public Set<LoggableElement> getChildren() { Set<LoggableElement> - * elements = new HashSet<LoggableElement>(); return elements; } - */ - } Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/Library.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/Library.java 2007-12-10 15:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/Library.java 2007-12-11 17:07:26 UTC (rev 63) @@ -2,14 +2,4 @@ public class Library extends LoggableElement { - /* - * @Override public Set<LoggableElement> getChildren() { Set<LoggableElement> - * elements = new HashSet<LoggableElement>(); return elements; } - * - * @Override public Map<String, ContentType> getContentTypes() throws - * Exception { Map<String, ContentType> contentTypes = - * super.getContentTypes(); contentTypes.put("library", ContentTypeFactory - * .getContentTypeInstance(RawType.class)); return contentTypes; } - */ - } Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/LoggableElement.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/LoggableElement.java 2007-12-10 15:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/data/LoggableElement.java 2007-12-11 17:07:26 UTC (rev 63) @@ -1,15 +1,11 @@ package fr.cemagref.simexplorer.is.storage.entities.data; -import java.util.Set; - import fr.cemagref.simexplorer.is.storage.entities.metadata.MetaDataEntity; public abstract class LoggableElement extends DataEntity { private MetaDataEntity metaData; - //public abstract Set<LoggableElement> getChildren(); - /** * @return the metaData */ Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/metadata/MetaDataEntity.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/metadata/MetaDataEntity.java 2007-12-10 15:21:40 UTC (rev 62) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/metadata/MetaDataEntity.java 2007-12-11 17:07:26 UTC (rev 63) @@ -19,6 +19,7 @@ private Date creationDate; private String hash; private Map<String, String> descriptors; + private Map<String, String> attachments; private MetaDataEntity parentData; private MetaDataEntity parentVersion; @@ -172,11 +173,18 @@ this.parentVersion = parentVersion; } - public Map<String, ContentType> getContentTypes() throws Exception { - Map<String, ContentType> contentTypes = new HashMap<String, ContentType>(); - contentTypes.put("xml", ContentTypeFactory - .getContentTypeInstance(RawType.class)); - return contentTypes; + /** + * @return the attachments + */ + public Map<String, String> getAttachments() { + return attachments; } + /** + * @param attachments the attachments to set + */ + public void setAttachments(Map<String, String> attachments) { + this.attachments = attachments; + } + }
participants (1)
-
glandais@users.labs.libre-entreprise.org