Skip to content

Commit

Permalink
JavaDoc reviewed
Browse files Browse the repository at this point in the history
  • Loading branch information
marat-gainullin committed Jan 9, 2019
1 parent 1b4b896 commit 42a5a75
Show file tree
Hide file tree
Showing 62 changed files with 197 additions and 164 deletions.
8 changes: 8 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Refactoring
* Make futures completable through arbitrary executor.
* Make maximum number of all variables private.
* Review all round trips.

## Tests
* Make tests for all round trips.
* Review other test cases.
2 changes: 2 additions & 0 deletions src/main/java/com/github/pgasync/ConnectionPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public interface ConnectionPool extends Db {
/**
* Gets a connection when available.
* {@link Connection#close()} method will return the connection into this pool instead of closing it.
*
* @return {@link CompletableFuture} that is queued and completed when pool has an available connection.
*/
CompletableFuture<Connection> getConnection();

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/github/pgasync/ConnectionPoolBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ public ConnectionPoolBuilder database(String database) {
return this;
}

public ConnectionPoolBuilder poolSize(int poolSize) {
properties.maxConnections = poolSize;
public ConnectionPoolBuilder maxConnections(int maxConnections) {
properties.maxConnections = maxConnections;
return this;
}

public ConnectionPoolBuilder maxStatements(int maxStatements) {
properties.maxStatements = maxStatements;
return this;
}

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/github/pgasync/PreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public interface PreparedStatement {

/**
* Fetches the whole row set and returns a {@link CompletableFuture} with an instance of {@link ResultSet}.
* Fetches the whole row set and returns a {@link CompletableFuture} completed with an instance of {@link ResultSet}.
* This future may be completed with an error. Use this method if you are sure, that all data, returned by the query can be placed into memory.
*
* @param params Array of query parameters values.
Expand All @@ -26,12 +26,19 @@ public interface PreparedStatement {
* Fetches data rows from Postgres one by one. Use this method when you are unsure, that all data, returned by the query can be placed into memory.
*
* @param onColumns {@link Consumer} of parameters by name map. Gets called when bind/describe chain succeeded.
* @param processor {@link Consumer} of single data row. Performs some processing of data.
* @param processor {@link Consumer} of single data row. Performs transformation from {@link com.github.pgasync.impl.message.backend.DataRow} message
* to {@link Row} implementation instance.
* @param params Array of query parameters values.
* @return CompletableFuture that completes when the whole process ends or when an error occurs. Future's value will indicate the number of affected rows by the query.
* @return CompletableFuture that completes when the whole process ends or when an error occurs. Future's value will indicate the number of rows affected by the query.
*/
CompletableFuture<Integer> fetch(Consumer<Map<String, PgColumn>> onColumns, Consumer<Row> processor, Object... params);

/**
* Closes this {@link PreparedStatement} and possibly frees resources. In case of pool statement it may be returned to a pool for future reuse.
* @return CompletableFuture that is completed when the network process ends.
* Network process may occur if returned statement has evicted some other statement from the pool in case of pooled statement.
* Closing of such evicted statement is network activity, we should be aware of.
*/
CompletableFuture<Void> close();

}
8 changes: 5 additions & 3 deletions src/main/java/com/github/pgasync/QueryExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ default CompletableFuture<Collection<ResultSet>> completeScript(String sql) {
*
* @param onColumns Columns fetched callback consumer.
* @param onRow A row fetched callback consumer.
* @param onAffected An affected rows callback consumer. It is called when a particular {@link ResultSet} is completely fetched with its affected rows count. This callback should be used to create a {@link ResultSet} instance from already fetched columns, rows and affected rows count.
* @param onAffected An affected rows callback consumer.
* It is called when a particular {@link ResultSet} is completely fetched with its affected rows count.
* This callback should be used to create a {@link ResultSet} instance from already fetched columns, rows and affected rows count.
* @param sql Sql Script text.
* @return CompletableFuture that is completed when the whole process of multiple {@link ResultSet}s fetching ends.
*/
CompletableFuture<Void> script(Consumer<Map<String, PgColumn>> onColumns, Consumer<Row> onRow, Consumer<Integer> onAffected, String sql);

/**
* Sends single query with parameters. Uses extended query protocol of Postgres.
* Accumulates fetched columns, rows and affected rows count into memory and transforms them into ResultSet when it is fetched.
* Accumulates fetched columns, rows and affected rows count into memory and transforms them into a {@link ResultSet} when it is fetched.
* Completes returned {@link CompletableFuture} when the whole process of {@link ResultSet} fetching ends.
*
* @param sql Sql query text with parameters substituted with ?.
Expand All @@ -88,7 +90,7 @@ default CompletableFuture<ResultSet> completeQuery(String sql, Object... params)
* Sends single query with parameters. Uses extended query protocol of Postgres.
* Unlike {@link #completeQuery(String, Object...)} doesn't accumulate columns, rows and affected rows counts into memory.
* Instead it calls passed in consumers, when columns, or particular row is fetched from Postgres.
* Completes returned {@link CompletableFuture} when the process of single {@link ResultSet}s fetching ends.
* Completes returned {@link CompletableFuture} with affected rows count when the process of single {@link ResultSet}s fetching ends.
*
* @param onColumns Columns fetched callback consumer.
* @param onRow A row fetched callback consumer.
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/github/pgasync/impl/PgConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package com.github.pgasync.impl;

import static com.github.pgasync.impl.message.b.RowDescription.ColumnDescription;
import static com.github.pgasync.impl.message.backend.RowDescription.ColumnDescription;

import java.util.ArrayList;
import java.util.LinkedHashMap;
Expand All @@ -34,17 +34,17 @@
import com.github.pgasync.Row;
import com.github.pgasync.Transaction;
import com.github.pgasync.impl.conversion.DataConverter;
import com.github.pgasync.impl.message.b.Authentication;
import com.github.pgasync.impl.message.b.RowDescription;
import com.github.pgasync.impl.message.f.Bind;
import com.github.pgasync.impl.message.f.Close;
import com.github.pgasync.impl.message.f.Describe;
import com.github.pgasync.impl.message.f.Execute;
import com.github.pgasync.impl.message.backend.Authentication;
import com.github.pgasync.impl.message.backend.RowDescription;
import com.github.pgasync.impl.message.frontend.Bind;
import com.github.pgasync.impl.message.frontend.Close;
import com.github.pgasync.impl.message.frontend.Describe;
import com.github.pgasync.impl.message.frontend.Execute;
import com.github.pgasync.impl.message.Message;
import com.github.pgasync.impl.message.f.Parse;
import com.github.pgasync.impl.message.f.PasswordMessage;
import com.github.pgasync.impl.message.f.Query;
import com.github.pgasync.impl.message.f.StartupMessage;
import com.github.pgasync.impl.message.frontend.Parse;
import com.github.pgasync.impl.message.frontend.PasswordMessage;
import com.github.pgasync.impl.message.frontend.Query;
import com.github.pgasync.impl.message.frontend.StartupMessage;

/**
* A connection to Postgres backend. The postmaster forks a backend process for
Expand Down Expand Up @@ -290,7 +290,7 @@ public CompletableFuture<Integer> query(Consumer<Map<String, PgColumn>> onColumn
}

/**
* Nested Transaction using savepoints.
* Nested transaction using savepoints.
*/
class PgConnectionNestedTransaction extends PgConnectionTransaction {

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/github/pgasync/impl/PgConnectionPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import java.util.function.Function;

/**
* Pool for backend connections. {@link CompletableFuture}s are queued and completed when pool has an available
* connection.
* Pool for backend connections.
*
* @author Antti Laisi
*/
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/github/pgasync/impl/PgProtocolStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
package com.github.pgasync.impl;

import com.github.pgasync.impl.message.Message;
import com.github.pgasync.impl.message.b.CommandComplete;
import com.github.pgasync.impl.message.b.DataRow;
import com.github.pgasync.impl.message.b.RowDescription;
import com.github.pgasync.impl.message.f.Execute;
import com.github.pgasync.impl.message.f.PasswordMessage;
import com.github.pgasync.impl.message.f.Query;
import com.github.pgasync.impl.message.f.StartupMessage;
import com.github.pgasync.impl.message.backend.CommandComplete;
import com.github.pgasync.impl.message.backend.DataRow;
import com.github.pgasync.impl.message.backend.RowDescription;
import com.github.pgasync.impl.message.frontend.Execute;
import com.github.pgasync.impl.message.frontend.PasswordMessage;
import com.github.pgasync.impl.message.frontend.Query;
import com.github.pgasync.impl.message.frontend.StartupMessage;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/pgasync/impl/PgRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.github.pgasync.Row;
import com.github.pgasync.SqlException;
import com.github.pgasync.impl.conversion.DataConverter;
import com.github.pgasync.impl.message.b.DataRow;
import com.github.pgasync.impl.message.backend.DataRow;

import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @author Antti Laisi
*/
public class BooleanConversions {
;

static final byte[] TRUE = new byte[]{ 't' };
static final byte[] FALSE = new byte[]{ 'f' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.sql.Time;
import java.sql.Timestamp;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.nio.charset.StandardCharsets.UTF_8;

Expand All @@ -17,14 +19,15 @@
*/
public class DataConverter {

final Map<Class<?>, Converter<?>> typeToConverter = new HashMap<>();
final Map<Class<?>, Converter<?>> typeToConverter;

public DataConverter(List<Converter<?>> converters) {
converters.forEach(c -> typeToConverter.put(c.type(), c));
typeToConverter = converters.stream()
.collect(Collectors.toMap(Converter::type, Function.identity()));
}

public DataConverter() {
this(Collections.emptyList());
this(List.of());
}

public String toString(Oid oid, byte[] value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* limitations under the License.
*/

package com.github.pgasync.impl.io.b;
package com.github.pgasync.impl.io.backend;

import com.github.pgasync.impl.io.Decoder;
import com.github.pgasync.impl.message.b.Authentication;
import com.github.pgasync.impl.message.backend.Authentication;

import java.nio.ByteBuffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* limitations under the License.
*/

package com.github.pgasync.impl.io.b;
package com.github.pgasync.impl.io.backend;

import com.github.pgasync.impl.io.Decoder;
import com.github.pgasync.impl.message.b.BIndicators;
import com.github.pgasync.impl.message.backend.BIndicators;

import java.nio.ByteBuffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* limitations under the License.
*/

package com.github.pgasync.impl.io.b;
package com.github.pgasync.impl.io.backend;

import com.github.pgasync.impl.io.Decoder;
import com.github.pgasync.impl.message.b.BIndicators;
import com.github.pgasync.impl.message.backend.BIndicators;

import java.nio.ByteBuffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* limitations under the License.
*/

package com.github.pgasync.impl.io.b;
package com.github.pgasync.impl.io.backend;

import com.github.pgasync.impl.io.Decoder;
import com.github.pgasync.impl.message.b.CommandComplete;
import com.github.pgasync.impl.message.backend.CommandComplete;

import java.nio.ByteBuffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* limitations under the License.
*/

package com.github.pgasync.impl.io.b;
package com.github.pgasync.impl.io.backend;

import com.github.pgasync.impl.io.Decoder;
import com.github.pgasync.impl.message.b.DataRow;
import com.github.pgasync.impl.message.backend.DataRow;

import java.nio.ByteBuffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* limitations under the License.
*/

package com.github.pgasync.impl.io.b;
package com.github.pgasync.impl.io.backend;

import com.github.pgasync.impl.io.Decoder;
import com.github.pgasync.impl.message.b.ErrorResponse;
import com.github.pgasync.impl.message.backend.ErrorResponse;

import java.nio.ByteBuffer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.pgasync.impl.io.b;
package com.github.pgasync.impl.io.backend;

import com.github.pgasync.impl.io.Decoder;
import com.github.pgasync.impl.message.b.NoticeResponse;
import com.github.pgasync.impl.message.backend.NoticeResponse;

import java.nio.ByteBuffer;
import java.util.ArrayList;
Expand Down Expand Up @@ -29,7 +29,7 @@
* The field value.
* </pre>
*
* @author Antti Laisi
* @author Marat Gainullin
*/
public class NoticeResponseDecoder implements Decoder<NoticeResponse> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.pgasync.impl.io.b;
package com.github.pgasync.impl.io.backend;

import com.github.pgasync.impl.io.Decoder;
import com.github.pgasync.impl.message.b.NotificationResponse;
import com.github.pgasync.impl.message.backend.NotificationResponse;

import java.nio.ByteBuffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* limitations under the License.
*/

package com.github.pgasync.impl.io.b;
package com.github.pgasync.impl.io.backend;

import com.github.pgasync.impl.io.Decoder;
import com.github.pgasync.impl.message.b.BIndicators;
import com.github.pgasync.impl.message.backend.BIndicators;

import java.nio.ByteBuffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* limitations under the License.
*/

package com.github.pgasync.impl.io.b;
package com.github.pgasync.impl.io.backend;

import com.github.pgasync.impl.io.Decoder;
import com.github.pgasync.impl.message.b.ReadyForQuery;
import com.github.pgasync.impl.message.backend.ReadyForQuery;

import java.nio.ByteBuffer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
* limitations under the License.
*/

package com.github.pgasync.impl.io.b;
package com.github.pgasync.impl.io.backend;

import com.github.pgasync.impl.Oid;
import com.github.pgasync.impl.io.Decoder;
import com.github.pgasync.impl.message.b.RowDescription;
import com.github.pgasync.impl.message.b.RowDescription.ColumnDescription;
import com.github.pgasync.impl.message.backend.RowDescription;

import java.nio.ByteBuffer;

Expand Down Expand Up @@ -62,14 +61,14 @@ public byte getMessageId() {

@Override
public RowDescription read(ByteBuffer buffer) {
ColumnDescription[] columns = new ColumnDescription[buffer.getShort()];
RowDescription.ColumnDescription[] columns = new RowDescription.ColumnDescription[buffer.getShort()];

for (int i = 0; i < columns.length; i++) {
String name = getCString(buffer);
buffer.position(buffer.position() + 6);
Oid type = Oid.valueOfId(buffer.getInt());
buffer.position(buffer.position() + 8);
columns[i] = new ColumnDescription(name, type);
columns[i] = new RowDescription.ColumnDescription(name, type);
}

return new RowDescription(columns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* limitations under the License.
*/

package com.github.pgasync.impl.io.f;
package com.github.pgasync.impl.io.frontend;

import com.github.pgasync.impl.io.IO;
import com.github.pgasync.impl.message.f.Bind;
import com.github.pgasync.impl.message.frontend.Bind;

import java.nio.ByteBuffer;

Expand Down
Loading

0 comments on commit 42a5a75

Please sign in to comment.