Merge 2 Immutable JS Ordered Map objects - react-native

I am using Immutable JS as a redux store for my React Native App. I have 2 Ordered Map which is a Keyed Collection of Lists. When I try to merge these 2 Ordered Maps, if the keys overlap, then the data is being overwritten.
For eg: Consider I have 2 Ordered Maps with dates as their keys. When I merge them, if both of them have keys with the same date, the data of that key will be replaced. How can I concatenate them without losing data?
OrderedMap1: {
'21-07-2017': List(10),
'22-07-2017': List(10),
'23-07-2017': List(10),
'24-07-2017': List(10)
}
OrderedMap2: {
'24-07-2017': List(5)
}
When I try to merge them, the data # key '24-07-2017' gets replaced
OrderedMap1.merge(OrderedMap2) gives
{
'21-07-2017': List(10),
'22-07-2017': List(10),
'23-07-2017': List(10),
'24-07-2017': List(5)
}
I have tried concat(), merge() and mergeDeep() methods

Maybe http://facebook.github.io/immutable-js/docs/#/Map/mergeWith is what you're looking for. In the merger function you can concat the lists.
According the the provided example for mergeWith it should look like this, I guess:
OrderedMap1.mergeWith(
(dates1, dates2) => dates1.concat(dates2),
OrderedMap2
)

Related

Combine lists of 2 different objects into list of single object using Java 8 Stream API

I have two lists as below:
List ids = Arrays.asList(1,2,3);
List reps = Arrays.asList("abc","pqr","xyz");
Now I want to create list of Prediction objects with values mapped from above two lists in sequence like below:
List results = [ Prediction(1,"abc") , Prediction(2,"pqr"), Prediction(3,"xyz") ]
class Prediction {
int id;
String rep;
}
How this can be done using Java8 Stream API.
The operation you described is called zipping.
If you are sure the lists are evenly of length, you could do something like:
IntStream.range(0, ids.size())
.mapToObj(i -> new Prediction(ids.get(i), reps.get(i)))
.toList()
Assuming the Prediction class has a constructor like that...

Merging data from different graphql resolvers in vue.js client side for simple outputting

I do query cars from an api with a single query but two resolvers (listing and listings)(hopefully resolver is the right name for it). One car I get by the id via listing and the other cars I get without filters by listings. The resolvers output the data i a little different structure on the server-side but I do get the same fields just at different „places“. I want to merge the structure in order to get a single array I can simply loop over in vue.js. For the apicalls I do use vue-apollo.
Couldn't find any information to merge data client-side inside graphqlqueries. All I found is about handling it serverside with resolvers but it's an api I do not own.
Is it possible with graphql or do I have to merge it inside my vuecomponent and if so what would be the best way to do so?
The output will be a grid of cars where I show the car of the week (requested by id) together with the newest cars of the regarding cardealer.
Full screenshot including response: https://i.imgur.com/gkCZczY.png
Stripped down example with just the id to show the problem:
query CarTeaser ($guid: String! $withVehicleDetails: Boolean!) {
search {
listing(guid: $guid){
details{
identifier{
id #for example: here I get the id under details->identifier
}
}
}
listings( metadata: { size: 2 sort:{ field: Age order: Asc}}) {
listings{
id #here it's right under listings
details{
…
}
}
}
}
}
}
Ideally you're right, it should be handled server-side, but if it's not your API the only solution is to manipulate the data on the client side, meaning in your component.
It's probably a lot simpler to leave the listings array untouched and to just merge the listing element with it, like this for instance:
// assuming 'search' holds the entire data queried from the api
const fullListing = [
// car of the week, data reformatted to have an identical structure as
// the 'other' cars
{
id: search.listing.details.identifier.id,
details: {
vehicle: search.listing.details.vehicle,
},
},
...search.listings.listings, // the 'other' cars
]

RavenDB: If I'm creating an index with Ids, should I specify any index options?

From RavenDB's documentation:
The indexes each RavenDB server instance uses to facilitate fast
queries are powered by Lucene, the full-text search engine.
Lucene takes a Document , breaks it down into Fields , and then splits
all text in a Field into tokens ( Terms ) in a process called
Tokenization . Those tokens are what will be stored in the index, and
be later searched upon.
{...}
After the tokenization and analysis process is complete, the resulting
tokens are stored in an index, which is now ready to be search with.
{...}
The default values for each field are FieldStorage.No in Stores and
FieldIndexing.Default in Indexes.
Setting FieldIndexing.No causes values to not be available in where
clauses when querying (similarly to not being present in the original
projection). FieldIndexing.NotAnalyzed causes whole properties to be
treated as a single token and matches must be exact, similarly to
using a KeywordAnalyzer on this field. The latter is useful for
product Ids, for example. FieldIndexing.Analyzed allows to perform
full text search operations against the field. FieldIndexing.Default
will index the field as a single term, in lower case.
As I understand it, to create a RavenDB index, you simply need to specify the Map propertly, like the following:
public class PlayersIndex : AbstractIndexCreationTask<Player>
{
public PlayersIndex()
{
Map = players => from doc in players
select new { doc.PlayerId, doc.TeamId, doc.PositionId };
}
}
Here is my question:
If you assume that PlayerId is a Guid, TeamId is an int, and PositionId is an enum, should I:
Refrain from specifying any indexing options?
Configure each field as FieldIndexing.NotAnalyzed?
In other words, should I entertain the idea of specifying the fields like the following?
public class PlayersIndex : AbstractIndexCreationTask<Player>
{
public PlayersIndex()
{
Map = players => from doc in players
select new { doc.PlayerId, doc.TeamId, doc.PositionId };
Indexes.Add(x => x.PlayerId, FieldIndexing.NotAnalyzed);
Indexes.Add(x => x.TeamId, FieldIndexing.NotAnalyzed);
Indexes.Add(x => x.PositionId, FieldIndexing.NotAnalyzed);
}
}
Jim,
For your needs, you aren't going to have to specify any indexing options.

RavenDB Index created incorrectly

I have a document in RavenDB that looks looks like:
{
"ItemId": 1,
"Title": "Villa
}
With the following metadata:
Raven-Clr-Type: MyNamespace.Item, MyNamespace
Raven-Entity-Name: Doelkaarten
So I serialized with a type MyNamespace.Item, but gave it my own Raven-Entity-Name, so it get its own collection.
In my code I define an index:
public class DoelkaartenIndex : AbstractIndexCreationTask<Item>
{
public DoelkaartenIndex()
{
// MetadataFor(doc)["Raven-Entity-Name"].ToString() == "Doelkaarten"
Map = items => from item in items
where MetadataFor(item)["Raven-Entity-Name"].ToString() == "Doelkaarten"
select new {Id = item.ItemId, Name = item.Title};
}
}
In the Index it is translated in the "Maps" field to:
docs.Items
.Where(item => item["#metadata"]["Raven-Entity-Name"].ToString() == "Doelkaarten")
.Select(item => new {Id = item.ItemId, Name = item.Title})
A query on the index never gives results.
If the Maps field is manually changed to the code below it works...
from doc in docs
where doc["#metadata"]["Raven-Entity-Name"] == "Doelkaarten"
select new { Id = doc.ItemId, Name=doc.Title };
How is it possible to define in code the index that gives the required result?
RavenDB used: RavenHQ, Build #961
UPDATE:
What I'm doing is the following: I want to use SharePoint as a CMS, and use RavenDB as a ready-only replication of the SharePoint list data. I created a tool to sync from SharePoint lists to RavenDB. I have a generic type Item that I create from a SharePoint list item and that I serialize into RavenDB. So all my docs are of type Item. But they come from different lists with different properties, so I want to be able to differentiate. You propose to differentiate on an additional property, this would perfectly work. But then I will see all list items from all lists in one big Items collection... What would you think to be the best approach to this problem? Or just live with it? I want to use the indexes to create projections from all data in an Item to the actual data that I need.
You can't easily change the name of a collection this way. The server-side will use the Raven-Entity-Name metadata, but the client side will determine the collection name via the conventions registered with the document store. The default convention being to use the type name of the entity.
You can provide your own custom convention by assigning a new function to DocumentStore.Conventions.FindTypeTagName - but it would probably be cumbersome to do that for every entity. You could create a custom attribute to apply to your entities and then write the function to look for and understand that attribute.
Really the simplest way is just to call your entity Doelkaarten instead of Item.
Regarding why the change in indexing works - it's not because of the switch in linq syntax. It's because you said from doc in docs instead of from doc in docs.Items. You probably could have done from doc in docs.Doelkaartens instead of using the where clause. They are equivalent. See this page in the docs for further examples.

Data structure to use in Sencha Touch similar to Vector in Blackberry

I am a beginner to sencha Touch, basically i am a blackberry developer. Currently we are migrating our application to support Sencha Touch 1.1. Now i have some business solutions like i want to store the selected values in the local database. I mean i have multiple screens where, Once the user selects a value in each of the screen the data should save in the below following format.
[{'key1': "value1", 'key2': "value2", 'key3': "value3" ,'key4': "value4", 'key5': "value5"}]
1. First, the values need to be saved in key value pairs
2. The keys should play the role of primary key, key shouldn't be duplicated.
3. Should be available till the application life cycle or application session, don't need to save the data permanently.
I have come across the concepts like LocalStorageProxy, JsonStore and some others. I don't understand which one i can use for my specific requirements.
May be my question is bit more confusing. I have achieved the same using vector, in Blackberry Java so any data structure similar to this could help me. Need the basic operations like
Create
Add
Remove
Remove all
Fetch elements based on key
Please suggest me some samples or some code snapshots, which may help me to achieve this.
Edit: 1
I have done the changes as per #Ilya139 's answer. Now I am able to add the data with key,
// this is my Object declared in App.js
NSDictionary: {},
// adding the data to object with key
MyApp.NSDictionary['PROD'] = 'SONY JUKE BOX';
//trying to retrieve the elements from vector
var prod = MyApp.NSDictionary['PROD'];
Nut not able to retrieve the elements using the above syntax.
If you don't need to save the data permanently then you can just have a global object with the properties you need. First define the object like this:
new Ext.Application({
name: 'MyApp',
vectorYouNeed: {},
launch: function () { ...
Then add the key-value pairs to the object like this
MyApp.vectorYouNeed[key] = value;
And fetch them like this
value = MyApp.vectorYouNeed[key];
Note that key is a string object i.e. var key='key1'; and value can be any type of object.
To remove one value MyApp.vectorYouNeed[key] = null; And to remove all of them MyApp.vectorYouNeed = {};