sbLocalDatabasePropertyCache.h
Go to the documentation of this file.
1 /*
2  //
3  // BEGIN SONGBIRD GPL
4  //
5  // This file is part of the Songbird web player.
6  //
7  // Copyright(c) 2005-2008 POTI, Inc.
8  // http://songbirdnest.com
9  //
10  // This file may be licensed under the terms of of the
11  // GNU General Public License Version 2 (the "GPL").
12  //
13  // Software distributed under the License is distributed
14  // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15  // express or implied. See the GPL for the specific language
16  // governing rights and limitations.
17  //
18  // You should have received a copy of the GPL along with this
19  // program. If not, go to http://www.gnu.org/licenses/gpl.html
20  // or write to the Free Software Foundation, Inc.,
21  // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  //
23  // END SONGBIRD GPL
24  //
25  */
26 
27 #ifndef __SBLOCALDATABASEPROPERTYCACHE_H__
28 #define __SBLOCALDATABASEPROPERTYCACHE_H__
29 
30 #include <nsIStringEnumerator.h>
31 #include <sbILocalDatabasePropertyCache.h>
32 
33 #include <nsCOMArray.h>
34 #include <nsCOMPtr.h>
35 #include <nsIClassInfo.h>
36 #include <nsDataHashtable.h>
37 #include <nsInterfaceHashtable.h>
38 #include <nsIObserver.h>
39 #include <nsIThread.h>
40 #include <nsIThreadPool.h>
41 #include <nsStringGlue.h>
42 #include <nsTArray.h>
43 #include <nsTHashtable.h>
44 #include <nsIRunnable.h>
45 
46 #include <sbIJobProgress.h>
47 #include <sbIMediaListListener.h>
48 #include <nsITimer.h>
49 
50 #include <sbWeakReference.h>
51 
53 #include "sbFixedInterfaceCache.h"
54 #include "sbLocalDatabaseSQL.h"
55 
56 #include <map>
57 
58 struct PRLock;
59 struct PRMonitor;
60 
61 class nsIURI;
62 class sbIDatabaseQuery;
65 class sbIPropertyManager;
68 
70  public nsIObserver
71 {
72 public:
78  static PRUint32 const CACHE_SIZE = 1024;
82  static PRUint32 const BATCH_READ_SIZE = 128;
83 
85  NS_DECL_SBILOCALDATABASEPROPERTYCACHE
86  NS_DECL_NSIOBSERVER
87 
88  typedef sbFixedInterfaceCache<nsStringHashKey,
90 
92 
93  // This is public so that it can be used with nsAutoPtr and friends.
95 
96  nsresult Init(sbLocalDatabaseLibrary* aLibrary,
97  const nsAString& aLibraryResourceGUID);
98 
99  PRBool GetPropertyID(PRUint32 aPropertyDBID, nsAString& aPropertyID);
100 
101  void GetColumnForPropertyID(PRUint32 aPropertyID, nsAString &aColumn);
102 
103  // Called when mSortInvalidateJob completes
104  nsresult InvalidateSortDataComplete();
105 
106  // Determine the pre-baked secondary sort string for a property
107  // in a given bag
109  PRUint32 aPropertyDBID,
110  nsAString& _retval);
111 private:
112  nsresult Shutdown();
113 
114  nsresult MakeQuery(sbIDatabaseQuery** _retval);
115  nsresult LoadProperties();
116 
117  nsresult AddDirty(const nsAString &aGuid,
119 
120  nsresult InvalidateGUIDArrays();
121 
122  PRUint32 GetPropertyDBIDInternal(const nsAString& aPropertyID);
123 
124  nsresult InsertPropertyIDInLibrary(const nsAString& aPropertyID,
125  PRUint32 *aPropertyDBID);
126 
127  // Used to persist invalid sorting state in case mSortInvalidateJob
128  // is interrupted.
129  nsresult GetSetInvalidSortDataPref(PRBool aWrite, PRBool& aValue);
130 
131  typedef nsInterfaceHashtable<nsUint32HashKey, sbLocalDatabaseResourcePropertyBag> IDToBagMap;
132 
133  nsresult RetrieveSecondaryProperties(sbIDatabaseQuery* query, nsTArray<PRUint32> itemIDs,
134  IDToBagMap const & bags);
135 
136  nsresult RetrieveLibraryProperties(sbLocalDatabaseResourcePropertyBag * aBag);
137 
147  template <class T>
148  nsresult RetrieveProperties(T & aGUIDs,
149  nsCOMArray<sbLocalDatabaseResourcePropertyBag> & aBags);
150 
165  template <class T>
166  nsresult
167  RetrievePrimaryProperties(sbIDatabaseQuery* query,
168  T const & aGuids,
169  IDToBagMap & aIDToBagMap,
170  nsCOMArray<sbLocalDatabaseResourcePropertyBag> & aBags,
171  nsTArray<PRUint32> & aItemIDs);
172 
173  // Pending write count.
174  PRUint32 mWritePendingCount;
175 
176  // Database GUID
177  nsString mDatabaseGUID;
178 
179  // Database Location
180  nsCOMPtr<nsIURI> mDatabaseLocation;
181 
182  // Cache the property name list
183  nsDataHashtableMT<nsUint32HashKey, nsString> mPropertyDBIDToID;
184  nsDataHashtableMT<nsStringHashKey, PRUint32> mPropertyIDToDBID;
185 
186  // Depedent GUID Array map and protecting monitor
187  PRMonitor* mDependentGUIDArrayMonitor;
188  typedef std::map<nsISupports *,
189  nsCOMPtr<nsIWeakReference> > DependentGUIDArrays_t;
190  DependentGUIDArrays_t mDependentGUIDArrays;
191 
192  // Used to protect the cache and all of the resource property bags
193  PRMonitor* mMonitor;
194 
195  // Cache for GUID -> property bag
196  InterfaceCache mCache;
197 
198  // Dirty GUIDs
199  nsInterfaceHashtable<nsStringHashKey, sbLocalDatabaseResourcePropertyBag> mDirty;
200 
201  // Dirty Property IDs for use with invalidation of GUID arrays
202  std::set<PRUint32> mDirtyForInvalidation;
203 
204  // flushing on a background thread
205  struct FlushQueryData {
206  nsCOMPtr<sbIDatabaseQuery> query;
207  PRUint32 dirtyGuidCount;
208  };
209  nsresult DispatchFlush();
210  void RunFlushThread();
211 
212  static
213  nsresult ProcessQueries(nsTArray<FlushQueryData> & aQueries);
214 
215  // The GUID of the library resource
216  nsString mLibraryResourceGUID;
217 
218  // Cache Flush Interval Timer
219  nsCOMPtr<nsITimer> mFlushTimer;
220  nsCOMPtr<nsIThreadPool> mThreadPoolService;
221 
222  // GUID Array Invalidation Timer
223  nsCOMPtr<nsITimer> mInvalidateTimer;
224 
225  // Backstage pass to our parent library. Can't use an nsRefPtr because the
226  // library owns us and that would create a cycle.
227  sbLocalDatabaseLibrary* mLibrary;
228 
229  nsCOMPtr<sbIPropertyManager> mPropertyManager;
230 
231  // When invalidating sort data this keeps the job alive, and
232  // is used to cancel if shutdown occurs.
233  nsRefPtr<sbLocalDatabaseSortInvalidateJob> mSortInvalidateJob;
234 
235  sbLocalDatabaseSQL mSQLStrings;
236  nsCOMPtr<sbIDatabasePreparedStatement> mItemSelectPreparedStatement;
237  nsCOMPtr<sbIDatabasePreparedStatement> mSecondaryPropertySelectPreparedStatement;
238  nsCOMPtr<sbIDatabasePreparedStatement> mMediaItemsFtsAllDeletePreparedStatement;
239  nsCOMPtr<sbIDatabasePreparedStatement> mMediaItemsFtsAllInsertPreparedStatement;
240  nsCOMPtr<sbIDatabasePreparedStatement> mPropertiesDeletePreparedStatement;
241  nsCOMPtr<sbIDatabasePreparedStatement> mPropertiesInsertPreparedStatement;
242 
243  // There's a separate update statement for each top level property.
244  // This is because we have no efficient way to /not/ update a property
245  // if we try to only update some of the top level properties.
246  nsInterfaceHashtable<nsUint32HashKey, sbIDatabasePreparedStatement> mMediaItemsUpdatePreparedStatements;
247  nsInterfaceHashtable<nsUint32HashKey, sbIDatabasePreparedStatement> mLibraryMediaItemUpdatePreparedStatements;
248 
249 };
250 
258  public nsIClassInfo,
259  public nsIRunnable,
261  public nsIObserver
262 {
263 public:
265  NS_DECL_NSICLASSINFO
266  NS_DECL_SBIJOBPROGRESS
267  NS_DECL_NSIRUNNABLE
268  NS_DECL_SBIMEDIALISTENUMERATIONLISTENER
269  NS_DECL_NSIOBSERVER
270 
272 
274 
275  nsresult Init(sbLocalDatabasePropertyCache* aPropCache,
276  sbLocalDatabaseLibrary* aLibrary);
277 
278  nsresult Shutdown();
279 
280 private:
281 
287  static nsresult RunLibraryBatch(nsISupports* aUserData);
288 
289  PRPackedBool mShouldShutdown;
290  nsCOMPtr<nsIThread> mThread;
291 
292  sbLocalDatabaseLibrary* mLibrary;
293  sbLocalDatabasePropertyCache* mPropCache;
294 
295  // Timer used to send job progress notifications()
296  nsCOMPtr<nsITimer> mNotificationTimer;
297 
298  // sbIJobProgress variables
299  PRUint16 mStatus;
300  PRUint32 mCompletedItemCount;
301  PRUint32 mTotalItemCount;
302  nsString mTitleText;
303  nsString mStatusText;
304  nsString mFailedText;
305  nsCOMArray<sbIJobProgressListener> mListeners;
306 };
307 
308 #endif /* __SBLOCALDATABASEPROPERTYCACHE_H__ */
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
Interface used to enumerate the items in a media list.
onPageChanged aValue
Definition: FeedWriter.js:1395
Generic interface for exposing long running jobs to the UI.
NS_DECL_ISUPPORTS NS_DECL_SBILOCALDATABASEPROPERTYCACHE NS_DECL_NSIOBSERVER typedef sbFixedInterfaceCache< nsStringHashKey, sbLocalDatabaseResourcePropertyBag > InterfaceCache
PRBool GetPropertyID(PRUint32 aPropertyDBID, nsAString &aPropertyID)
nsresult Init(sbLocalDatabaseLibrary *aLibrary, const nsAString &aLibraryResourceGUID)
nsresult Init(sbLocalDatabasePropertyCache *aPropCache, sbLocalDatabaseLibrary *aLibrary)
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
An object responsible for executing SQL queries on the database.
NS_DECL_ISUPPORTS NS_DECL_NSICLASSINFO NS_DECL_SBIJOBPROGRESS NS_DECL_NSIRUNNABLE NS_DECL_SBIMEDIALISTENUMERATIONLISTENER NS_DECL_NSIOBSERVER sbLocalDatabaseSortInvalidateJob()
nsresult CreateSecondarySortValue(sbILocalDatabaseResourcePropertyBag *aBag, PRUint32 aPropertyDBID, nsAString &_retval)
Manager for system wide metadata properties.
A prepared database statement.
void GetColumnForPropertyID(PRUint32 aPropertyID, nsAString &aColumn)