Skip to content

Commit

Permalink
#1: Code improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenetics committed Jan 7, 2017
1 parent 6caedb5 commit 9040610
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 24 deletions.
42 changes: 40 additions & 2 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/DAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package io.jenetics.jpx.jdbc;

import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

Expand All @@ -35,6 +36,9 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.jenetics.jpx.jdbc.SQL.ListMapper;
import io.jenetics.jpx.jdbc.SQL.OptionMapper;

/**
* Abstract DAO class
*
Expand All @@ -55,7 +59,7 @@ abstract class DAO {
_conn = conn;
}

<T> T dao(final Function<Connection, T> create) {
<T> T with(final Function<Connection, T> create) {
return create.apply(_conn);
}

Expand Down Expand Up @@ -118,6 +122,31 @@ static <T, K> List<Stored<T>> put(
return result;
}

static <A, B> Map<B, Long> set(
final List<A> values,
final ListMapper<A, B> mapper,
final SQL.Function<List<B>, List<Stored<B>>> set
)
throws SQLException
{
final List<B> mapped = values.stream()
.flatMap(v -> mapper.apply(v).stream())
.collect(Collectors.toList());

return set.apply(mapped).stream()
.collect(toMap(Stored::value, Stored::id, (a, b) -> b));
}

static <A, B> Map<B, Long> set(
final List<A> values,
final OptionMapper<A, B> mapper,
final SQL.Function<List<B>, List<Stored<B>>> set
)
throws SQLException
{
return set(values, mapper.toListMapper(), set);
}

/**
* Reads the auto increment id from the previously inserted record.
*
Expand All @@ -142,12 +171,21 @@ static <A, B> List<B> map(final List<A> values, final Function<A, B> mapper) {
}

static <A, B> List<B> flatMap(
final List<A> values,
final Function<A, List<B>> mapper
) {
return values.stream()
.flatMap(v -> mapper.apply(v).stream())
.collect(toList());
}

static <A, B> List<B> flatMapOption(
final List<A> values,
final Function<A, Optional<B>> mapper
) {
return values.stream()
.flatMap(v -> mapper.apply(v).map(Stream::of).orElse(Stream.empty()))
.collect(Collectors.toList());
.collect(toList());
}

}
18 changes: 17 additions & 1 deletion jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import io.jenetics.jpx.Link;

Expand Down Expand Up @@ -148,12 +150,26 @@ public List<Stored<Link>> put(final List<Link> links) throws SQLException {
: DAO.put(
links,
Link::getHref,
list -> selectByHrefs(map(list, Link::getHref)),
//list -> selectByHrefs(map(list, Link::getHref)),
values -> select(values, Link::getHref, this::selectByHrefs),
this::insert,
this::update
);
}

private <A, B> List<Stored<B>> select(
final List<B> values,
final Function<B, A> mapper,
final SQL.Function<List<A>, List<Stored<B>>> select
)
throws SQLException
{
final List<A> keys = values.stream()
.map(mapper)
.collect(Collectors.toList());

return select.apply(keys);
}

/* *************************************************************************
* SELECT queries
Expand Down
190 changes: 190 additions & 0 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/MetadataDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* Java Genetic Algorithm Library (@__identifier__@).
* Copyright (c) @__year__@ Franz Wilhelmstötter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author:
* Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
*/
package io.jenetics.jpx.jdbc;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toMap;

import java.sql.Connection;
import java.sql.SQLException;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;

import io.jenetics.jpx.Bounds;
import io.jenetics.jpx.Copyright;
import io.jenetics.jpx.Link;
import io.jenetics.jpx.Metadata;
import io.jenetics.jpx.Person;

/**
* @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
* @version !__version__!
* @since !__version__!
*/
public class MetadataDAO extends DAO {

/**
* Represents a row in the "metadata" tables.
*/
private static final class Row {
final String name;
final String description;
final Long personID;
final Long copyrightID;
final ZonedDateTime time;
final String keywords;
final Long boundsID;

Row(
final String name,
final String description,
final Long personID,
final Long copyrightID,
final ZonedDateTime time,
final String keywords,
final Long boundsID
) {
this.name = name;
this.description = description;
this.personID = personID;
this.copyrightID = copyrightID;
this.time = time;
this.keywords = keywords;
this.boundsID = boundsID;
}
}


public MetadataDAO(final Connection connection) {
super(connection);
}

/**
* The link row parser which creates a {@link Link} object from a given DB
* row.
*/
private static final RowParser<Stored<Row>> RowParser = rs -> Stored.of(
rs.getLong("id"),
new Row(
rs.getString("name"),
rs.getString("desc"),
rs.get(Long.class, "person_id"),
rs.get(Long.class, "copyright_id"),
rs.getZonedDateTime("time"),
rs.getString("keywords"),
rs.get(Long.class, "bounds_id")
)
);

/*
CREATE TABLE metadata(
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255),
`desc` TEXT,
person_id BIGINT REFERENCES person(id),
copyright_id BIGINT,
time TIMESTAMP,
keywords VARCHAR(255),
bounds_id BIGINT REFERENCES bounds(id)
);
CREATE INDEX i_metadata_name ON metadata(name);
CREATE INDEX i_metadata_keywords ON metadata(keywords);
CREATE TABLE metadata_link(
metadata_id BIGINT NOT NULL REFERENCES metadata(id) ON DELETE CASCADE,
link_id BIGINT NOT NULL REFERENCES link(id),
CONSTRAINT c_metadata_link_metadata_id_link_id UNIQUE (metadata_id, link_id)
);
*/

/* *************************************************************************
* INSERT queries
**************************************************************************/

/**
* Insert the given person list into the DB.
*
* @param metadata the persons to insert
* @return return the stored persons
* @throws SQLException if inserting fails
*/
public List<Stored<Metadata>> insert(final List<Metadata> metadata)
throws SQLException
{
final Map<Person, Long> persons = DAO
.set(metadata, Metadata::getAuthor, with(PersonDAO::new)::put);

final Map<Copyright, Long> copyrights = DAO
.set(metadata, Metadata::getCopyright, with(CopyrightDAO::new)::put);

final Map<Bounds, Long> bounds = DAO
.set(metadata, Metadata::getBounds, with(BoundsDAO::new)::insert);

final String query =
"INSERT INTO person(name, email, link_id) " +
"VALUES({name}, {email}, {link_id});";

final List<Stored<Metadata>> inserted =
Batch(query).insert(metadata, md -> asList(
Param.value("name", md.getName()),
Param.value("desc", md.getDescription()),
Param.value("person_id", md.getAuthor().map(persons::get)),
Param.value("copyright_id", md.getCopyright().map(copyrights::get)),
Param.value("time", md.getTime()),
Param.value("keywords", md.getKeywords()),
Param.value("bounds_id", md.getBounds().map(bounds::get))
));

final Map<Link, Long> links = DAO
.set(metadata, Metadata::getLinks, with(LinkDAO::new)::put);

return null;
}

/* *************************************************************************
* SELECT queries
**************************************************************************/

/**
* Select all available copyrights.
*
* @return all stored copyrights
* @throws SQLException if the select fails
*/
/*
public List<Stored<Metadata>> select() throws SQLException {
final String query =
"SELECT id, " +
"name, " +
"description, " +
"person.name AS person_name, " +
"person.email AS person_email, " +
"link.href AS link_href, " +
"link.text AS link_text, " +
"link.type AS link_type " +
"person.";
return SQL(query).as(RowParser.list());
}
*/

}
26 changes: 12 additions & 14 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/PersonDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toMap;

import java.sql.Connection;
import java.sql.SQLException;
Expand Down Expand Up @@ -75,7 +74,8 @@ public PersonDAO(final Connection conn) {
public List<Stored<Person>> insert(final List<Person> persons)
throws SQLException
{
final Map<Link, Long> links = putLinks(persons);
final Map<Link, Long> links = DAO
.set(persons, Person::getLink, with(LinkDAO::new)::put);

final String query =
"INSERT INTO person(name, email, link_id) " +
Expand All @@ -88,16 +88,6 @@ public List<Stored<Person>> insert(final List<Person> persons)
));
}

private Map<Link, Long> putLinks(final List<Person> persons)
throws SQLException
{
final List<Stored<Link>> links = dao(LinkDAO::new)
.put(flatMap(persons, Person::getLink));

return links.stream()
.collect(toMap(Stored::value, Stored::id, (a, b) -> a));
}

/**
* Insert the given person into the DB.
*
Expand All @@ -124,7 +114,11 @@ public Stored<Person> insert(final Person person) throws SQLException {
public List<Stored<Person>> update(final List<Stored<Person>> persons)
throws SQLException
{
final Map<Link, Long> links = putLinks(map(persons, Stored::value));
final Map<Link, Long> links = DAO.set(
persons,
(Stored<Person> p) -> p.value().getLink(),
with(LinkDAO::new)::put
);

final String query =
"UPDATE person " +
Expand Down Expand Up @@ -168,12 +162,16 @@ public List<Stored<Person>> put(final List<Person> persons)
return DAO.put(
persons,
Person::getName,
list -> selectByNames(flatMap(list, Person::getName)),
list -> selectByNames(flatMapOption(list, Person::getName)),
this::insert,
this::update
);
}

public Stored<Person> put(final Person person) throws SQLException {
return put(singletonList(person)).get(0);
}

/* *************************************************************************
* SELECT queries
**************************************************************************/
Expand Down
Loading

0 comments on commit 9040610

Please sign in to comment.