2011-11-22 01:03:20 +01:00
|
|
|
//bookmarksDB.java
|
2005-12-26 15:21:01 +01:00
|
|
|
//-------------------------------------
|
|
|
|
//part of YACY
|
2008-07-20 19:14:51 +02:00
|
|
|
//(C) by Michael Peter Christen; mc@yacy.net
|
2005-12-26 15:21:01 +01:00
|
|
|
//first published on http://www.anomic.de
|
|
|
|
//Frankfurt, Germany, 2004
|
|
|
|
//
|
2008-01-22 20:08:32 +01:00
|
|
|
//This file has been originally contributed by Alexander Schier
|
2005-12-26 15:21:01 +01:00
|
|
|
//
|
|
|
|
//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
|
|
|
|
//the Free Software Foundation; either version 2 of the License, or
|
|
|
|
//(at your option) any later version.
|
|
|
|
//
|
|
|
|
//This program is distributed in the hope that it will be useful,
|
|
|
|
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
//GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
//You should have received a copy of the GNU General Public License
|
|
|
|
//along with this program; if not, write to the Free Software
|
|
|
|
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2008-07-20 02:21:07 +02:00
|
|
|
|
2012-09-21 15:48:16 +02:00
|
|
|
package net.yacy.data;
|
2005-12-26 15:21:01 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2008-08-06 21:43:12 +02:00
|
|
|
import java.io.Serializable;
|
2007-09-05 11:01:35 +02:00
|
|
|
import java.net.MalformedURLException;
|
2005-12-26 22:03:42 +01:00
|
|
|
import java.util.Comparator;
|
2005-12-26 15:21:01 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map;
|
2007-09-10 08:20:27 +02:00
|
|
|
import java.util.Set;
|
2005-12-26 22:03:42 +01:00
|
|
|
import java.util.TreeSet;
|
2010-01-22 15:20:24 +01:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2005-12-26 15:21:01 +01:00
|
|
|
|
2013-09-15 00:30:23 +02:00
|
|
|
import net.yacy.cora.document.encoding.ASCII;
|
|
|
|
import net.yacy.cora.document.encoding.UTF8;
|
|
|
|
import net.yacy.cora.document.id.DigestURL;
|
2012-09-21 16:46:57 +02:00
|
|
|
import net.yacy.cora.order.NaturalOrder;
|
2013-07-09 14:28:25 +02:00
|
|
|
import net.yacy.cora.util.ConcurrentLog;
|
2013-11-07 21:30:17 +01:00
|
|
|
import net.yacy.cora.util.SpaceExceededException;
|
2010-01-13 17:21:37 +01:00
|
|
|
import net.yacy.kelondro.blob.MapHeap;
|
2014-01-20 18:31:46 +01:00
|
|
|
import net.yacy.kelondro.data.word.Word;
|
2013-11-07 21:30:17 +01:00
|
|
|
import net.yacy.kelondro.index.RowHandleSet;
|
2005-12-26 15:21:01 +01:00
|
|
|
|
2010-11-21 02:29:32 +01:00
|
|
|
public class BookmarksDB {
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
// ------------------------------------
|
2008-01-29 17:41:09 +01:00
|
|
|
// Declaration of Class-Attributes
|
2008-01-22 20:08:32 +01:00
|
|
|
// ------------------------------------
|
|
|
|
|
2010-04-13 03:16:09 +02:00
|
|
|
//final static int SORT_ALPHA = 1;
|
|
|
|
private final static int SORT_SIZE = 2;
|
|
|
|
private final static int SHOW_ALL = -1;
|
|
|
|
|
|
|
|
// bookmarks
|
2011-11-22 01:03:20 +01:00
|
|
|
private final MapHeap bookmarks;
|
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
// tags
|
2011-11-22 01:03:20 +01:00
|
|
|
private final ConcurrentHashMap<String, Tag> tags;
|
|
|
|
|
|
|
|
private final BookmarkDate dates;
|
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
// ------------------------------------
|
|
|
|
// bookmarksDB's class constructor
|
|
|
|
// ------------------------------------
|
|
|
|
|
2010-11-21 02:29:32 +01:00
|
|
|
public BookmarksDB(final File bookmarksFile, final File datesFile) throws IOException {
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2006-08-24 04:19:25 +02:00
|
|
|
// bookmarks
|
|
|
|
bookmarksFile.getParentFile().mkdirs();
|
2007-01-29 20:10:55 +01:00
|
|
|
//this.bookmarksTable = new kelondroMap(kelondroDyn.open(bookmarksFile, bufferkb * 1024, preloadTime, 12, 256, '_', true, false));
|
2009-06-28 23:02:56 +02:00
|
|
|
//this.bookmarksTable = new MapView(BLOBTree.toHeap(bookmarksFile, true, true, 12, 256, '_', NaturalOrder.naturalOrder, bookmarksFileNew), 1000, '_');
|
2011-04-04 14:20:20 +02:00
|
|
|
this.bookmarks = new MapHeap(bookmarksFile, 12, NaturalOrder.naturalOrder, 1024 * 64, 1000, ' ');
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2006-08-24 04:19:25 +02:00
|
|
|
// tags
|
2011-11-22 01:03:20 +01:00
|
|
|
this.tags = new ConcurrentHashMap<String, Tag>();
|
2013-07-09 14:28:25 +02:00
|
|
|
ConcurrentLog.info("BOOKMARKS", "started init of tags from bookmarks.db...");
|
2010-02-02 00:04:52 +01:00
|
|
|
final Iterator<Bookmark> it = new bookmarkIterator(true);
|
2010-02-01 23:44:59 +01:00
|
|
|
Bookmark bookmark;
|
|
|
|
Tag tag;
|
2010-04-13 03:16:09 +02:00
|
|
|
String[] tagArray;
|
2012-01-19 23:06:09 +01:00
|
|
|
while (it.hasNext()) {
|
|
|
|
try {
|
|
|
|
bookmark = it.next();
|
2013-07-17 18:31:30 +02:00
|
|
|
} catch (final Throwable e) {
|
2012-06-05 15:04:23 +02:00
|
|
|
//Log.logException(e);
|
2012-01-19 23:06:09 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (bookmark == null) continue;
|
2010-04-13 03:16:09 +02:00
|
|
|
tagArray = BookmarkHelper.cleanTagsString(bookmark.getTagsString() + bookmark.getFoldersString()).split(",");
|
2010-02-01 23:44:59 +01:00
|
|
|
tag = null;
|
2010-04-13 03:16:09 +02:00
|
|
|
for (final String element : tagArray) {
|
|
|
|
tag = getTag(BookmarkHelper.tagHash(element));
|
2010-02-01 23:44:59 +01:00
|
|
|
if (tag == null) {
|
2010-04-13 03:16:09 +02:00
|
|
|
tag = new Tag(element);
|
2010-02-01 23:44:59 +01:00
|
|
|
}
|
|
|
|
tag.addUrl(bookmark.getUrlHash());
|
2010-02-02 00:04:52 +01:00
|
|
|
putTag(tag);
|
2010-02-01 23:44:59 +01:00
|
|
|
}
|
|
|
|
}
|
2013-07-09 14:28:25 +02:00
|
|
|
ConcurrentLog.info("BOOKMARKS", "finished init " + this.tags.size() + " tags using your "+this.bookmarks.size()+" bookmarks.");
|
2006-08-24 04:19:25 +02:00
|
|
|
|
2005-12-30 13:34:44 +01:00
|
|
|
// dates
|
2008-08-02 14:12:04 +02:00
|
|
|
final boolean datesExisted = datesFile.exists();
|
2009-06-28 23:02:56 +02:00
|
|
|
//this.datesTable = new MapView(BLOBTree.toHeap(datesFile, true, true, 20, 256, '_', NaturalOrder.naturalOrder, datesFileNew), 500, '_');
|
2010-02-01 23:18:56 +01:00
|
|
|
this.dates = new BookmarkDate(datesFile);
|
2010-02-02 00:04:52 +01:00
|
|
|
if (!datesExisted) this.dates.init(new bookmarkIterator(true));
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2008-01-22 20:08:32 +01:00
|
|
|
|
|
|
|
// -----------------------------------------------------
|
2010-04-13 03:16:09 +02:00
|
|
|
// bookmarksDB's functions for 'destructing' the class
|
|
|
|
// -----------------------------------------------------
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2012-05-14 07:41:55 +02:00
|
|
|
public synchronized void close(){
|
2011-11-22 01:03:20 +01:00
|
|
|
this.bookmarks.close();
|
|
|
|
this.tags.clear();
|
|
|
|
this.dates.close();
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
// -----------------------------------------------------------
|
2010-04-13 03:16:09 +02:00
|
|
|
// bookmarksDB's functions for bookmarksTable / bookmarkCache
|
|
|
|
// -----------------------------------------------------------
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public Bookmark createBookmark(final String url, final String user){
|
2012-07-10 22:59:03 +02:00
|
|
|
if (url == null || url.isEmpty()) return null;
|
2011-11-22 01:03:20 +01:00
|
|
|
Bookmark bk;
|
|
|
|
try {
|
|
|
|
bk = new Bookmark(url);
|
|
|
|
bk.setOwner(user);
|
|
|
|
return (bk.getUrlHash() == null || bk.toMap() == null) ? null : bk;
|
|
|
|
} catch (final MalformedURLException e) {
|
|
|
|
return null;
|
|
|
|
}
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
// returning the number of bookmarks
|
|
|
|
public int bookmarksSize(){
|
2011-11-22 01:03:20 +01:00
|
|
|
return this.bookmarks.size();
|
2006-03-23 20:04:05 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
// adding a bookmark to the bookmarksDB
|
2008-08-02 14:12:04 +02:00
|
|
|
public void saveBookmark(final Bookmark bookmark){
|
2008-01-22 20:08:32 +01:00
|
|
|
try {
|
2011-11-22 01:03:20 +01:00
|
|
|
this.bookmarks.insert(ASCII.getBytes(bookmark.getUrlHash()), bookmark.entry);
|
2009-12-10 00:27:26 +01:00
|
|
|
} catch (final Exception e) {
|
2013-07-09 14:28:25 +02:00
|
|
|
ConcurrentLog.logException(e);
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-02-09 06:55:57 +01:00
|
|
|
public Bookmark getBookmark(final String urlHash) throws IOException {
|
2005-12-26 15:21:01 +01:00
|
|
|
try {
|
2011-11-22 01:03:20 +01:00
|
|
|
final Map<String, String> map = this.bookmarks.get(ASCII.getBytes(urlHash));
|
2013-02-09 06:55:57 +01:00
|
|
|
if (map == null) throw new IOException("cannot get bookmark for url hash " + urlHash);
|
|
|
|
return new Bookmark(map);
|
2013-07-17 18:31:30 +02:00
|
|
|
} catch (final Throwable e) {
|
2013-02-09 06:55:57 +01:00
|
|
|
throw new IOException(e.getMessage());
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public boolean removeBookmark(final String urlHash){
|
2013-02-09 06:55:57 +01:00
|
|
|
Bookmark bookmark;
|
|
|
|
try {
|
|
|
|
bookmark = getBookmark(urlHash);
|
2013-07-17 18:31:30 +02:00
|
|
|
} catch (final IOException e1) {
|
2013-02-09 06:55:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-13 03:16:09 +02:00
|
|
|
final Set<String> tagSet = bookmark.getTags();
|
2010-11-21 02:29:32 +01:00
|
|
|
BookmarksDB.Tag tag=null;
|
2010-04-13 03:16:09 +02:00
|
|
|
final Iterator<String> it=tagSet.iterator();
|
2008-01-22 20:08:32 +01:00
|
|
|
while(it.hasNext()){
|
2010-02-01 23:18:56 +01:00
|
|
|
tag=getTag(BookmarkHelper.tagHash(it.next()));
|
2008-01-22 20:08:32 +01:00
|
|
|
if(tag!=null){
|
|
|
|
tag.delete(urlHash);
|
2010-02-02 00:04:52 +01:00
|
|
|
putTag(tag);
|
2008-01-22 20:08:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Bookmark b;
|
|
|
|
try {
|
|
|
|
b = getBookmark(urlHash);
|
2011-11-22 01:03:20 +01:00
|
|
|
this.bookmarks.delete(ASCII.getBytes(urlHash));
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final IOException e) {
|
2008-01-22 20:08:32 +01:00
|
|
|
b = null;
|
|
|
|
}
|
|
|
|
return b != null;
|
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2010-02-02 00:04:52 +01:00
|
|
|
public Iterator<String> getBookmarksIterator(final boolean priv) {
|
2008-08-02 14:12:04 +02:00
|
|
|
final TreeSet<String> set=new TreeSet<String>(new bookmarkComparator(true));
|
2010-02-02 00:04:52 +01:00
|
|
|
Iterator<Bookmark> it;
|
|
|
|
try {
|
|
|
|
it = new bookmarkIterator(true);
|
2011-11-22 01:03:20 +01:00
|
|
|
} catch (final IOException e) {
|
2013-07-09 14:28:25 +02:00
|
|
|
ConcurrentLog.logException(e);
|
2010-02-02 00:04:52 +01:00
|
|
|
return set.iterator();
|
|
|
|
}
|
2005-12-26 22:03:42 +01:00
|
|
|
Bookmark bm;
|
|
|
|
while(it.hasNext()){
|
2008-06-06 18:01:27 +02:00
|
|
|
bm=it.next();
|
2010-04-14 01:33:50 +02:00
|
|
|
// if (bm == null) continue;
|
2006-02-08 18:13:27 +01:00
|
|
|
if(priv || bm.getPublic()){
|
|
|
|
set.add(bm.getUrlHash());
|
|
|
|
}
|
2005-12-26 22:03:42 +01:00
|
|
|
}
|
|
|
|
return set.iterator();
|
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public Iterator<String> getBookmarksIterator(final String tagName, final boolean priv){
|
|
|
|
final TreeSet<String> set=new TreeSet<String>(new bookmarkComparator(true));
|
2010-02-01 23:18:56 +01:00
|
|
|
final String tagHash=BookmarkHelper.tagHash(tagName);
|
2008-08-02 14:12:04 +02:00
|
|
|
final Tag tag=getTag(tagHash);
|
2014-01-20 18:31:46 +01:00
|
|
|
RowHandleSet hashes = tag == null ? new RowHandleSet(Word.commonHashLength, Word.commonHashOrder, 10) : tag.getUrlHashes();
|
2013-02-09 06:55:57 +01:00
|
|
|
if (priv) {
|
2013-11-07 21:30:17 +01:00
|
|
|
for (byte[] hash: hashes) set.add(ASCII.String(hash));
|
2013-02-09 06:55:57 +01:00
|
|
|
} else {
|
2013-11-07 21:30:17 +01:00
|
|
|
for (byte[] hash: hashes) {
|
2013-02-09 06:55:57 +01:00
|
|
|
try {
|
2013-11-07 21:30:17 +01:00
|
|
|
Bookmark bm = getBookmark(ASCII.String(hash));
|
2013-02-09 06:55:57 +01:00
|
|
|
if (bm != null && bm.getPublic()) {
|
|
|
|
set.add(bm.getUrlHash());
|
|
|
|
}
|
2013-07-17 18:31:30 +02:00
|
|
|
} catch (final IOException e) {
|
2010-04-14 01:33:50 +02:00
|
|
|
}
|
|
|
|
}
|
2006-02-08 18:13:27 +01:00
|
|
|
}
|
|
|
|
return set.iterator();
|
2005-12-26 22:03:42 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
// -------------------------------------------------
|
|
|
|
// bookmarksDB's functions for tagsTable / tagCache
|
|
|
|
// -------------------------------------------------
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
// returning the number of tags
|
2008-06-06 19:13:31 +02:00
|
|
|
public int tagsSize(){
|
2010-02-01 23:44:59 +01:00
|
|
|
return this.tags.size();
|
2008-01-22 20:08:32 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2013-11-07 21:30:17 +01:00
|
|
|
private Tag getTag(final String hash){
|
2010-02-02 00:04:52 +01:00
|
|
|
return this.tags.get(hash); //null if it does not exists
|
2010-02-01 23:44:59 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
/**
|
|
|
|
* store a Tag in tagsTable or remove an empty tag
|
|
|
|
* @param tag an object of type Tag to be stored/removed
|
|
|
|
*/
|
2013-11-07 21:30:17 +01:00
|
|
|
private void putTag(final Tag tag){
|
2008-12-16 14:36:13 +01:00
|
|
|
if (tag == null) return;
|
2012-07-12 11:12:21 +02:00
|
|
|
if (tag.isEmpty()) {
|
2010-02-01 23:44:59 +01:00
|
|
|
this.tags.remove(tag.getTagHash());
|
2012-07-12 11:12:21 +02:00
|
|
|
} else {
|
|
|
|
this.tags.put(tag.getTagHash(), tag);
|
2009-12-10 00:27:26 +01:00
|
|
|
}
|
2010-02-01 23:44:59 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private void removeTag(final String hash) {
|
2011-11-22 01:03:20 +01:00
|
|
|
this.tags.remove(hash);
|
2008-01-22 20:08:32 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public Iterator<Tag> getTagIterator(final boolean priv) {
|
2010-02-02 00:04:52 +01:00
|
|
|
return getTagIterator(priv, 1);
|
2008-01-22 20:08:32 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2010-02-02 00:04:52 +01:00
|
|
|
private Iterator<Tag> getTagIterator(final boolean priv, final int c) {
|
2010-04-13 03:16:09 +02:00
|
|
|
final Set<Tag> set = new TreeSet<Tag>((c == SORT_SIZE) ? tagSizeComparator : tagComparator);
|
2010-02-02 00:04:52 +01:00
|
|
|
final Iterator<Tag> it = this.tags.values().iterator();
|
2006-02-13 15:42:19 +01:00
|
|
|
Tag tag;
|
2010-02-02 00:04:52 +01:00
|
|
|
while (it.hasNext()) {
|
2010-04-14 01:33:50 +02:00
|
|
|
tag=it.next();
|
|
|
|
if (tag == null) continue;
|
2010-02-02 00:04:52 +01:00
|
|
|
if (priv ||tag.hasPublicItems()) {
|
2010-04-14 01:33:50 +02:00
|
|
|
set.add(tag);
|
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
}
|
2008-01-22 20:08:32 +01:00
|
|
|
return set.iterator();
|
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public Iterator<Tag> getTagIterator(final boolean priv, final int comp, final int max){
|
2011-11-22 01:03:20 +01:00
|
|
|
if (max==SHOW_ALL)
|
|
|
|
return getTagIterator(priv, comp);
|
2008-08-02 14:12:04 +02:00
|
|
|
final Iterator<Tag> it = getTagIterator(priv, SORT_SIZE);
|
2009-11-18 17:03:28 +01:00
|
|
|
final TreeSet<Tag> set=new TreeSet<Tag>((comp == SORT_SIZE) ? tagSizeComparator : tagComparator);
|
2008-01-22 20:08:32 +01:00
|
|
|
int count = 0;
|
|
|
|
while (it.hasNext() && count<=max) {
|
2010-04-14 01:33:50 +02:00
|
|
|
set.add(it.next());
|
|
|
|
count++;
|
2011-11-22 01:03:20 +01:00
|
|
|
}
|
2006-02-13 15:42:19 +01:00
|
|
|
return set.iterator();
|
2006-02-09 21:08:45 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private Iterator<Tag> getTagIterator(final String tagName, final boolean priv, final int comp) {
|
2009-11-18 17:03:28 +01:00
|
|
|
final TreeSet<Tag> set=new TreeSet<Tag>((comp == SORT_SIZE) ? tagSizeComparator : tagComparator);
|
2008-01-29 11:12:48 +01:00
|
|
|
Iterator<String> it=null;
|
2008-08-02 14:12:04 +02:00
|
|
|
final Iterator<String> bit=getBookmarksIterator(tagName, priv);
|
2008-01-09 16:58:47 +01:00
|
|
|
Bookmark bm;
|
|
|
|
Tag tag;
|
2010-04-13 03:16:09 +02:00
|
|
|
Set<String> tagSet;
|
2013-02-09 06:55:57 +01:00
|
|
|
while (bit.hasNext()) {
|
|
|
|
try {
|
|
|
|
bm = getBookmark(bit.next());
|
|
|
|
if (bm == null) continue;
|
|
|
|
tagSet = bm.getTags();
|
|
|
|
it = tagSet.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
tag=getTag(BookmarkHelper.tagHash(it.next()) );
|
|
|
|
if((priv ||tag.hasPublicItems()) && tag != null){
|
|
|
|
set.add(tag);
|
|
|
|
}
|
2010-04-14 01:33:50 +02:00
|
|
|
}
|
2013-07-17 18:31:30 +02:00
|
|
|
} catch (final IOException e) {
|
2010-04-14 01:33:50 +02:00
|
|
|
}
|
2008-01-09 16:58:47 +01:00
|
|
|
}
|
|
|
|
return set.iterator();
|
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2010-02-02 00:04:52 +01:00
|
|
|
public Iterator<Tag> getTagIterator(final String tagName, final boolean priv, final int comp, final int max) {
|
|
|
|
if (max==SHOW_ALL) return getTagIterator(priv, comp);
|
2010-04-14 01:33:50 +02:00
|
|
|
final Iterator<Tag> it = getTagIterator(tagName, priv, SORT_SIZE);
|
|
|
|
final TreeSet<Tag> set=new TreeSet<Tag>((comp == SORT_SIZE) ? tagSizeComparator : tagComparator);
|
|
|
|
int count = 0;
|
|
|
|
while (it.hasNext() && count<=max) {
|
|
|
|
set.add(it.next());
|
|
|
|
count++;
|
|
|
|
}
|
2008-01-22 20:08:32 +01:00
|
|
|
return set.iterator();
|
2011-11-22 01:03:20 +01:00
|
|
|
}
|
|
|
|
|
2010-02-01 23:44:59 +01:00
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
// -------------------------------------
|
|
|
|
// bookmarksDB's experimental functions
|
|
|
|
// -------------------------------------
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public boolean renameTag(final String oldName, final String newName){
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2010-02-01 23:18:56 +01:00
|
|
|
final Tag oldTag=getTag(BookmarkHelper.tagHash(oldName));
|
2009-01-28 01:15:43 +01:00
|
|
|
if (oldTag != null) {
|
2013-11-07 21:30:17 +01:00
|
|
|
final RowHandleSet urlHashes = oldTag.getUrlHashes(); // preserve urlHashes of oldTag
|
2010-04-13 03:16:09 +02:00
|
|
|
removeTag(BookmarkHelper.tagHash(oldName)); // remove oldHash from TagsDB
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2009-01-28 01:15:43 +01:00
|
|
|
Bookmark bookmark;
|
2010-04-13 03:16:09 +02:00
|
|
|
Set<String> tagSet = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
|
2013-11-07 21:30:17 +01:00
|
|
|
for (final byte[] urlHash : urlHashes) { // looping through all bookmarks which were tagged with oldName
|
2013-02-09 06:55:57 +01:00
|
|
|
try {
|
2013-11-07 21:30:17 +01:00
|
|
|
bookmark = getBookmark(ASCII.String(urlHash));
|
2013-02-09 06:55:57 +01:00
|
|
|
tagSet = bookmark.getTags();
|
|
|
|
tagSet.remove(oldName);
|
|
|
|
bookmark.setTags(tagSet, true); // might not be needed, but doesn't hurt
|
|
|
|
if(!"".equals(newName)) bookmark.addTag(newName);
|
|
|
|
saveBookmark(bookmark);
|
2013-07-17 18:31:30 +02:00
|
|
|
} catch (final IOException e) {
|
2013-02-09 06:55:57 +01:00
|
|
|
}
|
2008-01-22 20:08:32 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
|
|
|
public void addTag(final String selectTag, final String newTag) {
|
|
|
|
|
2009-01-28 01:15:43 +01:00
|
|
|
Bookmark bookmark;
|
2013-11-07 21:30:17 +01:00
|
|
|
for (final byte[] urlHash : getTag(BookmarkHelper.tagHash(selectTag)).getUrlHashes()) { // looping through all bookmarks which were tagged with selectTag
|
2013-02-09 06:55:57 +01:00
|
|
|
try {
|
2013-11-07 21:30:17 +01:00
|
|
|
bookmark = getBookmark(ASCII.String(urlHash));
|
2013-02-09 06:55:57 +01:00
|
|
|
bookmark.addTag(newTag);
|
|
|
|
saveBookmark(bookmark);
|
2013-07-17 18:31:30 +02:00
|
|
|
} catch (final IOException e) {
|
2013-02-09 06:55:57 +01:00
|
|
|
}
|
2009-01-28 01:15:43 +01:00
|
|
|
}
|
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
// --------------------------------------
|
|
|
|
// bookmarksDB's Subclasses
|
|
|
|
// --------------------------------------
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2005-12-26 15:21:01 +01:00
|
|
|
/**
|
2008-01-22 20:08:32 +01:00
|
|
|
* Subclass of bookmarksDB, which provides the Tag object-type
|
2005-12-26 15:21:01 +01:00
|
|
|
*/
|
2010-02-01 23:44:59 +01:00
|
|
|
public class Tag {
|
2008-08-02 14:12:04 +02:00
|
|
|
private final String tagHash;
|
2013-11-07 21:30:17 +01:00
|
|
|
private final String tagName;
|
|
|
|
private RowHandleSet urlHashes;
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private Tag(final String name) {
|
2011-11-22 01:03:20 +01:00
|
|
|
this.tagHash = BookmarkHelper.tagHash(name);
|
2013-11-07 21:30:17 +01:00
|
|
|
this.tagName = name;
|
2014-01-20 18:31:46 +01:00
|
|
|
this.urlHashes = new RowHandleSet(Word.commonHashLength, Word.commonHashOrder, 10);
|
2006-03-23 18:56:32 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2006-02-13 22:27:50 +01:00
|
|
|
/**
|
|
|
|
* get the lowercase Tagname
|
|
|
|
*/
|
2005-12-26 15:21:01 +01:00
|
|
|
public String getTagName(){
|
2006-02-13 22:27:50 +01:00
|
|
|
return getFriendlyName().toLowerCase();
|
2006-02-13 09:38:32 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private String getTagHash(){
|
2011-11-22 01:03:20 +01:00
|
|
|
return this.tagHash;
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2006-02-13 22:27:50 +01:00
|
|
|
/**
|
2007-02-26 13:56:50 +01:00
|
|
|
* @return the tag name, with all uppercase chars
|
2006-02-13 22:27:50 +01:00
|
|
|
*/
|
2006-01-13 16:10:24 +01:00
|
|
|
public String getFriendlyName(){
|
2013-11-07 21:30:17 +01:00
|
|
|
return this.tagName;
|
2006-01-13 16:10:24 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private RowHandleSet getUrlHashes(){
|
2011-11-22 01:03:20 +01:00
|
|
|
return this.urlHashes;
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private boolean hasPublicItems(){
|
2011-11-22 01:03:20 +01:00
|
|
|
return getBookmarksIterator(getTagName(), false).hasNext();
|
2006-02-09 21:08:45 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private void addUrl(final String urlHash){
|
|
|
|
try {
|
|
|
|
this.urlHashes.put(ASCII.getBytes(urlHash));
|
|
|
|
} catch (SpaceExceededException e) {
|
|
|
|
}
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private void delete(final String urlHash){
|
|
|
|
this.urlHashes.remove(ASCII.getBytes(urlHash));
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2005-12-26 22:03:42 +01:00
|
|
|
public int size(){
|
2011-11-22 01:03:20 +01:00
|
|
|
return this.urlHashes.size();
|
2005-12-26 22:03:42 +01:00
|
|
|
}
|
2012-07-12 11:12:21 +02:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private boolean isEmpty() {
|
2012-07-12 11:12:21 +02:00
|
|
|
return this.urlHashes.isEmpty();
|
|
|
|
}
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2005-12-26 15:21:01 +01:00
|
|
|
/**
|
2008-01-22 20:08:32 +01:00
|
|
|
* Subclass of bookmarksDB, which provides the Bookmark object-type
|
2005-12-26 15:21:01 +01:00
|
|
|
*/
|
2008-07-08 01:52:53 +02:00
|
|
|
public class Bookmark {
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private static final String BOOKMARK_URL = "bookmarkUrl";
|
2010-04-13 03:16:09 +02:00
|
|
|
public static final String BOOKMARK_TITLE = "bookmarkTitle";
|
|
|
|
public static final String BOOKMARK_DESCRIPTION = "bookmarkDesc";
|
2013-11-07 21:30:17 +01:00
|
|
|
private static final String BOOKMARK_TAGS = "bookmarkTags";
|
|
|
|
private static final String BOOKMARK_PUBLIC = "bookmarkPublic";
|
|
|
|
private static final String BOOKMARK_TIMESTAMP = "bookmarkTimestamp";
|
|
|
|
private static final String BOOKMARK_OWNER = "bookmarkOwner";
|
|
|
|
private static final String BOOKMARK_IS_FEED = "bookmarkIsFeed";
|
2011-11-22 01:03:20 +01:00
|
|
|
private final String urlHash;
|
2010-02-01 23:44:59 +01:00
|
|
|
private Set<String> tagNames;
|
2006-03-24 16:08:27 +01:00
|
|
|
private long timestamp;
|
2011-11-22 01:03:20 +01:00
|
|
|
private final Map<String, String> entry;
|
|
|
|
|
2013-09-15 00:30:23 +02:00
|
|
|
public Bookmark(final DigestURL url) {
|
2011-11-22 01:03:20 +01:00
|
|
|
this.entry = new HashMap<String, String>();
|
|
|
|
this.urlHash = ASCII.String(url.hash());
|
2012-10-10 11:46:22 +02:00
|
|
|
this.entry.put(BOOKMARK_URL, url.toNormalform(false));
|
2011-11-22 01:03:20 +01:00
|
|
|
this.tagNames = new HashSet<String>();
|
|
|
|
this.timestamp = System.currentTimeMillis();
|
2013-02-09 06:55:57 +01:00
|
|
|
Bookmark oldBm;
|
|
|
|
try {
|
|
|
|
oldBm = getBookmark(this.urlHash);
|
|
|
|
if(oldBm!=null && oldBm.entry.containsKey(BOOKMARK_TIMESTAMP)){
|
|
|
|
this.entry.put(BOOKMARK_TIMESTAMP, oldBm.entry.get(BOOKMARK_TIMESTAMP)); //preserve timestamp on edit
|
|
|
|
}else{
|
|
|
|
this.entry.put(BOOKMARK_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
|
|
|
|
}
|
2013-07-17 18:31:30 +02:00
|
|
|
} catch (final IOException e) {
|
2011-11-22 01:03:20 +01:00
|
|
|
}
|
|
|
|
final BookmarkDate.Entry bmDate=BookmarksDB.this.dates.getDate(this.entry.get(BOOKMARK_TIMESTAMP));
|
2006-03-24 16:08:27 +01:00
|
|
|
bmDate.add(this.urlHash);
|
|
|
|
bmDate.setDatesTable();
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2006-03-24 16:08:27 +01:00
|
|
|
removeBookmark(this.urlHash); //prevent empty tags
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
|
|
|
public Bookmark(final String url) throws MalformedURLException {
|
2013-09-15 00:30:23 +02:00
|
|
|
this(new DigestURL((url.indexOf("://") < 0) ? "http://" + url : url));
|
2011-11-22 01:03:20 +01:00
|
|
|
}
|
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private Bookmark(final Map<String, String> map) throws MalformedURLException {
|
|
|
|
this.entry = map;
|
|
|
|
this.urlHash = ASCII.String((new DigestURL(map.get(BOOKMARK_URL))).hash());
|
|
|
|
this.tagNames = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
|
|
|
|
if (map.containsKey(BOOKMARK_TAGS)) this.tagNames.addAll(ListManager.string2set(map.get(BOOKMARK_TAGS)));
|
|
|
|
loadTimestamp();
|
2007-03-01 14:54:14 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private Map<String, String> toMap() {
|
2011-11-22 01:03:20 +01:00
|
|
|
this.entry.put(BOOKMARK_TAGS, ListManager.collection2string(this.tagNames));
|
|
|
|
this.entry.put(BOOKMARK_TIMESTAMP, String.valueOf(this.timestamp));
|
|
|
|
return this.entry;
|
2006-03-24 15:34:27 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-29 23:13:30 +01:00
|
|
|
private void loadTimestamp() {
|
2011-11-22 01:03:20 +01:00
|
|
|
if(this.entry.containsKey(BOOKMARK_TIMESTAMP))
|
|
|
|
this.timestamp=Long.parseLong(this.entry.get(BOOKMARK_TIMESTAMP));
|
2006-03-24 16:08:27 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-29 23:13:30 +01:00
|
|
|
public String getUrlHash() {
|
2011-11-22 01:03:20 +01:00
|
|
|
return this.urlHash;
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-29 23:13:30 +01:00
|
|
|
public String getUrl() {
|
2011-11-22 01:03:20 +01:00
|
|
|
return this.entry.get(BOOKMARK_URL);
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-29 23:13:30 +01:00
|
|
|
public Set<String> getTags() {
|
2011-11-22 01:03:20 +01:00
|
|
|
return this.tagNames;
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
|
|
|
public String getTagsString() {
|
2010-11-21 02:29:32 +01:00
|
|
|
final String s[] = ListManager.collection2string(getTags()).split(",");
|
2010-04-13 03:16:09 +02:00
|
|
|
final StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
for (final String element : s){
|
|
|
|
if(!element.startsWith("/")){
|
|
|
|
stringBuilder.append(element);
|
|
|
|
stringBuilder.append(",");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stringBuilder.toString();
|
2008-01-24 21:11:57 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-24 21:11:57 +01:00
|
|
|
public String getFoldersString(){
|
2010-11-21 02:29:32 +01:00
|
|
|
final String s[] = ListManager.collection2string(getTags()).split(",");
|
2010-04-13 03:16:09 +02:00
|
|
|
final StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
for (final String element : s){
|
2010-04-20 19:55:59 +02:00
|
|
|
if(element.startsWith("/")){
|
2010-04-13 03:16:09 +02:00
|
|
|
stringBuilder.append(element);
|
|
|
|
stringBuilder.append(",");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stringBuilder.toString();
|
2008-01-29 23:13:30 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2005-12-27 21:47:16 +01:00
|
|
|
public String getDescription(){
|
2011-11-22 01:03:20 +01:00
|
|
|
if(this.entry.containsKey(BOOKMARK_DESCRIPTION)){
|
|
|
|
return this.entry.get(BOOKMARK_DESCRIPTION);
|
2005-12-27 21:47:16 +01:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2005-12-26 15:21:01 +01:00
|
|
|
public String getTitle(){
|
2011-11-22 01:03:20 +01:00
|
|
|
if(this.entry.containsKey(BOOKMARK_TITLE)){
|
|
|
|
return this.entry.get(BOOKMARK_TITLE);
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
return this.entry.get(BOOKMARK_URL);
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2007-01-19 20:42:50 +01:00
|
|
|
public String getOwner(){
|
2011-11-22 01:03:20 +01:00
|
|
|
if(this.entry.containsKey(BOOKMARK_OWNER)){
|
|
|
|
return this.entry.get(BOOKMARK_OWNER);
|
2007-01-19 20:42:50 +01:00
|
|
|
}
|
2007-06-26 16:37:10 +02:00
|
|
|
return null; //null means admin
|
2007-01-19 20:42:50 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public void setOwner(final String owner){
|
2011-11-22 01:03:20 +01:00
|
|
|
this.entry.put(BOOKMARK_OWNER, owner);
|
2007-01-19 20:42:50 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2005-12-26 23:10:12 +01:00
|
|
|
public boolean getPublic(){
|
2011-11-22 01:03:20 +01:00
|
|
|
if(this.entry.containsKey(BOOKMARK_PUBLIC)){
|
|
|
|
return "public".equals(this.entry.get(BOOKMARK_PUBLIC));
|
2005-12-26 23:10:12 +01:00
|
|
|
}
|
2007-06-26 16:37:10 +02:00
|
|
|
return false;
|
2005-12-26 23:10:12 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2007-06-09 01:27:42 +02:00
|
|
|
public boolean getFeed(){
|
2011-11-22 01:03:20 +01:00
|
|
|
if(this.entry.containsKey(BOOKMARK_IS_FEED)){
|
|
|
|
return "true".equals(this.entry.get(BOOKMARK_IS_FEED));
|
2007-06-09 01:27:42 +02:00
|
|
|
}
|
2007-06-26 16:37:10 +02:00
|
|
|
return false;
|
2007-06-09 01:27:42 +02:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public void setPublic(final boolean isPublic){
|
2010-04-13 03:16:09 +02:00
|
|
|
if(isPublic){
|
2011-11-22 01:03:20 +01:00
|
|
|
this.entry.put(BOOKMARK_PUBLIC, "public");
|
2010-04-13 03:16:09 +02:00
|
|
|
} else {
|
2011-11-22 01:03:20 +01:00
|
|
|
this.entry.put(BOOKMARK_PUBLIC, "private");
|
2010-04-13 03:16:09 +02:00
|
|
|
}
|
2006-02-12 15:51:04 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public void setFeed(final boolean isFeed){
|
2010-04-13 03:16:09 +02:00
|
|
|
if(isFeed){
|
2011-11-22 01:03:20 +01:00
|
|
|
this.entry.put(BOOKMARK_IS_FEED, "true");
|
2010-04-13 03:16:09 +02:00
|
|
|
}else{
|
2011-11-22 01:03:20 +01:00
|
|
|
this.entry.put(BOOKMARK_IS_FEED, "false");
|
2010-04-13 03:16:09 +02:00
|
|
|
}
|
2007-06-09 01:27:42 +02:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public void setProperty(final String name, final String value){
|
2011-11-22 01:03:20 +01:00
|
|
|
this.entry.put(name, value);
|
2005-12-26 15:21:01 +01:00
|
|
|
//setBookmarksTable();
|
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2009-01-28 01:15:43 +01:00
|
|
|
public void addTag(final String tagName){
|
2011-11-22 01:03:20 +01:00
|
|
|
this.tagNames.add(tagName);
|
|
|
|
setTags(this.tagNames);
|
2009-01-28 07:57:11 +01:00
|
|
|
saveBookmark(this);
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2006-02-18 18:49:24 +01:00
|
|
|
/**
|
|
|
|
* set the Tags of the bookmark, and write them into the tags table.
|
2007-09-10 08:20:27 +02:00
|
|
|
* @param tags2 a ArrayList with the tags
|
2006-02-18 18:49:24 +01:00
|
|
|
*/
|
2008-08-02 14:12:04 +02:00
|
|
|
public void setTags(final Set<String> tags2){
|
2007-09-10 08:20:27 +02:00
|
|
|
setTags(tags2, true);
|
2006-02-15 14:16:10 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2006-02-15 14:16:10 +01:00
|
|
|
/**
|
|
|
|
* set the Tags of the bookmark
|
2010-02-01 23:44:59 +01:00
|
|
|
* @param tagNames ArrayList with the tagnames
|
2006-02-15 14:16:10 +01:00
|
|
|
* @param local sets, whether the updated tags should be stored to tagsDB
|
|
|
|
*/
|
2008-08-02 14:12:04 +02:00
|
|
|
public void setTags(final Set<String> tags2, final boolean local){
|
2011-11-22 01:03:20 +01:00
|
|
|
this.tagNames = tags2; // TODO: check if this is safe
|
2008-01-22 20:08:32 +01:00
|
|
|
// tags.addAll(tags2); // in order for renameTag() to work I had to change this form 'add' to 'set'
|
2011-11-22 01:03:20 +01:00
|
|
|
for (final String tagName : this.tagNames) {
|
2010-04-13 03:16:09 +02:00
|
|
|
Tag tag = getTag(BookmarkHelper.tagHash(tagName));
|
|
|
|
if (tag == null) {
|
|
|
|
tag = new Tag(tagName);
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2006-03-24 16:08:27 +01:00
|
|
|
tag.addUrl(getUrlHash());
|
2010-04-13 03:16:09 +02:00
|
|
|
if (local) {
|
2010-02-02 00:04:52 +01:00
|
|
|
putTag(tag);
|
2006-02-15 14:16:10 +01:00
|
|
|
}
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2007-01-30 12:52:15 +01:00
|
|
|
toMap();
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2006-03-23 22:33:09 +01:00
|
|
|
|
2005-12-26 22:03:42 +01:00
|
|
|
public long getTimeStamp(){
|
2011-11-22 01:03:20 +01:00
|
|
|
return this.timestamp;
|
2005-12-26 22:03:42 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public void setTimeStamp(final long ts){
|
2010-04-13 03:16:09 +02:00
|
|
|
this.timestamp = ts;
|
2006-02-12 15:03:56 +01:00
|
|
|
}
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
/**
|
|
|
|
* Subclass of bookmarksDB, which provides the bookmarkIterator object-type
|
|
|
|
*/
|
2013-11-07 21:30:17 +01:00
|
|
|
private class bookmarkIterator implements Iterator<Bookmark> {
|
2010-02-02 00:04:52 +01:00
|
|
|
|
2008-07-10 02:47:37 +02:00
|
|
|
Iterator<byte[]> bookmarkIter;
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private bookmarkIterator(final boolean up) throws IOException {
|
2007-01-29 20:10:55 +01:00
|
|
|
//flushBookmarkCache(); //XXX: this will cost performance
|
2010-11-21 02:29:32 +01:00
|
|
|
this.bookmarkIter = BookmarksDB.this.bookmarks.keys(up, false);
|
2008-08-06 21:43:12 +02:00
|
|
|
//this.nextEntry = null;
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2012-06-05 15:04:23 +02:00
|
|
|
@Override
|
2005-12-26 15:21:01 +01:00
|
|
|
public boolean hasNext() {
|
2010-04-14 01:33:50 +02:00
|
|
|
return this.bookmarkIter.hasNext();
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2012-06-05 15:04:23 +02:00
|
|
|
@Override
|
2008-01-06 20:23:38 +01:00
|
|
|
public Bookmark next() {
|
2013-02-09 06:55:57 +01:00
|
|
|
try {
|
|
|
|
return getBookmark(UTF8.String(this.bookmarkIter.next()));
|
2013-07-17 18:31:30 +02:00
|
|
|
} catch (final IOException e) {
|
2013-02-09 06:55:57 +01:00
|
|
|
this.bookmarkIter.remove();
|
|
|
|
return null;
|
|
|
|
}
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2012-06-05 15:04:23 +02:00
|
|
|
@Override
|
2005-12-26 15:21:01 +01:00
|
|
|
public void remove() {
|
2010-02-02 00:04:52 +01:00
|
|
|
throw new UnsupportedOperationException();
|
2005-12-26 15:21:01 +01:00
|
|
|
}
|
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2005-12-26 22:03:42 +01:00
|
|
|
/**
|
2008-01-22 20:08:32 +01:00
|
|
|
* Comparator to sort objects of type Bookmark according to their timestamps
|
2005-12-26 22:03:42 +01:00
|
|
|
*/
|
2013-11-07 21:30:17 +01:00
|
|
|
private class bookmarkComparator implements Comparator<String> {
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
private final boolean newestFirst;
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2006-02-17 19:14:11 +01:00
|
|
|
/**
|
|
|
|
* @param newestFirst newest first, or oldest first?
|
|
|
|
*/
|
2013-11-07 21:30:17 +01:00
|
|
|
private bookmarkComparator(final boolean newestFirst){
|
2010-04-13 03:16:09 +02:00
|
|
|
this.newestFirst = newestFirst;
|
2006-02-17 19:14:11 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2012-06-05 15:04:23 +02:00
|
|
|
@Override
|
2008-08-02 14:12:04 +02:00
|
|
|
public int compare(final String obj1, final String obj2) {
|
2013-02-09 06:55:57 +01:00
|
|
|
try {
|
|
|
|
Bookmark bm1 = getBookmark(obj1);
|
|
|
|
Bookmark bm2 = getBookmark(obj2);
|
|
|
|
if (bm1 == null || bm2 == null) {
|
|
|
|
return 0; //XXX: i think this should not happen? maybe this needs further tracing of the bug
|
|
|
|
}
|
|
|
|
if (this.newestFirst){
|
|
|
|
if (bm2.getTimeStamp() - bm1.getTimeStamp() >0) return 1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (bm1.getTimeStamp() - bm2.getTimeStamp() > 0) return 1;
|
2013-07-17 18:31:30 +02:00
|
|
|
} catch (final IOException e) {
|
2006-02-17 19:14:11 +01:00
|
|
|
}
|
2007-06-26 16:37:10 +02:00
|
|
|
return -1;
|
2005-12-26 22:03:42 +01:00
|
|
|
}
|
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private static final TagComparator tagComparator = new TagComparator();
|
|
|
|
private static final TagSizeComparator tagSizeComparator = new TagSizeComparator();
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2006-02-13 15:42:19 +01:00
|
|
|
/**
|
2008-01-22 20:08:32 +01:00
|
|
|
* Comparator to sort objects of type Tag according to their names
|
2006-02-13 15:42:19 +01:00
|
|
|
*/
|
2013-11-07 21:30:17 +01:00
|
|
|
private static class TagComparator implements Comparator<Tag>, Serializable {
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-06 21:43:12 +02:00
|
|
|
/**
|
2010-04-13 03:16:09 +02:00
|
|
|
* generated serial
|
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = 3105057490088903930L;
|
2008-08-06 21:43:12 +02:00
|
|
|
|
2012-06-05 15:04:23 +02:00
|
|
|
@Override
|
2010-04-13 03:16:09 +02:00
|
|
|
public int compare(final Tag obj1, final Tag obj2){
|
|
|
|
return obj1.getTagName().compareTo(obj2.getTagName());
|
2006-02-13 15:42:19 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2006-02-13 15:42:19 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2013-11-07 21:30:17 +01:00
|
|
|
private static class TagSizeComparator implements Comparator<Tag>, Serializable {
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-08-06 21:43:12 +02:00
|
|
|
/**
|
2010-04-13 03:16:09 +02:00
|
|
|
* generated serial
|
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = 4149185397646373251L;
|
2008-08-06 21:43:12 +02:00
|
|
|
|
2012-06-05 15:04:23 +02:00
|
|
|
@Override
|
2010-04-13 03:16:09 +02:00
|
|
|
public int compare(final Tag obj1, final Tag obj2) {
|
|
|
|
if (obj1.size() < obj2.size()) {
|
|
|
|
return 1;
|
|
|
|
} else if (obj1.getTagName().equals(obj2.getTagName())) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-01-22 20:08:32 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2008-01-22 20:08:32 +01:00
|
|
|
}
|
2011-11-22 01:03:20 +01:00
|
|
|
|
2010-02-02 00:04:52 +01:00
|
|
|
public BookmarkDate.Entry getDate(final String date) {
|
2011-11-22 01:03:20 +01:00
|
|
|
return this.dates.getDate(date);
|
2010-02-01 23:18:56 +01:00
|
|
|
}
|
2012-05-14 07:41:55 +02:00
|
|
|
}
|