r1076 - in trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication: externalize synchro
Author: mfortun Date: 2011-07-12 10:46:28 +0200 (Tue, 12 Jul 2011) New Revision: 1076 Url: http://nuiton.org/repositories/revision/wikitty/1076 Log: * code optimization move findbyid and findallid to abstract wikitty service on file system * finds implemented for wikitty service jar loader Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/externalize/WikittyServiceJarLoader.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/AbstractWikittyFileService.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/externalize/WikittyServiceJarLoader.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/externalize/WikittyServiceJarLoader.java 2011-07-11 15:48:08 UTC (rev 1075) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/externalize/WikittyServiceJarLoader.java 2011-07-12 08:46:28 UTC (rev 1076) @@ -9,8 +9,10 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.util.Collection; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -158,9 +160,14 @@ public boolean canRead(String securityToken, String wikittyId) { return exists(securityToken, wikittyId); } + @Override public boolean exists(String securityToken, String wikittyId) { + return exists(wikittyId); + } + + protected boolean exists(String wikittyId) { return wikittyIndex.containsKey(wikittyId); } @@ -245,87 +252,12 @@ List<Wikitty> result = new LinkedList<Wikitty>(); - try { - for (String wikittyId : ids) { - // if wikitty doesn't exist jump to the next id - if (!exists(securityToken, wikittyId)) { + for (String wikittyId : ids) { + // if wikitty doesn't exist jump to the next id - continue; - } + result.add(restore(wikittyId)); + } - String fileExtension = wikittyMetadata.getProperty(wikittyId - + WikittyPublicationExternalize.EXTENSION_SUFFIX); - String path = wikittyIndex.getProperty(wikittyId); - String version = wikittyMetadata.getProperty(wikittyId - + WikittyPublicationExternalize.VERSION_SUFFIX); - - JarEntry wikittyJared = wikittyJarRepository.getJarEntry(path - + "." + fileExtension); - - MimeTypePubHelper mimeHelper = new MimeTypePubHelper(); - - Wikitty wikit = new WikittyImpl(wikittyId); - wikit.setVersion(version); - - wikit.addExtension(WikittyLabelImpl.extensionWikittyLabel); - - // set labels computate name - String sep = wikittyMetadata - .getProperty(WikittyPublicationExternalize.PATH_SEPARATOR); - String[] namPath = StringUtil.split(path, sep); - String wikittyName = namPath[namPath.length - 1]; - - String label = ""; - for (int i = 0; i < namPath.length - 1; i++) { - label += namPath[i]; - if (i < namPath.length - 2) { - label += WikittyPublicationFileSystem.WIKITTYLABEL_SEPARATOR; - } - } - - WikittyLabelHelper.addLabels(wikit, label); - - if (mimeHelper.isPubTextMime(fileExtension)) { - // wikitty pub text compiled - wikit.addExtension(WikittyPubTextImpl.extensions); - wikit.addExtension(WikittyPubTextCompiledImpl.extensionWikittyPubTextCompiled); - - // basic field - WikittyPubTextHelper.setFileExtension(wikit, fileExtension); - WikittyPubTextHelper.setMimeType(wikit, - mimeHelper.getMimeForExtension(fileExtension)); - WikittyPubTextHelper.setName(wikit, wikittyName); - - // contents fields: - JarEntry wikittyclass = wikittyJarRepository - .getJarEntry(path - + WikittyPublicationExternalize.COMPILED_EXTENSION); - - WikittyPubTextHelper.setContent(wikit, - getStringContent(wikittyJared)); - WikittyPubTextCompiledHelper.setByteCode(wikit, - this.getByteContent(wikittyclass)); - - } else { - // wikitty pub data - wikit.addExtension(WikittyPubDataImpl.extensionWikittyPubData); - WikittyPubDataHelper.setMimeType(wikit, - mimeHelper.getMimeForExtension(fileExtension)); - - WikittyPubDataHelper.setName(wikit, wikittyName); - - WikittyPubDataHelper.setFileExtension(wikit, fileExtension); - WikittyPubDataHelper.setContent(wikit, - this.getByteContent(wikittyJared)); - } - - result.add(wikit); - } - - } catch (Exception e) { - // TODO mfortun-2011-07-11 really handle exception - e.printStackTrace(); - } return result; } @@ -337,23 +269,22 @@ } - @Override - public List<PagedResult<String>> findAllByCriteria(String securityToken, - List<Criteria> criteria) { - // TODO mfortun - throw new UnsupportedOperationException("not yet implemented"); - // return null; - } - + + + + @Override - public List<String> findByCriteria(String securityToken, - List<Criteria> criteria) { - // TODO mfortun - throw new UnsupportedOperationException("not yet implemented"); - // return null; - + protected Map<String, Wikitty> getAllWikitties() { + Map<String, Wikitty> result = new HashMap<String, Wikitty>(); + + for (Object oId: wikittyIndex.keySet()){ + String id= oId.toString(); + result.put(id, restore(id)); + } + return result; } + @Override public WikittyEvent deleteTree(String securityToken, String treeNodeId) { @@ -389,6 +320,91 @@ } + + + + protected Wikitty restore(String wikittyId) { + + Wikitty wikit = null; + try { + if (!exists(wikittyId)) { + return null; + } + + String fileExtension = wikittyMetadata.getProperty(wikittyId + + WikittyPublicationExternalize.EXTENSION_SUFFIX); + String path = wikittyIndex.getProperty(wikittyId); + String version = wikittyMetadata.getProperty(wikittyId + + WikittyPublicationExternalize.VERSION_SUFFIX); + + JarEntry wikittyJared = wikittyJarRepository.getJarEntry(path + "." + + fileExtension); + + MimeTypePubHelper mimeHelper = new MimeTypePubHelper(); + + wikit = new WikittyImpl(wikittyId); + wikit.setVersion(version); + + wikit.addExtension(WikittyLabelImpl.extensionWikittyLabel); + + // set labels computate name + String sep = wikittyMetadata + .getProperty(WikittyPublicationExternalize.PATH_SEPARATOR); + String[] namPath = StringUtil.split(path, sep); + String wikittyName = namPath[namPath.length - 1]; + + String label = ""; + for (int i = 0; i < namPath.length - 1; i++) { + label += namPath[i]; + if (i < namPath.length - 2) { + label += WikittyPublicationFileSystem.WIKITTYLABEL_SEPARATOR; + } + } + + WikittyLabelHelper.addLabels(wikit, label); + + if (mimeHelper.isPubTextMime(fileExtension)) { + // wikitty pub text compiled + wikit.addExtension(WikittyPubTextImpl.extensions); + wikit.addExtension(WikittyPubTextCompiledImpl.extensionWikittyPubTextCompiled); + + // basic field + WikittyPubTextHelper.setFileExtension(wikit, fileExtension); + WikittyPubTextHelper.setMimeType(wikit, + mimeHelper.getMimeForExtension(fileExtension)); + WikittyPubTextHelper.setName(wikit, wikittyName); + + // contents fields: + JarEntry wikittyclass = wikittyJarRepository.getJarEntry(path + + WikittyPublicationExternalize.COMPILED_EXTENSION); + + WikittyPubTextHelper.setContent(wikit, + this.getStringContent(wikittyJared)); + WikittyPubTextCompiledHelper.setByteCode(wikit, + this.getByteContent(wikittyclass)); + + } else { + // wikitty pub data + wikit.addExtension(WikittyPubDataImpl.extensionWikittyPubData); + WikittyPubDataHelper.setMimeType(wikit, + mimeHelper.getMimeForExtension(fileExtension)); + + WikittyPubDataHelper.setName(wikit, wikittyName); + + WikittyPubDataHelper.setFileExtension(wikit, fileExtension); + WikittyPubDataHelper.setContent(wikit, + this.getByteContent(wikittyJared)); + } + + } catch (Exception e) { + // TODO mfortun-2011-07-11 really handle exception + e.printStackTrace(); + } + + return wikit; + } + + public String getStringContent(JarEntry jarEntry) throws IOException { BufferedReader buffer = new BufferedReader(new InputStreamReader( wikittyJarRepository.getInputStream(jarEntry))); Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/AbstractWikittyFileService.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/AbstractWikittyFileService.java 2011-07-11 15:48:08 UTC (rev 1075) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/AbstractWikittyFileService.java 2011-07-12 08:46:28 UTC (rev 1076) @@ -5,7 +5,10 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -429,5 +432,96 @@ } } + + protected abstract Map<String, Wikitty> getAllWikitties(); + + + @Override + public List<PagedResult<String>> findAllByCriteria(String securityToken, + List<Criteria> criteria) { + + + List<PagedResult<String>> result = new ArrayList<PagedResult<String>>(); + + Map<String, Wikitty> wikitties = getAllWikitties(); + + // for each criteria + for (Criteria cr : criteria) { + // prepare restriction on result + int firstIndex = cr.getFirstIndex(); + int endIndex = cr.getEndIndex(); + List<String> ids = new LinkedList<String>(); + int currentIndex = 0; + Restriction restriction = cr.getRestriction(); + // for each wikitty check if it match the resttriction + for (Entry<String, Wikitty> entry : wikitties.entrySet()) { + String id = entry.getKey(); + Wikitty w = entry.getValue(); + // if macth + + + if (checkRestriction(restriction, w)) { + + // increment result number + currentIndex++; + if (currentIndex > firstIndex) { + ids.add(id); + } + // if the number of wikitty found is match + // stop the search for other wikitty + if (endIndex >= 0 && currentIndex >= endIndex) { + break; + } + } + } + result.add(new PagedResult<String>(firstIndex, ids.size(), + restriction.toString(), null, ids)); + + } + return result; + } + + + @Override + public List<String> findByCriteria(String securityToken, + List<Criteria> criteria) { + List<String> result = new ArrayList<String>(); + + Map<String, Wikitty> wikitties = getAllWikitties(); + // for each criteria + for (Criteria cr : criteria) { + // prepare restriction on result + int firstIndex = cr.getFirstIndex(); + int endIndex = cr.getEndIndex(); + List<String> ids = new LinkedList<String>(); + int currentIndex = 0; + Restriction restriction = cr.getRestriction(); + // for each wikitty check if it match the resttriction + for (Entry<String, Wikitty> entry : wikitties.entrySet()) { + String id = entry.getKey(); + Wikitty w = entry.getValue(); + // if macth + + log.debug("Check restriction for wikitty: " + w + + " Restriction:" + restriction); + + if (checkRestriction(restriction, w)) { + // increment result number + currentIndex++; + if (currentIndex > firstIndex) { + ids.add(id); + } + // if the number of wikitty found is match + // stop the search for other wikitty + if (endIndex >= 0 && currentIndex >= endIndex) { + break; + } + } + } + result.addAll(ids); + } + return result; + } + } \ No newline at end of file Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java 2011-07-11 15:48:08 UTC (rev 1075) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java 2011-07-12 08:46:28 UTC (rev 1076) @@ -646,10 +646,10 @@ return result; } + + @Override - public List<PagedResult<String>> findAllByCriteria(String securityToken, - List<Criteria> criteria) { - + protected Map<String, Wikitty> getAllWikitties() { Map<String, Wikitty> wikitties = new HashMap<String, Wikitty>(); try { // construct properly with the working directory and the label @@ -680,113 +680,12 @@ // TODO mfortun-2011-04-20 really handle exception e.printStackTrace(); } - - List<PagedResult<String>> result = new ArrayList<PagedResult<String>>(); - - // for each criteria - for (Criteria cr : criteria) { - // prepare restriction on result - int firstIndex = cr.getFirstIndex(); - int endIndex = cr.getEndIndex(); - List<String> ids = new LinkedList<String>(); - int currentIndex = 0; - Restriction restriction = cr.getRestriction(); - // for each wikitty check if it match the resttriction - for (Entry<String, Wikitty> entry : wikitties.entrySet()) { - String id = entry.getKey(); - Wikitty w = entry.getValue(); - // if macth - - - if (checkRestriction(restriction, w)) { - - // increment result number - currentIndex++; - if (currentIndex > firstIndex) { - ids.add(id); - } - // if the number of wikitty found is match - // stop the search for other wikitty - if (endIndex >= 0 && currentIndex >= endIndex) { - break; - } - } - } - result.add(new PagedResult<String>(firstIndex, ids.size(), - restriction.toString(), null, ids)); - - } - return result; + return wikitties; } + - @Override - public List<String> findByCriteria(String securityToken, - List<Criteria> criteria) { - List<String> result = new ArrayList<String>(); + - Map<String, Wikitty> wikitties = new HashMap<String, Wikitty>(); - try { - // construct properly with the working directory and the label - // the starts directory - String labelToDir = WikittyFileUtil.labelToPath(label); - File starts = new File(homeFile.getAbsolutePath() + File.separator - + labelToDir); - if (starts.exists()) { - // check for new file, modifications and deleted wikitty - harvestNewCheckModificationsAndDeleted(starts, label); - } - - // load all locals wikitty - BidiMap map = harvestLocalWikitties(starts, true); - - // put all local wikitties in a map - for (Object o : map.keySet()) { - String id = (String) o; - FileSystemWIkittyId location = (FileSystemWIkittyId) map - .get(id); - - wikitties.put(id, restore(id, location)); - - } - } catch (Exception e) { - // TODO mfortun-2011-04-21 really handle exception - e.printStackTrace(); - } - // for each criteria - for (Criteria cr : criteria) { - // prepare restriction on result - int firstIndex = cr.getFirstIndex(); - int endIndex = cr.getEndIndex(); - List<String> ids = new LinkedList<String>(); - int currentIndex = 0; - Restriction restriction = cr.getRestriction(); - // for each wikitty check if it match the resttriction - for (Entry<String, Wikitty> entry : wikitties.entrySet()) { - String id = entry.getKey(); - Wikitty w = entry.getValue(); - // if macth - - log.debug("Check restriction for wikitty: " + w - + " Restriction:" + restriction); - - if (checkRestriction(restriction, w)) { - // increment result number - currentIndex++; - if (currentIndex > firstIndex) { - ids.add(id); - } - // if the number of wikitty found is match - // stop the search for other wikitty - if (endIndex >= 0 && currentIndex >= endIndex) { - break; - } - } - } - result.addAll(ids); - } - return result; - } - @Override public WikittyEvent deleteTree(String securityToken, String treeNodeId) { // TODO mfortun-2011-04-05
participants (1)
-
mfortun@users.nuiton.org