refactor getBookmark

to consistenly check existance by != null (w/o throwing exception on not found)
pull/8/head
reger 10 years ago
parent 1481a8ab56
commit 609c52e987

@ -200,11 +200,8 @@ public class Bookmarks {
prop.put("mode_public", "0");
prop.put("mode_feed", "0");
} else {
BookmarksDB.Bookmark bookmark = null;
try {
bookmark = sb.bookmarksDB.getBookmark(urlHash);
} catch (final IOException e1) {
}
BookmarksDB.Bookmark bookmark = sb.bookmarksDB.getBookmark(urlHash);
if (bookmark == null) {
// try to get the bookmark from the LURL database
final URIMetadataNode urlentry = sb.index.fulltext().getMetadata(ASCII.getBytes(urlHash));
@ -324,11 +321,7 @@ public class Bookmarks {
count = 0;
while(count < max_count && it.hasNext()) {
Bookmark bookmark = null;
try {
bookmark = sb.bookmarksDB.getBookmark(it.next());
} catch (final IOException e) {
}
Bookmark bookmark = bookmark = sb.bookmarksDB.getBookmark(it.next());
if (bookmark != null){
if (bookmark.getFeed() && isAdmin) {
@ -490,11 +483,7 @@ public class Bookmarks {
final Iterator<String> bit = sb.bookmarksDB.getBookmarksIterator(fn, isAdmin);
while (bit.hasNext()) {
Bookmark bookmark = null;
try {
bookmark = sb.bookmarksDB.getBookmark(bit.next());
} catch (final IOException e) {
}
Bookmark bookmark = sb.bookmarksDB.getBookmark(bit.next());
if (bookmark == null) break;
prop.put("display_folderlist_" + count + "_folder", "<li><a href=\"" + bookmark.getUrl() + "\" title=\"" + bookmark.getDescription() + "\">" + bookmark.getTitle() + "</a></li>");
count++;

@ -1,6 +1,5 @@
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
@ -154,10 +153,9 @@ public class get_bookmarks {
total += count;
}
count = 0;
BookmarksDB.Bookmark bookmark = null;
while (count < itemsPerPage && it.hasNext()) {
try {
bookmark = sb.bookmarksDB.getBookmark(it.next());
BookmarksDB.Bookmark bookmark = sb.bookmarksDB.getBookmark(it.next());
if (bookmark != null) {
prop.put("display_bookmarks_"+count+"_id",count);
prop.put("display_bookmarks_"+count+"_link",bookmark.getUrl());
@ -187,8 +185,6 @@ public class get_bookmarks {
count++;
}
} catch (final IOException e) {
}
}
// eliminate the trailing comma for Json output
@ -251,11 +247,10 @@ public class get_bookmarks {
}
private static int print_XBEL(final Iterator<String> bit, int count) {
BookmarksDB.Bookmark bookmark = null;
Date date;
while(bit.hasNext()){
try {
bookmark = sb.bookmarksDB.getBookmark(bit.next());
BookmarksDB.Bookmark bookmark = sb.bookmarksDB.getBookmark(bit.next());
if (bookmark != null) {
date = new Date(bookmark.getTimeStamp());
prop.put("display_xbel_"+count+"_elements", "<bookmark id=\"" + bookmark.getUrlHash()
+ "\" href=\"" + CharacterCoding.unicode2xml(bookmark.getUrl(), true)
@ -285,7 +280,6 @@ public class get_bookmarks {
count++;
prop.put("display_xbel_"+count+"_elements", "</bookmark>");
count++;
} catch (final IOException e) {
}
}
return count;

@ -1,6 +1,5 @@
import java.io.IOException;
import java.util.Iterator;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.cora.util.CommonPattern;
@ -72,10 +71,9 @@ public class get_folders {
// loop through bookmarkList
it = sb.bookmarksDB.getBookmarksIterator(root, isAdmin);
BookmarksDB.Bookmark bm;
while (it.hasNext()) {
try {
bm = sb.bookmarksDB.getBookmark(it.next());
BookmarksDB.Bookmark bm = sb.bookmarksDB.getBookmark(it.next());
if (bm != null) {
// TODO: get rid of bmtype
if (post.containsKey("bmtype")) {
if (post.get("bmtype").equals("title")) {
@ -93,7 +91,6 @@ public class get_folders {
prop.put("folders_"+count+"_hasChildren", "false");
prop.put("folders_"+count+"_comma", ",");
count++;
} catch (final IOException e) {
}
}

@ -1,5 +1,4 @@
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
@ -29,11 +28,10 @@ public class all {
final boolean extendedXML = (post != null && post.containsKey("extendedXML"));
int count=0;
BookmarksDB.Bookmark bookmark;
Date date;
while(it.hasNext()){
try {
bookmark=switchboard.bookmarksDB.getBookmark(it.next());
BookmarksDB.Bookmark bookmark=switchboard.bookmarksDB.getBookmark(it.next());
if (bookmark != null) {
prop.putXML("posts_"+count+"_url", bookmark.getUrl());
prop.putXML("posts_"+count+"_title", bookmark.getTitle());
prop.putXML("posts_"+count+"_description", bookmark.getDescription());
@ -48,7 +46,6 @@ public class all {
prop.put("posts_"+count+"_isExtended_private", Boolean.toString(!bookmark.getPublic()));
}
count++;
} catch (final IOException e) {
}
}
prop.put("posts", count);

@ -1,6 +1,5 @@
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
@ -8,7 +7,7 @@ import java.util.List;
import net.yacy.cora.date.ISO8601Formatter;
import net.yacy.cora.order.Digest;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.data.BookmarksDB;
import net.yacy.data.BookmarksDB.Bookmark;
import net.yacy.search.Switchboard;
import net.yacy.server.serverObjects;
import net.yacy.server.serverSwitch;
@ -45,10 +44,9 @@ public class get {
}
final List<String> bookmark_hashes = switchboard.bookmarksDB.getDate(Long.toString(parsedDate.getTime())).getBookmarkList();
BookmarksDB.Bookmark bookmark = null;
for (final String bookmark_hash : bookmark_hashes){
try {
bookmark=switchboard.bookmarksDB.getBookmark(bookmark_hash);
Bookmark bookmark = switchboard.bookmarksDB.getBookmark(bookmark_hash);
if (bookmark != null) {
if (ISO8601Formatter.FORMATTER.format(new Date(bookmark.getTimeStamp())).equals(date) &&
tag==null || bookmark.getTags().contains(tag) &&
isAdmin || bookmark.getPublic()){
@ -66,7 +64,6 @@ public class get {
}
count++;
}
} catch (final IOException e) {
}
}
prop.put("posts", count);

@ -1,6 +1,4 @@
import java.io.IOException;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.data.BookmarksDB.Bookmark;
import net.yacy.search.Switchboard;
@ -19,19 +17,18 @@ public class addTag_p {
prop.put("result", "0");//error
//rename tags
if(post != null && isAdmin) {
if (post.containsKey("selectTag") && post.containsKey("addTag")) {
if (post.containsKey("selectTag") && post.containsKey("addTag")) {
switchboard.bookmarksDB.addTag(post.get("selectTag"), post.get("addTag"));
prop.put("result", "1");//success
} else if (post.containsKey("urlhash") && post.containsKey("addTag")) {
Bookmark bm;
try {
bm = switchboard.bookmarksDB.getBookmark(post.get("urlhash"));
} else if (post.containsKey("urlhash") && post.containsKey("addTag")) {
Bookmark bm = switchboard.bookmarksDB.getBookmark(post.get("urlhash"));
if (bm != null) {
bm.addTag(post.get("addTag"));
prop.put("result", "1");//success
} catch (final IOException e) {
} else {
prop.put("result", "0");//success
}
}
}
}
// return rewrite properties
return prop;

@ -1,6 +1,5 @@
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
@ -104,11 +103,10 @@ public class xbel {
return count;
}
private static int print_XBEL(final Iterator<String> bit, int count) {
BookmarksDB.Bookmark bookmark;
Date date;
while(bit.hasNext()){
try {
bookmark=switchboard.bookmarksDB.getBookmark(bit.next());
BookmarksDB.Bookmark bookmark=switchboard.bookmarksDB.getBookmark(bit.next());
if (bookmark != null) {
date=new Date(bookmark.getTimeStamp());
prop.put("xbel_"+count+"_elements", "<bookmark id=\"" + bookmark.getUrlHash()
+ "\" href=\"" + CharacterCoding.unicode2xml(bookmark.getUrl(), true)
@ -138,9 +136,8 @@ public class xbel {
count++;
prop.put("xbel_"+count+"_elements", "</bookmark>");
count++;
} catch (final IOException e) {
}
}
}
return count;
}
}

@ -16,7 +16,7 @@ import net.yacy.cora.document.encoding.UTF8;
import net.yacy.cora.protocol.ClientIdentification;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.data.BookmarksDB;
import net.yacy.data.BookmarksDB.Bookmark;
import net.yacy.data.UserDB;
import net.yacy.data.WorkTables;
import net.yacy.data.ymark.MonitoredReader;
@ -173,10 +173,9 @@ public class import_ymark {
return prop;
}
final Iterator<String> bit=sb.bookmarksDB.getBookmarksIterator(isAdmin);
BookmarksDB.Bookmark bookmark;
while(bit.hasNext()){
try {
bookmark=sb.bookmarksDB.getBookmark(bit.next());
Bookmark bookmark=sb.bookmarksDB.getBookmark(bit.next());
if (bookmark != null) {
final YMarkEntry bmk_entry = new YMarkEntry(false);
bmk_entry.put(YMarkEntry.BOOKMARK.URL.key(), bookmark.getUrl());
try {
@ -197,7 +196,6 @@ public class import_ymark {
} catch (final IOException e) {
ConcurrentLog.logException(e);
}
} catch (final IOException e1) {
}
}
} else if(post.containsKey("importer") && post.get("importer").equals("dmoz")) {

@ -24,7 +24,6 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.Date;
@ -137,11 +136,8 @@ public class yacysearchitem {
final String urlhash = ASCII.String(result.hash());
if (authenticated) { // only needed if authorized
boolean bookmarkexists;
try { // check url exists in bookkmarks
bookmarkexists = sb.bookmarksDB.getBookmark(urlhash) != null;
} catch (IOException ex) {
bookmarkexists = false;
}
// check url exists in bookkmarks
bookmarkexists = sb.bookmarksDB.getBookmark(urlhash) != null;
prop.put("content_authorized_bookmark", !bookmarkexists);
// bookmark icon check for YMarks
//prop.put("content_authorized_bookmark", sb.tables.bookmarks.hasBookmark("admin", urlhash) ? "0" : "1");

@ -134,14 +134,9 @@ public class BookmarksDB {
*/
public Bookmark createorgetBookmark(final String url, final String user){
if (url == null || url.isEmpty()) return null;
Bookmark bk;
try {
try {
DigestURL durl = new DigestURL((url.indexOf("://") < 0) ? "http://" + url : url);
bk = this.getBookmark(ASCII.String(durl.hash()));
} catch (IOException ex) {
bk = null;
}
DigestURL durl = new DigestURL((url.indexOf("://") < 0) ? "http://" + url : url);
Bookmark bk = this.getBookmark(ASCII.String(durl.hash()));
if (bk == null) {
bk = new Bookmark(url);
}
@ -166,21 +161,28 @@ public class BookmarksDB {
}
}
public Bookmark getBookmark(final String urlHash) throws IOException {
/**
* Get bookmark for urlHash
* @param urlHash
* @return bookmark or null if not exist
*/
public Bookmark getBookmark(final String urlHash) {
try {
final Map<String, String> map = this.bookmarks.get(ASCII.getBytes(urlHash));
if (map == null) throw new IOException("cannot get bookmark for url hash " + urlHash);
return new Bookmark(map);
if (map == null) {
ConcurrentLog.info("BOOKMARKS", "cannot get bookmark for url hash " + urlHash);
return null;
} else {
return new Bookmark(map);
}
} catch (final Throwable e) {
throw new IOException(e.getMessage());
return null;
}
}
public boolean removeBookmark(final String urlHash){
Bookmark bookmark;
try {
bookmark = getBookmark(urlHash);
} catch (final IOException e1) {
Bookmark bookmark = getBookmark(urlHash);
if (bookmark == null) {
return false;
}
final Set<String> tagSet = bookmark.getTags();
@ -193,10 +195,9 @@ public class BookmarksDB {
putTag(tag);
}
}
Bookmark b;
Bookmark b = getBookmark(urlHash);
try {
b = getBookmark(urlHash);
this.bookmarks.delete(ASCII.getBytes(urlHash));
if (b != null) this.bookmarks.delete(ASCII.getBytes(urlHash));
} catch (final IOException e) {
b = null;
}
@ -241,12 +242,9 @@ public class BookmarksDB {
for (byte[] hash: hashes) set.add(ASCII.String(hash));
} else {
for (byte[] hash: hashes) {
try {
Bookmark bm = getBookmark(ASCII.String(hash));
if (bm != null && bm.getPublic()) {
set.add(bm.getUrlHash());
}
} catch (final IOException e) {
Bookmark bm = getBookmark(ASCII.String(hash));
if (bm != null && bm.getPublic()) {
set.add(bm.getUrlHash());
}
}
}
@ -322,24 +320,20 @@ public class BookmarksDB {
final TreeSet<Tag> set=new TreeSet<Tag>((comp == SORT_SIZE) ? tagSizeComparator : tagComparator);
Iterator<String> it=null;
final Iterator<String> bit=getBookmarksIterator(tagName, priv);
Bookmark bm;
Tag tag;
Set<String> tagSet;
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);
}
while (bit.hasNext()) {
Bookmark 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);
}
} catch (final IOException e) {
}
}
}
return set.iterator();
}
@ -367,18 +361,18 @@ public class BookmarksDB {
final RowHandleSet urlHashes = oldTag.getUrlHashes(); // preserve urlHashes of oldTag
removeTag(BookmarkHelper.tagHash(oldName)); // remove oldHash from TagsDB
Bookmark bookmark;
Set<String> tagSet = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (final byte[] urlHash : urlHashes) { // looping through all bookmarks which were tagged with oldName
try {
bookmark = getBookmark(ASCII.String(urlHash));
Bookmark bookmark = getBookmark(ASCII.String(urlHash));
if (bookmark != null) {
tagSet = bookmark.getTags();
tagSet.remove(oldName);
bookmark.setTags(tagSet, true); // might not be needed, but doesn't hurt
if(!"".equals(newName)) bookmark.addTag(newName);
if (!"".equals(newName)) {
bookmark.addTag(newName);
}
saveBookmark(bookmark);
} catch (final IOException e) {
}
}
}
return true;
}
@ -387,13 +381,11 @@ public class BookmarksDB {
public void addTag(final String selectTag, final String newTag) {
Bookmark bookmark;
for (final byte[] urlHash : getTag(BookmarkHelper.tagHash(selectTag)).getUrlHashes()) { // looping through all bookmarks which were tagged with selectTag
try {
bookmark = getBookmark(ASCII.String(urlHash));
for (final byte[] urlHash : getTag(BookmarkHelper.tagHash(selectTag)).getUrlHashes()) { // looping through all bookmarks which were tagged with selectTag
Bookmark bookmark = getBookmark(ASCII.String(urlHash));
if (bookmark != null) {
bookmark.addTag(newTag);
saveBookmark(bookmark);
} catch (final IOException e) {
}
}
}
@ -487,15 +479,12 @@ public class BookmarksDB {
this.entry.put(BOOKMARK_URL, url.toNormalform(false));
this.tagNames = new HashSet<String>();
this.timestamp = System.currentTimeMillis();
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()));
}
} catch (final IOException e) {
Bookmark 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()));
}
final BookmarkDate.Entry bmDate=BookmarksDB.this.dates.getDate(this.entry.get(BOOKMARK_TIMESTAMP));
bmDate.add(this.urlHash);
@ -706,12 +695,7 @@ public class BookmarksDB {
@Override
public Bookmark next() {
try {
return getBookmark(UTF8.String(this.bookmarkIter.next()));
} catch (final IOException e) {
this.bookmarkIter.remove();
return null;
}
return getBookmark(UTF8.String(this.bookmarkIter.next()));
}
@Override
@ -736,19 +720,16 @@ public class BookmarksDB {
@Override
public int compare(final String obj1, final String obj2) {
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;
} catch (final IOException e) {
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;
return -1;
}
}

Loading…
Cancel
Save