From 840527689b53f7543f7ba14876a424b18d3c1eb4 Mon Sep 17 00:00:00 2001 From: orbiter Date: Mon, 1 Feb 2010 23:04:52 +0000 Subject: [PATCH] more simplification of bookmark class git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6639 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- htroot/Bookmarks.java | 2 +- htroot/api/bookmarks/get_bookmarks.java | 2 +- htroot/api/bookmarks/get_folders.java | 3 +- htroot/api/bookmarks/xbel/xbel.java | 2 +- source/de/anomic/data/BookmarkDate.java | 2 +- source/de/anomic/data/BookmarkHelper.java | 25 ++++ source/de/anomic/data/bookmarksDB.java | 141 ++++++---------------- 7 files changed, 71 insertions(+), 106 deletions(-) diff --git a/htroot/Bookmarks.java b/htroot/Bookmarks.java index 78b201434..de4509c6d 100644 --- a/htroot/Bookmarks.java +++ b/htroot/Bookmarks.java @@ -349,7 +349,7 @@ public class Bookmarks { //----------------------- count = 0; - count = recurseFolders(sb.bookmarksDB.getFolderList(isAdmin),"/",0,true,""); + count = recurseFolders(BookmarkHelper.getFolderList("/", sb.bookmarksDB.getTagIterator(isAdmin)), "/", 0, true, ""); prop.put("display_folderlist", count); } return prop; // return from serverObjects respond() diff --git a/htroot/api/bookmarks/get_bookmarks.java b/htroot/api/bookmarks/get_bookmarks.java index 07c200135..375f282c8 100644 --- a/htroot/api/bookmarks/get_bookmarks.java +++ b/htroot/api/bookmarks/get_bookmarks.java @@ -108,7 +108,7 @@ public class get_bookmarks { } prop.putHTML("display_folder", "0"); R = root.replaceAll("[^/]","").length() - 1; - count = recurseFolders(sb.bookmarksDB.getFolderList(root, isAdmin),root,0,true,""); + count = recurseFolders(BookmarkHelper.getFolderList(root, sb.bookmarksDB.getTagIterator(isAdmin)),root,0,true,""); prop.put("display_xbel", count); break; diff --git a/htroot/api/bookmarks/get_folders.java b/htroot/api/bookmarks/get_folders.java index 39604b083..45cf89c97 100644 --- a/htroot/api/bookmarks/get_folders.java +++ b/htroot/api/bookmarks/get_folders.java @@ -2,6 +2,7 @@ import java.util.Iterator; +import de.anomic.data.BookmarkHelper; import de.anomic.data.bookmarksDB; import de.anomic.data.userDB; import de.anomic.http.server.RequestHeader; @@ -49,7 +50,7 @@ public class get_folders { Iterator it = null; // loop through folderList - it = sb.bookmarksDB.getFolderList(root, isAdmin); + it = BookmarkHelper.getFolderList(root, sb.bookmarksDB.getTagIterator(isAdmin)); int n = root.split("/").length; if (n == 0) n = 1; int count = 0; diff --git a/htroot/api/bookmarks/xbel/xbel.java b/htroot/api/bookmarks/xbel/xbel.java index addeb98af..92578cc0c 100755 --- a/htroot/api/bookmarks/xbel/xbel.java +++ b/htroot/api/bookmarks/xbel/xbel.java @@ -59,7 +59,7 @@ public class xbel { } prop.put("style", style); R = root.replaceAll("[^/]","").length() - 1; - count = recurseFolders(switchboard.bookmarksDB.getFolderList(root, isAdmin),root,0,true,""); + count = recurseFolders(BookmarkHelper.getFolderList(root, switchboard.bookmarksDB.getTagIterator(isAdmin)),root,0,true,""); prop.put("xbel", count); return prop; // return from serverObjects respond() } diff --git a/source/de/anomic/data/BookmarkDate.java b/source/de/anomic/data/BookmarkDate.java index d9c2d3ada..36f13f392 100644 --- a/source/de/anomic/data/BookmarkDate.java +++ b/source/de/anomic/data/BookmarkDate.java @@ -70,7 +70,7 @@ public class BookmarkDate { String date; Entry bmDate; int count = 0; - while(it.hasNext()){ + while (it.hasNext()) { bookmark=it.next(); date = String.valueOf(bookmark.getTimeStamp()); bmDate=getDate(date); diff --git a/source/de/anomic/data/BookmarkHelper.java b/source/de/anomic/data/BookmarkHelper.java index 3560c0759..d2d62e57c 100644 --- a/source/de/anomic/data/BookmarkHelper.java +++ b/source/de/anomic/data/BookmarkHelper.java @@ -34,8 +34,10 @@ import java.text.ParseException; import java.util.Date; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.Map; import java.util.Set; +import java.util.TreeSet; import java.util.Map.Entry; import javax.xml.parsers.DocumentBuilder; @@ -49,6 +51,7 @@ import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import de.anomic.data.bookmarksDB.Bookmark; +import de.anomic.data.bookmarksDB.Tag; import net.yacy.document.parser.html.ContentScraper; import net.yacy.document.parser.html.TransformerWriter; import net.yacy.kelondro.data.meta.DigestURI; @@ -249,4 +252,26 @@ public class BookmarkHelper { return importCount; } + + public static Iterator getFolderList(final String root, Iterator tagIterator) { + + final Set folders = new TreeSet(); + String path = ""; + Tag tag; + + while (tagIterator.hasNext()) { + tag=tagIterator.next(); + if (tag.getFriendlyName().startsWith((root.equals("/") ? root : root+"/"))) { + path = tag.getFriendlyName(); + path = BookmarkHelper.cleanTagsString(path); + while(path.length() > 0 && !path.equals(root)){ + folders.add(path); + path = path.replaceAll("(/.[^/]*$)", ""); // create missing folders in path + } + } + } + if (!root.equals("/")) { folders.add(root); } + folders.add("\uffff"); + return folders.iterator(); + } } diff --git a/source/de/anomic/data/bookmarksDB.java b/source/de/anomic/data/bookmarksDB.java index 36c2f8ba1..db251f974 100644 --- a/source/de/anomic/data/bookmarksDB.java +++ b/source/de/anomic/data/bookmarksDB.java @@ -55,7 +55,6 @@ import net.yacy.kelondro.workflow.InstantBusyThread; import de.anomic.crawler.CrawlProfile; import de.anomic.crawler.retrieval.Request; -import de.anomic.data.BookmarkDate.Entry; import de.anomic.search.Segments; import de.anomic.search.Switchboard; import de.anomic.yacy.yacyNewsPool; @@ -97,7 +96,7 @@ public class bookmarksDB { // tags tags = new ConcurrentHashMap(); Log.logInfo("BOOKMARKS", "started init of tags from bookmarks.db..."); - final Iterator it = bookmarkIterator(true); + final Iterator it = new bookmarkIterator(true); Bookmark bookmark; Tag tag; String[] tags; @@ -111,7 +110,7 @@ public class bookmarksDB { tag = new Tag(tags[i]); } tag.addUrl(bookmark.getUrlHash()); - saveTag(tag); + putTag(tag); } } Log.logInfo("BOOKMARKS", "finished init " + this.tags.size() + " tags using your "+bookmarks.size()+" bookmarks."); @@ -120,7 +119,7 @@ public class bookmarksDB { final boolean datesExisted = datesFile.exists(); //this.datesTable = new MapView(BLOBTree.toHeap(datesFile, true, true, 20, 256, '_', NaturalOrder.naturalOrder, datesFileNew), 500, '_'); this.dates = new BookmarkDate(datesFile); - if (!datesExisted) this.dates.init(bookmarkIterator(true)); + if (!datesExisted) this.dates.init(new bookmarkIterator(true)); // autoReCrawl Switchboard sb = Switchboard.getSwitchboard(); @@ -299,37 +298,6 @@ public class bookmarksDB { return; } // } autoReCrawl() - // ------------------------------------- - // bookmarksDB's public helper functions - // ------------------------------------- - - public Iterator getFolderList(final boolean priv){ - return getFolderList("/", priv); - } - - public Iterator getFolderList(final String root, final boolean priv){ - - final Set folders = new TreeSet(); - String path = ""; - final Iterator it = this.getTagIterator(priv); - Tag tag; - - while(it.hasNext()){ - tag=it.next(); - if (tag.getFriendlyName().startsWith((root.equals("/") ? root : root+"/"))) { - path = tag.getFriendlyName(); - path = BookmarkHelper.cleanTagsString(path); - while(path.length() > 0 && !path.equals(root)){ - folders.add(path); - path = path.replaceAll("(/.[^/]*$)", ""); // create missing folders in path - } - } - } - if (!root.equals("/")) { folders.add(root); } - folders.add("\uffff"); - return folders.iterator(); - } - // ----------------------------------------------------------- // bookmarksDB's functions for bookmarksTable / bookmarkCache // ----------------------------------------------------------- @@ -380,7 +348,7 @@ public class bookmarksDB { tag=getTag(BookmarkHelper.tagHash(it.next())); if(tag!=null){ tag.delete(urlHash); - saveTag(tag); + putTag(tag); } } Bookmark b; @@ -393,17 +361,15 @@ public class bookmarksDB { return b != null; } - public Iterator bookmarkIterator(final boolean up){ - try { - return new bookmarkIterator(up); - } catch (final IOException e) { - return new HashSet().iterator(); - } - } - - public Iterator getBookmarksIterator(final boolean priv){ + public Iterator getBookmarksIterator(final boolean priv) { final TreeSet set=new TreeSet(new bookmarkComparator(true)); - final Iterator it=bookmarkIterator(true); + Iterator it; + try { + it = new bookmarkIterator(true); + } catch (IOException e) { + Log.logException(e); + return set.iterator(); + } Bookmark bm; while(it.hasNext()){ bm=it.next(); @@ -446,30 +412,19 @@ public class bookmarksDB { return this.tags.size(); } - /** - * load/retrieve an object of type Tag from the tagsTable (also save it in tagCache) - * @param hash an object of type String, containing a tagHash - */ - private Tag loadTag(final String hash){ - return this.tags.get(hash); - } - /** * retrieve an object of type Tag from the the tagCache, if object is not cached return loadTag(hash) * @param hash an object of type String, containing a tagHash */ public Tag getTag(final String hash){ - if(tags.containsKey(hash)){ - return tags.get(hash); - } - return loadTag(hash); //null if it does not exists + return this.tags.get(hash); //null if it does not exists } /** * store a Tag in tagsTable or remove an empty tag * @param tag an object of type Tag to be stored/removed */ - public void storeTag(final Tag tag){ + public void putTag(final Tag tag){ if (tag == null) return; if (tag.size() > 0) { this.tags.put(tag.getTagHash(), tag); @@ -478,43 +433,22 @@ public class bookmarksDB { } } - /** - * save a Tag in tagCache; see also flushTagCache(), addTag(), loadTag() - * @param tag an object of type Tag to be saved in tagCache - */ - public void saveTag(final Tag tag) { - if (tag != null) { - tags.put(tag.getTagHash(), tag); - } - } - - public String addTag(final Tag tag) { // TODO: is addTag() really needed - check storeTag() and saveTag() - //tagsTable.set(tag.getTagName(), tag.getMap()); - //tagCache.put(tag.getTagHash(), tag); - saveTag(tag); - return tag.getTagName(); - } - public void removeTag(final String hash) { tags.remove(hash); } - public Iterator tagIterator(final boolean up) { - return this.tags.values().iterator(); - } - public Iterator getTagIterator(final boolean priv) { - return getTagIterator(priv,1); + return getTagIterator(priv, 1); } - public Iterator getTagIterator(final boolean priv, final int c) { + private Iterator getTagIterator(final boolean priv, final int c) { final TreeSet set=new TreeSet((c == SORT_SIZE) ? tagSizeComparator : tagComparator); - final Iterator it = tagIterator(true); + final Iterator it = this.tags.values().iterator(); Tag tag; - while(it.hasNext()){ + while (it.hasNext()) { tag=it.next(); if (tag == null) continue; - if(priv ||tag.hasPublicItems()){ + if (priv ||tag.hasPublicItems()) { set.add(tag); } } @@ -534,7 +468,7 @@ public class bookmarksDB { return set.iterator(); } - public Iterator getTagIterator(final String tagName, final boolean priv, final int comp){ + public Iterator getTagIterator(final String tagName, final boolean priv, final int comp) { final TreeSet set=new TreeSet((comp == SORT_SIZE) ? tagSizeComparator : tagComparator); Iterator it=null; final Iterator bit=getBookmarksIterator(tagName, priv); @@ -555,9 +489,8 @@ public class bookmarksDB { return set.iterator(); } - public Iterator getTagIterator(final String tagName, final boolean priv, final int comp, final int max){ - if (max==SHOW_ALL) - return getTagIterator(priv, comp); + public Iterator getTagIterator(final String tagName, final boolean priv, final int comp, final int max) { + if (max==SHOW_ALL) return getTagIterator(priv, comp); final Iterator it = getTagIterator(tagName, priv, SORT_SIZE); final TreeSet set=new TreeSet((comp == SORT_SIZE) ? tagSizeComparator : tagComparator); int count = 0; @@ -594,7 +527,8 @@ public class bookmarksDB { } return false; } - public void addTag(final String selectTag, final String newTag){ + + public void addTag(final String selectTag, final String newTag) { final Iterator it = getTag(BookmarkHelper.tagHash(selectTag)).getUrlHashes().iterator(); // get urlHashes for selectTag Bookmark bookmark; while (it.hasNext()) { // looping through all bookmarks which were tagged with selectTag @@ -626,6 +560,7 @@ public class bookmarksDB { else urlHashes = new HashSet(); } + public Tag(final String name, final HashSet entries){ tagHash=BookmarkHelper.tagHash(name); mem=new HashMap(); @@ -633,6 +568,7 @@ public class bookmarksDB { urlHashes=entries; mem.put(TAG_NAME, name); } + public Tag(final String name){ tagHash=BookmarkHelper.tagHash(name); mem=new HashMap(); @@ -644,6 +580,7 @@ public class bookmarksDB { mem.put(URL_HASHES, listManager.collection2string(this.urlHashes)); return mem; } + /** * get the lowercase Tagname */ @@ -654,9 +591,11 @@ public class bookmarksDB { return "";*/ return getFriendlyName().toLowerCase(); } + public String getTagHash(){ return tagHash; } + /** * @return the tag name, with all uppercase chars */ @@ -670,9 +609,11 @@ public class bookmarksDB { } return "notagname"; } + public Set getUrlHashes(){ return urlHashes; } + public boolean hasPublicItems(){ final Iterator it=getBookmarksIterator(this.getTagName(), false); if(it.hasNext()){ @@ -680,12 +621,15 @@ public class bookmarksDB { } return false; } + public void addUrl(final String urlHash){ urlHashes.add(urlHash); } + public void delete(final String urlHash){ urlHashes.remove(urlHash); } + public int size(){ return urlHashes.size(); } @@ -695,6 +639,7 @@ public class bookmarksDB { * Subclass of bookmarksDB, which provides the Bookmark object-type */ public class Bookmark { + public static final String BOOKMARK_URL="bookmarkUrl"; public static final String BOOKMARK_TITLE="bookmarkTitle"; public static final String BOOKMARK_DESCRIPTION="bookmarkDesc"; @@ -898,7 +843,7 @@ public class bookmarksDB { } tag.addUrl(getUrlHash()); if(local){ - saveTag(tag); + putTag(tag); } } toMap(); @@ -918,8 +863,9 @@ public class bookmarksDB { * Subclass of bookmarksDB, which provides the bookmarkIterator object-type */ public class bookmarkIterator implements Iterator { + Iterator bookmarkIter; - //bookmarksDB.Bookmark nextEntry; + public bookmarkIterator(final boolean up) throws IOException { //flushBookmarkCache(); //XXX: this will cost performance this.bookmarkIter = bookmarksDB.this.bookmarks.keys(up, false); @@ -946,14 +892,7 @@ public class bookmarksDB { } public void remove() { -// if (this.nextEntry != null) { -// try { -// final Object bookmarkName = this.nextEntry.getUrlHash(); -// if (bookmarkName != null) removeBookmark((String) bookmarkName); -// } catch (final kelondroException e) { -// //resetDatabase(); -// } -// } + throw new UnsupportedOperationException(); } } @@ -1019,7 +958,7 @@ public class bookmarksDB { } - public Entry getDate(final String date) { + public BookmarkDate.Entry getDate(final String date) { return dates.getDate(date); } } \ No newline at end of file