<commit_before>/**
 * Copyright 2016 DynomiteDB
 *
 * Licensed 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, 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.
 */

/*
 * Hashes
 *
 * Run
 * ---
 *
 * node hash.js
 *
 * Expected output
 * ---------------
 *
 * --- BEGIN ---
 * hset: user:id=bob (1)
 * hmset: user:id=bob (OK)
 * hmset: user:id=sue (OK)
 * hlen: user:id=bob (6)
 * hlen: user:id=sue (5)
 * hexists: user:id=bob has citizenship (1)
 * hkeys: user:id=sue (fname,lname,age,gender,occupation)
 * hget: user:id=sue occupation (CTO)
 * hmget: user:id=bob (Bob,Smith,40.5)
 * hincrbyfloat: user:id=bob age (41)
 * hincrby: user:id=sue age (38)
 * hgetall: user:id=bob ({"fname":"Bob","lname":"Smith","age":"41","citizenship":"USA","gender":"male","occupation":"Software engineer"})
 * hgetall: user:id=sue ({"fname":"Sue","lname":"Jones","age":"38","gender":"female","occupation":"CTO"})
 * hvals: user:id=sue (Sue,Jones,38,female,CTO)
 * Unable to hsetnx: user:id=bob fname (0)
 * hdel: occupation from user:id=bob (1)
 * hgetall: user:id=sue ({"fname":"Sue","lname":"Jones","age":"38","gender":"female","occupation":"CTO"})
 * hscan: user:id=sue (0,fname,Sue,lname,Jones)
 * --- END ---
 *
 * Commands used in this file
 * --------------------------
 *
 * [x] HSET: Set the value associated with a field
 * [x] HMSET: Set the value(s) associated with one or more fields
 * [x] HGET: Get the value associated with a field
 * [x] HMGET: Get the value(s) associated with one or more fields
 * [x] HDEL: Delete one or more fields
 * [x] HEXISTS: Return 1 if a field exists, else return 0
 * [x] HGETALL: Get all fields and values
 * [x] HINCRBY: Increment an integer value stored at the specified field
 * [x] HINCRBYFLOAT: Increment a float value stored at the specified field
 * [x] HKEYS: Get all field names
 * [x] HLEN: Count the number of fields
 * [x] HSETNX: Set the value associated with a field, only if the field does not exist
 * [x] HSTRLEN: Get the string length of a value associated with the specified field
 * [x] HVALS: Get all values
 * [x] HSCAN: Iterate over the field/value pairs
 */


var redis = require('redis');

// Connect to a DynomiteDB cluster
// var db = redis.createClient(8102, 'localhost');

// Connect to a local dynomitedb-redis instance
// var db = redis.createClient(22122, 'localhost');

// Connect to a single server Redis instance
var db = redis.createClient(6379, 'localhost');

var bobKey = 'user:id=bob';
var sueKey = 'user:id=sue';

db.on('error', function(err) {
    console.log(err);
    process.exit(1);
});

/*
 * Wait until we're connected to the Dynomite server before we issue commands
 */
db.on('connect', function() {
    // Let's add some keys
    console.log('--- BEGIN ---');
    createBob(bobKey, 'fname', 'Bob');
});

function createBob(key, field, value) {
    db.hset(key, field, value, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hset: ' + key + ' (' + reply + ')');
            nextStep();
        } else {
            console.log('Unable to hset: ' + key + ' (' + reply + ')');
            nextStep();
        }

        function nextStep() {
            updateBob(bobKey,
                'lname', 'Smith',
                'age', 40.5,
                'citizenship', 'USA',
                'gender', 'male',
                'occupation', 'Software engineer');
        }
    });
}

function updateBob(key, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5) {
    db.hmset(key, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hmset: ' + key + ' (' + reply + ')');
            nextStep();
        } else {
            console.log('Unable to hmset: ' + key + ' (' + reply + ')');
            nextStep();
        }
    });

    function nextStep() {
        createSue(sueKey,
            'fname', 'Sue',
            'lname', 'Jones',
            'age', 37,
            'gender', 'female',
            'occupation', 'CTO');
    }
}

function createSue(key, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5) {
    db.hmset(key, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hmset: ' + key + ' (' + reply + ')');
            nextStep(bobKey);
        } else {
            console.log('Unable to hmset: ' + key + ' (' + reply + ')');
            nextStep(bobKey);
        }

        function nextStep(key) {
            countBobFields(key);
        }
    });
}

function countBobFields(key) {
    db.hlen(key, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hlen: ' + key + ' (' + reply + ')');
            countSueFields(sueKey);
        } else {
            console.log('Unable to hlen: ' + key);
        }
    });
}

function countSueFields(key) {
    db.hlen(key, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hlen: ' + key + ' (' + reply + ')');
            bobHasCitizenship(bobKey, 'citizenship');
        } else {
            console.log('Unable to hlen: ' + key);
        }
    });
}

function bobHasCitizenship(key, field) {
    db.hexists(key, field, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hexists: ' + key + ' has ' + field + ' (' + reply + ')');
            getSueFields(sueKey, 'citizenship');
        } else {
            console.log('hexists:  ' + key + ' does not have ' + field + ' (' + reply + ')');
            db.quit();
        }
    });
}

function getSueFields(key) {
    db.hkeys(key, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hkeys: ' + key + ' (' + reply + ')');<commit_msg>remove extra argument<commit_after>/**
 * Copyright 2016 DynomiteDB
 *
 * Licensed 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, 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.
 */

/*
 * Hashes
 *
 * Run
 * ---
 *
 * node hash.js
 *
 * Expected output
 * ---------------
 *
 * --- BEGIN ---
 * hset: user:id=bob (1)
 * hmset: user:id=bob (OK)
 * hmset: user:id=sue (OK)
 * hlen: user:id=bob (6)
 * hlen: user:id=sue (5)
 * hexists: user:id=bob has citizenship (1)
 * hkeys: user:id=sue (fname,lname,age,gender,occupation)
 * hget: user:id=sue occupation (CTO)
 * hmget: user:id=bob (Bob,Smith,40.5)
 * hincrbyfloat: user:id=bob age (41)
 * hincrby: user:id=sue age (38)
 * hgetall: user:id=bob ({"fname":"Bob","lname":"Smith","age":"41","citizenship":"USA","gender":"male","occupation":"Software engineer"})
 * hgetall: user:id=sue ({"fname":"Sue","lname":"Jones","age":"38","gender":"female","occupation":"CTO"})
 * hvals: user:id=sue (Sue,Jones,38,female,CTO)
 * Unable to hsetnx: user:id=bob fname (0)
 * hdel: occupation from user:id=bob (1)
 * hgetall: user:id=sue ({"fname":"Sue","lname":"Jones","age":"38","gender":"female","occupation":"CTO"})
 * hscan: user:id=sue (0,fname,Sue,lname,Jones)
 * --- END ---
 *
 * Commands used in this file
 * --------------------------
 *
 * [x] HSET: Set the value associated with a field
 * [x] HMSET: Set the value(s) associated with one or more fields
 * [x] HGET: Get the value associated with a field
 * [x] HMGET: Get the value(s) associated with one or more fields
 * [x] HDEL: Delete one or more fields
 * [x] HEXISTS: Return 1 if a field exists, else return 0
 * [x] HGETALL: Get all fields and values
 * [x] HINCRBY: Increment an integer value stored at the specified field
 * [x] HINCRBYFLOAT: Increment a float value stored at the specified field
 * [x] HKEYS: Get all field names
 * [x] HLEN: Count the number of fields
 * [x] HSETNX: Set the value associated with a field, only if the field does not exist
 * [x] HSTRLEN: Get the string length of a value associated with the specified field
 * [x] HVALS: Get all values
 * [x] HSCAN: Iterate over the field/value pairs
 */


var redis = require('redis');

// Connect to a DynomiteDB cluster
// var db = redis.createClient(8102, 'localhost');

// Connect to a local dynomitedb-redis instance
// var db = redis.createClient(22122, 'localhost');

// Connect to a single server Redis instance
var db = redis.createClient(6379, 'localhost');

var bobKey = 'user:id=bob';
var sueKey = 'user:id=sue';

db.on('error', function(err) {
    console.log(err);
    process.exit(1);
});

/*
 * Wait until we're connected to the Dynomite server before we issue commands
 */
db.on('connect', function() {
    // Let's add some keys
    console.log('--- BEGIN ---');
    createBob(bobKey, 'fname', 'Bob');
});

function createBob(key, field, value) {
    db.hset(key, field, value, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hset: ' + key + ' (' + reply + ')');
            nextStep();
        } else {
            console.log('Unable to hset: ' + key + ' (' + reply + ')');
            nextStep();
        }

        function nextStep() {
            updateBob(bobKey,
                'lname', 'Smith',
                'age', 40.5,
                'citizenship', 'USA',
                'gender', 'male',
                'occupation', 'Software engineer');
        }
    });
}

function updateBob(key, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5) {
    db.hmset(key, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hmset: ' + key + ' (' + reply + ')');
            nextStep();
        } else {
            console.log('Unable to hmset: ' + key + ' (' + reply + ')');
            nextStep();
        }
    });

    function nextStep() {
        createSue(sueKey,
            'fname', 'Sue',
            'lname', 'Jones',
            'age', 37,
            'gender', 'female',
            'occupation', 'CTO');
    }
}

function createSue(key, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5) {
    db.hmset(key, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hmset: ' + key + ' (' + reply + ')');
            nextStep(bobKey);
        } else {
            console.log('Unable to hmset: ' + key + ' (' + reply + ')');
            nextStep(bobKey);
        }

        function nextStep(key) {
            countBobFields(key);
        }
    });
}

function countBobFields(key) {
    db.hlen(key, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hlen: ' + key + ' (' + reply + ')');
            countSueFields(sueKey);
        } else {
            console.log('Unable to hlen: ' + key);
        }
    });
}

function countSueFields(key) {
    db.hlen(key, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hlen: ' + key + ' (' + reply + ')');
            bobHasCitizenship(bobKey, 'citizenship');
        } else {
            console.log('Unable to hlen: ' + key);
        }
    });
}

function bobHasCitizenship(key, field) {
    db.hexists(key, field, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hexists: ' + key + ' has ' + field + ' (' + reply + ')');
            getSueFields(sueKey);
        } else {
            console.log('hexists:  ' + key + ' does not have ' + field + ' (' + reply + ')');
            db.quit();
        }
    });
}

function getSueFields(key) {
    db.hkeys(key, function(err, reply) {
        if (err) console.log(err);

        if (reply) {
            console.log('hkeys: ' + key + ' (' + reply + ')');
{'fraction_non_alphanumeric': 0.17009940357852882, 'fraction_numerical': 0.014473161033797217, 'mean_word_length': 2.432314410480349, 'pattern_counts': {'":': 32, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5488569', 'n_tokens_mistral': 4415, 'n_tokens_neox': 4227, 'n_words': 1171}