<commit_before>#-  Ruby source code

#-  defs-mongo.rb ~~
#                                                       ~~ (c) SRW, 16 Jul 2014
#                                                   ~~ last updated 28 Jul 2014

require 'json'
require 'mongo'
require 'sinatra/base'
require 'uri'

module Sinatra

    module MongoConnect

        def mongo_api_connect()
          # This function needs documentation.
            db = URI.parse(settings.persistent_storage[:mongo])
            db_name = db.path.gsub(/^\//, '')
            conn = Mongo::Connection.new(db.host, db.port).db(db_name)
            unless db.user.nil? or db.password.nil?
                conn.authenticate(db.user, db.password)
            end
            set api_db: conn
            settings.api_db['avars'].ensure_index('box_status', {
                background: true,
                sparse: true
            })
            settings.api_db['avars'].ensure_index('exp_date', {
                expireAfterSeconds: settings.avar_ttl
            })
            return
        end

        def mongo_log_connect()
          # This function needs documentation.
            db = URI.parse(settings.trafficlog_storage[:mongo])
            db_name = db.path.gsub(/^\//, '')
            conn = Mongo::Connection.new(db.host, db.port).db(db_name)
            unless db.user.nil? or db.password.nil?
                conn.authenticate(db.user, db.password)
            end
            set log_db: conn, logging: false
            return
        end

    end

    module MongoAPIDefs

        def get_avar(params)
          # This helper function needs documentation.
            bk, db = "#{params[0]}&#{params[1]}", settings.api_db
            x = db['avars'].find_one({_id: bk})
            y = (x.nil?) ? '{}' : x['body']
            return y
        end

        def get_list(params)
          # This helper function needs documentation.
            bs, db, x = "#{params[0]}&#{params[1]}", settings.api_db, []
            db['avars'].find({box_status: bs}).each do |doc|
              # This block needs documentation.
                x.push(doc['key'])
            end
            y = (x.length == 0) ? '[]' : x.to_json
            return y
        end

        def set_avar(params)
          # This helper function needs documentation.
            db = settings.api_db
            doc = {
                _id: "#{params[0]}&#{params[1]}",
                body: params[2],
                exp_date: Time.now,
                key: params[1]
            }
            options = {upsert: true}#, w: 1}
            if (params.length == 4) then
                doc['body'] = params[3]
                doc['box_status'] = "#{params[0]}&#{params[2]}"
            end
            db['avars'].update({_id: doc[:_id]}, doc, options)
            return
        end

    end

    module MongoLogDefs

        def log_to_db()
          # This method needs documentation.<commit_msg>Changed TTL index, now using find_and_modify<commit_after>#-  Ruby source code

#-  defs-mongo.rb ~~
#                                                       ~~ (c) SRW, 16 Jul 2014
#                                                   ~~ last updated 02 Aug 2014

require 'json'
require 'mongo'
require 'sinatra/base'
require 'uri'

module Sinatra

    module MongoConnect

        def mongo_api_connect()
          # This function needs documentation.
            db = URI.parse(settings.persistent_storage[:mongo])
            db_name = db.path.gsub(/^\//, '')
            conn = Mongo::Connection.new(db.host, db.port).db(db_name)
            unless db.user.nil? or db.password.nil?
                conn.authenticate(db.user, db.password)
            end
            set api_db: conn
            settings.api_db['avars'].ensure_index('box_status', {
                background: true,
                sparse: true
            })
            settings.api_db['avars'].ensure_index('exp_date', {
                expireAfterSeconds: 0
            })
            return
        end

        def mongo_log_connect()
          # This function needs documentation.
            db = URI.parse(settings.trafficlog_storage[:mongo])
            db_name = db.path.gsub(/^\//, '')
            conn = Mongo::Connection.new(db.host, db.port).db(db_name)
            unless db.user.nil? or db.password.nil?
                conn.authenticate(db.user, db.password)
            end
            set log_db: conn, logging: false
            return
        end

    end

    module MongoAPIDefs

        def get_avar(params)
          # This helper function needs documentation.
            bk, db = "#{params[0]}&#{params[1]}", settings.api_db
            x = db['avars'].find_and_modify({
                query: {_id: "#{params[0]}&#{params[1]}"},
                update: {'$set': {exp_date: Time.now + settings.avar_ttl}},
                fields: {body: 1}
            })
            y = (x.nil?) ? '{}' : x['body']
            return y
        end

        def get_list(params)
          # This helper function needs documentation.
            bs, db, x = "#{params[0]}&#{params[1]}", settings.api_db, []
            db['avars'].find({box_status: bs}).each do |doc|
              # This block needs documentation.
                x.push(doc['key'])
            end
            y = (x.length == 0) ? '[]' : x.to_json
            return y
        end

        def set_avar(params)
          # This helper function needs documentation.
            db = settings.api_db
            doc = {
                _id: "#{params[0]}&#{params[1]}",
                body: params[2],
                exp_date: Time.now + settings.avar_ttl,
                key: params[1]
            }
            options = {upsert: true}#, w: 1}
            if (params.length == 4) then
                doc['body'] = params[3]
                doc['box_status'] = "#{params[0]}&#{params[2]}"
            end
            db['avars'].update({_id: doc[:_id]}, doc, options)
            return
        end

    end

    module MongoLogDefs

        def log_to_db()
          # This method needs documentation.
{'fraction_non_alphanumeric': 0.12428172713840092, 'fraction_numerical': 0.009193892628468231, 'mean_word_length': 1.4290271132376395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1601382', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1710, 'n_words': 387}