Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.linkedin.thirdeye.datalayer.dao;

import com.google.inject.Inject;
import com.linkedin.thirdeye.datalayer.entity.AbstractJsonEntity;
import com.linkedin.thirdeye.datalayer.util.GenericResultSetMapper;
import com.linkedin.thirdeye.datalayer.util.Predicate;
import com.linkedin.thirdeye.datalayer.util.SqlQueryBuilder;
Expand All @@ -17,7 +18,7 @@
import java.util.Set;
import javax.sql.DataSource;

public class AbstractBaseDAO<E extends AbstractBaseEntity> {
public class AbstractBaseDAO<E extends AbstractJsonEntity> {

final Class<E> entityClass;

Expand Down Expand Up @@ -49,7 +50,7 @@ public Long save(E entity) {
throw new RuntimeException(
"id must be null when inserting new record. If you are trying to update call update");
}
return runTask(new Task<Long>() {
return runTask(new QueryTask<Long>() {
@Override
public Long handle(Connection connection) throws Exception {
PreparedStatement insertStatement =
Expand All @@ -69,7 +70,7 @@ public Long handle(Connection connection) throws Exception {

@SuppressWarnings("unchecked")
public E findById(Long id) {
return runTask(new Task<E>() {
return runTask(new QueryTask<E>() {
@Override
public E handle(Connection connection) throws Exception {
PreparedStatement selectStatement =
Expand All @@ -82,7 +83,7 @@ public E handle(Connection connection) throws Exception {

@SuppressWarnings("unchecked")
public List<E> findAll() {
return runTask(new Task<List<E>>() {
return runTask(new QueryTask<List<E>>() {
@Override
public List<E> handle(Connection connection) throws Exception {
PreparedStatement selectStatement =
Expand All @@ -94,7 +95,7 @@ public List<E> handle(Connection connection) throws Exception {
}

public int deleteById(Long id) {
return runTask(new Task<Integer>() {
return runTask(new QueryTask<Integer>() {
@Override
public Integer handle(Connection connection) throws Exception {
Map<String, Object> filters = new HashMap<>();
Expand All @@ -107,7 +108,7 @@ public Integer handle(Connection connection) throws Exception {
}

public int deleteByParams(Map<String, Object> filters) {
return runTask(new Task<Integer>() {
return runTask(new QueryTask<Integer>() {
@Override
public Integer handle(Connection connection) throws Exception {
PreparedStatement deleteStatement =
Expand All @@ -120,7 +121,7 @@ public Integer handle(Connection connection) throws Exception {
@SuppressWarnings("unchecked")
public List<E> executeParameterizedSQL(String parameterizedSQL,
Map<String, Object> parameterMap) {
return runTask(new Task<List<E>>() {
return runTask(new QueryTask<List<E>>() {
@Override
public List<E> handle(Connection connection) throws Exception {
PreparedStatement selectStatement = sqlQueryBuilder.createStatementFromSQL(connection,
Expand All @@ -134,7 +135,7 @@ public List<E> handle(Connection connection) throws Exception {

@SuppressWarnings("unchecked")
public List<E> findByParams(Map<String, Object> filters) {
return runTask(new Task<List<E>>() {
return runTask(new QueryTask<List<E>>() {
@Override
public List<E> handle(Connection connection) throws Exception {
PreparedStatement selectStatement =
Expand All @@ -147,7 +148,7 @@ public List<E> handle(Connection connection) throws Exception {

@SuppressWarnings("unchecked")
public List<E> findByParams(Predicate predicate) {
return runTask(new Task<List<E>>() {
return runTask(new QueryTask<List<E>>() {
@Override
public List<E> handle(Connection connection) throws Exception {
PreparedStatement selectStatement =
Expand All @@ -159,7 +160,7 @@ public List<E> handle(Connection connection) throws Exception {
}

public int update(E entity) {
return runTask(new Task<Integer>() {
return runTask(new QueryTask<Integer>() {
@Override
public Integer handle(Connection connection) throws Exception {
PreparedStatement updateStatement;
Expand All @@ -170,7 +171,7 @@ public Integer handle(Connection connection) throws Exception {
}

public Integer update(E entity, Set<String> fieldsToUpdate) {
return runTask(new Task<Integer>() {
return runTask(new QueryTask<Integer>() {
@Override
public Integer handle(Connection connection) throws Exception {
try (PreparedStatement updateStatement =
Expand All @@ -181,11 +182,11 @@ public Integer handle(Connection connection) throws Exception {
}, 0);
}

interface Task<T> {
interface QueryTask<T> {
T handle(Connection connection) throws Exception;
}

<T> T runTask(Task<T> task, T defaultReturnValue) {
<T> T runTask(QueryTask<T> task, T defaultReturnValue) {
try (Connection connection = getConnection()) {
return task.handle(connection);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.linkedin.thirdeye.datalayer.dao;

import com.linkedin.thirdeye.db.entity.AnomalyFeedback;
import com.linkedin.thirdeye.datalayer.entity.AnomalyFeedback;;

public class AnomalyFeedbackDAO extends AbstractBaseDAO<AnomalyFeedback> {
public class AnomalyFeedbackDAO extends AbstractBaseDAO<AnomalyFeedback>{

public AnomalyFeedbackDAO() {
super(AnomalyFeedback.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.linkedin.thirdeye.datalayer.dao;

import com.linkedin.thirdeye.datalayer.entity.AnomalyFunction;

public class AnomalyFunctionDAO extends AbstractBaseDAO<AnomalyFunction> {

public AnomalyFunctionDAO() {
super(AnomalyFunction.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.linkedin.thirdeye.datalayer.dao;

import com.linkedin.thirdeye.datalayer.entity.AnomalyMergedResult;

public class AnomalyMergedResultDAO extends AbstractBaseDAO<AnomalyMergedResult> {

public AnomalyMergedResultDAO() {
super(AnomalyMergedResult.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.linkedin.thirdeye.datalayer.dao;

import com.linkedin.thirdeye.datalayer.entity.EmailConfiguration;

public class EmailConfigurationDAO extends AbstractBaseDAO<EmailConfiguration> {

public EmailConfigurationDAO() {
super(EmailConfiguration.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.linkedin.thirdeye.datalayer.dao;

import com.linkedin.thirdeye.datalayer.entity.Job;
import com.linkedin.thirdeye.db.entity.AnomalyFeedback;

public class JobDAO extends AbstractBaseDAO<Job> {

public JobDAO() {
super(Job.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.linkedin.thirdeye.datalayer.dao;

import com.linkedin.thirdeye.datalayer.entity.Task;

public class TaskDAO extends AbstractBaseDAO<Task> {

public TaskDAO() {
super(Task.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.linkedin.thirdeye.datalayer.dao;

import com.linkedin.thirdeye.datalayer.entity.WebappConfig;

public class WebappConfigDAO extends AbstractBaseDAO<WebappConfig> {

public WebappConfigDAO() {
super(WebappConfig.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.linkedin.thirdeye.datalayer.util;

import com.google.common.collect.BiMap;
import com.linkedin.thirdeye.db.entity.AbstractBaseEntity;
import com.linkedin.thirdeye.datalayer.entity.AbstractJsonEntity;
import com.linkedin.thirdeye.db.entity.AnomalyTaskSpec;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -42,33 +42,33 @@ public GenericResultSetMapper(EntityMappingHolder entityMappingHolder) {
this.entityMappingHolder = entityMappingHolder;
}

public AbstractBaseEntity mapSingle(ResultSet rs, Class<? extends AbstractBaseEntity> entityClass)
public AbstractJsonEntity mapSingle(ResultSet rs, Class<? extends AbstractJsonEntity> entityClass)
throws Exception {
List<AbstractBaseEntity> resultMapList = toEntityList(rs, entityClass);
List<AbstractJsonEntity> resultMapList = toEntityList(rs, entityClass);
if (resultMapList.size() > 0) {
return resultMapList.get(0);
}
return null;
}

public List<AbstractBaseEntity> mapAll(ResultSet rs,
Class<? extends AbstractBaseEntity> entityClass) throws Exception {
public List<AbstractJsonEntity> mapAll(ResultSet rs,
Class<? extends AbstractJsonEntity> entityClass) throws Exception {
return toEntityList(rs, entityClass);
}



private List<AbstractBaseEntity> toEntityList(ResultSet rs,
Class<? extends AbstractBaseEntity> entityClass) throws Exception {
private List<AbstractJsonEntity> toEntityList(ResultSet rs,
Class<? extends AbstractJsonEntity> entityClass) throws Exception {
String tableName =
entityMappingHolder.tableToEntityNameMap.inverse().get(entityClass.getSimpleName());
LinkedHashMap<String, ColumnInfo> columnInfoMap =
entityMappingHolder.columnInfoPerTable.get(tableName);
List<AbstractBaseEntity> entityList = new ArrayList<>();
List<AbstractJsonEntity> entityList = new ArrayList<>();

ObjectMapper mapper = new ObjectMapper();
while (rs.next()) {
AbstractBaseEntity entityObj = entityClass.newInstance();
AbstractJsonEntity entityObj = entityClass.newInstance();
ResultSetMetaData resultSetMetaData = rs.getMetaData();
int numColumns = resultSetMetaData.getColumnCount();
ObjectNode objectNode = mapper.createObjectNode();
Expand Down Expand Up @@ -110,8 +110,8 @@ private List<AbstractBaseEntity> toEntityList(ResultSet rs,

}

public AbstractBaseEntity mapSingleOLD(ResultSet rs,
Class<? extends AbstractBaseEntity> entityClass) throws Exception {
public AbstractJsonEntity mapSingleOLD(ResultSet rs,
Class<? extends AbstractJsonEntity> entityClass) throws Exception {
List<Map<String, Object>> resultMapList = toResultMapList(rs, entityClass);
if (resultMapList.size() > 0) {
Map<String, Object> map = resultMapList.get(0);
Expand All @@ -122,28 +122,28 @@ public AbstractBaseEntity mapSingleOLD(ResultSet rs,
oos.close();
System.out.println(map);
}
AbstractBaseEntity entity = modelMapper.map(map, entityClass);
AbstractJsonEntity entity = modelMapper.map(map, entityClass);
System.out.println(entity);
return entity;
}
return null;
}

public List<AbstractBaseEntity> mapAllOLD(ResultSet rs,
Class<? extends AbstractBaseEntity> entityClass) throws Exception {
public List<AbstractJsonEntity> mapAllOLD(ResultSet rs,
Class<? extends AbstractJsonEntity> entityClass) throws Exception {
List<Map<String, Object>> resultMapList = toResultMapList(rs, entityClass);
List<AbstractBaseEntity> resultEntityList = new ArrayList<>();
List<AbstractJsonEntity> resultEntityList = new ArrayList<>();
if (resultMapList.size() > 0) {
for (Map<String, Object> map : resultMapList) {
AbstractBaseEntity entity = modelMapper.map(map, entityClass);
AbstractJsonEntity entity = modelMapper.map(map, entityClass);
resultEntityList.add(entity);
}
}
return resultEntityList;
}

List<Map<String, Object>> toResultMapList(ResultSet rs,
Class<? extends AbstractBaseEntity> entityClass) throws Exception {
Class<? extends AbstractJsonEntity> entityClass) throws Exception {
List<Map<String, Object>> resultMapList = new ArrayList<>();
String tableName =
entityMappingHolder.tableToEntityNameMap.inverse().get(entityClass.getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.common.collect.BiMap;
import com.google.common.collect.Sets;
import com.linkedin.thirdeye.db.entity.AbstractBaseEntity;
import com.linkedin.thirdeye.datalayer.entity.AbstractJsonEntity;
import com.mysql.jdbc.Statement;
import java.sql.Clob;
import java.sql.Connection;
Expand Down Expand Up @@ -64,15 +64,15 @@ public static String generateInsertSql(String tableName,
return sb.toString();
}

public PreparedStatement createInsertStatement(Connection conn, AbstractBaseEntity entity)
public PreparedStatement createInsertStatement(Connection conn, AbstractJsonEntity entity)
throws Exception {
String tableName =
entityMappingHolder.tableToEntityNameMap.inverse().get(entity.getClass().getSimpleName());
return createInsertStatement(conn, tableName, entity);
}

public PreparedStatement createInsertStatement(Connection conn, String tableName,
AbstractBaseEntity entity) throws Exception {
AbstractJsonEntity entity) throws Exception {
if (!insertSqlMap.containsKey(tableName)) {
String insertSql =
generateInsertSql(tableName, entityMappingHolder.columnInfoPerTable.get(tableName));
Expand Down Expand Up @@ -109,7 +109,7 @@ public PreparedStatement createInsertStatement(Connection conn, String tableName


public PreparedStatement createFindByIdStatement(Connection connection,
Class<? extends AbstractBaseEntity> entityClass, Long id) throws Exception {
Class<? extends AbstractJsonEntity> entityClass, Long id) throws Exception {
String tableName =
entityMappingHolder.tableToEntityNameMap.inverse().get(entityClass.getSimpleName());
String sql = "Select * from " + tableName + " where id=?";
Expand All @@ -119,7 +119,7 @@ public PreparedStatement createFindByIdStatement(Connection connection,
}

public PreparedStatement createFindByParamsStatement(Connection connection,
Class<? extends AbstractBaseEntity> entityClass, Map<String, Object> filters)
Class<? extends AbstractJsonEntity> entityClass, Map<String, Object> filters)
throws Exception {
String tableName =
entityMappingHolder.tableToEntityNameMap.inverse().get(entityClass.getSimpleName());
Expand Down Expand Up @@ -151,7 +151,7 @@ public PreparedStatement createFindByParamsStatement(Connection connection,
return prepareStatement;
}

public PreparedStatement createUpdateStatement(Connection connection, AbstractBaseEntity entity,
public PreparedStatement createUpdateStatement(Connection connection, AbstractJsonEntity entity,
Set<String> fieldsToUpdate) throws Exception {
String tableName =
entityMappingHolder.tableToEntityNameMap.inverse().get(entity.getClass().getSimpleName());
Expand Down Expand Up @@ -195,7 +195,7 @@ public PreparedStatement createUpdateStatement(Connection connection, AbstractBa
}

public PreparedStatement createDeleteByIdStatement(Connection connection,
Class<? extends AbstractBaseEntity> entityClass, Map<String, Object> filters)
Class<? extends AbstractJsonEntity> entityClass, Map<String, Object> filters)
throws Exception {
String tableName =
entityMappingHolder.tableToEntityNameMap.inverse().get(entityClass.getSimpleName());
Expand Down Expand Up @@ -225,7 +225,7 @@ public PreparedStatement createDeleteByIdStatement(Connection connection,


public PreparedStatement createFindAllStatement(Connection connection,
Class<? extends AbstractBaseEntity> entityClass) throws Exception {
Class<? extends AbstractJsonEntity> entityClass) throws Exception {
String tableName =
entityMappingHolder.tableToEntityNameMap.inverse().get(entityClass.getSimpleName());
String sql = "Select * from " + tableName;
Expand All @@ -234,7 +234,7 @@ public PreparedStatement createFindAllStatement(Connection connection,
}

public PreparedStatement createFindByParamsStatement(Connection connection,
Class<? extends AbstractBaseEntity> entityClass, Predicate predicate) throws Exception {
Class<? extends AbstractJsonEntity> entityClass, Predicate predicate) throws Exception {
String tableName =
entityMappingHolder.tableToEntityNameMap.inverse().get(entityClass.getSimpleName());
BiMap<String, String> entityNameToDBNameMapping =
Expand Down Expand Up @@ -286,7 +286,7 @@ private void generateWhereClause(BiMap<String, String> entityNameToDBNameMapping
}

public PreparedStatement createStatementFromSQL(Connection connection, String parameterizedSQL,
Map<String, Object> parameterMap, Class<? extends AbstractBaseEntity> entityClass)
Map<String, Object> parameterMap, Class<? extends AbstractJsonEntity> entityClass)
throws Exception {
String tableName =
entityMappingHolder.tableToEntityNameMap.inverse().get(entityClass.getSimpleName());
Expand Down