com.palantir.atlasdb.keyvalue.dbkvs.impl.postgres.PostgresQueryFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of atlasdb-dbkvs Show documentation
Show all versions of atlasdb-dbkvs Show documentation
Palantir open source project
/*
* (c) Copyright 2018 Palantir Technologies Inc. All rights reserved.
*
* 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.
*/
package com.palantir.atlasdb.keyvalue.dbkvs.impl.postgres;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.palantir.atlasdb.keyvalue.api.BatchColumnRangeSelection;
import com.palantir.atlasdb.keyvalue.api.Cell;
import com.palantir.atlasdb.keyvalue.api.ColumnRangeSelection;
import com.palantir.atlasdb.keyvalue.api.ColumnSelection;
import com.palantir.atlasdb.keyvalue.api.RangeRequest;
import com.palantir.atlasdb.keyvalue.dbkvs.PostgresDdlConfig;
import com.palantir.atlasdb.keyvalue.dbkvs.impl.AbstractDbQueryFactory;
import com.palantir.atlasdb.keyvalue.dbkvs.impl.FullQuery;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class PostgresQueryFactory extends AbstractDbQueryFactory {
private final String tableName;
private final PostgresDdlConfig config;
public PostgresQueryFactory(String tableName, PostgresDdlConfig config) {
this.tableName = tableName;
this.config = config;
}
@Override
public FullQuery getLatestRowQuery(byte[] row, long ts, ColumnSelection columns, boolean includeValue) {
String query = " /* GET_LATEST_ROW_INNER (" + tableName + ") */ "
+ " SELECT m.row_name, m.col_name, max(m.ts) as ts "
+ " FROM " + prefixedTableName() + " m "
+ " WHERE m.row_name = ? "
+ " AND m.ts < ? "
+ (columns.allColumnsSelected()
? ""
: " AND m.col_name IN " + numParams(Iterables.size(columns.getSelectedColumns())))
+ " GROUP BY m.row_name, m.col_name";
query = wrapQueryWithIncludeValue("GET_LATEST_ROW", query, includeValue);
FullQuery fullQuery = new FullQuery(query).withArgs(row, ts);
return columns.allColumnsSelected() ? fullQuery : fullQuery.withArgs(columns.getSelectedColumns());
}
@Override
public FullQuery getLatestRowsQuery(Iterable rows, long ts, ColumnSelection columns, boolean includeValue) {
String query = " /* GET_LATEST_ROWS_INNER (" + tableName + ") */ "
+ " SELECT m.row_name, m.col_name, max(m.ts) as ts "
+ " FROM " + prefixedTableName() + " m "
+ " WHERE m.row_name IN " + numParams(Iterables.size(rows))
+ " AND m.ts < ? "
+ (columns.allColumnsSelected()
? ""
: " AND m.col_name IN " + numParams(Iterables.size(columns.getSelectedColumns())))
+ " GROUP BY m.row_name, m.col_name ";
query = wrapQueryWithIncludeValue("GET_LATEST_ROW", query, includeValue);
FullQuery fullQuery = new FullQuery(query).withArgs(rows).withArg(ts);
return columns.allColumnsSelected() ? fullQuery : fullQuery.withArgs(columns.getSelectedColumns());
}
@Override
public FullQuery getLatestRowsQuery(
Collection> rows, ColumnSelection columns, boolean includeValue) {
String query = " /* GET_LATEST_ROWS_INNER (" + tableName + ") */ "
+ " SELECT m.row_name, m.col_name, max(m.ts) as ts "
+ " FROM " + prefixedTableName() + " m,"
+ " (VALUES " + groupOfNumParams(2, rows.size()) + ") t(row_name, ts) "
+ " WHERE m.row_name = t.row_name "
+ " AND m.ts < t.ts "
+ (columns.allColumnsSelected()
? ""
: " AND m.col_name IN " + numParams(Iterables.size(columns.getSelectedColumns())))
+ " GROUP BY m.row_name, m.col_name ";
query = wrapQueryWithIncludeValue("GET_LATEST_ROW", query, includeValue);
FullQuery fullQuery = addRowTsArgs(new FullQuery(query), rows);
return columns.allColumnsSelected() ? fullQuery : fullQuery.withArgs(columns.getSelectedColumns());
}
@Override
public FullQuery getAllRowQuery(byte[] row, long ts, ColumnSelection columns, boolean includeValue) {
String query = " /* GET_ALL_ROW (" + tableName + ") */ "
+ " SELECT m.row_name, m.col_name, m.ts" + (includeValue ? ", m.val " : " ")
+ " FROM " + prefixedTableName() + " m "
+ " WHERE m.row_name = ? "
+ " AND m.ts < ? "
+ (columns.allColumnsSelected()
? ""
: " AND m.col_name IN " + numParams(Iterables.size(columns.getSelectedColumns())));
FullQuery fullQuery = new FullQuery(query).withArgs(row, ts);
return columns.allColumnsSelected() ? fullQuery : fullQuery.withArgs(columns.getSelectedColumns());
}
@Override
public FullQuery getAllRowsQuery(Iterable rows, long ts, ColumnSelection columns, boolean includeValue) {
String query = " /* GET_ALL_ROWS (" + tableName + ") */ "
+ " SELECT m.row_name, m.col_name, m.ts" + (includeValue ? ", m.val " : " ")
+ " FROM " + prefixedTableName() + " m "
+ " WHERE m.row_name IN " + numParams(Iterables.size(rows))
+ " AND m.ts < ? "
+ (columns.allColumnsSelected()
? ""
: " AND m.col_name IN " + numParams(Iterables.size(columns.getSelectedColumns())));
FullQuery fullQuery = new FullQuery(query).withArgs(rows).withArg(ts);
return columns.allColumnsSelected() ? fullQuery : fullQuery.withArgs(columns.getSelectedColumns());
}
@Override
public FullQuery getAllRowsQuery(
Collection> rows, ColumnSelection columns, boolean includeValue) {
String query = " /* GET_ALL_ROWS (" + tableName + ") */ "
+ " SELECT m.row_name, m.col_name, m.ts" + (includeValue ? ", m.val " : " ")
+ " FROM " + prefixedTableName() + " m,"
+ " (VALUES " + groupOfNumParams(2, rows.size()) + ") t(row_name, ts) "
+ " WHERE m.row_name = t.row_name "
+ " AND m.ts < t.ts "
+ (columns.allColumnsSelected()
? ""
: " AND m.col_name IN " + numParams(Iterables.size(columns.getSelectedColumns())));
FullQuery fullQuery = addRowTsArgs(new FullQuery(query), rows);
return columns.allColumnsSelected() ? fullQuery : fullQuery.withArgs(columns.getSelectedColumns());
}
@Override
public FullQuery getLatestCellQuery(Cell cell, long ts, boolean includeValue) {
String query = " /* GET_LATEST_CELL_INNER (" + tableName + ") */ "
+ " SELECT m.row_name, m.col_name, max(m.ts) as ts "
+ " FROM " + prefixedTableName() + " m "
+ " WHERE m.row_name = ? "
+ " AND m.col_name = ? "
+ " AND m.ts < ? "
+ " GROUP BY m.row_name, m.col_name "
+ " LIMIT 1";
query = wrapQueryWithIncludeValue("GET_LATEST_CELL", query, includeValue);
return new FullQuery(query).withArgs(cell.getRowName(), cell.getColumnName(), ts);
}
@Override
public FullQuery getLatestCellsQuery(Iterable cells, long ts, boolean includeValue) {
String query = " /* GET_LATEST_CELLS_INNER (" + tableName + ") */ "
+ " SELECT m.row_name, m.col_name, max(m.ts) as ts "
+ " FROM " + prefixedTableName() + " m,"
+ " (VALUES " + groupOfNumParams(2, Iterables.size(cells)) + ") t(row_name, col_name) "
+ " WHERE m.row_name = t.row_name "
+ " AND m.col_name = t.col_name "
+ " AND m.ts < ? "
+ " GROUP BY m.row_name, m.col_name ";
query = wrapQueryWithIncludeValue("GET_LATEST_CELLS", query, includeValue);
return addCellArgs(new FullQuery(query), cells).withArg(ts);
}
@Override
public FullQuery getLatestCellsQuery(Collection> cells, boolean includeValue) {
String query = " /* GET_LATEST_CELLS_INNER (" + tableName + ") */ "
+ " SELECT m.row_name, m.col_name, max(m.ts) as ts "
+ " FROM " + prefixedTableName() + " m,"
+ " (VALUES " + groupOfNumParams(3, Iterables.size(cells)) + ") t(row_name, col_name, ts) "
+ " WHERE m.row_name = t.row_name "
+ " AND m.col_name = t.col_name "
+ " AND m.ts < t.ts "
+ " GROUP BY m.row_name, m.col_name ";
query = wrapQueryWithIncludeValue("GET_LATEST_CELLS", query, includeValue);
return addCellTsArgs(new FullQuery(query), cells);
}
@Override
public FullQuery getAllCellQuery(Cell cell, long ts, boolean includeValue) {
String query = " /* GET_ALL_CELL (" + tableName + ") */ "
+ " SELECT m.row_name, m.col_name, m.ts" + (includeValue ? ", m.val " : " ")
+ " FROM " + prefixedTableName() + " m "
+ " WHERE m.row_name = ? "
+ " AND m.col_name = ? "
+ " AND m.ts < ? ";
return new FullQuery(query).withArgs(cell.getRowName(), cell.getColumnName(), ts);
}
@Override
public FullQuery getAllCellsQuery(Iterable cells, long ts, boolean includeValue) {
String query = " /* GET_ALL_CELLS (" + tableName + ") */ "
+ " SELECT m.row_name, m.col_name, m.ts" + (includeValue ? ", m.val " : " ")
+ " FROM " + prefixedTableName() + " m,"
+ " (VALUES " + groupOfNumParams(2, Iterables.size(cells)) + ") t(row_name, col_name) "
+ " WHERE m.row_name = t.row_name "
+ " AND m.col_name = t.col_name "
+ " AND m.ts < ? ";
return addCellArgs(new FullQuery(query), cells).withArg(ts);
}
@Override
public FullQuery getAllCellsQuery(Collection> cells, boolean includeValue) {
String query = " /* GET_ALL_CELLS (" + tableName + ") */ "
+ " SELECT m.row_name, m.col_name, m.ts" + (includeValue ? ", m.val " : " ")
+ " FROM " + prefixedTableName() + " m,"
+ " (VALUES " + groupOfNumParams(3, Iterables.size(cells)) + ") t(row_name, col_name, ts) "
+ " WHERE m.row_name = t.row_name "
+ " AND m.col_name = t.col_name "
+ " AND m.ts < t.ts ";
return addCellTsArgs(new FullQuery(query), cells);
}
@Override
public FullQuery getRangeQuery(RangeRequest range, long ts, int maxRows) {
List bounds = new ArrayList<>(2);
List | |