Bug 707952 - Increase bookmarks cache usefulness.

r=dietrich
This commit is contained in:
Marco Bonardo 2011-12-07 21:56:20 +01:00
parent 86e79b64e2
commit 31804d7c68
3 changed files with 79 additions and 43 deletions

View File

@ -523,6 +523,9 @@ nsNavBookmarks::InsertBookmarkInDB(PRInt64 aPlaceId,
const nsACString& aTitle,
PRTime aDateAdded,
PRTime aLastModified,
const nsACString& aParentGuid,
PRInt64 aGrandParentId,
nsIURI* aURI,
PRInt64* _itemId,
nsACString& _guid)
{
@ -613,6 +616,40 @@ nsNavBookmarks::InsertBookmarkInDB(PRInt64 aPlaceId,
NS_ENSURE_SUCCESS(rv, rv);
}
// Add a cache entry since we know everything about this bookmark.
BookmarkData bookmark;
bookmark.id = *_itemId;
bookmark.guid.Assign(_guid);
if (aTitle.IsVoid()) {
bookmark.title.SetIsVoid(true);
}
else {
bookmark.title.Assign(aTitle);
}
bookmark.position = aIndex;
bookmark.placeId = aPlaceId;
bookmark.parentId = aParentId;
bookmark.type = aItemType;
bookmark.dateAdded = aDateAdded;
if (aLastModified)
bookmark.lastModified = aLastModified;
else
bookmark.lastModified = aDateAdded;
if (aURI) {
rv = aURI->GetSpec(bookmark.url);
NS_ENSURE_SUCCESS(rv, rv);
}
bookmark.parentGuid = aParentGuid;
bookmark.grandParentId = aGrandParentId;
// Make space for the new entry.
ExpireNonrecentBookmarks(&mRecentBookmarksCache);
// Update the recent bookmarks cache.
BookmarkKeyClass* key = mRecentBookmarksCache.PutEntry(*_itemId);
if (key) {
key->bookmark = bookmark;
}
return NS_OK;
}
@ -661,7 +698,8 @@ nsNavBookmarks::InsertBookmark(PRInt64 aFolder,
TruncateTitle(aTitle, title);
rv = InsertBookmarkInDB(placeId, BOOKMARK, aFolder, index, title, dateAdded,
nsnull, aNewBookmarkId, guid);
nsnull, folderGuid, grandParentId, aURI,
aNewBookmarkId, guid);
NS_ENSURE_SUCCESS(rv, rv);
// If not a tag, recalculate frecency for this entry, since it changed.
@ -732,11 +770,14 @@ nsNavBookmarks::RemoveItem(PRInt64 aItemId)
mozStorageTransaction transaction(mDB->MainConn(), false);
// First, remove item annotations
nsAnnotationService* annosvc = nsAnnotationService::GetAnnotationService();
NS_ENSURE_TRUE(annosvc, NS_ERROR_OUT_OF_MEMORY);
rv = annosvc->RemoveItemAnnotations(bookmark.id);
NS_ENSURE_SUCCESS(rv, rv);
// First, if not a tag, remove item annotations.
if (bookmark.parentId != mTagsRoot &&
bookmark.grandParentId != mTagsRoot) {
nsAnnotationService* annosvc = nsAnnotationService::GetAnnotationService();
NS_ENSURE_TRUE(annosvc, NS_ERROR_OUT_OF_MEMORY);
rv = annosvc->RemoveItemAnnotations(bookmark.id);
NS_ENSURE_SUCCESS(rv, rv);
}
if (bookmark.type == TYPE_FOLDER) {
// Remove all of the folder's children.
@ -927,8 +968,8 @@ nsNavBookmarks::CreateContainerWithID(PRInt64 aItemId,
TruncateTitle(aTitle, title);
rv = InsertBookmarkInDB(-1, FOLDER, aParent, index,
title, dateAdded, nsnull, aNewFolder,
guid);
title, dateAdded, nsnull, folderGuid, grandParentId,
nsnull, aNewFolder, guid);
NS_ENSURE_SUCCESS(rv, rv);
rv = transaction.Commit();
@ -984,7 +1025,8 @@ nsNavBookmarks::InsertSeparator(PRInt64 aParent,
nsCAutoString guid;
PRTime dateAdded = PR_Now();
rv = InsertBookmarkInDB(-1, SEPARATOR, aParent, index, voidString, dateAdded,
nsnull, aNewItemId, guid);
nsnull, folderGuid, grandParentId, nsnull,
aNewItemId, guid);
NS_ENSURE_SUCCESS(rv, rv);
rv = transaction.Commit();
@ -1539,8 +1581,6 @@ nsNavBookmarks::SetItemDateInternal(enum BookmarkDate aDateType,
PRInt64 aItemId,
PRTime aValue)
{
BEGIN_CRITICAL_BOOKMARK_CACHE_SECTION(aItemId);
nsCOMPtr<mozIStorageStatement> stmt;
if (aDateType == DATE_ADDED) {
// lastModified is set to the same value as dateAdded. We do this for
@ -1567,7 +1607,15 @@ nsNavBookmarks::SetItemDateInternal(enum BookmarkDate aDateType,
rv = stmt->Execute();
NS_ENSURE_SUCCESS(rv, rv);
END_CRITICAL_BOOKMARK_CACHE_SECTION(aItemId);
// Update the cache entry, if needed.
BookmarkKeyClass* key = mRecentBookmarksCache.GetEntry(aItemId);
if (key) {
if (aDateType == DATE_ADDED) {
key->bookmark.dateAdded = aValue;
}
// Set lastModified in both cases.
key->bookmark.lastModified = aValue;
}
// note, we are not notifying the observers
// that the item has changed.
@ -1771,8 +1819,6 @@ nsNavBookmarks::SetItemTitle(PRInt64 aItemId, const nsACString& aTitle)
nsresult rv = FetchItemInfo(aItemId, bookmark);
NS_ENSURE_SUCCESS(rv, rv);
BEGIN_CRITICAL_BOOKMARK_CACHE_SECTION(bookmark.id);
nsCOMPtr<mozIStorageStatement> statement = mDB->GetStatement(
"UPDATE moz_bookmarks SET title = :item_title, lastModified = :date "
"WHERE id = :item_id "
@ -1780,7 +1826,6 @@ nsNavBookmarks::SetItemTitle(PRInt64 aItemId, const nsACString& aTitle)
NS_ENSURE_STATE(statement);
mozStorageStatementScoper scoper(statement);
nsCString title;
TruncateTitle(aTitle, title);
@ -1803,7 +1848,17 @@ nsNavBookmarks::SetItemTitle(PRInt64 aItemId, const nsACString& aTitle)
rv = statement->Execute();
NS_ENSURE_SUCCESS(rv, rv);
END_CRITICAL_BOOKMARK_CACHE_SECTION(bookmark.id);
// Update the cache entry, if needed.
BookmarkKeyClass* key = mRecentBookmarksCache.GetEntry(aItemId);
if (key) {
if (title.IsVoid()) {
key->bookmark.title.SetIsVoid(true);
}
else {
key->bookmark.title.Assign(title);
}
key->bookmark.lastModified = bookmark.lastModified;
}
NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers,
nsINavBookmarkObserver,
@ -2557,8 +2612,6 @@ nsNavBookmarks::SetKeywordForBookmark(PRInt64 aBookmarkId,
if (keyword.Equals(oldKeyword) || (keyword.IsEmpty() && oldKeyword.IsEmpty()))
return NS_OK;
BEGIN_CRITICAL_BOOKMARK_CACHE_SECTION(bookmark.id);
mozStorageTransaction transaction(mDB->MainConn(), false);
nsCOMPtr<mozIStorageStatement> updateBookmarkStmt = mDB->GetStatement(
@ -2610,7 +2663,11 @@ nsNavBookmarks::SetKeywordForBookmark(PRInt64 aBookmarkId,
rv = transaction.Commit();
NS_ENSURE_SUCCESS(rv, rv);
END_CRITICAL_BOOKMARK_CACHE_SECTION(bookmark.id);
// Update the cache entry, if needed.
BookmarkKeyClass* key = mRecentBookmarksCache.GetEntry(aBookmarkId);
if (key) {
key->bookmark.lastModified = bookmark.lastModified;
}
NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers,
nsINavBookmarkObserver,

View File

@ -389,6 +389,9 @@ private:
const nsACString& aTitle,
PRTime aDateAdded,
PRTime aLastModified,
const nsACString& aParentGuid,
PRInt64 aGrandParentId,
nsIURI* aURI,
PRInt64* _itemId,
nsACString& _guid);

View File

@ -195,18 +195,6 @@ add_test(function onItemChanged_tags_bookmark() {
{ name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
{ name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
] },
{ name: "onItemChanged", // This is an unfortunate effect of bug 653910.
args: [
{ name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
{ name: "property", check: function (v) v === "" },
{ name: "isAnno", check: function (v) v === true },
{ name: "newValue", check: function (v) v === "" },
{ name: "lastModified", check: function (v) typeof(v) == "number" && v > 0 },
{ name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_BOOKMARK },
{ name: "parentId", check: function (v) typeof(v) == "number" && v > 0 },
{ name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
{ name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
] },
{ name: "onItemRemoved", // This is the tag.
args: [
{ name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
@ -225,18 +213,6 @@ add_test(function onItemChanged_tags_bookmark() {
{ name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
{ name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
] },
{ name: "onItemChanged", // This is an unfortunate effect of bug 653910.
args: [
{ name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
{ name: "property", check: function (v) v === "" },
{ name: "isAnno", check: function (v) v === true },
{ name: "newValue", check: function (v) v === "" },
{ name: "lastModified", check: function (v) typeof(v) == "number" && v > 0 },
{ name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_FOLDER },
{ name: "parentId", check: function (v) v === PlacesUtils.tagsFolderId },
{ name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
{ name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
] },
{ name: "onItemRemoved", // This is the tag folder.
args: [
{ name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },