I tried a bunch of framework and other language and I usually feel overwhelm when its time to do something real with what I've learned. So that why I created this site, to write my progress and help some noob like me that stumble into the sames problems.
There is a 70 M lottery draw this week plus 70 one million prizes, I know this is no powerball, but here in Canada we are pretty excited. Even my wife is like: “We need to buy a ticket”. And she bought one with her father because 35M is still an insane amount of money. Well when the draw was 50M or “just” 30M it was still an insane amount of money, enough to change your life (sometime for the worst).
I usually don’t buy lottery tickets, but when I do, it’s for the fuzzy feeling of what I will do if I win the jackpot. I would probably try to figure out what I need from it to generate a decent income each year to live a good life. From there, the rest would be to give away to family, friends and charity and maybe some upgrade (my beat up toyota definitely needs an upgrade). What weird we almost always imagine ourselves what it would be like to win the jackpot but never think about the secondary prize. Winning a million is nice too, but not life changing like 70M.
While putting money in the stock market may seem like playing in a casino, you are still buying an asset (except if you bought Theranos) and while it may go down in value, its value is likely to be higher than your lottery ticket the day after the draw.
Now back to work! I wish you all good luck and if you win don’t forget to give some back to your communities.
Note: There is no tax on lotteries here in Canada, because let’s be honest, lotteries are already a tax on the poor.
In the previous post, we discuss own we change from using mongorepository to using directly mongodb method to access and modify directly the object in db. In this posts we look at code that was deleting with mongorepository and change it to use directly mongodb method with MongoTemplate.
Using MongoTemplate is faster and less likely to have error due to concurrency.
Here a example of the code original code with mongorepository:
@Autowired
private MongoContentRepository mongoContentRepository;
private void removeStudentFromSchool(final Student student) {
Optional<School> optionalSchool = mongoContentRepository.findById(student.getSchoolId());
if (optionalSchool.isPresent()) {
School school = optionalSchool.get();
List<Student> students = school.getStudents();
if (students != null && !students.isEmpty()) {
Optional<Student> optionalStudent = getStudentInSchool(students, student);
optionalStudent.ifPresent(studentInDb -> students.remove(studentInDb));
school.setStudents(students);
mongoContentRepository.save(school);
}
}
}
We had at work a piece of code that was using mongorepository and updating the arrays of the object on a collection instead of using mongodb method directly. The way it was coded had a big issues with concurrency and we were losing update because of it. The problem we had with the way it was updating the array and saving using mongorepository and we are going to fix it using directly mongo queries with MongoTemplate.
Example of code with mongorepository
@Autowired
private MongoContentRepository mongoContentRepository;
private void addStudentToSchool(final Student student) {
Optional<School> optionalSchool = mongoContentRepository.findById(student.getSchoolId());
if (optionalSchool.isPresent()) {
School school = optionalSchool.get();
List<Student> students = school.getStudents();
if (students != null && !students.isEmpty()) {
Optional<Student> optionalStudent = getStudentInSchool(students, student);
optionalStudent.ifPresentOrElse(studentInDb -> {
if (hasStudentChanged(studentInDb, student)) {
students.remove(studentInDb);
students.add(student);
}
}, () -> students.add(student));
school.setStudents(students);
} else {
school.setStudents(List.of(students));
}
mongoContentRepository.save(school);
}
}
There are a lot of things wrong here. It saving the whole object each time, which means if there a student is save in mongodb while another one is being save in a longer transaction, it will override the first update.
Using MongoTemplate
You can prevent that problem, by using directly mongo queries with MongoTemplate Reference: https://docs.mongodb.com/manual/reference/operator/update/positional/
What does that look like in Java : If we take the first query and convert to the equivalent code with MongoTemplate:
public void addStudentInSchool(String objectId, Student student, int index) {
mongoTemplate.updateFirst(
Query.query(Criteria.where("_id").is(objectId)),
new Update().set("student.$[" + index + "]", student), COLLECTION_NAME);
}
For the second query we get:
public void updateStudentInSchool(String schoolId, Student student) {
mongoTemplate.updateFirst(
Query.query(Criteria.where("_id")
.is(objectId)
.andOperator(Criteria.where("student")
.elemMatch(Criteria.where("_id").is(student.getId())))),
new Update().set("student.$", student), COLLECTION_NAME);
}
Note: WP code block seems to mess up the indentation.
End result
If we take our initial code and use our mongo template code we get the following end result.
Let’s update addStudentToSchool with MongoTemplate instead of mongoContentRepository:
private static final String COLLECTION_NAME = "schoolCollection";
private final MongoTemplate mongoTemplate;
public void addStudentInSchool(String schoolId, Student student) {
mongoTemplate.updateFirst(
Query.query(Criteria.where("_id").is(schoolId)),
new Update().addToSet("student", student), COLLECTION_NAME);
}
public void updateStudentInSchool(String schoolId, Student student) {
mongoTemplate.updateFirst(
Query.query(Criteria.where("_id").is(schoolId).andOperator(Criteria.where("student").elemMatch(Criteria.where("_id").is(student.getId())))),
new Update().set("student.$", student), COLLECTION_NAME);
}
private void addStudentToSchool(final Student student) {
Optional<School> optionalSchool = mongoContentRepository.findById(student.getSchoolId());
if (optionalSchool.isPresent()) {
School school = optionalSchool.get();
List<Student> students = school.getStudents();
if (students != null && !students.isEmpty()) {
Optional<Student> optionalStudent = getStudentInSchool(students, student);
optionalStudent.ifPresentOrElse(
studentInDb -> contentCollection.updateStudentInSchool(school.getId(), student),
() -> contentCollection.addStudentInSchool(school.getId(), student));
} else {
contentCollection.addStudentInSchool(school.getId(), student);
}
}
This worked fine for us and we stop having our mongdb update disappearing due to concurrency.
Why did it solve the concurrency issue ?
All $inc $set $unset $push $pushAll $addToSet $pop $pull $pullAll $rename $bit operations are atomic. This mean if multiple $set or $addToSet are done in concurrency all the operations and lock that entry.
For more information on mongodb concurrency: https://stackoverflow.com/questions/6997835/how-does-mongodb-deal-with-concurrent-updates https://docs.mongodb.com/manual/faq/concurrency/
At the beginning of 2020, I wanted to read more than my abysmal 2019 three books total. I’ve already read my 2019 total before the whole 2020 ‘situation’ hit. This made it harder for me to read since I was stuck at home most of 2020 and I usually read in my commute. Nonetheless, I managed to read 12 books last year. Here is the rundown of all the books I’ve read last year, I think I might do a full reviews of some of the non-fiction books later this year, we’ll see.
Stillness is the Key
This is the third installment of Ryan Holiday books on stoicism, the two others being Obstacle is the way and Ego is the enemy.
Sometimes when everything seems to get faster, more stressful and even crazy around us. While some philosophy might say to go with the flow, stoics show us that deliberately slowing down is what you should do. By slowing down, you will notice what usually goes unnoticed and, in a Matrix like slow motion montage, dodge what is unimportant and focus on what truly matter.
If you’re into stoicism or like the obstacle is the way or ego is the enemy, then is book is definitely for you. If not I would recommend reading the Obstacle is the way first, I found that some idea are repeated and Obstacle made more of an impact on me. Note that I say this and I’ve read Ego is the enemy first.
Piège au royaume des ombres
Piège au royaume des ombres is the third installment of the FrenchFantasy Fiction ‘’Les Chevaliers d’Émeraude’’ series. Kira has finally reached an age where she can show what she is capable of and set on an expedition with Wellan, her master and other fellow knights and squire.
I tend not to review too much fiction because the fun of it is to discover the story and reviewing quickly enters spoiler territory whereas non-fiction having a review will help you decide if the book talks about subject matter you want to expand on or not.
Everything is F*cked
From the author of The Subtle Art of Not Giving A F*ck comes a counter-intuitive guide to the problems of hope.
I really liked The Subtle Art of NotGiving A F*ck with his gritty writing and mind opening view so I was really excited when I’ve heard that Mark Manson was writing another book. While I liked the book, I would recommend reading first book before this one, they’re not mutually exclusive and can be read on their own. However, is first book is more packed with information and reflection than this one.
The Power of Creativity
A small book I bought a while ago seems to be part of a series by the same author. It gives some good advice even if it’s mostly stuff I already knew. Advice that is always good to revisit like what your audience demands vs your craft and that you should invest in your side project.
If you follow Seth Godin or read Steven Presfield non fiction you will know most of what he talks about in the book.
Dividend Investing for Everyone
It’s been awhile since I haven’t read a book about investing and I was looking for a way to invest that could create recurring revenue from investing. This book is short and sweet if you already know a bit about investing in general. However, the lack of a table of index is a bit annoying for this review though. He goes on explaining what is dividend investing and how it will generate more money for you that bond and saving account. The most interesting part of the book is when he shows how he picks his own dividend stock, what are his criteria and what type of dividend stock there is.
The Lost Symbol (Fiction)
Note I’ve read this one in french, Le symbole perdu by Dan Brown.
The sequel to the famous Da Vinci Code, we follow again professor Robert Langdon, this time involved in a plot linking the Freemason and the founding father.
I won’t spoil you much, if you liked Da Vinci Code you will like this one. I follow the same formula of his last book, a little too much in my taste. I had the impression that in some part of the book I was just rereading Da Vinci code, but except for being in Paris we’re in Washington.
Conspiracy
Another book from Ryan Holiday, this time not really the kind of book we are you to get from him. Holiday dissects the involvement of Billionaire Peter Tiel in helping bring down publisher Gawker by helping Hulk Hogan in his trial against them without Hogan knowing it.
Tiny MBA
When I saw one of stackingthebricks author wrote a book I immediately ordered it. Unfortunately it didn’t live up to the level of their amazing long form post they usually do. It’s more like someone was following every business thought leader and CEO and collected all theirs insightful tweets then put them in a book. While some can really be inspiring and insightful, you kind of eager to want more after going through it.
Stop People Pleasing
Stop people pleasing is about how to balance helping other people and not being a yes man that other people don’t value your time. I think I got this one through a newsletter that sells books for one dollar and sometimes gives freebies.
The main takeaway of the book: is to learn what you want to say yes to and no to, common phrases to use to imply a no without saying it and stay away from manipulators.
Live of the Stoics
A third book from Ryan Holiday this year, this one is more in the vein of the Daily stoic I’ve read through in 2018 (http://cjacques.me/the-20-books-ive-read-in-2017/). He goes through the list of all the major stoics from the founder of stoicism Zeno to philosopher king (emperor) Marcus Aurelius. I’m kind of a sucker for this book because it’s a mix of things I like to read about, Roman history and philosophy. If you’re interested in one or both of these subjects it’s definitely a good read.
The Art of Learning
Josh Waitzkin, former chess prodigy and martial Tai Chi World champion, walk us thought is life and how he learned to master those two disciplines in this self-help memoir. While Waitzkin can kinda sometime sound like a d*&k and that can be off putting at time, he goes deep into what was going in his mind in different key learning points in his life. While being world class might not be your thing, understanding what got them to that point can definitely be useful.
Learn Like a Polymath
I got this one on a kindle sale. It might be a good idea to do the same. There are some tips and tactics that I’ve probably read in other books like space your learning, deconstruct what you learn to a manageable piece. However, instead of focusing these on the broader concept of learning the author apply them at connecting knowledge between disciplines.
If you’re interested in learning in general or searching for a way to connect knowledge between your field of interest you will probably find some good nuggets here and there.
This is old news, but having work with big bank and insurance companies they tend to stick with the old version of java and they’re underlying libraries, I’ve finally been expose to new ways of testing exception with junit.
I use to test exception by adding an expected right after the @Test annotation. @Test(expected = NullpointerException)
However they’re many other ways to do that with only junit5 and mockito
Option 1: Stinking with junit4 you can define an expected exception:
Option 2: JUnit5 introduces the assertThrows method for asserting exceptions. This takes the type of the expected exception and an executable functional interface where we can pass the code under test through a lambda expression.
Option 3: Using mockito & assertJ: If you’re using mokito and assertJ in your test already, there an another clean way to do it. It’s so far my favorite.
given(xxx).willThrow(new MyException())
when {() -> myService.doSomething()}
then(caughtException()).isInstanceOf(MyException.class)
They are other way to do it just with mockito using doThrow, thenThrow and spy which are explained here.
We recently added a feature that allowed direct modification on our content for some client vs using the same path (queues) that the batched content is using. Our user were really happy with it, but after our team implemented the new feature, we started having some LazyInitializationException.
exception for that problem
org.hibernate.LazyInitializationException: could not initialize proxy [com.company.dbservice.model.Image#1004291143] - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:170) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:310) at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45) at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95) at com.company.dbservice.model.Image$HibernateProxy$aWvtUHZY.getContent(Unknown Source) at com.company.dbservice.search.resolver.ImageResolver.getImageUrl(ImageResolver.java:50) at com.company.dbservice.search.resolver.ImageResolver.getContentImageUrl(ImageResolver.java:34) ….. at com.company.dbservice.aop.ReadOnlyRouteInterceptor.proceed(ReadOnlyRouteInterceptor.java) at com.company.dbservice.sqs.reader.MessageHandler.processMessage(MessageHandler.java) at com.company.dbservice.sqs.reader.MessageHandler.receiveMessage(MessageHandler.java)
After looking at, it those errors seem to keep happening to object in object in object that were not needed anyway so the unnecessary acces were remove to prevent processing these object. It a solution you might say but this don’t solve the underlying issue. Here an example of the table surrounding the object.
When I look in new relic the next they I notice that the error were still happening.
This time it was for an object that was needed. After some digging, I found out that the new endpoint for the direct route didn’t have the @Transaction annotation. It was calling the same code to save and process the object the batch part was using though.
For those who don’t know @Transactional tells spring to create a proxy around the object. The proxy intercepts calls to the object from other objects. The proxy does not intercept calls within the object.
So to fixed we had to add @Transactional on the method that is invoked from “outside” and it fixed the problem.
Once upon a time an engineer with years of experience encounter something so basic he doesn’t understand why he didn’t encounter it sooner.
This week we had a strange error in our log stating that should have a null values in a map.
After some digging in the service where that log was found, it seems to be not coming from us…
But why can’t a map have a null values. It’s easy to understand why we can have a null key value but why can’t the value be null?
Well it make sense, why keep an empty record in a key when you could just remove the key and it’s not in the map it means it’s null. That free up the map to make it more efficient.
If you straight up initialize a map with Map.of() and include a null value, it will throw a null pointer exception. However, if you do it with a HashMap it will work fine. You can even have null key that way. Most modern implementation of map won’t allow null in their keys or values.
Allowing null in collections is by now seen as a design error. This has a variety of reasons. A good one is usability, where the most prominent trouble maker is Map::get. If it returns null, it is unclear whether the key is missing or the value was null. Generally speaking, collections that are guaranteed null free are easier to use. Implementation-wise, they also require less special casing, making the code easier to maintain and more performant.
Null elements, keys, and values will be disallowed. (No recently introduced collections have supported nulls.) In addition, prohibiting nulls offers opportunities for a more compact internal representation, faster access, and fewer special cases.
Since HashMap was already out in the wild when this was slowly discovered, it was too late to change it without breaking existing code but most recent implementations of those interfaces (e.g. ConcurrentHashMap) do not allow null anymore and the new collections for the factory methods are no exception.
So disallowing null had some technical reason but it was also done to improve the robustness of the code using the created collections.
I know 2019 is like light years away, but I haven’t had the time to write and you will see that I haven’t had much time to read either in 2019. Having a baby who doesn’t sleep will do that to you.
How to be miserable
I got this book after watching CGP Grey’s “7 Ways to Maximize Misery“. I recommend both the video and the book – the video is based on seven of the 40 strategies given in the book.
Excellent reverse psychology to avoid doing the list of actions that will make you miserable. Some of the excerpts had me in tears laughing because I could see what I had been doing without really realizing it. After starting that list I’ve read some part of the book again, I definitely will put that book in my re-read list.
The gift of failure: How the Best Parents Learn to Let Go So Their Children Can Succeed
In our overprotective society, Jessica Lahey warns us that shielding our kids too much from mistakes is not actually helping. We need to let them make their own mistakes in order to learn and be ready later in life. Failure should not be looked at as a negative, but rather a stepping stone to success.
A great book that state the obvious, today kids are so shielded from everything they don’t get to experience what it’s like to live and this might have some dire consequences when they get older and need to leave the nest. It’s helped me worry less about grades (a little less) and everything around them and let them be kids, let them experience the world with their genuine curiosity and thirst for knowledge.
The storm before the storm
From the triumph to the fall of the Roman republic this book goes into the bloody details of what happened during that time.
I was a big fan of Mike Duncan podcast on Roman history and I love how he tells the stories of these Roman citizens. This book is no different, I couldn’t put the book down near the end even though I knew where this was heading and the implication of what would come next.
If you’re a fan of Roman history I highly recommend it. If you want to understand the parallel between that period in history and our current US event you can checkout his interview on Ryan Holiday Daily Stoic podcast.
I was having that weird error when I was working with mapstruct, turn out that the default java version for mapstruct is java 6. Since our project was in java 11 I was getting the error “Constructor XXX in enum XXX cannot be applied to given types”.
My starting config was :
<dependencies> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>${org.mapstruct.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <release>${java.version}</release> <source>1.6</source> <!-- or higher, depending on your project --> <target>1.6</target> <!-- or higher, depending on your project --> <annotationProcessorPaths> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${org.mapstruct.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
After some research, turn out there a mapstruct version for java 8 and more. You can use the following maven to configure mapstruct for java 8 or higher :
<dependencies> <dependency> <groupId>org.mapstruct</groupId><!-- use mapstruct-jdk8 for Java 8 or higher --> <artifactId>mapstruct-jdk8</artifactId> <version>${org.mapstruct.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <release>${java.version}</release><!-- or higher, depending on your project --> <source>1.11</source> <target>1.11</target> <annotationProcessorPaths> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${org.mapstruct.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
Let me know if you had that problem and if this helped you.
Perenial Seller: The Art of Making and Marketing Work that Lasts
I’m subscribed to Ryan Holiday newsletter and read is latest work on stoic philosophy (Obstacle if the way, Ego is the Enemy, Daily stoic) and when he usually get a book out I buy it. In his book, Perenial Seller, we study on what it takes to create timeless, lasting work. Ignore the trends of the day to focus on what matters and what will lead to real impact. If you want to write, produce, or build something amazing, read this book.
Principle by Ray Dalio
A big book (600+ page) about one of the great Investor of our time, Ray dalio and is life and work principle.
Ray Dalio is the founder and co-chairman of Bridgewater Associates, which, over the last forty years, has become the largest and best performing hedge fund in the world. Mr. Dalio let us dive in in this part memoir, part how-to guide and show us what priciple guide is life and business. My main take away of this book is to pratice radical honesty, which is the ability to open oneself to appreciate pointed criticism and use it to improve and have a sense of humility and introspection. There 3 main part in the book, is biography to understand where he come from, is life principle and is work principle that he used to build Bridgewater.
Factfulness
I bought this book after seeing Bill Gates recommendation, and my god this book is eye opening. Hans Rosling explains how what we see on the media, our preconceptions and statistical illiteracy make us believe in the wrong worldview. Surprise, surprise, other countries aren’t like they used to be 50 years ago. The book carefully break the 10 most important sources of bias and misconceptions and give ways to avoid them. Forget about the “Developping world”, most countries today would be considered developed. We should categorize the developments in four income level instead and at the end of the book there are examples on what different in the life of a level one vs other level of income. Each countries have average income level that help us determine the problem of the population and the degree of extremes poverty.
Recent Comments