<commit_before>/*
  Awesome is a redis clone for node.js

  Initial code by <NAME> <<EMAIL>>

  MIT License
*/

var PORT = 6379;

var tcp = require("tcp");
var sys = require("sys");


var enable_debug = true;

var store = require("./store");

var server = tcp.createServer(function(socket) {
  // requests and responses have this as a trailer
  var eol = "\r\n";
  var ok = "+OK" + eol;

  function Command(line) {

    function parseCommand(s) {
      var cmd = "";
      for(var idx = 0; idx < s.length; ++idx) {
        var chr = s[idx];
        if(chr == " " || chr == "\r" || chr == "\n") {
          return cmd;
        }
        cmd += chr;
      }
      return cmd;
    }

    function parseArgs(s) {
      var args = [];
      var arg = "";
      var argidx = 0;
      for(var idx = 0; idx < s.length; ++idx) {
        var chr = s[idx];
        if(chr == " " || chr == "\r" || chr == "\n") {
          if(arg) {
            args.push(arg);
            argidx = argidx + 1;
            arg = "";
          }
        } else {
          arg += chr;
        }
      }
      return args;
    }

    function reply(line) {
      socket.send(line + eol);
    }

    this.cmd = parseCommand(line).toLowerCase();
    this.args = parseArgs(line);

    var that = this;

    var callbacks = {
      // keep sorted alphabetically
      dbsize: {
        inline: true,
        callback: function() {
          debug("received DBSIZE command");
          var size = store.dbsize();
          reply(":" + size);
        }
      },

      del: {
        inline: true,
        callback:function() {
          debug("received DEL command");
          if(that.args.length > 2) {
            var keys = that.args.slice(1);
            var deleted = store.del(keys);
            reply(":" + deleted);
          } else {
            var key = that.args[1];
            if(store.has(key)) {
              store.del(key);
              reply(":1");
            } else {
              reply(":0");
            }
          }
        }
      },

      get: {
        inline: true,
        callback: function() {
          debug("received GET command");
          var key = that.args[1];
          if(store.has(key)) {
            var value = store.get(key);
            reply("$" + value.toString().length);
            reply(value);
          } else { // not found
            reply("$-1");
          }
        }
      },

      getset: {
        inline: false,
        callback: function() {
          debug("received GETSET command");
          var key = that.args[1];
          if(store.has(key)) {
            var value = store.get(key);
            reply("$" + value.length);
            reply(value);
          } else { // not found
            reply("-1");
          }
        }
      },

      incr: {
        inline: true,
        callback: function() {
          var key = that.args[1];
          var value = store.incr(key);
          reply(":" + value);
        }
      },

      decr: {
        inline: true,
        callback: function() {
          var key = that.args[1];
          var value = store.decr(key);
          reply(":" + value);
        }
      },

      incrby: {
        inline: true,
        callback: function() {
          var key = that.args[1];
          var increment = that.args[2];
          var value = store.incrby(key, increment);
          reply(":" + value);
        }
      },

      decrby: {
        inline: true,
        callback: function() {
          var key = that.args[1];
          var decrement = that.args[2];
          var value = store.decrby(key, decrement);
          reply(":" + value);
        }
      },

      info: {<commit_msg>added exists command<commit_after>/*
  Awesome is a redis clone for node.js

  Initial code by <NAME> <<EMAIL>>

  MIT License
*/

var PORT = 6379;

var tcp = require("tcp");
var sys = require("sys");


var enable_debug = true;

var store = require("./store");

var server = tcp.createServer(function(socket) {
  // requests and responses have this as a trailer
  var eol = "\r\n";
  var ok = "+OK" + eol;

  function Command(line) {

    function parseCommand(s) {
      var cmd = "";
      for(var idx = 0; idx < s.length; ++idx) {
        var chr = s[idx];
        if(chr == " " || chr == "\r" || chr == "\n") {
          return cmd;
        }
        cmd += chr;
      }
      return cmd;
    }

    function parseArgs(s) {
      var args = [];
      var arg = "";
      var argidx = 0;
      for(var idx = 0; idx < s.length; ++idx) {
        var chr = s[idx];
        if(chr == " " || chr == "\r" || chr == "\n") {
          if(arg) {
            args.push(arg);
            argidx = argidx + 1;
            arg = "";
          }
        } else {
          arg += chr;
        }
      }
      return args;
    }

    function reply(line) {
      socket.send(line + eol);
    }

    this.cmd = parseCommand(line).toLowerCase();
    this.args = parseArgs(line);

    var that = this;

    var callbacks = {
      // keep sorted alphabetically
      dbsize: {
        inline: true,
        callback: function() {
          debug("received DBSIZE command");
          var size = store.dbsize();
          reply(":" + size);
        }
      },

      del: {
        inline: true,
        callback:function() {
          debug("received DEL command");
          if(that.args.length > 2) {
            var keys = that.args.slice(1);
            var deleted = store.del(keys);
            reply(":" + deleted);
          } else {
            var key = that.args[1];
            if(store.has(key)) {
              store.del(key);
              reply(":1");
            } else {
              reply(":0");
            }
          }
        }
      },

      get: {
        inline: true,
        callback: function() {
          debug("received GET command");
          var key = that.args[1];
          if(store.has(key)) {
            var value = store.get(key);
            reply("$" + value.toString().length);
            reply(value);
          } else { // not found
            reply("$-1");
          }
        }
      },

      getset: {
        inline: false,
        callback: function() {
          debug("received GETSET command");
          var key = that.args[1];
          if(store.has(key)) {
            var value = store.get(key);
            reply("$" + value.length);
            reply(value);
          } else { // not found
            reply("-1");
          }
        }
      },

      incr: {
        inline: true,
        callback: function() {
          var key = that.args[1];
          var value = store.incr(key);
          reply(":" + value);
        }
      },

      decr: {
        inline: true,
        callback: function() {
          var key = that.args[1];
          var value = store.decr(key);
          reply(":" + value);
        }
      },

      incrby: {
        inline: true,
        callback: function() {
          var key = that.args[1];
          var increment = that.args[2];
          var value = store.incrby(key, increment);
          reply(":" + value);
        }
      },

      decrby: {
        inline: true,
        callback: function() {
          var key = that.args[1];
          var decrement = that.args[2];
          var value = store.decrby(key, decrement);
          reply(":" + value);
        }
      },

      exists: {
        inline: true,
        callback: function() {
          debug("received EXISTS command");
          var key = that.args[1];
          if(store.has(key)){
              reply(":1");  
          }else{
              reply(":0");
          }
        }
      },

      info: {
{'fraction_non_alphanumeric': 0.14953515778447035, 'fraction_numerical': 0.006416131989000917, 'mean_word_length': 1.301295570955107, 'pattern_counts': {'":': 18, '<': 13, '<?xml version=': 0, '>': 11, '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': '699569', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2265, 'n_words': 553}