sbLocalDatabaseGUIDArray.h
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 #ifndef __SBLOCALDATABASEGUIDARRAY_H__
26 #define __SBLOCALDATABASEGUIDARRAY_H__
27 
28 #include "sbILocalDatabaseGUIDArray.h"
29 #include "sbILocalDatabasePropertyCache.h"
31 
32 #include <nsAutoPtr.h>
33 #include <nsCOMPtr.h>
34 #include <nsDataHashtable.h>
35 #include <nsStringGlue.h>
36 #include <nsTArray.h>
37 #include <sbIDatabaseQuery.h>
38 #include <sbISQLBuilder.h>
39 #include <nsISimpleEnumerator.h>
40 #include <nsIStringEnumerator.h>
41 #include <sbIDatabasePreparedStatement.h>
42 #include <sbILocalDatabaseLibrary.h>
43 #include <sbIMediaItem.h>
44 #include <sbHashKeys.h>
45 #include <sbWeakReference.h>
46 
47 #include <set>
48 #include <map>
49 
50 class nsIURI;
51 class nsIWeakReference;
52 class sbILibrary;
53 class sbIPropertyManager;
55 
58 {
59 public:
61  NS_DECL_SBILOCALDATABASEGUIDARRAY
62 
64 
65  struct FilterSpec {
66  nsString property;
67  nsTArray<nsString> values;
68  PRBool isSearch;
69  };
70 
71  struct SortSpec {
72  nsString property;
73  PRUint32 propertyId;
74  PRBool ascending;
75  PRBool secondary;
76  };
77 
78 private:
79 
80  struct ArrayItem {
81  ArrayItem(const ArrayItem& aOther) :
82  mediaItemId(aOther.mediaItemId),
83  guid(aOther.guid),
84  sortPropertyValue(aOther.sortPropertyValue),
85  ordinal(aOther.ordinal),
86  rowid(aOther.rowid)
87  {
88  MOZ_COUNT_CTOR(ArrayItem);
89  }
90 
91  ArrayItem(PRUint32 aMediaItemId,
92  const nsAString& aGuid,
93  const nsAString& aValue,
94  const nsAString& aOrdinal,
95  PRUint64 aRowid) :
96  mediaItemId(aMediaItemId),
97  guid(aGuid),
98  sortPropertyValue(aValue),
99  ordinal(aOrdinal),
100  rowid(aRowid)
101  {
102  MOZ_COUNT_CTOR(ArrayItem);
103  };
104 
105  ArrayItem(PRUint32 aMediaItemId,
106  const PRUnichar* aGuid,
107  const PRUnichar* aValue,
108  const PRUnichar* aOrdinal,
109  PRUint64 aRowid) :
110  mediaItemId(aMediaItemId),
111  guid(aGuid),
112  sortPropertyValue(aValue),
113  ordinal(aOrdinal),
114  rowid(aRowid)
115  {
116  MOZ_COUNT_CTOR(ArrayItem);
117  };
118 
119  ~ArrayItem()
120  {
121  MOZ_COUNT_DTOR(ArrayItem);
122  }
123 
124  PRUint32 mediaItemId;
125  nsString guid;
126  nsString sortPropertyValue;
127  nsString ordinal;
128  PRUint64 rowid;
129  };
130 
131  ~sbLocalDatabaseGUIDArray();
132 
133  nsresult Initialize();
134 
135  nsresult UpdateLength();
136 
137  nsresult RunLengthQuery(sbIDatabasePreparedStatement *aStatement,
138  PRUint32* _retval);
139 
140  nsresult UpdateQueries();
141 
142  nsresult GetPrimarySortKeyPosition(const nsAString& aValue,
143  PRUint32 *_retval);
144 
145  nsresult MakeQuery(sbIDatabasePreparedStatement *aStatement,
146  sbIDatabaseQuery** _retval);
147 
148  nsresult FetchRows(PRUint32 aRequestedIndex, PRUint32 aFetchSize);
149 
150  nsresult SortRows(PRUint32 aStartIndex,
151  PRUint32 aEndIndex,
152  const nsAString& aKey,
153  PRBool aIsFirst,
154  PRBool aIsLast,
155  PRBool aIsOnly,
156  PRBool isNull);
157 
158  nsresult ReadRowRange(sbIDatabasePreparedStatement *aStatement,
159  PRUint32 aStartIndex,
160  PRUint32 aCount,
161  PRUint32 aDestIndexOffset,
162  PRBool isNull);
163 
164  nsresult GetByIndexInternal(PRUint32 aIndex, ArrayItem** _retval);
165 
166  PRInt32 GetPropertyId(const nsAString& aProperty);
167 
168  static int SortBags(const void* a, const void* b, void* closure);
169 
170  nsresult AddSortInternal(const nsAString& aProperty,
171  PRBool aAscending,
172  PRBool aSecondary);
173  nsresult ClearSecondarySorts();
174 
175  nsresult GetMTListener(sbILocalDatabaseGUIDArrayListener ** aListener);
176 
177  void QueryInvalidate() {
178  mQueriesValid = PR_FALSE;
179  }
180 
181  // GUID Array Length Caching Key and Hashtables.
182  void GenerateCachedLengthKey();
183  PRPackedBool mNeedNewKey;
184  nsString mCachedLengthKey;
185 
186  nsCOMPtr<sbILocalDatabaseGUIDArrayLengthCache> mLengthCache;
187 
188  // Set of property IDs used in the length cache key; the cache entry should
189  // be removed if any of these property IDs are invalidated.
190  std::set<PRUint32> mPropIdsUsedInCacheKey;
191  PRLock* mPropIdsLock;
192 
193  // Cached property manager
194  nsCOMPtr<sbIPropertyManager> mPropMan;
195 
196  // Database GUID
197  nsString mDatabaseGUID;
198 
199  // Database Location
200  nsCOMPtr<nsIURI> mDatabaseLocation;
201 
202  // Query base table
203  nsString mBaseTable;
204 
205  // Optional column constraint to use for base query
206  nsString mBaseConstraintColumn;
207 
208  // Optional column constraint value to use for base query
209  PRUint32 mBaseConstraintValue;
210 
211  // Number of rows to featch on cache miss
212  PRUint32 mFetchSize;
213 
214  // Length of complete array
215  PRUint32 mLength;
216 
217  // Length of non-null portion of the array
218  PRUint32 mNonNullLength;
219 
220  // Current sort configuration
221  nsTArray<SortSpec> mSorts;
222 
223  // Number of primary sorts
224  PRInt32 mPrimarySortsCount;
225 
226  // Current filter configuration
227  nsTArray<FilterSpec> mFilters;
228 
229  // Monitor to protect mCache
230  PRMonitor* mCacheMonitor;
231  // Ordered array of GUIDs
232  nsTArray<nsAutoPtr<ArrayItem> > mCache;
233 
234  // Cache of primary sort key positions
235  nsDataHashtable<nsStringHashKey, PRUint32> mPrimarySortKeyPositionCache;
236 
237  // Query used to count full length of the array
238  nsString mFullCountQuery;
239  nsCOMPtr<sbIDatabasePreparedStatement> mFullCountStatement;
240 
241  // Query used to count full length of the array where the primary sort key
242  // is null
243  nsString mNonNullCountQuery;
244  nsCOMPtr<sbIDatabasePreparedStatement> mNonNullCountStatement;
245 
246  // Query used to return sorted GUIDs when the primary sort key is non-null
247  nsString mFullGuidRangeQuery;
248  nsCOMPtr<sbIDatabasePreparedStatement> mFullGuidRangeStatement;
249 
250  // Query used to return sorted GUIDs when the primary sort key is null
251  nsString mNullGuidRangeQuery;
252  nsCOMPtr<sbIDatabasePreparedStatement> mNullGuidRangeStatement;
253 
254  // Query used to resort chunks of results
255  nsString mResortQuery;
256  nsCOMPtr<sbIDatabasePreparedStatement> mResortStatement;
257 
258  // Query used to resort a chunk of results with a null primary sort key
259  nsString mNullResortQuery;
260  nsCOMPtr<sbIDatabasePreparedStatement> mNullResortStatement;
261 
262  // Query used to search for the position of a value in the primary sort
263  nsString mPrefixSearchQuery;
264  nsCOMPtr<sbIDatabasePreparedStatement> mPrefixSearchStatement;
265 
266 
267  // Query used to find the position of a primary sort key in the library
268  nsString mPrimarySortKeyPositionQuery;
269  nsCOMPtr<sbIDatabasePreparedStatement> mPrimarySortKeyPositionStatement;
270 
271  // Cached versions of some of the above variables used my the fetch
272  nsCOMPtr<sbIDatabasePreparedStatement> mStatementX;
273  nsCOMPtr<sbIDatabasePreparedStatement> mStatementY;
274  PRUint32 mLengthX;
275 
276  // Our listener
277  nsCOMPtr<nsIWeakReference> mListener;
278 
279  // Paired property cache
280  nsCOMPtr<sbILocalDatabasePropertyCache> mPropertyCache;
281 
282  // Map of guid -> first array index
283  nsDataHashtable<nsStringHashKey, PRUint32> mGuidToFirstIndexMap;
284 
285  // Map of mediaitemid + "-" + rowid -> array index for retrieval of the index
286  nsDataHashtable<nsStringHashKey, PRUint32> mViewItemUIDToIndexMap;
287 
288  // Get distinct values?
289  PRPackedBool mIsDistinct;
290  // Distinct values are the sortable versions and not human readable?
291  PRPackedBool mDistinctWithSortableValues;
292 
293  // Is the cache valid
294  PRPackedBool mValid;
295 
296  // Are the queries valid
297  PRPackedBool mQueriesValid;
298 
299  // Is there a search filter with at least one active search term
300  PRPackedBool mHasActiveSearch;
301 
302  // How nulls are sorted
303  PRPackedBool mNullsFirst;
304 
305  // True if rows have been prefetched
306  PRPackedBool mPrefetchedRows;
307 
308  // True if this GUID array is for a library
309  PRPackedBool mIsFullLibrary;
310 
311  // Counter for suppression of invalidation.
312  PRUint32 mSuppress;
313 };
314 
316 {
317 public:
319  NS_DECL_NSISIMPLEENUMERATOR
320 
322  sbILocalDatabaseGUIDArray* aArray);
323 
325 
326 private:
327  nsRefPtr<sbLocalDatabaseLibrary> mLibrary;
328  nsCOMPtr<sbILocalDatabaseGUIDArray> mArray;
329 
330  PRUint32 mNextIndex;
331  nsString mNextGUID;
332 
333  PRUint32 mPreviousLength;
334 };
335 
337 {
338 public:
340  NS_DECL_NSISTRINGENUMERATOR
341 
343 
345 
346 private:
347  nsCOMPtr<sbILocalDatabaseGUIDArray> mArray;
348  PRUint32 mNextIndex;
349 };
350 
351 #endif /* __SBLOCALDATABASEGUIDARRAY_H__ */
352 
onPageChanged aValue
Definition: FeedWriter.js:1395
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
NS_DECL_ISUPPORTS NS_DECL_NSISIMPLEENUMERATOR sbGUIDArrayEnumerator(sbLocalDatabaseLibrary *aLibrary, sbILocalDatabaseGUIDArray *aArray)
NS_DECL_ISUPPORTS NS_DECL_SBILOCALDATABASEGUIDARRAY sbLocalDatabaseGUIDArray()
Media library abstraction.
Definition: sbILibrary.idl:82
An object responsible for executing SQL queries on the database.
restoreHistoryPrecursor aCount
Manager for system wide metadata properties.
NS_DECL_ISUPPORTS NS_DECL_NSISTRINGENUMERATOR sbGUIDArrayStringEnumerator(sbILocalDatabaseGUIDArray *aArray)
A prepared database statement.