<commit_before>"use strict"

const crypto = require('crypto');

// RDF constants
const RDF_SYNTAX = '<http://www.w3.org/1999/02/22-rdf-syntax-ns#';
const FLOW_VOCAB = '<http://schema.jillix.net/vocab/';
const SCHEMA = '<http://schema.org/';

function inArray(array, id) {
    for (let i = 0, l = array.length; i < l; ++i) {
        if (array[i].id === id) {
            return true;
        }
    }
}

// TODO clean up!
// TODO remove functionality

// request types:
// add + name + object  ->  node.id > type > "name" + "object"
// add + name           ->  node.id > type > "name"
// set + name + object  ->  node.id > type > "name" + "object"
// set + name           ->  node.id > type > "name"

// HASH > string > ""
function setHash (type, value) {
    // hash value
    // exists logic
    // set type
}

// UID > type > HASH
// UID > type > UID
function setTriple (subject, predicate, object) {
    // check if triple exists
    // set triple
}
function addTriple (subject, predicate, object) {
    // add triple
}

// create HASH > string > ""
// create UID/HASH > name > HASH
// delete HASH (outs, ins)
// delete UID (outs, ins)
//exports.create = (client, type, name, object) => {

//};

//exports.set_name = (client, id, type, name) => {
    // from > edge > to
    // name
//};

//exports.set_object = (client, id, type, name) => {
    // from > edge > to
    // name + object
//};

module.exports = (client, req) => {

    switch (req.mode) {
        case "set":
            // ensure: node > type > hash ..
            console.log('Set ' + req.type + ' "' + req.name + '" on node "' + req.node + '"');
            break;
        case "add":
            // add: node > type > hash
            console.log('Add ' + req.type + ' "' + req.name + '" to node "' + req.node + '"');
            break;
        case "edit":
            // edit node 
            console.log('Change ' + req.type + ' to "' + req.name + '" of node "' + req.node + '"');
            break;
    }

    console.log('Has object:', req.object);

    return; 

    if (triple.length !== 3) {
        return new Error('Flow-api.put: Invalid triple "' + triple.join(', ') + '"');
    }

    const hash = crypto.createHash('md5').update(triple[2]).digest('hex');

    // check if hash exists and get in edges
    client.g.V('_:' + hash).In().GetLimit(1, (err, result) => {

        if (err) {
            return err;
        }

        if (result) {

            // check if hash has in connection not from node.id 
            if (!inArray(result, triple[0])) {

                // get existing name
                client.g.V(triple[0]).Out(SCHEMA + "name>").GetLimit(1, (err, result) => {

                    if (err) {
                        return;
                    }

                    const existingName = result[0].id;

                    client.delete([{
                        subject: triple[0],
                        predicate: SCHEMA + "name>",
                        object: existingName
                    }], (err, body) => {

                        if (err || body.error) {
                            return;
                        }

                        client.write([{
                            subject: triple[0],
                            predicate: SCHEMA + "name>",
                            object: '_:' + hash
                        }], (err, body) => {
                            console.log('Cayley.PUT: Edge added:', err, body)
                        });
                    });
                });
            }

            return;
        }

        // create hash and edge to node.id
        const nameTriples = [

            // id > name > hash 
            {
                subject: triple[0],
                predicate: SCHEMA + "name>",
                object: "_:" + hash
            },
            // hash > string > ""
            {
                subject: "_:" + hash,
                predicate: RDF_SYNTAX + "string>",
                object: triple[2]
            },
            // hash > type > NAME
            {
                subject: "_:" + hash,
                predicate: RDF_SYNTAX + "type>",
                object: FLOW_VOCAB + "Name>"
            }
        ];

        // get existing name
        client.g.V(triple[0]).Out(SCHEMA + "name>").GetLimit(1, (err, result) => {

            if (err) {
                return;
            }

            // TODO change state name results in:
            // TypeError: Cannot read property '0' of null
            // flow-api/lib/cayley_put.js:107
            // const existingName = result[0].id;

            const existingName = result[0].id;
            client.g.V(existingName).In().All((err, body) => {

                if (err || (body && body.error)) {
                    return;
                }

                // remove string if it has no in connections
                if (!body || (body.length === 1 && body[0].id === triple[0])) {
                    // TODO remove string
                    console.log('TODO: remove string:', existingName);
                }

                client.delete([{
                    subject: triple[0],
                    predicate: SCHEMA + "name>",
                    object: existingName
                }], (err, body) => {

                    if (err || body.error) {
                        return;
                    }

                    client.write(nameTriples, (err, body) => {
                        console.log('Cayley.PUT: Name triples added:', err, body)
                    });
                });
            });
        });
    });
};
<commit_msg>post api<commit_after>"use strict"

const Network = require('./network');

// RDF constants
const FLOW_VOCAB = '<http://schema.jillix.net/vocab/';
const SCHEMA = '<http://schema.org/';

function addTarget (client, triples, node, type, target, callback) {

    if (!target) {
        Network.createNodeOfType(client, type, (err, target) => {

            if (err) {
                return callback(err);
            }

            triples.push([node, FLOW_VOCAB + type + '>', target]);
            callback(null, target, triples);
        });
    } else {
        triples.push([node, FLOW_VOCAB + type + '>', target]);
        callback(null, target, triples);
    }
}

function addHashTriples (client, triples, node, name, json, callback) {

    const values = [name];
    json && values.push(json);

    Network.ensureHashs(client, values, (err, hashs, strings) => {

        if (err) {
            return callback(err);
        }

        triples.push([node, SCHEMA + 'name>', hashs[0]]);
        hashs[1] && triples.push([node, FLOW_VOCAB + 'json>', hashs[1]]);

        // add hash > string > value triples to write data
        if (strings && strings.length) {
            triples = triples.concat(strings);
        }

        callback(null, triples);
    });
}

module.exports = (client, req, callback) => {

    const triples = [];
    const save = (err, triples) => {

        console.log('SAVE:', err, triples);
        if (err) {
            return callback(err);
        }

        //client.write(triples);
    };

    switch (req.mode) {
        case "set":

            // ensure target and set new edge on node
            addTarget(client, triples, req.node, req.type, req.target, (err, target, triples) => {

                if (err) {
                    return callback(err);
                }

                // set new values on target, if no replace target is in request
                if (!req.target) {
                    addHashTriples(client, triples, target, req.name, req.json, (err, triples) => {

                        if (err) {
                            return callback(err);
                        }

                        // remove existing type edge on node
                        Network.removeOut(client, req.node, [req.type]);

                        save(null, triples);
                    });
                } else {

                    // remove existing type edge on node
                    Network.removeOut(client, req.node, [req.type]);

                    save(null, triples);
                }
            });
            break;

        case "add":

            // add new edge on node
            addTarget(client, triples, req.node, req.type, null, (err, target, triples) => {

                if (err) {
                    return callback(err);
                }

                // set hash edges on target node
                addHashTriples(client, triples, target, req.name, req.json, save);
            });
            break;

        case "edit":

            // set new name and json edges on node
            addHashTriples(client, triples, req.node, req.name, req.json, (err) => {

                if (err) {
                    return callback(err);
                }

                // remove name and json eges on node
                Network.removeOut(client, req.node, ['name', 'json']);

                save(null, triples);
            });
            break; 
    }
};

{'fraction_non_alphanumeric': 0.14271555996035679, 'fraction_numerical': 0.00407444114084352, 'mean_word_length': 1.2614541832669324, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '3468478', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2497, 'n_words': 642}