<commit_before>import mysql from "mysql";
import {PartialOrder} from "po-utils";

// "serverlessproject.c1kfax8igvaq.us-west-1.rds.amazonaws.com:3306"
// "vmwuser"
// "serverlessifc",
class SecureKV_PO {
    constructor(h, u, pwd, partialOrder) {
        this.con = mysql.createConnection({
            host: h,
            user: u,
            password: <PASSWORD>,
            database: "securekv"
        });
        this.po = new PartialOrder(partialOrder)
    }

    init(callback) {
        function createTable() {
            const createTableSql = `
CREATE TABLE kvstore (
    rowkey VARCHAR(31) NOT NULL,
    rowvalues VARCHAR(255),
    label INTEGER NOT NULL,
    PRIMARY KEY (rowkey, label)
);
            `;
            this.con.query(createTableSql, function (err, result) {
                if (err) callback(err);
                // console.log(result);
            });

            const cond = getCondFromPOTC(this.po.potc);

            const addInsertTrigger = `
CREATE TRIGGER PO_put_semantics BEFORE INSERT ON ? 
    DELETE FROM ? WHERE ?;
`;
            this.con.query(addInsertTrigger, ['kvstore', 'kvstore', cond], function (err, result) {
                if (err) callback(err);
                // console.log(result);
            });
        }

        this.con.connect(function (err) {
            if (err) callback(err);
            console.log("** Secure K-V (PO) Connected Successfully!")
        });

        const tableSql = `
SHOW TABLES like ?;
        `;
        this.con.query(tableSql, ['kvstore'], function (err, result) {
            if (err) callback(err);
            if (result.length === 0) {
                createTable()
            }
        })
    }

    close(callback) {
        this.con.end(function (err) {
            if (err) callback(err);
            console.out("** Secure K-V (PO) Connection Closed Successfully!")

        })
    }

    put (k, v, l, callback) {
        const sql = `
INSERT INTO kvstore (rowkey,rowvalues,label) 
    VALUES (?, ?, ?)
    ON DUPLICATE KEY UPDATE 
        rowvalues=VALUES(rowvalues);
        `;

        this.con.query(sql,[k,v,l], function (err, result) {
            if (err) callback(err);
            // console.log(result);
        });
    }

    /**
     * Performs a get operation from the key-value store for a given key and label.
     *
     * @param k Key
     * @param l Security label
     * @param callback
     */
    get (k, l, callback) {
        const sql = `
SELECT rowvalue 
FROM kvstore 
WHERE rowkey = ? AND
      label IN ?;
    `;

        // let gtLabels = new Set(this.po[l]);
        // gtLabels.delete(l);

        let leLabels = this.po.getAllLE(l);

        this.con.query(sql, [k,"(" + [...leLabels].join(", ") + ")"], function (err, result) {
            if (err) callback(err);
            if (result.length === 0) callback(null,"");
            else {
                callback(null,
                    result.reduce(function(max, curr) {
                        if (this.po.potc[max["label"]].has(curr["label"])) {
                            return curr;
                        } else {
                            return max;
                        }
                    })["rowvalue"]);
            }
        });
    }
}

module.exports.SecureKV_PO = SecureKV_PO;

function getCondFromPOTC(potc) {
    return "(" +<commit_msg>po k-v minor change<commit_after>import mysql from "mysql";
import {PartialOrder} from "po-utils";

// "serverlessproject.c1kfax8igvaq.us-west-1.rds.amazonaws.com:3306"
// "vmwuser"
// "serverlessifc",
class SecureKV_PO {
    constructor(h, u, pwd, partialOrder) {
        this.con = mysql.createConnection({
            host: h,
            user: u,
            password: <PASSWORD>,
            database: "securekv"
        });
        this.po = new PartialOrder(partialOrder)
    }

    init(callback) {
        function createTable() {
            const createTableSql = `
CREATE TABLE kvstore (
    rowkey VARCHAR(31) NOT NULL,
    rowvalues VARCHAR(255),
    label INTEGER NOT NULL,
    PRIMARY KEY (rowkey, label)
);
            `;
            this.con.query(createTableSql, function (err, result) {
                if (err) callback(err);
                // console.log(result);
            });

            const cond = getCondFromPOTC(this.po.potc);

            const addInsertTrigger = `
CREATE TRIGGER PO_put_semantics BEFORE INSERT ON ? 
    DELETE FROM ? WHERE ?;
`;
            this.con.query(addInsertTrigger, ['kvstore', 'kvstore', cond], function (err, result) {
                if (err) callback(err);
                // console.log(result);
            });
        }

        this.con.connect(function (err) {
            if (err) callback(err);
            console.log("** Secure K-V (PO) Connected Successfully!")
        });

        const tableSql = `
SHOW TABLES like ?;
        `;
        this.con.query(tableSql, ['kvstore'], function (err, result) {
            if (err) callback(err);
            if (result.length === 0) {
                createTable()
            }
        })
    }

    close(callback) {
        this.con.end(function (err) {
            if (err) callback(err);
            console.out("** Secure K-V (PO) Connection Closed Successfully!")

        })
    }

    put (k, v, l, callback) {
        const sql = `
INSERT INTO kvstore (rowkey,rowvalues,label) 
    VALUES (?, ?, ?)
    ON DUPLICATE KEY UPDATE 
        rowvalues=VALUES(rowvalues);
        `;

        this.con.query(sql,[k,v,l], function (err, result) {
            if (err) callback(err);
            // console.log(result);
        });
    }

    /**
     * Performs a get operation from the key-value store for a given key and label.
     *
     * @param k Key
     * @param l Security label
     * @param callback
     */
    get (k, l, callback) {
        const sql = `
SELECT rowvalue 
FROM kvstore 
WHERE rowkey = ? AND
      label IN ?;
    `;

        // let gtLabels = new Set(this.po[l]);
        // gtLabels.delete(l);

        let leLabels = this.po.getAllLE(l);

        this.con.query(sql, [k,"(" + [...leLabels].join(", ") + ")"], function (err, result) {
            if (err) callback(err);
            if (result.length === 0) callback(null,"");
            else {
                callback(null,
                    result.reduce(function(max, curr) {
                        if (this.po.potc[max["label"]].has(curr["label"])) {
                            return curr;
                        } else {
                            return max;
                        }
                    })["rowvalue"]);
            }
        });
    }
}

export {SecureKV_PO}

function getCondFromPOTC(potc) {
    return "(" +
{'fraction_non_alphanumeric': 0.13475600656618414, 'fraction_numerical': 0.004178480823757648, 'mean_word_length': 1.6077821011673152, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2342046', 'n_tokens_mistral': 2083, 'n_tokens_neox': 1989, 'n_words': 457}