Skip to content

Commit

Permalink
chimera:optimise db call
Browse files Browse the repository at this point in the history
Motivation

as it has been suggested in review for the patch https://rb.dcache.org/r/13421/,
using  WHERE NOT EXISTS will use fewer DB round-trips and it doesn't force strong transactional isolation.

Target: master
Acked-by: Tigran Mkrtchyan
Requires-notes: no
Requires-book: no
  • Loading branch information
mksahakyan committed Oct 25, 2022
1 parent 14b9853 commit 93284a7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
Expand Up @@ -2044,14 +2044,14 @@ void addLabel(FsInode inode, String labelname) throws ChimeraFsException {
}, keyHolder);
Long label_id = (Long) keyHolder.getKeys().get("label_id");

//TODO change to WHERE NOT EXISTS
_jdbc.update("INSERT INTO t_labels_ref (label_id, inumber) VALUES (?,?)",
label_id, inode.ino());

} else {

Long label_id = getLabel(labelname);


Integer n = _jdbc.queryForObject(
"SELECT count(*) FROM t_labels_ref WHERE inumber=? and label_id = ?",
Integer.class, inode.ino(), label_id);
Expand All @@ -2060,6 +2060,21 @@ void addLabel(FsInode inode, String labelname) throws ChimeraFsException {
_jdbc.update("INSERT INTO t_labels_ref (label_id, inumber) VALUES (?,?)",
label_id, inode.ino());
}

_jdbc.update(
"INSERT INTO t_labels_ref (label_id, inumber) (SELECT * FROM (VALUES (?,?)) v WHERE NOT EXISTS "
+
"(SELECT label_id FROM t_labels_ref WHERE label_id = ? and inumber=?))",
ps -> {
ps.setLong(1, label_id);
ps.setLong(2, inode.ino());
ps.setLong(3, label_id);
ps.setLong(4, inode.ino());

});



}

} catch (EmptyResultDataAccessException e) {
Expand Down
Expand Up @@ -19,7 +19,9 @@
import com.google.common.base.Throwables;
import java.io.File;
import java.net.SocketException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -34,7 +36,10 @@
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

/**
* PostgreSQL 9.5 and later specific
Expand Down Expand Up @@ -312,6 +317,61 @@ void addInodeLocation(FsInode inode, int type, String location) {
});
}


/**
* Attache a given label to a given file system object.
*
* @param inode file system object.
* @param labelname label name.
* @throws ChimeraFsException
*/
void addLabel(FsInode inode, String labelname) throws ChimeraFsException {

KeyHolder keyHolder = new GeneratedKeyHolder();
try {

int n = _jdbc.update(
con -> {
PreparedStatement ps = con.prepareStatement(
"INSERT INTO t_labels ( labelname) VALUES (?)"
+ "ON CONFLICT ON CONSTRAINT labelname DO NOTHING",
Statement.RETURN_GENERATED_KEYS);
ps.setString(1, labelname);

return ps;
}, keyHolder);
if (n != 0) {
Long label_id = (Long) keyHolder.getKeys().get("label_id");

_jdbc.update("INSERT INTO t_labels_ref (label_id, inumber) VALUES (?,?)",
label_id, inode.ino());

} else {


Long label_id = getLabel(labelname);

_jdbc.update(
"INSERT INTO t_labels_ref (label_id, inumber) (SELECT * FROM (VALUES (?,?)) "
+ "ON CONFLICT ON CONSTRAINT i_label_pkey DO NOTHING",

ps -> {
ps.setLong(1, label_id);
ps.setLong(2, inode.ino());
ps.setLong(3, label_id);
ps.setLong(4, inode.ino());

});


}

} catch (EmptyResultDataAccessException e) {
throw new NoLabelChimeraException(labelname);
}

}

@Override
void copyTags(FsInode orign, FsInode destination) {
_jdbc.queryForList(
Expand Down

0 comments on commit 93284a7

Please sign in to comment.