What is returned in .then() for Table.bulkPut in Dexie? - dexie

I'm using Dexie for my webapp. However from the documentation I couldn't get what bulkPut exactly return on success.
For example:
Let's say in my DB I have three objects with the id's 1, 3 and 5. For the example I'll just show it as an array of ints: DB = [1, 3, 5].
My application has asked the server to give me all items from the server. I received an array with four objects: result = [2, 3, 4, 5].
2 and 4 are new items, 3 is the same as 3 in my DB, but has been modified. 5 is not modified and therefore equal to my DB object.
I expect the following output from bulkAdd and bulkPut:
When I use Table.bulkAdd(result, {allKeys: true}) where I catch the errors, I expect the following outcome:
My DB contains: [1, 2, 3, 4, 5] and my .then() will return a promise, containing the id's of 2 and 4.
When I use Table.bulkPut(result, {allKeys: true}) where I catch the errors, I expect the following outcome:
My DB contains: [1, 2, 3, 4, 5] and my .then() will return a promise, containing the id's 2, 3 and 4. Because 5 was not modified and therefore hasn't been replaced.
I expect the promises of the methods only to return the keys of the values that actually have been added/modified. Is this assumption correct? Because in practise this is not what happens in my application.
In my application I use bulkPut (so that I can add and update my data with one method), but somehow it always returns 2 keys of items, regardless of the input being an array containing modified items or existing items.

bulkPut() with option {allKeys: true} returns an array with the same length as given input array. All keys are returned regardless of whether the put operation would result in new, updated or identical data. If exception occurs and you catch it, you will only have the error object.

Related

Index match on first non-zero result

Trying to do an index/match lookup to find the first non-zero result among multiple matches. Not sure exactly how to do this. Sample data below.
Foo 1
bar 0
bar 2
Want to do the following:
=INDEX(B:B,MATCH("Foo",A:A,0))
=INDEX(B:B,MATCH("bar",B:B,0))
Want the above results to return 1 and 2, but obviously based on the table above will get 1 and 0. How can I exclude 0-values from the match?
Filter and sortn is the way to go, but if you did want to do it by classic index/match you'd need
=index(B:B,match(1,(A:A="Bar")*(B:B<>0),0))
try:
=FILTER(B:B, B:B<>0)
if that is not enough you can do:
=INDEX(SORTN(FILTER(A:B, B:B<>0), 9^9, 2, 1, 0),,2)
if even that is still not enough try:
=INDEX(SORT(SORTN(FILTER({A:B, ROW(A:A)}, B:B<>0), 9^9, 2, 1, 0), 3, 1),,2)

Can I count on partition preserving order?

Say I have a sorted Array, such as this:
myArray = [1, 2, 3, 4, 5, 6]
Suppose I call Enumerable#partition on it:
p myArray.partition(&:odd?)
Must the output always be the following?
[[1, 3, 5], [2, 4, 6]]
The documentation doesn't state this; this is what it says:
partition { |obj| block } → [ true_array, false_array ]
partition → an_enumerator
Returns two arrays, the first containing the elements of enum for which the block evaluates to true, the second containing the rest.
If no block is given, an enumerator is returned instead.
But it seems logical to assume partition works this way.
Through testing Matz's interpreter, it appears to be the case that the output works like this, and it makes full sense for it to be like this. However, can I count on partition working this way regardless of the Ruby version or interpreter?
Note: I made implementation-agnostic because I couldn't find any other tag that describes my concern. Feel free to change the tag to something better if you know about it.
No, you can't rely on the order. The reason is parallelism.
A traditional serial implementation of partition would loop through each element of the array evaluating the block one at a time in order. As each call to odd returns, it's immediately pushed into the appropriate true or false array.
Now imagine an implementation which takes advantage of multiple CPU cores. It still iterates through the array in order, but each call to odd can return out of order. odd(myArray[2]) might return before odd(myArray[0]) resulting in [[3, 1, 5], [2, 4, 6]].
List processing idioms such as partition which run a list through a function (most of Enumerable) benefit greatly from parallel processing, and most computers these days have multiple cores. I wouldn't be surprised if a future Ruby implementation took advantage of this. The writers of the API documentation for Enumerable likely carefully omitted any mention of process ordering to leave this optimization possibility open.
The documentation makes no explicit mention of this, but judging from the official code, it does retain ordering:
static VALUE
partition_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arys))
{
struct MEMO *memo = MEMO_CAST(arys);
VALUE ary;
ENUM_WANT_SVALUE();
if (RTEST(enum_yield(argc, i))) {
ary = memo->v1;
}
else {
ary = memo->v2;
}
rb_ary_push(ary, i);
return Qnil;
}
This code gets called from the public interface.
Essentially, the ordering in which your enumerable emits objects gets retained with the above logic.

Parse.com query objects where the key's array value contains any of the elements

on https://parse.com/docs/js_guide#queries-arrays there is an example how to find objects where the key's array value contains each of the elements 2, 3, and 4 with the following:
// Find objects where the array in arrayKey contains all of the elements 2, 3, and 4.
query.containsAll("arrayKey", [2, 3, 4]);
However, I would like to find objects where the key's array value contains at least one (not necessarily all) of the elements 2,3, and 4.
Is that possible?
I'm not positive, but what happens if you try containedIn?
I think if you pass an array, it checks to see if any are contained.
query.containedIn("arrayKey", [2,3,4]);
I know that if you use equalTo with an array key and a singular value, it checks if the value is in the array and returns TRUE. I think this will do something similar and should work. I think it will check if any value in "arrayKey" is in the passed array. If any key object does, it will return the object.
swift 3.0
let Query:PFQuery = PFQuery(className: “className”)
Query.whereKey(“Field Name”, containedIn: array)// [“1”,”2”,”3”];

recursion in prolog - error in base case

I'm trying to write predicate range\3 that takes three parameters the first is the start, the second is the end and return the generated list in the third argument.
E.g rang(1,5,L).
L = [1, 2, 3, 4, 5]
I used this code
range(E,E,[E]).
range(S,E,L):-
S1 is S + 1,
range(S1,E,[S|L]).
But it does not work, when i used trace command to know where is the error i recognized that the base case is useless, I also tried the green cut !in the base case but it does not work range(E,E,[E]),!.
So, if any one knows what is the problem please help me
You're building the list in 'wrong' sense. Consider that when you'll call the base case, it will receive the consed list. How could match a single element list ? Try instead
range(S,E,[S|L]):-
S1 is S + 1,
range(S1,E,L).

Mocking Sort With Mocha

How can I mock an array's sort expect a lambda expression?
This is a trivial example of my problem:
# initializing the data
l = lambda { |a,b| a <=> b }
array = [ 1, 2, 3, 4, 5 ]
sorted_array = [ 2, 3, 8, 9, 1]
# I expect that sort will be called using the lambda as a parameter
array.expects(:sort).with( l ).returns( sorted_array )
# perform the sort using the lambda expression
temp = array.sort{|a,b| l.call(a,b) }
Now, at first I expected that this would work; however, I got the following error:
- expected exactly once, not yet invoked: [ 1, 2, 3, 4, 5 ].sort(#<Proc:0xb665eb48>)
I realize that this will not work because l is not passed as a parameter to l. However, is there another way to do what this code is trying to accomplish?
NOTE: I have figured out how to solve my issue without figuring out how to do the above. I will leave this open just in case someone else has a similar problem.
Cheers,
Joseph
Mocking methods with blocks can be quite confusing. One of the keys is to be clear about what behaviour you want to test. I can't tell from your sample code exactly what it is that you want to test. However, you might find the documentation for Mocha::Expectation#yields (or even Mocha::Expectation#multiple_yields) useful.

Resources