Copilot commented on code in PR #402: URL: https://github.com/apache/atlas/pull/402#discussion_r2212185233
########## repository/src/main/resources/META-INF/postgres/create_sequences.sql: ########## @@ -0,0 +1,16 @@ +-- Licensed to the Apache Software Foundation(ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You 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, softwaren Review Comment: There is a typo in the comment. 'softwaren' should be 'software'. ```suggestion -- Unless required by applicable law or agreed to in writing, software ``` ########## repository/src/main/resources/META-INF/postgres/create_schema.sql: ########## @@ -0,0 +1,22 @@ +-- Licensed to the Apache Software Foundation(ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You 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, softwaren Review Comment: There is a typo in the comment. 'softwaren' should be 'software'. ```suggestion -- Unless required by applicable law or agreed to in writing, software ``` ########## graphdb/janusgraph-rdbms/src/main/resources/META-INF/postgres/create_schema.sql: ########## @@ -0,0 +1,89 @@ +-- Licensed to the Apache Software Foundation(ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You 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, softwaren Review Comment: There is a typo in the comment. 'softwaren' should be 'software'. ```suggestion -- Unless required by applicable law or agreed to in writing, software ``` ########## graphdb/janusgraph-rdbms/src/main/java/org/janusgraph/diskstorage/rdbms/RdbmsUniqueKeyHandler.java: ########## @@ -0,0 +1,142 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.janusgraph.diskstorage.rdbms; + +import org.janusgraph.graphdb.relations.RelationIdentifier; + +public class RdbmsUniqueKeyHandler { + private static final String SQL_INSERT_UNIQUE_VERTEX_KEY = "INSERT INTO janus_unique_vertex_key (id, vertex_id, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY = "DELETE FROM janus_unique_vertex_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY = "INSERT INTO janus_unique_vertex_type_key (id, vertex_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY = "DELETE FROM janus_unique_vertex_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_key WHERE vertex_id = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_type_key WHERE vertex_id = ?"; + + private static final String SQL_INSERT_UNIQUE_EDGE_KEY = "INSERT INTO janus_unique_edge_key (id, edge_id, key_name, val) VALUES (NEXTVAL('janus_unique_edge_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY = "DELETE FROM janus_unique_edge_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_EDGE_TYPE_KEY = "INSERT INTO janus_unique_edge_type_key (id, edge_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_edge_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY = "DELETE FROM janus_unique_edge_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_key WHERE edge_id = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_type_key WHERE edge_id = ?"; + + public void addUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_KEY : SQL_INSERT_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) Review Comment: The parameter binding uses string literals instead of parameter indices. Should use .setParameter(1, ...) instead of .setParameter("1", ...). ```suggestion .setParameter(1, getNumberId(elementId)) .setParameter(2, keyName) .setParameter(3, value) ``` ########## repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasGraphUtilsV2.java: ########## @@ -229,17 +231,56 @@ public static <T extends AtlasElement> void setProperty(T element, String proper LOG.debug("==> setProperty({}, {}, {})", toString(element), propertyName, value); } + AtlasUniqueKeyHandler uniqueKeyHandler = getGraphInstance().getUniqueKeyHandler();; Review Comment: Remove the extra semicolon at the end of the line. ```suggestion AtlasUniqueKeyHandler uniqueKeyHandler = getGraphInstance().getUniqueKeyHandler(); ``` ########## graphdb/janusgraph-rdbms/src/main/java/org/janusgraph/diskstorage/rdbms/RdbmsUniqueKeyHandler.java: ########## @@ -0,0 +1,142 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.janusgraph.diskstorage.rdbms; + +import org.janusgraph.graphdb.relations.RelationIdentifier; + +public class RdbmsUniqueKeyHandler { + private static final String SQL_INSERT_UNIQUE_VERTEX_KEY = "INSERT INTO janus_unique_vertex_key (id, vertex_id, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY = "DELETE FROM janus_unique_vertex_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY = "INSERT INTO janus_unique_vertex_type_key (id, vertex_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY = "DELETE FROM janus_unique_vertex_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_key WHERE vertex_id = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_type_key WHERE vertex_id = ?"; + + private static final String SQL_INSERT_UNIQUE_EDGE_KEY = "INSERT INTO janus_unique_edge_key (id, edge_id, key_name, val) VALUES (NEXTVAL('janus_unique_edge_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY = "DELETE FROM janus_unique_edge_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_EDGE_TYPE_KEY = "INSERT INTO janus_unique_edge_type_key (id, edge_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_edge_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY = "DELETE FROM janus_unique_edge_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_key WHERE edge_id = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_type_key WHERE edge_id = ?"; + + public void addUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_KEY : SQL_INSERT_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to add unique key: keyName=" + keyName + ", value=" + value); + } + } + + public void removeUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_DELETE_UNIQUE_VERTEX_KEY : SQL_DELETE_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to remove unique key: keyName=" + keyName + ", value=" + value); + } + } + + public void addTypeUniqueKey(String typeName, String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY : SQL_INSERT_UNIQUE_EDGE_TYPE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", typeName) + .setParameter("3", keyName) + .setParameter("4", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to add type unique key: typeName=" + typeName + ", keyName=" + keyName + ", value=" + value); + } + } + + public void removeTypeUniqueKey(String typeName, String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY : SQL_DELETE_UNIQUE_EDGE_TYPE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", typeName) + .setParameter("3", keyName) + .setParameter("4", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to remove type unique key: typeName=" + typeName + ", keyName=" + keyName + ", value=" + value); + } + } + + public void removeUniqueKeysForVertexId(Object vertexId) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + final Number id = getNumberId(vertexId); + + trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_VERTEX_KEY_BY_VERTEX_ID) + .setParameter("1", id) + .executeUpdate(); + + trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID) + .setParameter("1", id) Review Comment: The parameter binding uses string literals instead of parameter indices. Should use .setParameter(1, ...) instead of .setParameter("1", ...). ```suggestion .setParameter(1, id) .executeUpdate(); trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID) .setParameter(1, id) ``` ########## graphdb/janusgraph-rdbms/src/main/resources/META-INF/postgres/create_sequences.sql: ########## @@ -0,0 +1,24 @@ +-- Licensed to the Apache Software Foundation(ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You 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, softwaren Review Comment: There is a typo in the comment. 'softwaren' should be 'software'. ```suggestion -- Unless required by applicable law or agreed to in writing, software ``` ########## graphdb/janusgraph-rdbms/src/main/java/org/janusgraph/diskstorage/rdbms/RdbmsUniqueKeyHandler.java: ########## @@ -0,0 +1,142 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.janusgraph.diskstorage.rdbms; + +import org.janusgraph.graphdb.relations.RelationIdentifier; + +public class RdbmsUniqueKeyHandler { + private static final String SQL_INSERT_UNIQUE_VERTEX_KEY = "INSERT INTO janus_unique_vertex_key (id, vertex_id, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY = "DELETE FROM janus_unique_vertex_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY = "INSERT INTO janus_unique_vertex_type_key (id, vertex_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY = "DELETE FROM janus_unique_vertex_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_key WHERE vertex_id = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_type_key WHERE vertex_id = ?"; + + private static final String SQL_INSERT_UNIQUE_EDGE_KEY = "INSERT INTO janus_unique_edge_key (id, edge_id, key_name, val) VALUES (NEXTVAL('janus_unique_edge_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY = "DELETE FROM janus_unique_edge_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_EDGE_TYPE_KEY = "INSERT INTO janus_unique_edge_type_key (id, edge_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_edge_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY = "DELETE FROM janus_unique_edge_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_key WHERE edge_id = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_type_key WHERE edge_id = ?"; + + public void addUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_KEY : SQL_INSERT_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to add unique key: keyName=" + keyName + ", value=" + value); + } + } + + public void removeUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_DELETE_UNIQUE_VERTEX_KEY : SQL_DELETE_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to remove unique key: keyName=" + keyName + ", value=" + value); + } + } + + public void addTypeUniqueKey(String typeName, String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY : SQL_INSERT_UNIQUE_EDGE_TYPE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", typeName) + .setParameter("3", keyName) + .setParameter("4", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to add type unique key: typeName=" + typeName + ", keyName=" + keyName + ", value=" + value); + } + } + + public void removeTypeUniqueKey(String typeName, String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY : SQL_DELETE_UNIQUE_EDGE_TYPE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", typeName) + .setParameter("3", keyName) + .setParameter("4", value) Review Comment: The parameter binding uses string literals instead of parameter indices. Should use .setParameter(1, ...) instead of .setParameter("1", ...). ```suggestion .setParameter(1, getNumberId(elementId)) .setParameter(2, typeName) .setParameter(3, keyName) .setParameter(4, value) ``` ########## graphdb/janusgraph-rdbms/src/main/java/org/janusgraph/diskstorage/rdbms/RdbmsUniqueKeyHandler.java: ########## @@ -0,0 +1,142 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.janusgraph.diskstorage.rdbms; + +import org.janusgraph.graphdb.relations.RelationIdentifier; + +public class RdbmsUniqueKeyHandler { + private static final String SQL_INSERT_UNIQUE_VERTEX_KEY = "INSERT INTO janus_unique_vertex_key (id, vertex_id, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY = "DELETE FROM janus_unique_vertex_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY = "INSERT INTO janus_unique_vertex_type_key (id, vertex_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY = "DELETE FROM janus_unique_vertex_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_key WHERE vertex_id = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_type_key WHERE vertex_id = ?"; + + private static final String SQL_INSERT_UNIQUE_EDGE_KEY = "INSERT INTO janus_unique_edge_key (id, edge_id, key_name, val) VALUES (NEXTVAL('janus_unique_edge_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY = "DELETE FROM janus_unique_edge_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_EDGE_TYPE_KEY = "INSERT INTO janus_unique_edge_type_key (id, edge_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_edge_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY = "DELETE FROM janus_unique_edge_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_key WHERE edge_id = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_type_key WHERE edge_id = ?"; + + public void addUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_KEY : SQL_INSERT_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to add unique key: keyName=" + keyName + ", value=" + value); + } + } + + public void removeUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_DELETE_UNIQUE_VERTEX_KEY : SQL_DELETE_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) Review Comment: The parameter binding uses string literals instead of parameter indices. Should use .setParameter(1, ...) instead of .setParameter("1", ...). ```suggestion .setParameter(1, getNumberId(elementId)) .setParameter(2, keyName) .setParameter(3, value) ``` ########## repository/src/main/java/org/apache/atlas/repository/audit/rdbms/RdbmsBasedAuditRepository.java: ########## @@ -0,0 +1,171 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.apache.atlas.repository.audit.rdbms; + +import org.apache.atlas.ApplicationProperties; +import org.apache.atlas.AtlasException; +import org.apache.atlas.EntityAuditEvent; +import org.apache.atlas.annotation.ConditionalOnAtlasProperty; +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.audit.EntityAuditEventV2; +import org.apache.atlas.repository.audit.AbstractStorageBasedAuditRepository; +import org.apache.atlas.repository.audit.rdbms.dao.DaoManager; +import org.apache.atlas.repository.audit.rdbms.dao.DbEntityAuditDao; +import org.apache.atlas.repository.audit.rdbms.entity.DbEntityAudit; +import org.apache.commons.configuration.Configuration; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import javax.inject.Singleton; + +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +@Singleton +@Component +@ConditionalOnAtlasProperty(property = "atlas.EntityAuditRepository.impl", isDefault = true) +@Order(0) +public class RdbmsBasedAuditRepository extends AbstractStorageBasedAuditRepository { + private DaoManager daoManager; + + @PostConstruct + public void init() { + daoManager = new DaoManager(getConfiguration().subset("atlas.graph.storage.rdbms.jpa")); + } + + @Override + public void putEventsV1(List<EntityAuditEvent> events) throws AtlasException { + // TODO: is V1 supported needed anymore? + } + + @Override + public List<EntityAuditEvent> listEventsV1(String entityId, String startKey, short n) throws AtlasException { + // TODO: is V1 supported needed anymore? Review Comment: Grammar error in comment. Should be 'is V1 support needed anymore?' or 'is V1 still needed?' ```suggestion // TODO: is V1 support needed anymore? } @Override public List<EntityAuditEvent> listEventsV1(String entityId, String startKey, short n) throws AtlasException { // TODO: is V1 support needed anymore? ``` ########## graphdb/janusgraph-rdbms/src/main/java/org/janusgraph/diskstorage/rdbms/RdbmsUniqueKeyHandler.java: ########## @@ -0,0 +1,142 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.janusgraph.diskstorage.rdbms; + +import org.janusgraph.graphdb.relations.RelationIdentifier; + +public class RdbmsUniqueKeyHandler { + private static final String SQL_INSERT_UNIQUE_VERTEX_KEY = "INSERT INTO janus_unique_vertex_key (id, vertex_id, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY = "DELETE FROM janus_unique_vertex_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY = "INSERT INTO janus_unique_vertex_type_key (id, vertex_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY = "DELETE FROM janus_unique_vertex_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_key WHERE vertex_id = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_type_key WHERE vertex_id = ?"; + + private static final String SQL_INSERT_UNIQUE_EDGE_KEY = "INSERT INTO janus_unique_edge_key (id, edge_id, key_name, val) VALUES (NEXTVAL('janus_unique_edge_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY = "DELETE FROM janus_unique_edge_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_EDGE_TYPE_KEY = "INSERT INTO janus_unique_edge_type_key (id, edge_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_edge_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY = "DELETE FROM janus_unique_edge_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_key WHERE edge_id = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_type_key WHERE edge_id = ?"; + + public void addUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_KEY : SQL_INSERT_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to add unique key: keyName=" + keyName + ", value=" + value); + } + } + + public void removeUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_DELETE_UNIQUE_VERTEX_KEY : SQL_DELETE_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to remove unique key: keyName=" + keyName + ", value=" + value); + } + } + + public void addTypeUniqueKey(String typeName, String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY : SQL_INSERT_UNIQUE_EDGE_TYPE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", typeName) + .setParameter("3", keyName) + .setParameter("4", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to add type unique key: typeName=" + typeName + ", keyName=" + keyName + ", value=" + value); + } + } + + public void removeTypeUniqueKey(String typeName, String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY : SQL_DELETE_UNIQUE_EDGE_TYPE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", typeName) + .setParameter("3", keyName) + .setParameter("4", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to remove type unique key: typeName=" + typeName + ", keyName=" + keyName + ", value=" + value); + } + } + + public void removeUniqueKeysForVertexId(Object vertexId) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + final Number id = getNumberId(vertexId); + + trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_VERTEX_KEY_BY_VERTEX_ID) + .setParameter("1", id) + .executeUpdate(); + + trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID) + .setParameter("1", id) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to remove unique keys for vertex: vertexId=" + vertexId); + } + } + + public void removeUniqueKeysForEdgeId(Object edgeId) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + final Number id = getNumberId(edgeId); + + trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_EDGE_KEY_BY_EDGE_ID) + .setParameter("1", id) + .executeUpdate(); + + trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_EDGE_TYPE_KEY_BY_EDGE_ID) + .setParameter("1", id) Review Comment: The parameter binding uses string literals instead of parameter indices. Should use .setParameter(1, ...) instead of .setParameter("1", ...). ```suggestion .setParameter(1, id) .executeUpdate(); trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_EDGE_TYPE_KEY_BY_EDGE_ID) .setParameter(1, id) ``` ########## graphdb/janusgraph-rdbms/src/main/java/org/janusgraph/diskstorage/rdbms/RdbmsUniqueKeyHandler.java: ########## @@ -0,0 +1,142 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.janusgraph.diskstorage.rdbms; + +import org.janusgraph.graphdb.relations.RelationIdentifier; + +public class RdbmsUniqueKeyHandler { + private static final String SQL_INSERT_UNIQUE_VERTEX_KEY = "INSERT INTO janus_unique_vertex_key (id, vertex_id, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY = "DELETE FROM janus_unique_vertex_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY = "INSERT INTO janus_unique_vertex_type_key (id, vertex_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY = "DELETE FROM janus_unique_vertex_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_key WHERE vertex_id = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_type_key WHERE vertex_id = ?"; + + private static final String SQL_INSERT_UNIQUE_EDGE_KEY = "INSERT INTO janus_unique_edge_key (id, edge_id, key_name, val) VALUES (NEXTVAL('janus_unique_edge_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY = "DELETE FROM janus_unique_edge_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_EDGE_TYPE_KEY = "INSERT INTO janus_unique_edge_type_key (id, edge_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_edge_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY = "DELETE FROM janus_unique_edge_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_key WHERE edge_id = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_type_key WHERE edge_id = ?"; + + public void addUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_KEY : SQL_INSERT_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to add unique key: keyName=" + keyName + ", value=" + value); + } + } + + public void removeUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_DELETE_UNIQUE_VERTEX_KEY : SQL_DELETE_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to remove unique key: keyName=" + keyName + ", value=" + value); + } + } + + public void addTypeUniqueKey(String typeName, String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY : SQL_INSERT_UNIQUE_EDGE_TYPE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", typeName) + .setParameter("3", keyName) + .setParameter("4", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to add type unique key: typeName=" + typeName + ", keyName=" + keyName + ", value=" + value); + } + } + + public void removeTypeUniqueKey(String typeName, String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY : SQL_DELETE_UNIQUE_EDGE_TYPE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", typeName) + .setParameter("3", keyName) + .setParameter("4", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to remove type unique key: typeName=" + typeName + ", keyName=" + keyName + ", value=" + value); + } + } + + public void removeUniqueKeysForVertexId(Object vertexId) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + final Number id = getNumberId(vertexId); + + trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_VERTEX_KEY_BY_VERTEX_ID) + .setParameter("1", id) + .executeUpdate(); + + trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID) + .setParameter("1", id) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to remove unique keys for vertex: vertexId=" + vertexId); + } + } + + public void removeUniqueKeysForEdgeId(Object edgeId) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + final Number id = getNumberId(edgeId); + + trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_EDGE_KEY_BY_EDGE_ID) + .setParameter("1", id) + .executeUpdate(); + + trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_EDGE_TYPE_KEY_BY_EDGE_ID) + .setParameter("1", id) Review Comment: The parameter binding uses string literals instead of parameter indices. Should use .setParameter(1, ...) instead of .setParameter("1", ...). ```suggestion .setParameter(1, id) .executeUpdate(); trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_EDGE_TYPE_KEY_BY_EDGE_ID) .setParameter(1, id) ``` ########## graphdb/janusgraph-rdbms/src/main/java/org/janusgraph/diskstorage/rdbms/RdbmsUniqueKeyHandler.java: ########## @@ -0,0 +1,142 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.janusgraph.diskstorage.rdbms; + +import org.janusgraph.graphdb.relations.RelationIdentifier; + +public class RdbmsUniqueKeyHandler { + private static final String SQL_INSERT_UNIQUE_VERTEX_KEY = "INSERT INTO janus_unique_vertex_key (id, vertex_id, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY = "DELETE FROM janus_unique_vertex_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY = "INSERT INTO janus_unique_vertex_type_key (id, vertex_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY = "DELETE FROM janus_unique_vertex_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_key WHERE vertex_id = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_type_key WHERE vertex_id = ?"; + + private static final String SQL_INSERT_UNIQUE_EDGE_KEY = "INSERT INTO janus_unique_edge_key (id, edge_id, key_name, val) VALUES (NEXTVAL('janus_unique_edge_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY = "DELETE FROM janus_unique_edge_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_EDGE_TYPE_KEY = "INSERT INTO janus_unique_edge_type_key (id, edge_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_edge_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY = "DELETE FROM janus_unique_edge_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_key WHERE edge_id = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_type_key WHERE edge_id = ?"; + + public void addUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_KEY : SQL_INSERT_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to add unique key: keyName=" + keyName + ", value=" + value); + } + } + + public void removeUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_DELETE_UNIQUE_VERTEX_KEY : SQL_DELETE_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to remove unique key: keyName=" + keyName + ", value=" + value); + } + } + + public void addTypeUniqueKey(String typeName, String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY : SQL_INSERT_UNIQUE_EDGE_TYPE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", typeName) + .setParameter("3", keyName) + .setParameter("4", value) Review Comment: The parameter binding uses string literals instead of parameter indices. Should use .setParameter(1, ...) instead of .setParameter("1", ...). ```suggestion .setParameter(1, getNumberId(elementId)) .setParameter(2, typeName) .setParameter(3, keyName) .setParameter(4, value) ``` ########## repository/src/main/java/org/apache/atlas/repository/audit/rdbms/RdbmsBasedAuditRepository.java: ########## @@ -0,0 +1,171 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.apache.atlas.repository.audit.rdbms; + +import org.apache.atlas.ApplicationProperties; +import org.apache.atlas.AtlasException; +import org.apache.atlas.EntityAuditEvent; +import org.apache.atlas.annotation.ConditionalOnAtlasProperty; +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.audit.EntityAuditEventV2; +import org.apache.atlas.repository.audit.AbstractStorageBasedAuditRepository; +import org.apache.atlas.repository.audit.rdbms.dao.DaoManager; +import org.apache.atlas.repository.audit.rdbms.dao.DbEntityAuditDao; +import org.apache.atlas.repository.audit.rdbms.entity.DbEntityAudit; +import org.apache.commons.configuration.Configuration; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import javax.inject.Singleton; + +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +@Singleton +@Component +@ConditionalOnAtlasProperty(property = "atlas.EntityAuditRepository.impl", isDefault = true) +@Order(0) +public class RdbmsBasedAuditRepository extends AbstractStorageBasedAuditRepository { + private DaoManager daoManager; + + @PostConstruct + public void init() { + daoManager = new DaoManager(getConfiguration().subset("atlas.graph.storage.rdbms.jpa")); + } + + @Override + public void putEventsV1(List<EntityAuditEvent> events) throws AtlasException { + // TODO: is V1 supported needed anymore? + } + + @Override + public List<EntityAuditEvent> listEventsV1(String entityId, String startKey, short n) throws AtlasException { + // TODO: is V1 supported needed anymore? Review Comment: Grammar error in comment. Should be 'is V1 support needed anymore?' or 'is V1 still needed?' ```suggestion // TODO: is V1 support needed anymore? } @Override public List<EntityAuditEvent> listEventsV1(String entityId, String startKey, short n) throws AtlasException { // TODO: is V1 support needed anymore? ``` ########## graphdb/janusgraph-rdbms/src/main/java/org/janusgraph/diskstorage/rdbms/RdbmsUniqueKeyHandler.java: ########## @@ -0,0 +1,142 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.janusgraph.diskstorage.rdbms; + +import org.janusgraph.graphdb.relations.RelationIdentifier; + +public class RdbmsUniqueKeyHandler { + private static final String SQL_INSERT_UNIQUE_VERTEX_KEY = "INSERT INTO janus_unique_vertex_key (id, vertex_id, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY = "DELETE FROM janus_unique_vertex_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY = "INSERT INTO janus_unique_vertex_type_key (id, vertex_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_vertex_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY = "DELETE FROM janus_unique_vertex_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_key WHERE vertex_id = ?"; + private static final String SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID = "DELETE FROM janus_unique_vertex_type_key WHERE vertex_id = ?"; + + private static final String SQL_INSERT_UNIQUE_EDGE_KEY = "INSERT INTO janus_unique_edge_key (id, edge_id, key_name, val) VALUES (NEXTVAL('janus_unique_edge_key_seq'), ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY = "DELETE FROM janus_unique_edge_key WHERE key_name = ? AND val = ?"; + private static final String SQL_INSERT_UNIQUE_EDGE_TYPE_KEY = "INSERT INTO janus_unique_edge_type_key (id, edge_id, type_name, key_name, val) VALUES (NEXTVAL('janus_unique_edge_type_key_seq'), ?, ?, ?, ?)"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY = "DELETE FROM janus_unique_edge_type_key WHERE type_name = ? AND key_name = ? AND val = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_key WHERE edge_id = ?"; + private static final String SQL_DELETE_UNIQUE_EDGE_TYPE_KEY_BY_EDGE_ID = "DELETE FROM janus_unique_edge_type_key WHERE edge_id = ?"; + + public void addUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_KEY : SQL_INSERT_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to add unique key: keyName=" + keyName + ", value=" + value); + } + } + + public void removeUniqueKey(String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_DELETE_UNIQUE_VERTEX_KEY : SQL_DELETE_UNIQUE_EDGE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", keyName) + .setParameter("3", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to remove unique key: keyName=" + keyName + ", value=" + value); + } + } + + public void addTypeUniqueKey(String typeName, String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_INSERT_UNIQUE_VERTEX_TYPE_KEY : SQL_INSERT_UNIQUE_EDGE_TYPE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", typeName) + .setParameter("3", keyName) + .setParameter("4", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to add type unique key: typeName=" + typeName + ", keyName=" + keyName + ", value=" + value); + } + } + + public void removeTypeUniqueKey(String typeName, String keyName, Object value, Object elementId, boolean isVertex) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + trx.getEntityManager().createNativeQuery(isVertex ? SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY : SQL_DELETE_UNIQUE_EDGE_TYPE_KEY) + .setParameter("1", getNumberId(elementId)) + .setParameter("2", typeName) + .setParameter("3", keyName) + .setParameter("4", value) + .executeUpdate(); + } else { + throw new IllegalStateException("No active transaction found to remove type unique key: typeName=" + typeName + ", keyName=" + keyName + ", value=" + value); + } + } + + public void removeUniqueKeysForVertexId(Object vertexId) { + RdbmsTransaction trx = RdbmsTransaction.getActiveTransaction(); + + if (trx != null) { + final Number id = getNumberId(vertexId); + + trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_VERTEX_KEY_BY_VERTEX_ID) + .setParameter("1", id) + .executeUpdate(); + + trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID) + .setParameter("1", id) Review Comment: The parameter binding uses string literals instead of parameter indices. Should use .setParameter(1, ...) instead of .setParameter("1", ...). ```suggestion .setParameter(1, id) .executeUpdate(); trx.getEntityManager().createNativeQuery(SQL_DELETE_UNIQUE_VERTEX_TYPE_KEY_BY_VERTEX_ID) .setParameter(1, id) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@atlas.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org