javascript - indexeddb 部分键搜索获取下一个

标签 javascript google-chrome-app indexeddb

在我调用 cursor.continue() 之前,indexeddb CursorWithValue 是否存储下一条或上一条记录?我可以查看 IDBCursorWithValue 对象,然后将指针存储到下一条记录吗?

是否可以通过部分键获取第一条记录,然后仅当用户单击下一条记录时获取下一条记录,而不缓冲数组中的记录集合?

我知道我可以使用cursor.continue()来获取所有匹配的记录并将其存储在数组中。我还明白,如果我只获取第一个匹配的记录,并终止对数据库的调用的 onsuccess 函数,那么这是异步的,我相当确定我将失去链接到的能力下一条记录。

通过以下方法,我可以获得部分 key 的一个或所有匹配记录。使用 \uffff 我基本上得到了匹配的 alpha 和所有更大的记录。

storeGet = indexTITLE.openCursor(IDBKeyRange.bound(x.value, x.value, '\uffff'), 'next');

这对我来说是全新的,也许我对这一切的看法都是错误的。任何建议表示赞赏。我一直在阅读这里和 github 上的每一个线程,希望其他人已经在使用 indexeddb 做到这一点。

最佳答案

让我尝试重述一下问题:

You've iterated a cursor part-way through a range. Now you want to stop and wait for user input before continuing. But the transaction will close, so you can't just continue on the click. What do you do instead?

首先:好问题!这很棘手。您有多种不同的选择。

在最简单的情况下,您有一个唯一的索引(或对象存储),因此不存在重复的键。

var currentKey = undefined;

// assumes you open a transaction and pass in the index to query
function getNextRecord(index, callback) {
  var range;
  if (currentKey === undefined) {
    range = null; // unbounded
  } else {
    range = IDBKeyRange.lowerBound(currentKey, true); // exclusive
  }
  var request = index.openCursor(range);
  request.onsuccess = function(e) {
    var cursor = request.result;
    if (!cursor) {
      // no records found/hit end of range
      callback();
      return;
    }
    // yay, found a record. remember the key for next time
    currentKey = cursor.key;
    callback(cursor.value);
  };
}

如果你有一个非唯一索引,那就更棘手了,因为你需要存储索引键和主键,并且无法在该位置打开光标。 (请参阅功能请求:https://github.com/w3c/IndexedDB/issues/14)因此您需要将光标前进到之前看到的键/主键位置:

var currentKey = undefined, primaryKey = undefined;

// assumes you open a transaction and pass in the index to query
function getNextRecord(index, callback) {
  var range;
  if (currentKey === undefined) {
    range = null; // unbounded
  } else {
    range = IDBKeyRange.lowerBound(currentKey, true); // exclusive
  }
  var request = index.openCursor(range);
  request.onsuccess = function(e) {
    var cursor = request.result;
    if (!cursor) {
      // no records found/hit end of range
      callback();
      return;
    }

    if (indexedDB.cmp(cursor.key, currentKey) === 0 &&
        indexedDB.cmp(cursor.primaryKey, primaryKey) <= 0) {
      // walk over duplicates until we are past where we were last time
      cursor.continue();
      return;
    }

    // yay, found a record. remember the keys for next time
    currentKey = cursor.key;
    primaryKey = cursor.primaryKey;
    callback(cursor.value);
  };
}

我假设没有上限,例如我们想要索引中的所有记录。您可以根据需要替换 range 的初始化。

关于javascript - indexeddb 部分键搜索获取下一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33836394/

相关文章:

javascript - 如何使用其 cssRules 克隆 Style 元素?

javascript - Windows chrome 浏览器中的 getImageData 内存泄漏问题 : javascript

javascript - IndexedDB - 检测是否建立索引

javascript - Mobile Safari 10 IndexedDB Blob

javascript - 可搜索的加密方法(对于傻瓜来说!)

javascript - 同上 HTTP API 服务器发送事件 CORS 错误

javascript - 对象不会被创建?

dart - 未捕获的TypeError:无法在dart中读取null的属性 'v0'

javascript - 将数组转换为树

javascript - Chrome应用程序文件系统API : Suggested file name has no extension in OSX