是否可以在IndexedDb事务期间将对象写入文件? [英] Is it possible to write objects to a file during an IndexedDb transaction?

查看:110
本文介绍了是否可以在IndexedDb事务期间将对象写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有数百个对象的objectStore,可以用以下代码查看:

I have an objectStore with hundreds of objects in it that I can view with code like this:

db // already set by call to window.indexedDB.open
.transaction(["mailing-list"])
.objectStore("mailing-list")
.openCursor()
.onsuccess = function (event) {
    var cursor = event.target.result;
    if (cursor) {
        console.log(cursor.value);
        cursor.continue();
    }
};

我想做的是使用FileWriter在检索到每个对象时写出它.我不想将所有对象堆积在一个巨大的数组中,然后一次将它们全部写出.我也不想为每个对象启动单独的事务,因为我想使用游标遍历所有对象. (无需关心他们的密钥.)

What I want to do is use a FileWriter to write out each object as it's retrieved; I don't want to accumulate all the objects in a giant array and write them all out at once. Nor do I want to start a separate transaction for each object, as I want to use a cursor to iterate through all the objects. (Without caring about their keys.)

我可以将调用放置到fileWriter.write,现在调用console.log.对cursor.continue()的调用将在onwrite回调中.否则,我会在上一个操作完成之前发出写操作,这是非法的.

I would put the call to fileWriter.write where the call to console.log is now. The call to cursor.continue() would be in the onwrite callback. Otherwise, I would be issuing a write before the previous one had completed, which is illegal.

这似乎是不可能的,因为,由于写入是异步的,因此在调用write之后,"onsuccess"回调将返回,从而结束事务并使光标无效,即使该光标被捕获在闭包中也是如此.

This seems to be impossible, because, as writing is asynchronous, the "onsuccess" callback would return after write is called, thus ending the transaction and rendering the cursor invalid, even if it's captured in a closure.

我对吗?我将对其进行编码以在内存中累积对象的整个集合,因此我可以在游标完成后将它们写入,尽管这不是我的首选.

Am I right about this? I'm about to code it to accumulate the entire collection of objects in memory, so I can write them after the cursor completes, although that's not my first choice.

(注意:我不认为有一种同步的书写形式,但是即使有,我也无法使用它,因为此代码将在Chrome应用程序中使用.)

(NOTE: I don't think there's a synchronous form of writing, but, even if there were, I couldn't use it as this code will be in a Chrome App.)

推荐答案

只要两个异步处理器之间有足够的缓冲区,就可以执行操作.

You can do as long as you has enough buffer between the two async processors.

我在博客上有一些介绍此处:

I have blog a bit how this is done here:

考虑使用大分隔文本中的记录填充商店.为避免保留用于保存所有记录的内存,CsvStreamer使用HTTP Range标头按块下载数据,并在读取记录时调用下一个回调.数据块包含多个记录,在这种情况下,CsvStreamer会同步写入.在等待下一个数据块时,CsvStreamer异步调用下一个回调.

Consider for populating a store with records from a large delimited text. To avoid holding memory for holding all records, data are download by chunk using HTTP Range header by CsvStreamer and invoke next callback when it read a record. A chunk of data comprise multiple records and in that case CsvStreamer write synchronously. While waiting for next chunk of data CsvStreamer invoke next callback asynchronously.

var db = new ydn.db.Storage(db_name, schema, options);
var stream = new CsvStreamer(url);
var isSerial = false;
var tdb = db.thread('multi', isSerial); // multi-request parallel transactions
var putData = function(data) {
  if (data) { 
    tdb.put('store1', data).then(function() {
      stream.next(function (data) {   
        putData(data);
    }), function(e) {
      throw e;
    });     
  }
});

stream.next(function (data) {
  putData(data);
}

诀窍是如果事务仍处于活动状态,则重用该事务.否则,请创建新交易.

The trick is reuse the transaction if it is still active. Otherwise create a new transaction.

是的,上面的示例正在写入idb,而您想阅读.用管道传输异步处理器时,您需要考虑最慢的一个,即FileWriter.这是示例:

Yeah, above example is writing to idb, whereas you want reading. When piping async processors, you need to concern about the slowest one, that is FileWriter. Here is example:

var dump = function(marker) {
  var q = marker ? db.from('store name', '>', marker) : db.from('store name');
  q.open(function(cur) {
    var cb = writeToFile(cur.getValue());
    if (cb) { // wait me callback
       var next_key = cur.getKey();
       cb(function() {
         dump(next_key); // notice de-referencing cur, though not necessary
       });
       cb = null; // also not necessary, but better be on safe side of memory leak
       return null; // stop continuing cursor
    } else { // file writer said, good to call me again
      return undefined; // continue cursor
    }
  }, iter, 'readonly');
}

dump();

在IndexedDb事务期间是否可以将对象写入文件?

Is it possible to write objects to a file during an IndexedDb transaction?

简短的回答:否

这篇关于是否可以在IndexedDb事务期间将对象写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆