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 8, 2017
1 parent 9040610 commit 1f1c628
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 83 deletions.
43 changes: 25 additions & 18 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/DAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -92,32 +93,38 @@ static <T, K> List<Stored<T>> put(
)
throws SQLException
{
final Map<K, Stored<T>> existing = select.apply(values).stream()
.collect(toMap(
value -> key.apply(value.value()),
value -> value,
(a, b) -> b));
final List<Stored<T>> result;

final Map<K, T> actual = values.stream()
.collect(toMap(key, value -> value, (a, b) -> b));
if (!values.isEmpty()) {
final Map<K, Stored<T>> existing = select.apply(values).stream()
.collect(toMap(
value -> key.apply(value.value()),
value -> value,
(a, b) -> b));

final Diff<K, Stored<T>, T> diff = Diff.of(existing, actual);
final Map<K, T> actual = values.stream()
.collect(toMap(key, value -> value, (a, b) -> b));

final List<T> missing = diff.missing();
final Diff<K, Stored<T>, T> diff = Diff.of(existing, actual);

final List<Stored<T>> updated = diff
.updated((e, a) -> Objects.equals(e.value(), a))
.entrySet().stream()
final List<T> missing = diff.missing();

final List<Stored<T>> updated = diff
.updated((e, a) -> Objects.equals(e.value(), a))
.entrySet().stream()
.map(entry -> entry.getKey().map(m -> entry.getValue()))
.collect(toList());

final List<Stored<T>> unchanged = diff
.unchanged((e, a) -> Objects.equals(e.value(), a));
final List<Stored<T>> unchanged = diff
.unchanged((e, a) -> Objects.equals(e.value(), a));

final List<Stored<T>> result = new ArrayList<>();
result.addAll(insert.apply(missing));
result.addAll(update.apply(updated));
result.addAll(unchanged);
result = new ArrayList<>();
result.addAll(insert.apply(missing));
result.addAll(update.apply(updated));
result.addAll(unchanged);
} else {
result = Collections.emptyList();
}

return result;
}
Expand Down
101 changes: 38 additions & 63 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/LinkDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;

import java.net.URI;
import java.sql.Connection;
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 @@ -59,6 +55,34 @@ public LinkDAO(final Connection connection) {
);


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

/**
* Select all available links.
*
* @return all stored links
* @throws SQLException if the select fails
*/
public List<Stored<Link>> select() throws SQLException {
return SQL("SELECT id, href, text, type FROM link")
.as(RowParser.list());
}

public List<Stored<Link>> select(final List<Link> links)
throws SQLException
{
final String query =
"SELECT id, href, text, type " +
"FROM link WHERE href IN ({hrefs})";

return SQL(query)
.on(Param.values("hrefs", links, Link::getHref))
.as(RowParser.list());
}


/* *************************************************************************
* INSERT queries
**************************************************************************/
Expand Down Expand Up @@ -145,73 +169,24 @@ public Stored<Link> update(final Stored<Link> link) throws SQLException {
* @throws SQLException if the insert/update fails
*/
public List<Stored<Link>> put(final List<Link> links) throws SQLException {
return links.isEmpty()
? Collections.emptyList()
: DAO.put(
links,
Link::getHref,
//list -> selectByHrefs(map(list, Link::getHref)),
values -> select(values, Link::getHref, this::selectByHrefs),
this::insert,
this::update
);
return DAO.put(
links,
Link::getHref,
this::select,
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
* DELETE queries
**************************************************************************/

/**
* Select all available links.
*
* @return all stored links
* @throws SQLException if the select fails
*/
public List<Stored<Link>> select() throws SQLException {
return SQL("SELECT id, href, text, type FROM link")
.as(RowParser.list());
}

/**
* Selects the links by its hrefs.
*
* @param hrefs the hrefs
* @return the link with the given hrefs currently in the DB
* @throws SQLException if the select fails
*/
public List<Stored<Link>> selectByHrefs(final List<URI> hrefs)
public List<Link> delete(final List<Stored<Link>> links)
throws SQLException
{
final String query =
"SELECT id, href, text, type FROM link WHERE href IN ({hrefs})";

return SQL(query)
.on(Param.values("hrefs", hrefs))
.as(RowParser.list());
}

/**
* Create a new {@code LinkDAO} for the given connection.
*
* @param conn the DB connection
* @return a new DAO object
*/
public static LinkDAO of(final Connection conn) {
return new LinkDAO(conn);
return null;
}

}
18 changes: 16 additions & 2 deletions jpx.jdbc/src/main/java/io/jenetics/jpx/jdbc/Param.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import static java.util.Objects.requireNonNull;

import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

Expand Down Expand Up @@ -89,13 +91,25 @@ public static Param value(final String name, final Object value) {
* @param values the parameter values
* @return a new parameter object
*/
public static Param values(
public static <T> Param values(
final String name,
final Collection<? extends Object> values
final Collection<T> values
) {
return new Param(name, values);
}

public static <A, B> Param values(
final String name,
final Collection<A> values,
final Function<A, B> mapper
) {
final List<B> converted = values.stream()
.map(mapper)
.collect(Collectors.toList());

return new Param(name, converted);
}

/**
* Return a new parameter object with the given name and long values.
*
Expand Down

0 comments on commit 1f1c628

Please sign in to comment.