eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

30
Wrap your Database in Java 8 Per-Åke Minborg Carina Dreifeldt Malin Weiss Emil Forslund

Transcript of eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Page 1: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Wrap your Database in Java 8

Per-Åke MinborgCarina DreifeldtMalin WeissEmil Forslund

Page 2: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Speedment

Company• Company started 2010• Speedment customers:

Fintech, manufacturing, media, telecom, transport, retail, publishing and entertainment.

•Office Palo Alto, California Gothenburg, Sweden

Open Source Project• Apache 2, started 2015• Fully Java 8 compatible• www.speedment.org, GitHub

Page 3: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Bio Per-Åke Minborg• Master of Science Sweden• Inventor and Serial Entrepreneur• Java >15 years• Speaker at Meetups and Javaforums around

the globe, (Java-One 2015)• Java 8 and OpenSource blogger

Page 4: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Speedment Started With A Problem

Page 5: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Do You Recognize the following?• When coding it is complex to connect to a database…..

…then you use an ORM

Source: ZeroTurnaround

Page 6: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

SOME PROS of Using an ORM:• You can work with a relational database as if it were object oriented.• Increased productivity.• Provides a certain degree of abstraction (you can replace your

DBMS).

Page 7: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

SOME CONS of Using an ORM:• Slows down the application• You must still write SQL, HQL, et. al• You can not access all Java 8 features

Page 8: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

How to skip these ORMs and work with Pure Java 8

• Lambda expressions• Stream API• Optional

Page 9: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Lambda Expressions• Anonymous methods can be expressed using the new ”->”

syntax

Deck deck = Deck.createShuffledDeck();List<Card> cards = deck.findWhere(card -> card.isHeart() && card.getValue() > 10);

Page 10: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Lambda Expressions• Any single-method interface can be implemented using lambda • can be annoted with @FunctionalInterface to show this

@FunctionalInterfaceinterface VectorOperation { Vector apply(Vector a, Vector b);}

VectorOperation plus = (a, b) -> new Vector( a.getX() + b.getX(), a.getY() + b.getY());

Page 11: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Lambda Expressions Functional pointers can also be used with the ”::” syntax

Set<Hero> warriors = typicalParty.where(Hero::isWarrior);…interface Hero { boolean isWarrior();}…class Party { private Set<Hero> heroes;

public Set<Hero> select(Predicate<Hero> selector) { … }}

Page 12: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Stream API• Streams are a modern way of working with groups of elements• Defining a stream is always an O(1)-operation• The elements are only traversed when used• Streams can often be executed in parallell

Page 13: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Stream API• Example: Get the average strength of all warriors with axes in the

party

Set<Hero> party = … ;int averageStrength = party.stream() .filter(hero -> Archtype.WARRIOR == hero.getArchtype()) .filter(hero -> Weapon.AXE == hero.getWeapon()) .mapToInt(Hero::getStrength) .average();

By replacing .stream() with .parallelStream() the operation can be performed using multiple processors

Page 14: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Optional• Null is often described as ”The Billion Dollar Mistake” of modern

programming languages• Using the Optional-interface we can now describe a value that might or

might not be present in a null-safe way

String getEmailAddress() { // Is always defined return this.emailAddress;}

Optional<String> getFirstname() { // Might not exist return Optional.ofNullable( this.firstname );}

Page 15: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Optional• You can work with Optionals in the same way you work with

streamsOptional<String> firstname = getFirstname() .filter(fn -> fn.length() > 3) .filter(fn -> fn.charAt(0) == 'A');

• When the value of an optional is retreived, you can handle every caseString absoluteFirstname = firstname.getOrDefault("Anonymous");

Page 16: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Speedment APIEmbrace Java 8 paradigm with stream(), filter(), sort(), limit() etc.

Forget about SQL, JQLFocus on Business Logic

Focus on what is fun

Develop a Database app within hours

No Query API

Stick to Java 8

Page 17: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Initialization

Speedment speedment = new HelloSpeedment().build();

Manager<Hare> hares = speedment.managerOf(Hare.class);

Page 18: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Persistence

Hare spire = hares.newInstance() .setName(“Spire") .setColor(“Blue") .setAge(1) .persist();

Page 19: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Querying

List<Hare> oldHares = hares.stream() .filter(h -> h.getAge() > 8) .collect(toList());

Page 20: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Optimised Predicate Look-up

Optional<Hare> spire= hares.stream() .filter(Hare.NAME.equal(“Spire")) .findAny();

Page 21: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Entities are Linked

Optional<Carrot> carrot = hares.stream() .filter(Hare.NAME.equal(“Spire”)) // Carrot is a foreign key. .flatMap(Hare::findCarrots) .findAny();

Page 22: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Multi-threading

hares.stream() .parallel() .filter(h -> humans.stream() .filter(Human.NAME.equal(h.getName())) .anyMatch() ).forEach(System.out::println);

Page 23: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Configuration• Groovy script like Gradle• Config file is placed relative to the POM

dbms { schema { table { name = ”hare"; column { name = "name"; } } }}

Page 24: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Ease of use:• Single Maven dependency in POM • Maven plugin for code generation/GUI• Maven targets• Works with Gradle• GUI to automatically derive the groovy config file from existing

databases

Page 25: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Conclusions• Code generation, No boiler plate code• Focus on the problem at hand!• Forget about the database• Use Java 8

Page 26: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Workshop: Social network We will focus on the server-side

MySQL database

Server application

Client GUI

http

Page 27: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Workshop: Social NetworkThe goal is to add features:

• Register• Login• Upload pictures

Page 29: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

Insane Mode...Want fast development + super duper fast execution ?

Speedment Enterprise Solution provides extreme performance

Page 30: eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015

We are Where You are!• www.speedment.org

• GitHub: https://github.com/speedment/speedment

• Twitter: @Pminborg

• Mail: [email protected]

• Blog: minborgsjavapot.blogspot.com

• Silicon Valley