Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/storage.js

Issue 8402021: Crawler frontend (Closed)
Patch Set: Created Sept. 25, 2012, 3:13 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« lib/crawler.js ('K') | « lib/main.js ('k') | metadata » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/storage.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/storage.js
@@ -0,0 +1,45 @@
+Cu.import("resource://gre/modules/FileUtils.jsm");
+
+let outputStream;
+let converterOutputStream;
+
+function createTemporaryFile(name)
+{
+ let file = FileUtils.getFile("TmpD", [name]);
+ file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
+ return file;
+}
+
+function openOutputStream(file)
+{
+ let outputStream = FileUtils.openSafeFileOutputStream(file);
+ let converterOutputStream = Cc["@mozilla.org/intl/converter-output-stream;1"]
+ .createInstance(Ci.nsIConverterOutputStream);
+ converterOutputStream.init(outputStream, "UTF-8", 0, 0);
+ return [outputStream, converterOutputStream];
+}
+
+let Storage = exports.Storage = {};
+
+Storage.init = function()
+{
+ Storage.dataFile = createTemporaryFile("crawler-data");
+ [outputStream, converterOutputStream] = openOutputStream(Storage.dataFile);
+};
+
+Storage.write = function(data)
+{
+ let line = JSON.stringify(data) + "\n";
+ converterOutputStream.writeString(line);
+};
+
+Storage.finish = function()
+{
+ converterOutputStream.flush();
+ FileUtils.closeSafeFileOutputStream(outputStream);
+};
+
+Storage.destroy = function()
+{
+ Storage.dataFile.remove(true);
+};
« lib/crawler.js ('K') | « lib/main.js ('k') | metadata » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld