From ba85e5af784febc6cfc2c69246e229a7d8d66648 Mon Sep 17 00:00:00 2001 From: "rjc%netscape.com" Date: Wed, 8 Sep 1999 03:04:45 +0000 Subject: [PATCH] Cache sort resource requests. --- .../xul/templates/src/nsXULSortService.cpp | 45 +++++++++++++++++-- rdf/content/src/nsXULSortService.cpp | 45 +++++++++++++++++-- 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/content/xul/templates/src/nsXULSortService.cpp b/content/xul/templates/src/nsXULSortService.cpp index a9968471c30a..a1ea87176b06 100644 --- a/content/xul/templates/src/nsXULSortService.cpp +++ b/content/xul/templates/src/nsXULSortService.cpp @@ -116,6 +116,8 @@ static NS_DEFINE_IID(kIDomXulElementIID, NS_IDOMXULELEMENT_IID); static NS_DEFINE_CID(kCollationFactoryCID, NS_COLLATIONFACTORY_CID); static NS_DEFINE_IID(kICollationFactoryIID, NS_ICOLLATIONFACTORY_IID); +static NS_DEFINE_CID(kRDFInMemoryDataSourceCID, NS_RDFINMEMORYDATASOURCE_CID); + static NS_DEFINE_CID(kLocaleFactoryCID, NS_LOCALEFACTORY_CID); static NS_DEFINE_IID(kILocaleFactoryIID, NS_ILOCALEFACTORY_IID); static NS_DEFINE_CID(kLocaleCID, NS_LOCALE_CID); @@ -139,6 +141,7 @@ typedef struct _sortStruct { PRBool descendingSort; PRBool naturalOrderSort; nsCOMPtr resCache; + nsCOMPtr mInner; } sortStruct, *sortPtr; @@ -201,6 +204,7 @@ nsresult RemoveAllChildren(nsIContent *node); nsresult SortTreeChildren(nsIContent *container, PRInt32 colIndex, sortPtr sortInfo); nsresult DoSort(nsIDOMNode* node, const nsString& sortResource, const nsString& sortDirection); +static nsresult GetCachedTarget(sortPtr sortInfo, nsIRDFResource* aSource, nsIRDFResource *aProperty, PRBool aTruthValue, nsIRDFNode **aResult); static nsresult GetCachedResource(sortPtr sortInfo, nsIRDFResource *sortProperty, const char *suffix, nsIRDFResource **res); static nsresult GetResourceValue(nsIRDFResource *res1, nsIRDFResource *sortProperty, sortPtr sortInfo, nsIRDFNode **, PRBool &isCollationKey); static nsresult GetNodeValue(nsIContent *node1, nsIRDFResource *sortProperty, sortPtr sortInfo, nsIRDFNode **, PRBool &isCollationKey); @@ -803,6 +807,38 @@ openSortCallback(const void *data1, const void *data2, void *privateData) +nsresult +XULSortServiceImpl::GetCachedTarget(sortPtr sortInfo, nsIRDFResource* aSource, + nsIRDFResource *aProperty, PRBool aTruthValue, nsIRDFNode **aResult) +{ + nsresult rv = NS_OK, rvTemp; + + if (!(sortInfo->mInner)) + { + // if we don't have a mInner, create one + rvTemp = nsComponentManager::CreateInstance(kRDFInMemoryDataSourceCID, + nsnull, nsIRDFDataSource::GetIID(), (void **)&(sortInfo->mInner)); + } + if (sortInfo->mInner) + { + // else, if we do have a mInner, look for the resource in it + rv = sortInfo->mInner->GetTarget(aSource, aProperty, aTruthValue, aResult); + } + if (NS_SUCCEEDED(rv) && (rv == NS_RDF_NO_VALUE) && (sortInfo->db)) + { + // if we don't have a cached value, look it up in the document's DB + if (NS_SUCCEEDED(rv = (sortInfo->db)->GetTarget(aSource, aProperty, + aTruthValue, aResult)) && (rv != NS_RDF_NO_VALUE)) + { + // and if we have a value, cache it away in our mInner also + rvTemp = sortInfo->mInner->Assert(aSource, aProperty, *aResult, PR_TRUE); + } + } + return(rv); +} + + + nsresult XULSortServiceImpl::GetCachedResource(sortPtr sortInfo, nsIRDFResource *sortProperty, const char *suffix, nsIRDFResource **res) { @@ -900,7 +936,7 @@ XULSortServiceImpl::GetResourceValue(nsIRDFResource *res1, nsIRDFResource *sortP if (NS_SUCCEEDED(rv = GetCachedResource(sortInfo, sortInfo->sortProperty, "?collation=true", getter_AddRefs(modSortRes))) && (modSortRes)) { - if (NS_SUCCEEDED(rv = (sortInfo->db)->GetTarget(res1, modSortRes, + if (NS_SUCCEEDED(rv = GetCachedTarget(sortInfo, res1, modSortRes, PR_TRUE, target)) && (rv != NS_RDF_NO_VALUE)) { isCollationKey = PR_TRUE; @@ -915,7 +951,7 @@ XULSortServiceImpl::GetResourceValue(nsIRDFResource *res1, nsIRDFResource *sortP if (NS_SUCCEEDED(rv = GetCachedResource(sortInfo, sortInfo->sortProperty, "?sort=true", getter_AddRefs(modSortRes))) && (modSortRes)) { - if (NS_SUCCEEDED(rv = (sortInfo->db)->GetTarget(res1, modSortRes, + if (NS_SUCCEEDED(rv = GetCachedTarget(sortInfo, res1, modSortRes, PR_TRUE, target)) && (rv != NS_RDF_NO_VALUE)) { } @@ -924,7 +960,7 @@ XULSortServiceImpl::GetResourceValue(nsIRDFResource *res1, nsIRDFResource *sortP if (!(*target)) { // if no collation key and no special sorting value, just get the property value - if (NS_SUCCEEDED(rv = (sortInfo->db)->GetTarget(res1, sortProperty, + if (NS_SUCCEEDED(rv = GetCachedTarget(sortInfo, res1, sortProperty, PR_TRUE, target) && (rv != NS_RDF_NO_VALUE))) { } @@ -1273,6 +1309,7 @@ XULSortServiceImpl::OpenContainer(nsIRDFCompositeDataSource *db, nsIContent *con sortInfo.rdfService = gRDFService; sortInfo.db = db; sortInfo.resCache = nsnull; + sortInfo.mInner = nsnull; sortInfo.kNaturalOrderPosAtom = kNaturalOrderPosAtom; sortInfo.kTreeCellAtom = kTreeCellAtom; sortInfo.kNameSpaceID_XUL = kNameSpaceID_XUL; @@ -1323,6 +1360,7 @@ XULSortServiceImpl::InsertContainerNode(nsIContent *container, nsIContent *node, sortInfo.rdfService = gRDFService; sortInfo.db = nsnull; sortInfo.resCache = nsnull; + sortInfo.mInner = nsnull; // Maintain an nsCOMPtr to _here_ to the composite datasource so // that we're sure that we'll hold a reference to it (and actually @@ -1526,6 +1564,7 @@ XULSortServiceImpl::DoSort(nsIDOMNode* node, const nsString& sortResource, sortInfo.rdfService = gRDFService; sortInfo.db = nsnull; sortInfo.resCache = nsnull; + sortInfo.mInner = nsnull; nsCOMPtr domXulTree = do_QueryInterface(treeNode); if (!domXulTree) return(NS_ERROR_FAILURE); diff --git a/rdf/content/src/nsXULSortService.cpp b/rdf/content/src/nsXULSortService.cpp index a9968471c30a..a1ea87176b06 100644 --- a/rdf/content/src/nsXULSortService.cpp +++ b/rdf/content/src/nsXULSortService.cpp @@ -116,6 +116,8 @@ static NS_DEFINE_IID(kIDomXulElementIID, NS_IDOMXULELEMENT_IID); static NS_DEFINE_CID(kCollationFactoryCID, NS_COLLATIONFACTORY_CID); static NS_DEFINE_IID(kICollationFactoryIID, NS_ICOLLATIONFACTORY_IID); +static NS_DEFINE_CID(kRDFInMemoryDataSourceCID, NS_RDFINMEMORYDATASOURCE_CID); + static NS_DEFINE_CID(kLocaleFactoryCID, NS_LOCALEFACTORY_CID); static NS_DEFINE_IID(kILocaleFactoryIID, NS_ILOCALEFACTORY_IID); static NS_DEFINE_CID(kLocaleCID, NS_LOCALE_CID); @@ -139,6 +141,7 @@ typedef struct _sortStruct { PRBool descendingSort; PRBool naturalOrderSort; nsCOMPtr resCache; + nsCOMPtr mInner; } sortStruct, *sortPtr; @@ -201,6 +204,7 @@ nsresult RemoveAllChildren(nsIContent *node); nsresult SortTreeChildren(nsIContent *container, PRInt32 colIndex, sortPtr sortInfo); nsresult DoSort(nsIDOMNode* node, const nsString& sortResource, const nsString& sortDirection); +static nsresult GetCachedTarget(sortPtr sortInfo, nsIRDFResource* aSource, nsIRDFResource *aProperty, PRBool aTruthValue, nsIRDFNode **aResult); static nsresult GetCachedResource(sortPtr sortInfo, nsIRDFResource *sortProperty, const char *suffix, nsIRDFResource **res); static nsresult GetResourceValue(nsIRDFResource *res1, nsIRDFResource *sortProperty, sortPtr sortInfo, nsIRDFNode **, PRBool &isCollationKey); static nsresult GetNodeValue(nsIContent *node1, nsIRDFResource *sortProperty, sortPtr sortInfo, nsIRDFNode **, PRBool &isCollationKey); @@ -803,6 +807,38 @@ openSortCallback(const void *data1, const void *data2, void *privateData) +nsresult +XULSortServiceImpl::GetCachedTarget(sortPtr sortInfo, nsIRDFResource* aSource, + nsIRDFResource *aProperty, PRBool aTruthValue, nsIRDFNode **aResult) +{ + nsresult rv = NS_OK, rvTemp; + + if (!(sortInfo->mInner)) + { + // if we don't have a mInner, create one + rvTemp = nsComponentManager::CreateInstance(kRDFInMemoryDataSourceCID, + nsnull, nsIRDFDataSource::GetIID(), (void **)&(sortInfo->mInner)); + } + if (sortInfo->mInner) + { + // else, if we do have a mInner, look for the resource in it + rv = sortInfo->mInner->GetTarget(aSource, aProperty, aTruthValue, aResult); + } + if (NS_SUCCEEDED(rv) && (rv == NS_RDF_NO_VALUE) && (sortInfo->db)) + { + // if we don't have a cached value, look it up in the document's DB + if (NS_SUCCEEDED(rv = (sortInfo->db)->GetTarget(aSource, aProperty, + aTruthValue, aResult)) && (rv != NS_RDF_NO_VALUE)) + { + // and if we have a value, cache it away in our mInner also + rvTemp = sortInfo->mInner->Assert(aSource, aProperty, *aResult, PR_TRUE); + } + } + return(rv); +} + + + nsresult XULSortServiceImpl::GetCachedResource(sortPtr sortInfo, nsIRDFResource *sortProperty, const char *suffix, nsIRDFResource **res) { @@ -900,7 +936,7 @@ XULSortServiceImpl::GetResourceValue(nsIRDFResource *res1, nsIRDFResource *sortP if (NS_SUCCEEDED(rv = GetCachedResource(sortInfo, sortInfo->sortProperty, "?collation=true", getter_AddRefs(modSortRes))) && (modSortRes)) { - if (NS_SUCCEEDED(rv = (sortInfo->db)->GetTarget(res1, modSortRes, + if (NS_SUCCEEDED(rv = GetCachedTarget(sortInfo, res1, modSortRes, PR_TRUE, target)) && (rv != NS_RDF_NO_VALUE)) { isCollationKey = PR_TRUE; @@ -915,7 +951,7 @@ XULSortServiceImpl::GetResourceValue(nsIRDFResource *res1, nsIRDFResource *sortP if (NS_SUCCEEDED(rv = GetCachedResource(sortInfo, sortInfo->sortProperty, "?sort=true", getter_AddRefs(modSortRes))) && (modSortRes)) { - if (NS_SUCCEEDED(rv = (sortInfo->db)->GetTarget(res1, modSortRes, + if (NS_SUCCEEDED(rv = GetCachedTarget(sortInfo, res1, modSortRes, PR_TRUE, target)) && (rv != NS_RDF_NO_VALUE)) { } @@ -924,7 +960,7 @@ XULSortServiceImpl::GetResourceValue(nsIRDFResource *res1, nsIRDFResource *sortP if (!(*target)) { // if no collation key and no special sorting value, just get the property value - if (NS_SUCCEEDED(rv = (sortInfo->db)->GetTarget(res1, sortProperty, + if (NS_SUCCEEDED(rv = GetCachedTarget(sortInfo, res1, sortProperty, PR_TRUE, target) && (rv != NS_RDF_NO_VALUE))) { } @@ -1273,6 +1309,7 @@ XULSortServiceImpl::OpenContainer(nsIRDFCompositeDataSource *db, nsIContent *con sortInfo.rdfService = gRDFService; sortInfo.db = db; sortInfo.resCache = nsnull; + sortInfo.mInner = nsnull; sortInfo.kNaturalOrderPosAtom = kNaturalOrderPosAtom; sortInfo.kTreeCellAtom = kTreeCellAtom; sortInfo.kNameSpaceID_XUL = kNameSpaceID_XUL; @@ -1323,6 +1360,7 @@ XULSortServiceImpl::InsertContainerNode(nsIContent *container, nsIContent *node, sortInfo.rdfService = gRDFService; sortInfo.db = nsnull; sortInfo.resCache = nsnull; + sortInfo.mInner = nsnull; // Maintain an nsCOMPtr to _here_ to the composite datasource so // that we're sure that we'll hold a reference to it (and actually @@ -1526,6 +1564,7 @@ XULSortServiceImpl::DoSort(nsIDOMNode* node, const nsString& sortResource, sortInfo.rdfService = gRDFService; sortInfo.db = nsnull; sortInfo.resCache = nsnull; + sortInfo.mInner = nsnull; nsCOMPtr domXulTree = do_QueryInterface(treeNode); if (!domXulTree) return(NS_ERROR_FAILURE);