Skip to content
Snippets Groups Projects
Verified Commit c5396f1d authored by Timm Fitschen's avatar Timm Fitschen
Browse files

WIP: String IDs

parent 21260e7a
Branches
Tags
2 merge requests!105Release 0.11.0,!78External IDs
Pipeline #41244 failed
Showing
with 123 additions and 124 deletions
......@@ -108,10 +108,10 @@ import org.caosdb.server.database.backend.interfaces.ListUsersImpl;
import org.caosdb.server.database.backend.interfaces.LogUserVisitImpl;
import org.caosdb.server.database.backend.interfaces.RetrieveAllImpl;
import org.caosdb.server.database.backend.interfaces.RetrieveAllUncheckedFilesImpl;
import org.caosdb.server.database.backend.interfaces.RetrieveCurrentMaxIdImpl;
import org.caosdb.server.database.backend.interfaces.RetrieveDatatypesImpl;
import org.caosdb.server.database.backend.interfaces.RetrieveEntityACLImpl;
import org.caosdb.server.database.backend.interfaces.RetrieveLogRecordImpl;
import org.caosdb.server.database.backend.interfaces.RetrieveCurrentMaxIdImpl;
import org.caosdb.server.database.backend.interfaces.RetrieveParentsImpl;
import org.caosdb.server.database.backend.interfaces.RetrievePasswordValidatorImpl;
import org.caosdb.server.database.backend.interfaces.RetrievePermissionRulesImpl;
......
......@@ -37,7 +37,7 @@ public class MySQLGetFileRecordByPath extends MySQLTransaction implements GetFil
}
public static final String STMT_GET_ID_BY_PATH =
"SELECT file_id, size, hex(hash) AS file_hash, checked_timestamp FROM files WHERE path=?";
"SELECT (Select id from entity_ids WHERE internal_id = files.file_id) as entity_id, size, hex(hash) AS file_hash, checked_timestamp FROM files WHERE path=?";
@Override
public SparseEntity execute(final String path) throws TransactionException {
......@@ -49,7 +49,7 @@ public class MySQLGetFileRecordByPath extends MySQLTransaction implements GetFil
try {
if (rs.next()) {
final SparseEntity ret = new SparseEntity();
ret.id = rs.getInt("file_id");
ret.id = rs.getInt("entity_id");
ret.fileHash = rs.getString("file_hash");
ret.fileSize = rs.getLong("size");
ret.fileChecked = rs.getLong("checked_timestamp");
......
......@@ -34,7 +34,7 @@ public class MySQLGetUpdateableChecksums extends MySQLTransaction
implements GetUpdateableChecksumsImpl {
private final String GET_UPDATEABLE_CHECKSUMS =
"SELECT file_id FROM files WHERE hash IS NULL LIMIT 1";
"SELECT (SELECT id FROM entity_ids WHERE internal_id = files.file_id) AS entity_id FROM files WHERE hash IS NULL LIMIT 1";
public MySQLGetUpdateableChecksums(final Access access) {
super(access);
......
......@@ -38,7 +38,7 @@ public class MySQLInsertSparseEntity extends MySQLTransaction implements InsertS
super(access);
}
public static final String STMT_INSERT_SPARSE_ENTITY = "call insertEntity(?,?,?,?)";
public static final String STMT_INSERT_SPARSE_ENTITY = "call insertEntity(?,?,?,?,?)";
public static final String STMT_INSERT_FILE_PROPERTIES = "call insertFile(?, ?, ?, ?)";
@Override
......@@ -57,7 +57,7 @@ public class MySQLInsertSparseEntity extends MySQLTransaction implements InsertS
if (rs.next()) {
entity.versionId = DatabaseUtils.bytes2UTF8(rs.getBytes("Version"));
} else {
throw new TransactionException("Didn't get new EntityID back.");
throw new TransactionException("Didn't get the version id back.");
}
}
......
......@@ -3,12 +3,12 @@ package org.caosdb.server.database.backend.implementation.MySQL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.caosdb.server.database.access.Access;
import org.caosdb.server.database.backend.interfaces.RetrieveCurrentMaxIdImpl;
import org.caosdb.server.database.exceptions.TransactionException;
public class MySQLRetrieveCurrentMaxId extends MySQLTransaction implements RetrieveCurrentMaxIdImpl {
public class MySQLRetrieveCurrentMaxId extends MySQLTransaction
implements RetrieveCurrentMaxIdImpl {
public MySQLRetrieveCurrentMaxId(Access access) {
super(access);
......@@ -19,9 +19,7 @@ public class MySQLRetrieveCurrentMaxId extends MySQLTransaction implements Retri
@Override
public Integer execute() {
try {
try (
PreparedStatement stmt = prepareStatement(STMT)
) {
try (PreparedStatement stmt = prepareStatement(STMT)) {
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
......@@ -35,5 +33,4 @@ try {
throw new TransactionException(e);
}
}
}
......@@ -19,7 +19,7 @@ public class MySQLSetFileChecksum extends MySQLTransaction implements SetFileChe
}
public static final String STMT_SET_CHECKSUM =
"UPDATE files SET hash = unhex(?) WHERE file_id = ?";
"UPDATE files SET hash = unhex(?) WHERE EXISTS (SELECT 1 FROM entity_ids AS eids WHERE eids.id = ? AND eids.internal_id = files.file_id)";
@Override
public void execute(final EntityID id, final String checksum) {
......
......@@ -37,7 +37,7 @@ public class MySQLSetQueryTemplateDefinition extends MySQLTransaction
}
public static final String STMT_INSERT_QUERY_TEMPLATE_DEF =
"INSERT INTO query_template_def (id, definition) VALUES (?,?) ON DUPLICATE KEY UPDATE definition=?;";
"INSERT INTO query_template_def (id, definition) VALUES ((SELECT internal_id FROM entity_ids WHERE entity_ids.id = ?),?) ON DUPLICATE KEY UPDATE definition=?;";
@Override
public void insert(final EntityID id, final String definition) {
......
......@@ -3,5 +3,4 @@ package org.caosdb.server.database.backend.interfaces;
public interface RetrieveCurrentMaxIdImpl extends BackendTransactionImpl {
public Integer execute();
}
......@@ -16,5 +16,4 @@ public class RetrieveCurrentMaxId extends BackendTransaction {
public Integer getCurrentMaxId() {
return this.maxId;
}
}
......@@ -647,7 +647,7 @@ public abstract class Entity extends AbstractObservable implements EntityInterfa
// Parse ID. Generate error if it isn't an integer.
if (element.getAttribute("id") != null && !element.getAttributeValue("id").equals("")) {
try {
setId(new EntityID(Integer.parseInt(element.getAttributeValue("id"))));
this.getId().setId(Integer.parseInt(element.getAttributeValue("id")));
} catch (final NumberFormatException e) {
addInfo("Id was " + element.getAttributeValue("id") + ".");
addError(ServerMessages.PARSING_FAILED);
......@@ -727,6 +727,9 @@ public abstract class Entity extends AbstractObservable implements EntityInterfa
// record.
final Property property = new Property(new WritableEntity(pe, Role.Property));
// Ignore parents of properties
property.getParents().clear();
property.setPIdx(pidx++);
addProperty(property);
} else if (pe.getName().equalsIgnoreCase("Parent")) {
......
......@@ -77,5 +77,4 @@ public class EntityID implements Serializable {
public static boolean isReserved(final EntityID id) {
return id.toInteger() < 100;
}
}
package org.caosdb.server.entity;
import org.caosdb.server.database.backend.transaction.RetrieveCurrentMaxId;
import org.caosdb.server.transaction.Transaction;
/**
* <p>
* Aufgaben:
*
* <ul>
......@@ -18,7 +16,6 @@ import org.caosdb.server.transaction.Transaction;
*
* @author tf
*/
public class EntityIdRegistry {
private boolean isInit;
......@@ -36,18 +33,15 @@ public class EntityIdRegistry {
}
private void initCurrentMaxId() {
this.currentMaxId = transaction.execute(new RetrieveCurrentMaxId(), transaction.getAccess()).getCurrentMaxId();
this.currentMaxId =
transaction.execute(new RetrieveCurrentMaxId(), transaction.getAccess()).getCurrentMaxId();
}
public Integer generate() {
if (!isInit) {
init();
}
++currentMaxId;
return ++currentMaxId;
}
public boolean isUnused(Integer i) {
return i>currentMaxId;
}
}
......@@ -194,6 +194,4 @@ private Integer currentMaxId =null;
public Query getQuery() {
return query;
}
}
package org.caosdb.server.jobs.core;
import org.caosdb.server.entity.EntityInterface;
......@@ -8,6 +7,7 @@ import org.caosdb.server.jobs.EntityJob;
import org.caosdb.server.jobs.JobAnnotation;
import org.caosdb.server.jobs.TransactionStage;
import org.caosdb.server.transaction.WriteTransactionInterface;
import org.caosdb.server.utils.EntityStatus;
@JobAnnotation(stage = TransactionStage.PRE_TRANSACTION)
public class EntityIdHandler extends EntityJob {
......@@ -15,10 +15,11 @@ public class EntityIdHandler extends EntityJob {
@Override
protected void run() throws Message {
EntityInterface entity = getEntity();
if(entity instanceof InsertEntity && getTransaction() instanceof WriteTransactionInterface) {
if (entity instanceof InsertEntity
&& getTransaction() instanceof WriteTransactionInterface
&& entity.getEntityStatus() == EntityStatus.QUALIFIED) {
Integer id = ((WriteTransactionInterface) getTransaction()).generateId();
entity.getId().setId(id);
}
}
}
......@@ -24,9 +24,9 @@ package org.caosdb.server.jobs.core;
import static org.caosdb.server.entity.MagicTypes.NAME;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import org.caosdb.server.datatype.AbstractDatatype;
import org.caosdb.server.entity.EntityID;
......@@ -133,19 +133,19 @@ public class ProcessNameProperties extends EntityJob {
}
private Collection<Object> getNearestValidParents(final Property prop) {
if (prop.hasId() && !prop.getId().isTemporary()) {
final ArrayList<Object> ret = new ArrayList<Object>();
if (prop.hasParents()) {
final Collection<Object> ret = new LinkedList<Object>();
for (final Parent par : prop.getParents()) {
if (par.hasId()) {
ret.add(par.getId());
}
} else {
ret.add(prop.getId());
}
return ret;
} else if (prop.hasId()) {
// property is new -> get valid parents of any depth
final EntityInterface propertyEntity = getEntityById(prop.getId());
EntityInterface propertyEntity = getEntityById(prop.getId());
if (propertyEntity == null) {
propertyEntity = retrieveValidEntity(prop.getId());
}
if (propertyEntity != null) {
return getNearestValidParents(propertyEntity);
}
......
......@@ -412,7 +412,14 @@ public class Query implements QueryInterface, ToElementable, TransactionInterfac
final Map<EntityID, String> queryTemplates = getQueryTemplates(query, resultSet);
final PreparedStatement removeQTStmt =
query.getConnection().prepareStatement("DELETE FROM `" + resultSet + "` WHERE id=?");
query
.getConnection()
.prepareStatement(
"DELETE FROM `"
+ resultSet
+ "` WHERE EXISTS (SELECT 1 FROM entity_ids AS eids WHERE eids.id=? AND eids.internal_id=`"
+ resultSet
+ "`.id)");
// Run thru all QTs found...
for (final Entry<EntityID, String> q : queryTemplates.entrySet()) {
......@@ -468,7 +475,7 @@ public class Query implements QueryInterface, ToElementable, TransactionInterfac
query
.getConnection()
.prepareCall(
"SELECT q.id, q.definition FROM query_template_def AS q INNER JOIN `"
"SELECT (SELECT eids.id FROM entity_ids AS eids WHERE eids.internal_id = q.id) as id, q.definition FROM query_template_def AS q INNER JOIN `"
+ resultSet
+ "` AS r ON (r.id=q.id);");
rs = stmt.executeQuery();
......@@ -645,15 +652,16 @@ public class Query implements QueryInterface, ToElementable, TransactionInterfac
private String generateSelectStatementForResultSet(
final String resultSetTableName, final boolean versioned) {
if (resultSetTableName.equals("entity_ids")) {
// TODO remove "DOMAIN"
final String baseStatement =
"SELECT (SELECT id FROM entity_ids WHERE internal_id = entities.id) as id, entity_acl.acl FROM entities INNER JOIN entity_acl ON entity_acl.id=entities.acl WHERE entities.role!='DOMAIN'";
"SELECT entities.id AS internal_id, (SELECT id FROM entity_ids WHERE internal_id = entities.id) as id, entity_acl.acl FROM entities INNER JOIN entity_acl ON entity_acl.id=entities.acl WHERE entities.role!='DOMAIN'";
if (!versioned) {
return baseStatement + ";";
}
// if versioned, the statement is surrounded with another SELECT and JOIN
return ("SELECT id, acl, version FROM ("
+ baseStatement
+ ") AS tmp JOIN entity_version ON entity_version.entity_id=tmp.id;");
+ ") AS tmp JOIN entity_version ON entity_version.entity_id=tmp.internal_id;");
} else {
if (!versioned) {
return (" SELECT (SELECT id FROM entity_ids WHERE internal_id = tmp.id) AS id, entity_acl.acl FROM "
......
......@@ -146,6 +146,7 @@ public class UpdateACL extends Transaction<TransactionContainer>
@Override
public Integer generateId() {
throw new UnsupportedOperationException("This is not implemented on purpose. This exception indicates a usage error of this UpdateACL class.");
throw new UnsupportedOperationException(
"This is not implemented on purpose. This exception indicates a usage error of this UpdateACL class.");
}
}
......@@ -599,7 +599,7 @@ private EntityIdRegistry idRegistry;
@Override
public Integer generateId() {
if (this.idRegistry == null) {
this.idRegistry = new EntityIdRegistry(getAccess());
this.idRegistry = new EntityIdRegistry(this);
}
return this.idRegistry.generate();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment