bookmarkCache

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@1956 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
allo 19 years ago
parent 250864406f
commit c58789177f

@ -115,7 +115,7 @@ public class Bookmarks {
bookmark.setPublic(false); bookmark.setPublic(false);
} }
bookmark.setTags(tags, true); bookmark.setTags(tags, true);
switchboard.bookmarksDB.setBookmarksTable(bookmark); switchboard.bookmarksDB.saveBookmark(bookmark);
}else{ }else{
//ERROR //ERROR

@ -82,6 +82,7 @@ public class bookmarksDB {
kelondroMap bookmarksTable; kelondroMap bookmarksTable;
kelondroMap datesTable; kelondroMap datesTable;
HashMap tagCache; HashMap tagCache;
HashMap bookmarkCache;
public static String tagHash(String tagName){ public static String tagHash(String tagName){
return plasmaWordIndexEntry.word2hash(tagName.toLowerCase()); return plasmaWordIndexEntry.word2hash(tagName.toLowerCase());
@ -122,6 +123,7 @@ public class bookmarksDB {
//bookmarks //bookmarks
//check if database exists //check if database exists
tagCache=new HashMap(); tagCache=new HashMap();
bookmarkCache=new HashMap();
if(bookmarksFile.exists()){ if(bookmarksFile.exists()){
try { try {
//open it //open it
@ -181,6 +183,7 @@ public class bookmarksDB {
} }
public void close(){ public void close(){
try { try {
flushBookmarkCache();
bookmarksTable.close(); bookmarksTable.close();
} catch (IOException e) {} } catch (IOException e) {}
try { try {
@ -192,26 +195,40 @@ public class bookmarksDB {
} catch (IOException e) {} } catch (IOException e) {}
} }
public int bookmarksSize(){ public int bookmarksSize(){
return bookmarksSize(false);
}
public int bookmarksSize(boolean flushed){
if(flushed)
flushBookmarkCache();
return bookmarksTable.size(); return bookmarksTable.size();
} }
public int tagSize(boolean flushed){ public int tagSize(boolean flushed){
if(flushed){ if(flushed)
flushTagCache(); flushTagCache();
}
return tagsTable.size(); return tagsTable.size();
} }
public int tagsSize(){ public int tagsSize(){
return tagSize(false); return tagSize(false);
} }
public void saveBookmark(Bookmark bookmark){
bookmarkCache.put(bookmark.getUrlHash(), bookmark);
}
/** /**
* Store a Bookmark in the Bookmarkstable * Store a Bookmark in the Bookmarkstable
* @param bookmark the bookmark to store/update in the bookmarksTable * @param bookmark the bookmark to store/update in the bookmarksTable
*/ */
public void setBookmarksTable(Bookmark bookmark){ public void storeBookmark(Bookmark bookmark){
try { try {
bookmarksDB.this.bookmarksTable.set(bookmark.getUrlHash(), bookmark.mem); bookmarksDB.this.bookmarksTable.set(bookmark.getUrlHash(), bookmark.mem);
} catch (IOException e) {} } catch (IOException e) {}
} }
public void flushBookmarkCache(){
Iterator it=bookmarkCache.keySet().iterator();
while(it.hasNext()){
storeBookmark((Bookmark) it.next());
}
bookmarkCache=new HashMap();
}
public Tag loadTag(String hash){ public Tag loadTag(String hash){
Map map; Map map;
@ -337,8 +354,9 @@ public class bookmarksDB {
tags.remove(oldName); //this will fail, if upper/lowercase is not matching tags.remove(oldName); //this will fail, if upper/lowercase is not matching
tags.add(newName); tags.add(newName);
bookmark.setTags(tags, true); bookmark.setTags(tags, true);
setBookmarksTable(bookmark); saveBookmark(bookmark);
} }
flushBookmarkCache(); //XXX: is important here?
return true; return true;
} }
return false; return false;
@ -352,15 +370,14 @@ public class bookmarksDB {
} catch (IOException e) {} } catch (IOException e) {}
} }
public String addBookmark(Bookmark bookmark){ public String addBookmark(Bookmark bookmark){
try { saveBookmark(bookmark);
bookmarksTable.set(bookmark.getUrlHash(), bookmark.mem); return bookmark.getUrlHash();
return bookmark.getUrlHash();
} catch (IOException e) {
return null;
}
} }
public Bookmark getBookmark(String urlHash){ public Bookmark getBookmark(String urlHash){
Map map; Map map;
if(bookmarkCache.containsKey(urlHash))
return (Bookmark) bookmarkCache.get(urlHash);
try { try {
map = bookmarksTable.get(urlHash); map = bookmarksTable.get(urlHash);
if(map==null) return null; if(map==null) return null;
@ -428,6 +445,8 @@ public class bookmarksDB {
} }
} }
try { try {
if(bookmarkCache.containsKey(urlHash))
bookmarkCache.remove(urlHash);
bookmarksTable.remove(urlHash); bookmarksTable.remove(urlHash);
return true; return true;
} catch (IOException e) { } catch (IOException e) {
@ -479,8 +498,9 @@ public class bookmarksDB {
bm.setProperty(Bookmark.BOOKMARK_TITLE, title); bm.setProperty(Bookmark.BOOKMARK_TITLE, title);
bm.setTags(tags); bm.setTags(tags);
bm.setPublic(importPublic); bm.setPublic(importPublic);
setBookmarksTable(bm); saveBookmark(bm);
} }
flushBookmarkCache();
flushTagCache(); flushTagCache();
} }
public void importFromXML(String input, boolean importPublic){ public void importFromXML(String input, boolean importPublic){
@ -538,7 +558,7 @@ public class bookmarksDB {
bm.setProperty(Bookmark.BOOKMARK_DESCRIPTION, description); bm.setProperty(Bookmark.BOOKMARK_DESCRIPTION, description);
} }
bm.setPublic(importPublic); bm.setPublic(importPublic);
setBookmarksTable(bm); saveBookmark(bm);
} }
NodeList children=doc.getChildNodes(); NodeList children=doc.getChildNodes();
if(children != null){ if(children != null){
@ -546,6 +566,7 @@ public class bookmarksDB {
parseXMLimport(children.item(i), importPublic); parseXMLimport(children.item(i), importPublic);
} }
} }
flushBookmarkCache();
flushTagCache(); flushTagCache();
} }
/** /**
@ -717,22 +738,17 @@ public class bookmarksDB {
this.urlHash=plasmaURL.urlHash(url); this.urlHash=plasmaURL.urlHash(url);
mem=new HashMap(); mem=new HashMap();
mem.put(BOOKMARK_URL, url); mem.put(BOOKMARK_URL, url);
try { Bookmark oldBm=getBookmark(this.urlHash);
Map oldmap= bookmarksTable.get(this.urlHash); if(oldBm!=null && oldBm.mem.containsKey(BOOKMARK_TIMESTAMP)){
if(oldmap != null && oldmap.containsKey(BOOKMARK_TIMESTAMP)){ mem.put(BOOKMARK_TIMESTAMP, oldBm.mem.get(BOOKMARK_TIMESTAMP)); //preserve timestamp on edit
mem.put(BOOKMARK_TIMESTAMP, oldmap.get(BOOKMARK_TIMESTAMP)); //preserve timestamp on edit }else{
}else{ mem.put(BOOKMARK_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
mem.put(BOOKMARK_TIMESTAMP, String.valueOf(System.currentTimeMillis())); }
}
bookmarksDate bmDate=getDate((String) mem.get(BOOKMARK_TIMESTAMP)); bookmarksDate bmDate=getDate((String) mem.get(BOOKMARK_TIMESTAMP));
bmDate.add(this.urlHash); bmDate.add(this.urlHash);
bmDate.setDatesTable(); bmDate.setDatesTable();
removeBookmark(this.urlHash); //prevent empty tags removeBookmark(this.urlHash); //prevent empty tags
} catch (IOException e) {
//entry not yet present (normal case)
mem.put(BOOKMARK_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
}
} }
public Bookmark(String urlHash, URL url){ public Bookmark(String urlHash, URL url){
this.urlHash=urlHash; this.urlHash=urlHash;
@ -826,11 +842,7 @@ public class bookmarksDB {
} }
} }
} }
public void setBookmarksTable(){
try {
bookmarksDB.this.bookmarksTable.set(urlHash, mem);
} catch (IOException e) {}
}
public long getTimeStamp(){ public long getTimeStamp(){
if(mem.containsKey(BOOKMARK_TIMESTAMP)){ if(mem.containsKey(BOOKMARK_TIMESTAMP)){
return Long.parseLong((String)mem.get(BOOKMARK_TIMESTAMP)); return Long.parseLong((String)mem.get(BOOKMARK_TIMESTAMP));
@ -881,6 +893,7 @@ public class bookmarksDB {
kelondroDyn.dynKeyIterator bookmarkIter; kelondroDyn.dynKeyIterator bookmarkIter;
bookmarksDB.Bookmark nextEntry; bookmarksDB.Bookmark nextEntry;
public bookmarkIterator(boolean up) throws IOException { public bookmarkIterator(boolean up) throws IOException {
flushBookmarkCache(); //XXX: this will cost performance
this.bookmarkIter = bookmarksDB.this.bookmarksTable.keys(up, false); this.bookmarkIter = bookmarksDB.this.bookmarksTable.keys(up, false);
this.nextEntry = null; this.nextEntry = null;
} }

Loading…
Cancel
Save