more simplification of bookmark class

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6639 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 15 years ago
parent d77782a8d5
commit 840527689b

@ -349,7 +349,7 @@ public class Bookmarks {
//----------------------- //-----------------------
count = 0; 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); prop.put("display_folderlist", count);
} }
return prop; // return from serverObjects respond() return prop; // return from serverObjects respond()

@ -108,7 +108,7 @@ public class get_bookmarks {
} }
prop.putHTML("display_folder", "0"); prop.putHTML("display_folder", "0");
R = root.replaceAll("[^/]","").length() - 1; 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); prop.put("display_xbel", count);
break; break;

@ -2,6 +2,7 @@
import java.util.Iterator; import java.util.Iterator;
import de.anomic.data.BookmarkHelper;
import de.anomic.data.bookmarksDB; import de.anomic.data.bookmarksDB;
import de.anomic.data.userDB; import de.anomic.data.userDB;
import de.anomic.http.server.RequestHeader; import de.anomic.http.server.RequestHeader;
@ -49,7 +50,7 @@ public class get_folders {
Iterator<String> it = null; Iterator<String> it = null;
// loop through folderList // loop through folderList
it = sb.bookmarksDB.getFolderList(root, isAdmin); it = BookmarkHelper.getFolderList(root, sb.bookmarksDB.getTagIterator(isAdmin));
int n = root.split("/").length; int n = root.split("/").length;
if (n == 0) n = 1; if (n == 0) n = 1;
int count = 0; int count = 0;

@ -59,7 +59,7 @@ public class xbel {
} }
prop.put("style", style); prop.put("style", style);
R = root.replaceAll("[^/]","").length() - 1; 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); prop.put("xbel", count);
return prop; // return from serverObjects respond() return prop; // return from serverObjects respond()
} }

@ -34,8 +34,10 @@ import java.text.ParseException;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;
import java.util.Map.Entry; import java.util.Map.Entry;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
@ -49,6 +51,7 @@ import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import de.anomic.data.bookmarksDB.Bookmark; 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.ContentScraper;
import net.yacy.document.parser.html.TransformerWriter; import net.yacy.document.parser.html.TransformerWriter;
import net.yacy.kelondro.data.meta.DigestURI; import net.yacy.kelondro.data.meta.DigestURI;
@ -249,4 +252,26 @@ public class BookmarkHelper {
return importCount; return importCount;
} }
public static Iterator<String> getFolderList(final String root, Iterator<Tag> tagIterator) {
final Set<String> folders = new TreeSet<String>();
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();
}
} }

@ -55,7 +55,6 @@ import net.yacy.kelondro.workflow.InstantBusyThread;
import de.anomic.crawler.CrawlProfile; import de.anomic.crawler.CrawlProfile;
import de.anomic.crawler.retrieval.Request; import de.anomic.crawler.retrieval.Request;
import de.anomic.data.BookmarkDate.Entry;
import de.anomic.search.Segments; import de.anomic.search.Segments;
import de.anomic.search.Switchboard; import de.anomic.search.Switchboard;
import de.anomic.yacy.yacyNewsPool; import de.anomic.yacy.yacyNewsPool;
@ -97,7 +96,7 @@ public class bookmarksDB {
// tags // tags
tags = new ConcurrentHashMap<String, Tag>(); tags = new ConcurrentHashMap<String, Tag>();
Log.logInfo("BOOKMARKS", "started init of tags from bookmarks.db..."); Log.logInfo("BOOKMARKS", "started init of tags from bookmarks.db...");
final Iterator<Bookmark> it = bookmarkIterator(true); final Iterator<Bookmark> it = new bookmarkIterator(true);
Bookmark bookmark; Bookmark bookmark;
Tag tag; Tag tag;
String[] tags; String[] tags;
@ -111,7 +110,7 @@ public class bookmarksDB {
tag = new Tag(tags[i]); tag = new Tag(tags[i]);
} }
tag.addUrl(bookmark.getUrlHash()); tag.addUrl(bookmark.getUrlHash());
saveTag(tag); putTag(tag);
} }
} }
Log.logInfo("BOOKMARKS", "finished init " + this.tags.size() + " tags using your "+bookmarks.size()+" bookmarks."); 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(); final boolean datesExisted = datesFile.exists();
//this.datesTable = new MapView(BLOBTree.toHeap(datesFile, true, true, 20, 256, '_', NaturalOrder.naturalOrder, datesFileNew), 500, '_'); //this.datesTable = new MapView(BLOBTree.toHeap(datesFile, true, true, 20, 256, '_', NaturalOrder.naturalOrder, datesFileNew), 500, '_');
this.dates = new BookmarkDate(datesFile); this.dates = new BookmarkDate(datesFile);
if (!datesExisted) this.dates.init(bookmarkIterator(true)); if (!datesExisted) this.dates.init(new bookmarkIterator(true));
// autoReCrawl // autoReCrawl
Switchboard sb = Switchboard.getSwitchboard(); Switchboard sb = Switchboard.getSwitchboard();
@ -299,37 +298,6 @@ public class bookmarksDB {
return; return;
} // } autoReCrawl() } // } autoReCrawl()
// -------------------------------------
// bookmarksDB's public helper functions
// -------------------------------------
public Iterator<String> getFolderList(final boolean priv){
return getFolderList("/", priv);
}
public Iterator<String> getFolderList(final String root, final boolean priv){
final Set<String> folders = new TreeSet<String>();
String path = "";
final Iterator<Tag> 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 // bookmarksDB's functions for bookmarksTable / bookmarkCache
// ----------------------------------------------------------- // -----------------------------------------------------------
@ -380,7 +348,7 @@ public class bookmarksDB {
tag=getTag(BookmarkHelper.tagHash(it.next())); tag=getTag(BookmarkHelper.tagHash(it.next()));
if(tag!=null){ if(tag!=null){
tag.delete(urlHash); tag.delete(urlHash);
saveTag(tag); putTag(tag);
} }
} }
Bookmark b; Bookmark b;
@ -393,17 +361,15 @@ public class bookmarksDB {
return b != null; return b != null;
} }
public Iterator<Bookmark> bookmarkIterator(final boolean up){
try {
return new bookmarkIterator(up);
} catch (final IOException e) {
return new HashSet<Bookmark>().iterator();
}
}
public Iterator<String> getBookmarksIterator(final boolean priv) { public Iterator<String> getBookmarksIterator(final boolean priv) {
final TreeSet<String> set=new TreeSet<String>(new bookmarkComparator(true)); final TreeSet<String> set=new TreeSet<String>(new bookmarkComparator(true));
final Iterator<Bookmark> it=bookmarkIterator(true); Iterator<Bookmark> it;
try {
it = new bookmarkIterator(true);
} catch (IOException e) {
Log.logException(e);
return set.iterator();
}
Bookmark bm; Bookmark bm;
while(it.hasNext()){ while(it.hasNext()){
bm=it.next(); bm=it.next();
@ -446,30 +412,19 @@ public class bookmarksDB {
return this.tags.size(); 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) * 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 * @param hash an object of type String, containing a tagHash
*/ */
public Tag getTag(final String hash){ public Tag getTag(final String hash){
if(tags.containsKey(hash)){ return this.tags.get(hash); //null if it does not exists
return tags.get(hash);
}
return loadTag(hash); //null if it does not exists
} }
/** /**
* store a Tag in tagsTable or remove an empty tag * store a Tag in tagsTable or remove an empty tag
* @param tag an object of type Tag to be stored/removed * @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 == null) return;
if (tag.size() > 0) { if (tag.size() > 0) {
this.tags.put(tag.getTagHash(), tag); this.tags.put(tag.getTagHash(), tag);
@ -478,38 +433,17 @@ 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) { public void removeTag(final String hash) {
tags.remove(hash); tags.remove(hash);
} }
public Iterator<Tag> tagIterator(final boolean up) {
return this.tags.values().iterator();
}
public Iterator<Tag> getTagIterator(final boolean priv) { public Iterator<Tag> getTagIterator(final boolean priv) {
return getTagIterator(priv, 1); return getTagIterator(priv, 1);
} }
public Iterator<Tag> getTagIterator(final boolean priv, final int c) { private Iterator<Tag> getTagIterator(final boolean priv, final int c) {
final TreeSet<Tag> set=new TreeSet<Tag>((c == SORT_SIZE) ? tagSizeComparator : tagComparator); final TreeSet<Tag> set=new TreeSet<Tag>((c == SORT_SIZE) ? tagSizeComparator : tagComparator);
final Iterator<Tag> it = tagIterator(true); final Iterator<Tag> it = this.tags.values().iterator();
Tag tag; Tag tag;
while (it.hasNext()) { while (it.hasNext()) {
tag=it.next(); tag=it.next();
@ -556,8 +490,7 @@ public class bookmarksDB {
} }
public Iterator<Tag> getTagIterator(final String tagName, final boolean priv, final int comp, final int max) { public Iterator<Tag> getTagIterator(final String tagName, final boolean priv, final int comp, final int max) {
if (max==SHOW_ALL) if (max==SHOW_ALL) return getTagIterator(priv, comp);
return getTagIterator(priv, comp);
final Iterator<Tag> it = getTagIterator(tagName, priv, SORT_SIZE); final Iterator<Tag> it = getTagIterator(tagName, priv, SORT_SIZE);
final TreeSet<Tag> set=new TreeSet<Tag>((comp == SORT_SIZE) ? tagSizeComparator : tagComparator); final TreeSet<Tag> set=new TreeSet<Tag>((comp == SORT_SIZE) ? tagSizeComparator : tagComparator);
int count = 0; int count = 0;
@ -594,6 +527,7 @@ public class bookmarksDB {
} }
return false; return false;
} }
public void addTag(final String selectTag, final String newTag) { public void addTag(final String selectTag, final String newTag) {
final Iterator<String> it = getTag(BookmarkHelper.tagHash(selectTag)).getUrlHashes().iterator(); // get urlHashes for selectTag final Iterator<String> it = getTag(BookmarkHelper.tagHash(selectTag)).getUrlHashes().iterator(); // get urlHashes for selectTag
Bookmark bookmark; Bookmark bookmark;
@ -626,6 +560,7 @@ public class bookmarksDB {
else else
urlHashes = new HashSet<String>(); urlHashes = new HashSet<String>();
} }
public Tag(final String name, final HashSet<String> entries){ public Tag(final String name, final HashSet<String> entries){
tagHash=BookmarkHelper.tagHash(name); tagHash=BookmarkHelper.tagHash(name);
mem=new HashMap<String, String>(); mem=new HashMap<String, String>();
@ -633,6 +568,7 @@ public class bookmarksDB {
urlHashes=entries; urlHashes=entries;
mem.put(TAG_NAME, name); mem.put(TAG_NAME, name);
} }
public Tag(final String name){ public Tag(final String name){
tagHash=BookmarkHelper.tagHash(name); tagHash=BookmarkHelper.tagHash(name);
mem=new HashMap<String, String>(); mem=new HashMap<String, String>();
@ -644,6 +580,7 @@ public class bookmarksDB {
mem.put(URL_HASHES, listManager.collection2string(this.urlHashes)); mem.put(URL_HASHES, listManager.collection2string(this.urlHashes));
return mem; return mem;
} }
/** /**
* get the lowercase Tagname * get the lowercase Tagname
*/ */
@ -654,9 +591,11 @@ public class bookmarksDB {
return "";*/ return "";*/
return getFriendlyName().toLowerCase(); return getFriendlyName().toLowerCase();
} }
public String getTagHash(){ public String getTagHash(){
return tagHash; return tagHash;
} }
/** /**
* @return the tag name, with all uppercase chars * @return the tag name, with all uppercase chars
*/ */
@ -670,9 +609,11 @@ public class bookmarksDB {
} }
return "notagname"; return "notagname";
} }
public Set<String> getUrlHashes(){ public Set<String> getUrlHashes(){
return urlHashes; return urlHashes;
} }
public boolean hasPublicItems(){ public boolean hasPublicItems(){
final Iterator<String> it=getBookmarksIterator(this.getTagName(), false); final Iterator<String> it=getBookmarksIterator(this.getTagName(), false);
if(it.hasNext()){ if(it.hasNext()){
@ -680,12 +621,15 @@ public class bookmarksDB {
} }
return false; return false;
} }
public void addUrl(final String urlHash){ public void addUrl(final String urlHash){
urlHashes.add(urlHash); urlHashes.add(urlHash);
} }
public void delete(final String urlHash){ public void delete(final String urlHash){
urlHashes.remove(urlHash); urlHashes.remove(urlHash);
} }
public int size(){ public int size(){
return urlHashes.size(); return urlHashes.size();
} }
@ -695,6 +639,7 @@ public class bookmarksDB {
* Subclass of bookmarksDB, which provides the Bookmark object-type * Subclass of bookmarksDB, which provides the Bookmark object-type
*/ */
public class Bookmark { public class Bookmark {
public static final String BOOKMARK_URL="bookmarkUrl"; public static final String BOOKMARK_URL="bookmarkUrl";
public static final String BOOKMARK_TITLE="bookmarkTitle"; public static final String BOOKMARK_TITLE="bookmarkTitle";
public static final String BOOKMARK_DESCRIPTION="bookmarkDesc"; public static final String BOOKMARK_DESCRIPTION="bookmarkDesc";
@ -898,7 +843,7 @@ public class bookmarksDB {
} }
tag.addUrl(getUrlHash()); tag.addUrl(getUrlHash());
if(local){ if(local){
saveTag(tag); putTag(tag);
} }
} }
toMap(); toMap();
@ -918,8 +863,9 @@ public class bookmarksDB {
* Subclass of bookmarksDB, which provides the bookmarkIterator object-type * Subclass of bookmarksDB, which provides the bookmarkIterator object-type
*/ */
public class bookmarkIterator implements Iterator<Bookmark> { public class bookmarkIterator implements Iterator<Bookmark> {
Iterator<byte[]> bookmarkIter; Iterator<byte[]> bookmarkIter;
//bookmarksDB.Bookmark nextEntry;
public bookmarkIterator(final boolean up) throws IOException { public bookmarkIterator(final boolean up) throws IOException {
//flushBookmarkCache(); //XXX: this will cost performance //flushBookmarkCache(); //XXX: this will cost performance
this.bookmarkIter = bookmarksDB.this.bookmarks.keys(up, false); this.bookmarkIter = bookmarksDB.this.bookmarks.keys(up, false);
@ -946,14 +892,7 @@ public class bookmarksDB {
} }
public void remove() { public void remove() {
// if (this.nextEntry != null) { throw new UnsupportedOperationException();
// try {
// final Object bookmarkName = this.nextEntry.getUrlHash();
// if (bookmarkName != null) removeBookmark((String) bookmarkName);
// } catch (final kelondroException e) {
// //resetDatabase();
// }
// }
} }
} }
@ -1019,7 +958,7 @@ public class bookmarksDB {
} }
public Entry getDate(final String date) { public BookmarkDate.Entry getDate(final String date) {
return dates.getDate(date); return dates.getDate(date);
} }
} }
Loading…
Cancel
Save