git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@1952 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
allo 19 years ago
parent 330eb9c74f
commit 2ed4fa96b7

@ -81,6 +81,7 @@ public class bookmarksDB {
kelondroMap tagsTable; kelondroMap tagsTable;
kelondroMap bookmarksTable; kelondroMap bookmarksTable;
kelondroMap datesTable; kelondroMap datesTable;
HashMap tagCache;
public static String tagHash(String tagName){ public static String tagHash(String tagName){
return plasmaWordIndexEntry.word2hash(tagName.toLowerCase()); return plasmaWordIndexEntry.word2hash(tagName.toLowerCase());
@ -120,6 +121,7 @@ public class bookmarksDB {
public bookmarksDB(File bookmarksFile, File tagsFile, File datesFile, int bufferkb){ public bookmarksDB(File bookmarksFile, File tagsFile, File datesFile, int bufferkb){
//bookmarks //bookmarks
//check if database exists //check if database exists
tagCache=new HashMap();
if(bookmarksFile.exists()){ if(bookmarksFile.exists()){
try { try {
//open it //open it
@ -182,6 +184,7 @@ public class bookmarksDB {
bookmarksTable.close(); bookmarksTable.close();
} catch (IOException e) {} } catch (IOException e) {}
try { try {
flushTagCache();
tagsTable.close(); tagsTable.close();
} catch (IOException e) {} } catch (IOException e) {}
try { try {
@ -191,9 +194,15 @@ public class bookmarksDB {
public int bookmarksSize(){ public int bookmarksSize(){
return bookmarksTable.size(); return bookmarksTable.size();
} }
public int tagsSize(){ public int tagSize(boolean flushed){
if(flushed){
flushTagCache();
}
return tagsTable.size(); return tagsTable.size();
} }
public int tagsSize(){
return tagSize(false);
}
/** /**
* 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
@ -203,11 +212,30 @@ public class bookmarksDB {
bookmarksDB.this.bookmarksTable.set(bookmark.getUrlHash(), bookmark.mem); bookmarksDB.this.bookmarksTable.set(bookmark.getUrlHash(), bookmark.mem);
} catch (IOException e) {} } catch (IOException e) {}
} }
public Tag loadTag(String hash){
Map map;
Tag ret=null;
try {
map = tagsTable.get(hash);
if(map!=null){
ret=new Tag(hash, map);
tagCache.put(hash, ret);
}
} catch (IOException e) {}
return ret;
}
public void saveTag(Tag tag){
if(tag!=null){
tagCache.put(tag.getTagName(), tag);
}
}
/** /**
* store a Tag in the tagsDB or remove an empty tag * store a Tag in the tagsDB or remove an empty tag
* @param tag the tagobject to be stored/removed * @param tag the tagobject to be stored/removed
*/ */
public void setTagsTable(Tag tag){ public void storeTag(Tag tag){
try { try {
if(tag.size() >0){ if(tag.size() >0){
bookmarksDB.this.tagsTable.set(tag.getTagHash(), tag.getMap()); bookmarksDB.this.tagsTable.set(tag.getTagHash(), tag.getMap());
@ -216,13 +244,18 @@ public class bookmarksDB {
} }
} catch (IOException e) {} } catch (IOException e) {}
} }
public String addTag(Tag tag){ public void flushTagCache(){
try { Iterator it=tagCache.keySet().iterator();
tagsTable.set(tag.getTagName(), tag.getMap()); while(it.hasNext()){
return tag.getTagName(); storeTag((Tag) tagCache.get(it.next()));
} catch (IOException e) {
return null;
} }
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(){ public void rebuildTags(){
serverLog.logInfo("BOOKMARKS", "rebuilding tags.db from bookmarks.db..."); serverLog.logInfo("BOOKMARKS", "rebuilding tags.db from bookmarks.db...");
@ -240,9 +273,10 @@ public class bookmarksDB {
tag=new Tag(tags[i]); tag=new Tag(tags[i]);
} }
tag.add(bookmark.getUrlHash()); tag.add(bookmark.getUrlHash());
setTagsTable(tag); saveTag(tag);
} }
} }
flushTagCache();
serverLog.logInfo("BOOKMARKS", "Rebuilt "+tagsTable.size()+" tags using your "+bookmarksTable.size()+" bookmarks."); serverLog.logInfo("BOOKMARKS", "Rebuilt "+tagsTable.size()+" tags using your "+bookmarksTable.size()+" bookmarks.");
} }
public void rebuildDates(){ public void rebuildDates(){
@ -264,14 +298,10 @@ public class bookmarksDB {
serverLog.logInfo("BOOKMARKS", "Rebuilt "+datesTable.size()+" dates using your "+bookmarksTable.size()+" bookmarks."); serverLog.logInfo("BOOKMARKS", "Rebuilt "+datesTable.size()+" dates using your "+bookmarksTable.size()+" bookmarks.");
} }
public Tag getTag(String hash){ public Tag getTag(String hash){
Map map; if(tagCache.containsKey(hash)){
try { return (Tag) tagCache.get(hash);
map = tagsTable.get(hash);
if(map==null) return null;
return new Tag(hash, map);
} catch (IOException e) {
return null;
} }
return loadTag(hash); //null if it does not exists
} }
public bookmarksDate getDate(String date){ public bookmarksDate getDate(String date){
Map map; Map map;
@ -290,12 +320,14 @@ public class bookmarksDB {
if (tag != null) { if (tag != null) {
HashSet urlHashes = tag.getUrlHashes(); HashSet urlHashes = tag.getUrlHashes();
try { try {
if(tagCache.containsKey(tagHash(oldName))){
tagCache.remove(tagHash(oldName));
}
tagsTable.remove(tagHash(oldName)); tagsTable.remove(tagHash(oldName));
} catch (IOException e) { } catch (IOException e) {
} }
tag=new Tag(tagHash(newName), tag.getMap()); tag=new Tag(tagHash(newName), tag.getMap());
tag.tagHash = tagHash(newName); saveTag(tag);
setTagsTable(tag);
Iterator it = urlHashes.iterator(); Iterator it = urlHashes.iterator();
Bookmark bookmark; Bookmark bookmark;
ArrayList tags; ArrayList tags;
@ -311,9 +343,12 @@ public class bookmarksDB {
} }
return false; return false;
} }
public void removeTag(String tagName){ public void removeTag(String hash){
try { try {
tagsTable.remove(tagName); if(tagCache.containsKey(hash)){
tagCache.remove(hash);
}
tagsTable.remove(hash);
} catch (IOException e) {} } catch (IOException e) {}
} }
public String addBookmark(Bookmark bookmark){ public String addBookmark(Bookmark bookmark){
@ -389,7 +424,7 @@ public class bookmarksDB {
tag=getTag(tagHash(tags[i])); tag=getTag(tagHash(tags[i]));
if(tag !=null){ if(tag !=null){
tag.delete(urlHash); tag.delete(urlHash);
setTagsTable(tag); saveTag(tag);
} }
} }
try { try {
@ -446,7 +481,7 @@ public class bookmarksDB {
bm.setPublic(importPublic); bm.setPublic(importPublic);
setBookmarksTable(bm); setBookmarksTable(bm);
} }
flushTagCache();
} }
public void importFromXML(String input, boolean importPublic){ public void importFromXML(String input, boolean importPublic){
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
@ -511,6 +546,7 @@ public class bookmarksDB {
parseXMLimport(children.item(i), importPublic); parseXMLimport(children.item(i), importPublic);
} }
} }
flushTagCache();
} }
/** /**
* Subclass, which stores an Tag * Subclass, which stores an Tag
@ -786,7 +822,7 @@ public class bookmarksDB {
} }
tag.add(getUrlHash()); tag.add(getUrlHash());
if(local){ if(local){
setTagsTable(tag); saveTag(tag);
} }
} }
} }
@ -810,6 +846,7 @@ public class bookmarksDB {
kelondroDyn.dynKeyIterator tagIter; kelondroDyn.dynKeyIterator tagIter;
bookmarksDB.Tag nextEntry; bookmarksDB.Tag nextEntry;
public tagIterator(boolean up) throws IOException { public tagIterator(boolean up) throws IOException {
flushTagCache(); //XXX: This costs performace :-((
this.tagIter = bookmarksDB.this.tagsTable.keys(up, false); this.tagIter = bookmarksDB.this.tagsTable.keys(up, false);
this.nextEntry = null; this.nextEntry = null;
} }

Loading…
Cancel
Save