Scala/Java BsonDocument append not working properly - java

I am new to scala and I am trying to create a custom BsonDocument. As far as I read in the documentation here, there's this method append(String key, BsonValue value) that calls the put method internally and I am trying to use it.
The problem is that when I append more than two fields, only the last two get appended. For example if I have a code like this:
var doc = new BsonDocument();
val mapAccounts = user.accounts.map(e => new BsonString(e))
doc.append("$set", new BsonDocument("userName", new BsonString(user.userName)))
.append("$set", new BsonDocument("color", new BsonString(user.color)))
.append("$addToSet", new BsonDocument("accounts", new BsonDocument("$each", new BsonArray(mapAccounts.toList.asJava))))
println(s"The Bson user is $doc")
In this case, I get an output like:
The Bson user is { "$set" : { "color" : "teal" }, "$addToSet" : { "accounts" : { "$each" : ["1"] } } }
As you can see, the userName is not being appended. And it repeats for the last two appended elements if I change the order.
I already tried to use put directly but still got the same result. Also tried to append individually like doc = doc.append(...) and still the same.
What am I missing here?

You cannot have two $set (a BSONDocument is basically a key-value mapping, and appending the same key again just resets it, in the same way a Map.put would).
What you want is
"$set" : {
"color" : "teal",
"username": "Jim"
}

You have to use $set key with both values as the BsonDocument is backed by Map.
doc.append("$set", new BsonDocument("userName", new BsonString(user.userName)).append("color", new BsonString(user.color)))
.append("$addToSet", new BsonDocument("accounts", new BsonDocument("$each", new BsonArray(mapAccounts.toList.asJava))))

Related

Merging two stream operation into one in Java for performance improvement

I have this object
Class A {
int count;
String name;
}
I have a list of my above custom object as below :
List<A> aList = new ArrayList<>();
A a = new A(1,"abc");
A b = new A(0,"def");
A c = new A(0,"xyz");
aList.add(a);
aList.add(b);
aList.add(c);
I will get this list as input in my service. Now based upon some scenario, first I need to set "count" to ZERO for all elements in the list and based on a check with "name" I need to set the count as ONE for a particular name.
This is how I am doing now :
String tempName = "Some Name like abc/def/xyz";
alist.stream().forEach(x -> x.setCount(0));
aList.stream().filter(x -> x.getName().equalsIgnoreCase(tempName))
.findFirst()
.ifPresent(y -> y.setCount(1));
This is doing my job, but I want to know if I can simplify the above logic and use one single stream instead of two and improve the performance by avoiding looping through the list twice.
Just check if the name matches in the first loop:
alist.forEach(x -> x.setCount(x.getName().equalsIgnoreCase(tempName) ? 1 : 0));

How to add specific "key" to an ASN1EncodableVector in BouncyCastle?

So, I asked a similar question before but the issue has since evolved. So, I'll give you a whole overview and then I'll ask the question.
Currently, I am reading from a .yml file and parsing through it. I then store the data in a HashMap<String, ArrayList > that looks something like this:
[{p_table=[p_event/Name, p_fault/Name]},
{s_table=[s_event/Name, s_fault/Name]},
{r_table=[r_event/Name, r_fault/Name]}]
Now, I understand that if I want to create an extension with bouncycastle I first have to add all of my data into an ASN1EncodableVector. What I'm doing is using certificates to tell my IoT things what topics they can subscribe/publish/receive from. Hence, I can do something like this:
while(iterator.hasNext()) {
Entry<String, ArrayList<String>> entry = iterator.next();
ASN1EncodableVector vector = new ASN1EncodableVector
for(String val : entry.getValue()) {
(vector).add(new DERUTF8String(val));
}
allowedTables.add(new DERSequence(vector));
}
This will only add the values from the arraylist eg p_event/Name or p_fault/Name, is there a way for me to specify that those two DERUTF8String(s) belong to the p_table? Or is there some sort of identifier I can use when using the .add method?
So could the code change from something like this:
(vector).add(new DERUTF8String(val));
to:
(vector).add(new aConstructorToIdentifyWhatTheUTF8BelongsTo(entry.getKey()), new DERUTF8String(val));
You can nest sequences, i.e. you can build one DERSequence for each entry as you are doing and then add each of them to an outer ASN1EncodableVector and make a final sequence from that. The inner sequences could contain key/val/val if the number of values is fixed at 2 as in your example. Or you could have yet another sequence to hold the values e.g.:
SEQUENCE {
SEQUENCE {
"p_table",
SEQUENCE {
"p_event/Name",
"p_fault/Name",
}
},
SEQUENCE {
"s_table",
SEQUENCE {
"s_event/Name",
"s_fault/Name",
"s_other/Name",
}
},
// and so on
}

How to add list of items to an ArrayList<String> in java?

I have list of words which I need to load to ArrayList< String >
prefix.properties
vocab\: = http://myweb.in/myvocab#
hydra\: = http://www.w3.org/ns/hydra/core#
schema\: = http://schema.org/
"vocab:" is actually "vocab:" .Slash(\) is used to read colon(:) character because it is special character.
Dictionary.java
public class Dictionary {
public static ArrayList<String> prefix = new ArrayList<>();
static {
Properties properties = new Properties();
InputStream input = null;
input = ClassLoader.getSystemResourceAsStream("prefix.properties");
System.out.println(input!=null);
try {
properties.load(input);
} catch (IOException e) {
e.printStackTrace();
}
Set<Map.Entry<Object, Object>> entries = properties.entrySet();
for(Map.Entry<Object, Object> E : entries)
{
prefix.add(E.getKey().toString());
prefix.add(E.getValue().toString());
}
}
}
In Dictionary.java , ArrayList prefix will have
prefix = [
"vocab:",
"http://myweb.in/myvocab#",
"hydra:",
"http://www.w3.org/ns/hydra/core#",
"schema:",
"http://schema.org/"
]
I am querying some data in another class.
For eg:
public class QueryClass
{
public ArrayList<String> queryResult(String findKey)
{
ArrayList<String> result = new ArrayList<String>();
ArrayList<String> prefix = Dictionary.prefix;
Iterator<String> iterator = prefix.iterator();
while (iterator.hasNext())
{
String currentKey = iterator.next()+findKey;
/**
Here my logic to search data with this currentKey
*/
}
return result;
}
}
Problem :
I want to avoid this method to load from .properties file because there is possibility of odd number of elements can be present while .properties file provide (key,value) pair way to store data.
Reason why I have to load from separate file ? Because In future I will have to add more keywords/String thats why I put it in prefix.properties file.
Is there any alternative way to do this?
Do not re-invent the wheel.
If you can define the file format, then just go for java properties.
You see, the Properties class has a method getProperty(String, String) where the second argument can be used to pass a default value for example. That method could be used in order to fetch keys that don't come with values.
I would be really careful about inventing your own format; instead I would look into ways of re-using what is already there. Writing code is similar to building roads: people forget that each new road that is built translates to maintenance efforts in the future.
Besides: you add string values to a list of strings by calling list.add(strValue). That is all that is to that.
Edit on your comment: when "java properties" are not what you are looking for; then consider using other formats. For example you could be persisting your data in some JSON based format. And then just go for some existing JSON parser. Actually, your data almost looks like JSON already.
I'm not sure why you need to use ArrayList but if you want to pass these property keys/values, there are 2 better ways:
Use Properties itself.
Convert to HashMap.

Avro iterate over GenericRecord

I have a GenericRecord, and want to iterate over the entire collection of key/values. The record is a java data structure that is the equivalent of a plain json string. Eg:
{"key1":"val1","key2":val2",...} but is quite long.
The problem is, I dont know how many key,vals are inside it.
I've tried:
AvroKeyValue<String,String> kv = new AvroKeyValue<>(record);
Iterator<String,String> iterator = new AvroKeyValue.Iterator<String,String>(kv);
But this does not work.
The apache docs about it are here:
http://avro.apache.org/docs/1.7.0/api/java/org/apache/avro/hadoop/io/AvroKeyValue.Iterator.html
Use the GenericRecord's schema to list the available fields
for (Field field : record.getSchema().getFields()) {
String fieldKey = field.name();
System.out.println(fieldKey + " : " + record.get(fieldKey));
}
You can get the number of fields of a GenericRecord with:
record.getSchema().getFields().size();

Change value of multidimensional ArrayList

I need a solution to change the value of an element in a multidimensional ArrayList in Java. This is my ArrayList:
public class Users {
ArrayList<ValidateUser> personer = new ArrayList<ValidateUser>();
public Users() {
personer.add(new ValidateUser("admin", "asdf123", 0.8, "admin"));
personer.add(new ValidateUser("jesper", "ukamm19", 2.5, "user"));
personer.add(new ValidateUser("lars", "lol123", 1.5, "user"));
}
I want to change the double value (0.8) at the user "admin", for example.
This would be done from another a class.
How to do so?
Thanks in advance! :)
As I've stated in my comment, just iterate through the list to find the object. If you're going to do this a lot, consider using a map.
for (ValidateUser user : personer)
if (user.getName().equals("admin"))
user.setNumber(someNumber);
First, note that this is not a multidimensional array, is just a list that holds elements of ValidateUser class object references. Second, you need to access to the element before updating it. You have several ways to accomplish this:
Implement the equals and hashCode methods in your ValidateUser class, then just retrieve the object from your List:
ValidateUser adminUser = personer.get(new ValidateUser("admin", "", 0.8, ""));
adminUser.set...
Note: this looks ridiculous but will work (assuming your equals method only checks by the field that holds this "admin" value.
Navigate through the array and seek for the desired element manually, then update it:
for (ValidateUser user : personer) {
if ("admin".equals(user.getXxx()) {
user.set...
break; //don't forget this!
}
}
Use a different data structure like a Map<String, ValidateUser> to store your data and faster retrieval:
Map<String, ValidateUser> personerMap = new LinkedHashMap<String, ValidateUser>();
personerMap.add("admin", new ValidateUser("admin", ...);
//fill the map with other values...
//if you still want a Collection<ValidateUser> personer variable
Collection<ValidateUser> personer = personerMap.values();
//now check for the desired element
ValidateUser admin = personerMap.get("admin");
if (admin != null) {
admin.set...
}
By comments, your ValidateUser is an immutable object, so you cannot update its fields using setters (because there aren't). So, the best approach here is to use a ListIterator<ValidateUser> instead (not to confuse with Iterator) and replace the element by your modified object. Here's an example:
//the new immutable ValidateUser that will replace the older one...
//set the parameters as needed
ValidateUser newAdmin = new ValidateUser("admin", ...);
ListIterator<ValidateUser> listIterator = personer.listIterator();
while (listIterator.hasNext()) {
ValidateUser validateUser = listIterator.next();
if ("admin".equals(validateUser.getXxx()) {
listIterator.set(newAdmin);
break;
}
}

Categories

Resources