RxSwift - order of emit on PublishSubject - ios

I know, that all types of Rx subjects can get elements in subscribe not ordered correctly, eg. if I send three elements in order 1,2,3, there is an option to get it on this order: 1,3,2.
I wonder, is there a way to force the order of emitted elements the same at the start and at the end?

Only if you update it from different threads. Use .serialize() to ensure correct order.

Related

Join of of multiple streams with the Python SDK

I would like to join multiple streams on a common key and trigger a result either as soon as all of the streams have contributed at least one element or at the end of the window. CoGroupByKey seems to be the appropriate building block, but there does not seem to be a way to express the early trigger condition (count trigger applies per input collection)?
I believe CoGroupByKey is implemented as Flatten + GroupByKey under the hood. Once multiple streams are flattened into one, data-driven trigger (or any other triggers) won't have enough control to achieve what you want.
Instead of using CoGroupByKey, you can use Flatten and StatefulDoFn that fills an object backed by State for each key. Also in this case, StatefulDoFn would have the chance to decide what to do when stream A has 2 elements arrived but stream B doesn't have any element yet.
Another potential solution that comes to mind is (a stateless) DoFn that filters the CoGBK results to remove those that don't have at least one occurrence for each joined stream. For the end of window result (which does not have the same restriction), it would then be necessary to have a parallel CoGBK and its result would not go through the filter. I don't think there is a way to tag results with the trigger that emitted it?

Representing a list of items in Neo4j

Suppose you have a list of items (instructions in a function, posts on a blog, episodes in a TV series etc) that need to be kept in order, what is the recommended way to store them in Neo4j? Two possibilities that come to mind:
Assuming the items don't already have a suitable property for sorting by, assign them incrementing sequence numbers.
Use a linked list of nodes.
Which of these is typically recommended? Or is there a third option I'm missing?
Use a linked list.
Sequence numbers still have to be sorted, which is unnecessary overhead. And to do the sort, neo4j has to iterate through every node in the sequence, even if you are only interested in a small part of the sequence.

How the get all sets keys in redis sorted by its SCARD?

I'm using redis for storing something like an inbox. Each key has the messages for a particular group.
And in rails I need to fetch it and bring the groups sorted by the messages count.
user:1:inbox:group:1: a, b, c
user:1:inbox:group:2: a, b
user:1:inbox:group:3: a
SCARD user:1:inbox:group:1 = 3
SCARD user:1:inbox:group:2 = 2
SCARD user:1:inbox:group:3 = 1
Again, I want to get all the group ids, in this case: 1, 2, 3, but they have to be in DESC order by its set count of elements. In other words, the groups with more messages will be in the first of the list.
I know I can do:
REDIS.keys("user:1:inbox:group:*")
and it returns an array of these keys. But can't figure if its possible or the right way to achieve that...
Any help and thoughts are welcome.
Thanks!
IMPORTANT: don't use the KEYS command - it is meant for non-production purposes only. See the doc page, specifically:
Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code.
Additionally, KEYS does not guarantee the order of results - as you've found out yourself :)
There are three possible approaches that I can suggest to address the challenge:
Sorted Sets: You can use a sorted set to store each user's inbox groups and and set each group's score to the cardinality/count (naively, ZADD user:1:inbox:groups 3 "1" 2 "2" 1 "3"). You can then use ZRANGE/ZREVRANGE and such to get back the results sorted.
SORT Command: Redis' SORT is quite powerful. For example, you can use a list/set for each user's groups and keep the weights (cardinality) in separate keys. Once done, you can retrieve the list with something like:
SORT user:1:inbox:groups DESC BY user:1:inbox:group_card_*
Do It Yourself: Sometimes it is simpler/more effective to perform the sort outside the database. Of course, this is dependent on your data model and requirements, but note that both previous suggestions require Redis space and compute power, whereas the DIY approach can delegate some of the cost to the app/client.

Choose properties to be COLLECTed

How can I choose which properties to be collected in a Cypher COLLECT statement?
I can do COLLECT([profile.name, profile.email]) but then I don't get the properties names.
Say I have ProfileA which is connected to several ProfileB's, I'd like to have returned ProfileA and a collection of ProfileB's where ProfileA-->ProfileB, but only ProfileB.name and ProfileB.email.
At the moment there's no support for literal maps in collections, so we can't build a collection of maps, which it sounds like you want to be able to do.
Your idea of passing a collection to collect should work, and you'll get a collection of collections where the name is always first and the email is always last.
Another option is to do collect(profile.name) as names, collect(profile.email) as emails and have two collections.
Or, you could just have the full nodes. Sorry that there's not a better way (AFAIK)!

How to get the uncommon elements of two linked list?

Given two linked lists of integers. I was asked to return a linked list which contains the non-common elements. I know how to do it in O(n^2), any way to do it in O(n)?
Use a hash table.
Iterate through the first linked list, entering the values you come across into a hash table.
Iterate through the second linked list, adding any element not found into the hash table into your list of non-common elements.
This solution should be O(n), assuming no collisions in the hash table.
create a new empty list. have a hash table and populate it with elements of both lists. complexity n. then iterate over each list sequentially and while iterating, put those elements in the new list which are not present in the hash table. complexity n. overall complexity=n
If they're unsorted, then I don't believe it is possible to get better than O(n^2). However, you can do better by sorting them... you can sort in reasonably fast time, and then get something like O(nlogn) (I'm not certain that's what it would be, but I think it can be that fast if you use the right algorithm).

Resources