Indexed DB cursor ranges on mulitiple properties

匿名 (未验证) 提交于 2019-12-03 02:06:01

问题:

I have a composite index of two properties on an indexeddb objectstore and wish to retrieve a cursor based on the range of both of these properties.

Here's an example object in the store :

{species: 'Oak',xcoord: 123456, ycoord: 654321} 

and index :

treeStore.createIndex("treelocation", ["xcoord","ycoord"], { unique: false }); 

The index creation is succesful and I can see it in the Chrome developer tools, however now I'd like to open a cursor with a keyrange on both the x and y co-ordinates (which will be the extent of a map).

Searching online I can't see how to do this, and opening an index with an array of key ranges doesn't seem to work.

Thanks

Andrew

回答1:

I have been told the solution is indeed IDBKeyRange.bound([lowX,lowY],[highX,highY]).



回答2:

The index you created is a composite index. It is query like this:

index = objectStore.index('treelocation'); index.get([123456, 654321]);  

Alternatively, you can use two indexes for each coorrd. In my opinion it is better.

x_index = objectStore.index('xcoord');  y_index = objectStore.index('ycoord'); x_species = x_index.get(IDBKeyRange.only(123456))  y_species = y_index.get(IDBKeyRange.only(654321))  species = x_species.intersect(y_species); // this is result 


回答3:

The range suggestion is part of the answer, but even with array keys it really is just a 1-dimensional range, not a 2- (or N-) dimensional selection of the data. With your current schema you'll need to do something like this:

index.openCursor([lowX, lowY], [highX, highY]).onsuccess = function(e) {     var cursor = e.target.result;     if (!cursor) return; // done!      var x = cursor.key[0], y = cursor.key[1];     // assert(lowX  highY) {         cursor.continue([x + 1, lowY]);     } else {         processRecord(cursor.value); // we got one!         cursor.continue();     } }; 

(if coordinates are not integral, replace + 1 with an appropriate epsilon)

I've posted an example of a generalized solution to:

https://gist.github.com/inexorabletash/704e9688f99ac12dd336



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!