diff --git a/source/de/anomic/data/bookmarksDB.java b/source/de/anomic/data/bookmarksDB.java index e831fe190..f69865abe 100644 --- a/source/de/anomic/data/bookmarksDB.java +++ b/source/de/anomic/data/bookmarksDB.java @@ -5,7 +5,7 @@ //first published on http://www.anomic.de //Frankfurt, Germany, 2004 // -//This file ist contributed by Alexander Schier +//This file has been originally contributed by Alexander Schier // //This program is free software; you can redistribute it and/or modify //it under the terms of the GNU General Public License as published by @@ -60,6 +60,7 @@ import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import java.util.regex.Pattern; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -87,19 +88,30 @@ import de.anomic.server.logging.serverLog; import de.anomic.yacy.yacyURL; public class bookmarksDB { + // ------------------------------------ + // Declacration of Class-Attributes + // ------------------------------------ + + final static int SORT_ALPHA = 1; + final static int SORT_SIZE = 2; + final static int SHOW_ALL = -1; + + // bookmarks + kelondroObjects bookmarksTable; // kelondroMap bookmarksTable; + HashMap bookmarkCache; + + // tags kelondroMapObjects tagsTable; - //kelondroMap bookmarksTable; - kelondroObjects bookmarksTable; + HashMap tagCache; + + // dates kelondroMapObjects datesTable; - HashMap tagCache; - HashMap bookmarkCache; + - public static String tagHash(String tagName){ - return plasmaCondenser.word2hash(tagName.toLowerCase()); - } - public static String tagHash(String tagName, String user){ - return plasmaCondenser.word2hash(user+":"+tagName.toLowerCase()); - } + // ------------------------------------ + // bookmarksDB's class constructor + // ------------------------------------ + public bookmarksDB(File bookmarksFile, File tagsFile, File datesFile, long preloadTime) { // bookmarks tagCache=new HashMap(); @@ -118,9 +130,13 @@ public class bookmarksDB { boolean datesExisted = datesFile.exists(); this.datesTable = new kelondroMapObjects(new kelondroDyn(datesFile, true, true, preloadTime, 20, 256, '_', kelondroNaturalOrder.naturalOrder, true, false, false), 500); if (!datesExisted) rebuildDates(); - + } - + + // ----------------------------------------------------- + // bookmarksDB's functions for 'destructing' the class + // ----------------------------------------------------- + public void close(){ bookmarksTable.close(); flushTagCache(); @@ -128,159 +144,46 @@ public class bookmarksDB { datesTable.close(); } - public int bookmarksSize(){ - return bookmarksTable.size(); + // ------------------------------------- + // bookmarksDB's public helper functions + // ------------------------------------- + + /** + * returns an object of type String that contains a tagHash + * @param tagName an object of type String with the name of the tag. + * tagName is converted to lower case before hash is generated! + */ + public static String tagHash(String tagName){ + return plasmaCondenser.word2hash(tagName.toLowerCase()); + } + public static String tagHash(String tagName, String user){ + return plasmaCondenser.word2hash(user+":"+tagName.toLowerCase()); } + + // ----------------------------------------------------------- + // bookmarksDB's functions for bookmarksTable / bookmarkCache + // ----------------------------------------------------------- - public int tagSize(boolean flushed){ - if(flushed) - flushTagCache(); - return tagsTable.size(); + public Bookmark createBookmark(String url, String user){ + if (url == null || url.length() == 0) return null; + Bookmark bk = new Bookmark(url); + bk.setOwner(user); + return (bk.getUrlHash() == null || bk.toMap() == null) ? null : bk; } - public int tagsSize(){ - return tagSize(false); + // returning the number of bookmarks + public int bookmarksSize(){ + return bookmarksTable.size(); } + // adding a bookmark to the bookmarksDB public void saveBookmark(Bookmark bookmark){ - try { - bookmarksTable.set(bookmark.getUrlHash(), bookmark); + try { + bookmarksTable.set(bookmark.getUrlHash(), bookmark); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public Tag loadTag(String hash){ - Map map; - Tag ret=null; - map = tagsTable.getMap(hash); - if(map!=null){ - ret=new Tag(hash, map); - tagCache.put(hash, ret); - } - - return ret; - } - public void saveTag(Tag tag){ - if(tag!=null){ - tagCache.put(tag.getTagHash(), tag); - } - } - /** - * store a Tag in the tagsDB or remove an empty tag - * @param tag the tagobject to be stored/removed - */ - public void storeTag(Tag tag){ - try { - if(tag.size() >0){ - bookmarksDB.this.tagsTable.set(tag.getTagHash(), tag.getMap()); - }else{ - bookmarksDB.this.tagsTable.remove(tag.getTagHash()); - } - } catch (IOException e) {} - } - public void flushTagCache(){ - Iterator it=tagCache.keySet().iterator(); - while(it.hasNext()){ - storeTag((Tag) tagCache.get(it.next())); - } - tagCache=new HashMap(); - } - - public String addTag(Tag tag){ - //tagsTable.set(tag.getTagName(), tag.getMap()); - tagCache.put(tag.getTagHash(), tag); - return tag.getTagName(); - } - public void rebuildTags(){ - serverLog.logInfo("BOOKMARKS", "rebuilding tags.db from bookmarks.db..."); - Iterator it=bookmarkIterator(true); - Bookmark bookmark; - Tag tag; - String[] tags; - while(it.hasNext()){ - bookmark=(Bookmark) it.next(); - tags = bookmark.getTagsString().split(","); - tag=null; - for(int i=0;i0){ + bookmarksDB.this.tagsTable.set(tag.getTagHash(), tag.getMap()); + }else{ + bookmarksDB.this.tagsTable.remove(tag.getTagHash()); + } + } catch (IOException e) {} + } + /** + * 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(Tag tag){ + if(tag!=null){ + tagCache.put(tag.getTagHash(), tag); + } + } + public void flushTagCache(){ + Iterator it=tagCache.keySet().iterator(); + while(it.hasNext()){ + storeTag((Tag) tagCache.get(it.next())); + } + tagCache=new HashMap(); + } + public String addTag(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(String hash){ + try { + if(tagCache.containsKey(hash)){ + tagCache.remove(hash); + } + tagsTable.remove(hash); + } catch (IOException e) {} + } + public Iterator tagIterator(boolean up){ + try { + return new tagIterator(up); + } catch (IOException e) { + return new HashSet().iterator(); + } + } public Iterator getTagIterator(boolean priv){ - TreeSet set=new TreeSet(new tagComparator()); + return getTagIterator(priv,1); + } + public Iterator getTagIterator(boolean priv, int c){ + Comparator comp; + if (c == SORT_SIZE) comp = new tagSizeComparator(); + else comp = new tagComparator(); + TreeSet set=new TreeSet(comp); Iterator it=tagIterator(true); Tag tag; while(it.hasNext()){ @@ -341,11 +369,29 @@ public class bookmarksDB { if(priv ||tag.hasPublicItems()){ set.add(tag); } - } + } + return set.iterator(); + } + public Iterator getTagIterator(boolean priv, int comp, int max){ + if (max==SHOW_ALL) + return getTagIterator(priv, comp); + Iterator it = getTagIterator(priv, comp); + TreeSet set=new TreeSet(new tagComparator()); + int count = 0; + while (it.hasNext() && count<=max) { + set.add(it.next()); + count++; + } return set.iterator(); } public Iterator getTagIterator(String tagName, boolean priv){ - TreeSet set=new TreeSet(new tagComparator()); + return getTagIterator(tagName, priv, 1); + } + public Iterator getTagIterator(String tagName, boolean priv, int comp){ + Comparator c; + if (comp == SORT_SIZE) c = new tagSizeComparator(); + else c = new tagComparator(); + TreeSet set=new TreeSet(c); Iterator it=null; Iterator bit=getBookmarksIterator(tagName, priv); Bookmark bm; @@ -364,52 +410,112 @@ public class bookmarksDB { } return set.iterator(); } - public boolean removeBookmark(String urlHash){ - Bookmark bookmark = getBookmark(urlHash); - if(bookmark == null) return false; //does not exist - Set tags = bookmark.getTags(); - bookmarksDB.Tag tag=null; - Iterator it=tags.iterator(); + public Iterator getTagIterator(String tagName, boolean priv, int comp, int max){ + if (max==SHOW_ALL) + return getTagIterator(priv, comp); + Iterator it = getTagIterator(tagName, priv, comp); + TreeSet set=new TreeSet(new tagComparator()); + int count = 0; + while (it.hasNext() && count<=max) { + set.add(it.next()); + count++; + } + return set.iterator(); + } + + // rebuilds the tagsDB from the bookmarksDB + public void rebuildTags(){ + serverLog.logInfo("BOOKMARKS", "rebuilding tags.db from bookmarks.db..."); + Iterator it=bookmarkIterator(true); + Bookmark bookmark; + Tag tag; + String[] tags; while(it.hasNext()){ - tag=getTag( tagHash((String) it.next()) ); - if(tag!=null){ - tag.delete(urlHash); + bookmark=(Bookmark) it.next(); + tags = bookmark.getTagsString().split(","); + tag=null; + for(int i=0;i tags is difficult with case sensitivity, so I tried + // Set tags = new TreeSet(String.CASE_INSENSITIVE_ORDER), but it didn't do the trick :-( + // so I chose the tagsString and replaceAll() as workaround + // unfortunately doing the replaceAll with Patterns (regexp) really costs performance + tags=listManager.string2set(Pattern.compile(oldName,66).matcher(tagsString).replaceAll(newName)); // TODO: need better solution for renaming tags + bookmark.setTags(tags, true); // I had to adjust setTags() for this to work + saveBookmark(bookmark); + } + return true; + } + return false; } + public void addBookmark(String url, String title, ArrayList tags){ } + + // -------------------------------------- + // bookmarksDB's Import/Export functions + // -------------------------------------- public int importFromBookmarks(yacyURL baseURL, String input, String tag, boolean importPublic){ try { @@ -460,7 +566,6 @@ public class bookmarksDB { return importCount; } - public int importFromXML(String input, boolean importPublic){ try { // convert string to inputstream @@ -472,7 +577,6 @@ public class bookmarksDB { return 0; } } - public int importFromXML(InputStream input, boolean importPublic){ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); factory.setValidating(false); @@ -551,9 +655,13 @@ public class bookmarksDB { return importCount; } + + // -------------------------------------- + // bookmarksDB's Subclasses + // -------------------------------------- + /** - * Subclass, which stores an Tag - * + * Subclass of bookmarksDB, which provides the Tag object-type */ public class Tag{ public static final String URL_HASHES="urlHashes"; @@ -634,6 +742,9 @@ public class bookmarksDB { return urlHashes.size(); } } + /** + * Subclass of bookmarksDB, which provide the bookmarksDate object-type + */ public class bookmarksDate{ public static final String URL_HASHES="urlHashes"; private Map mem; @@ -704,8 +815,7 @@ public class bookmarksDB { } } /** - * Subclass, which stores the bookmark - * + * Subclass of bookmarksDB, which provides the Bookmark object-type */ public class Bookmark extends kelondroObjectsMapEntry{ public static final String BOOKMARK_URL="bookmarkUrl"; @@ -860,7 +970,8 @@ public class bookmarksDB { * @param local sets, whether the updated tags should be stored to tagsDB */ public void setTags(Set tags2, boolean local){ - tags.addAll(tags2); + tags = tags2; // TODO: check if this is safe + // tags.addAll(tags2); // in order for renameTag() to work I had to change this form 'add' to 'set' Iterator it=tags.iterator(); while(it.hasNext()){ String tagName=(String) it.next(); @@ -883,6 +994,9 @@ public class bookmarksDB { this.timestamp=ts; } } + /** + * Subclass of bookmarksDB, which provides the tagIterator object-type + */ public class tagIterator implements Iterator { kelondroCloneableIterator tagIter; bookmarksDB.Tag nextEntry; @@ -918,6 +1032,9 @@ public class bookmarksDB { } } } + /** + * Subclass of bookmarksDB, which provides the bookmarkIterator object-type + */ public class bookmarkIterator implements Iterator { Iterator bookmarkIter; bookmarksDB.Bookmark nextEntry; @@ -954,7 +1071,7 @@ public class bookmarksDB { } } /** - * Comparator to sort the Bookmarks with Timestamps + * Comparator to sort objects of type Bookmark according to their timestamps */ public class bookmarkComparator implements Comparator { @@ -979,11 +1096,20 @@ public class bookmarksDB { } } /** - * sorts the tag for name + * Comparator to sort objects of type Tag according to their names */ public class tagComparator implements Comparator{ public int compare(Object obj1, Object obj2){ return ((Tag)obj1).getTagName().compareTo(((Tag)obj2).getTagName()); } } -} + public class tagSizeComparator implements Comparator{ + public int compare(Object obj1, Object obj2){ + Tag t1 = (Tag)obj1; + Tag t2 = (Tag)obj2; + if (t1.size()