test_load.js
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 
31 function runTest () {
32 
33  Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
34 
35 // Not part of the automated test suite. Only useful for
36 // manual testing.
37 
38 // makeLibrary(1000);
39 // makeLibrary(5000);
40 // makeLibrary(10000);
41 // makeLibrary(25000);
42 // makeLibrary(50000);
43 // makeLibrary(100000);
44 }
45 
46 function makeLibrary(aLength) {
47 
48  var library = createLibrary("test_" + aLength);
49  library.clear();
50 
51  var file = Components.classes["@mozilla.org/file/local;1"]
52  .createInstance(Components.interfaces.nsILocalFile);
53  file.initWithPath("/builds/songbird/trunk/big.txt");
54 
55  var data = "";
56  var fstream = Cc["@mozilla.org/network/file-input-stream;1"]
57  .createInstance(Ci.nsIFileInputStream);
58  var sstream = Cc["@mozilla.org/scriptableinputstream;1"]
59  .createInstance(Ci.nsIScriptableInputStream);
60  fstream.init(file, -1, 0, 0);
61  sstream.init(fstream);
62 
63  var str = sstream.read(4096);
64  while (str.length > 0) {
65  data += str;
66  str = sstream.read(4096);
67  }
68 
69  sstream.close();
70  fstream.close();
71 
72  var uris = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"].createInstance(Ci.nsIMutableArray);
73  var properties = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"].createInstance(Ci.nsIMutableArray);
74 
75  var a = data.split("\n");
76  for (var i = 0; i < a.length && i < aLength; i++) {
77 
78  var pa = SBProperties.createArray();
79  var b = a[i].split("\t");
80 
81  var artist = b[1];
82  if (artist && artist.length) {
83  pa.appendProperty(SBProperties.artistName, artist);
84  }
85 
86  var album = (b[3] == "" ? "" : b[3] + " ") + b[2];
87  if (album && album.length) {
88  pa.appendProperty(SBProperties.albumName, album);
89  }
90 
91  var genre = b[4];
92  if (genre && genre.length) {
93  pa.appendProperty(SBProperties.genre, genre);
94  }
95 
96  var year = b[5].length > 3 ? parseInt(b[5].substr(0, 4)) : null;
97  if (year && !isNaN(year)) {
98  try {
99  pa.appendProperty(SBProperties.year, year);
100  }
101  catch(e) {
102  // don't care
103  }
104  }
105 
106  var track = (b[8] == "" ? "" : b[8] + " ") + b[6];
107  if (track.length) {
108  pa.appendProperty(SBProperties.trackName, track);
109  }
110 
111  var trackNumber = parseInt(b[7]);
112  if (!isNaN(trackNumber)) {
113  pa.appendProperty(SBProperties.trackNumber, trackNumber);
114  }
115 
116  var length;
117  var c = b[9].split(":");
118  if (c.length == 2) {
119  length = (c[0] * 60) + c[1];
120  pa.appendProperty(SBProperties.duration, length);
121  }
122 
123  var uri = newURI("file:///foo/" + escape(artist) + "-" +
124  escape(album) + "-" + escape(track) + ".mp3");
125  uris.appendElement(uri, false);
126  properties.appendElement(pa, false);
127 
128 /*
129  dump("album: '" + album + "'\n");
130  dump("genre: '" + genre + "'\n");
131  dump("year: '" + year + "'\n");
132  dump("track: '" + track + "'\n");
133  dump("trackNumber: '" + trackNumber + "'\n");
134  dump("legth: '" + length + "'\n");
135 */
136 
137  if (i > 0 && (i + 1) % 1000 == 0) {
138  library.batchCreateMediaItems(uris, properties, true);
139  dump("loading " + i + " a.length " + a.length + " length + " + aLength + "\n");
140  var uris = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"].createInstance(Ci.nsIMutableArray);
141  var properties = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"].createInstance(Ci.nsIMutableArray);
142 
143  }
144 
145  }
146 
147  // library.batchCreateMediaItems(uris, properties, true);
148 
149 }
150 
151 function createLibrary(databaseGuid, databaseLocation) {
152 
153  var directory;
154  if (databaseLocation) {
155  directory = databaseLocation.QueryInterface(Ci.nsIFileURL).file;
156  }
157  else {
158  directory = Cc["@mozilla.org/file/directory_service;1"].
159  getService(Ci.nsIProperties).
160  get("ProfD", Ci.nsIFile);
161  directory.append("db");
162  }
163 
164  var file = directory.clone();
165  file.append(databaseGuid + ".db");
166 
167  var libraryFactory =
168  Cc["@songbirdnest.com/Songbird/Library/LocalDatabase/LibraryFactory;1"]
169  .getService(Ci.sbILibraryFactory);
170  var hashBag = Cc["@mozilla.org/hash-property-bag;1"].
171  createInstance(Ci.nsIWritablePropertyBag2);
172  hashBag.setPropertyAsInterface("databaseFile", file);
173  var library = libraryFactory.createLibrary(hashBag);
174  library.clear();
175  return library;
176 }
function makeLibrary(aLength)
Definition: test_load.js:46
const Cc
var uris
sidebarFactory createInstance
Definition: nsSidebar.js:351
function runTest()
Hack script to load a songbird library from a preformatted text file.
Definition: test_load.js:31
getService(Ci.sbIFaceplateManager)
var getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow('Songbird SBProperties artist
Definition: tuner2.js:40
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
var uri
Definition: FeedWriter.js:1135
const Ci
observe data
Definition: FeedWriter.js:1329
_getSelectedPageStyle s i
var file