<commit_before>###
PostgreSQL -- basic queries and database interface

COPYRIGHT : (c) 2017 SageMath, Inc.
LICENSE   : AGPLv3
###

QUERY_ALERT_THRESH_MS=5000

EventEmitter = require('events')

fs      = require('fs')
async   = require('async')
pg      = require('pg')

winston = require('winston')
winston.remove(winston.transports.Console)
winston.add(winston.transports.Console, {level: 'debug', timestamp:true, colorize:true})

misc_node = require('smc-util-node/misc_node')

{defaults} = misc = require('smc-util/misc')
required = defaults.required

{SCHEMA, client_db} = require('smc-util/schema')

read_password_from_disk = ->
    filename = (process.env.SMC_ROOT ? '.') + '/data/secrets/postgres'
    try
        #winston.debug("Loading password from '#{filename}'")
        return fs.readFileSync(filename).toString().trim()
    catch
        #winston.debug("NO PASSWORD FILE!")
        # no password file
        return

class exports.PostgreSQL extends EventEmitter    # emits a 'connect' event whenever we successfully connect to the database.
    constructor: (opts) ->
        opts = defaults opts,
            host         : process.env['PGHOST'] ? 'localhost'    # or 'hostname:port'
            database     : process.env['SMC_DB'] ? 'smc'
            user         : process.env['PGUSER'] ? 'smc'
            debug        : true
            connect      : true
            password     : <PASSWORD>
            pool         : undefined   # IGNORED for now.
            cache_expiry : 30000  # expire cached queries after this many milliseconds
                                 # keep this very short; it's just meant to reduce impact of a bunch of
                                 # identical permission checks in a single user query.
            cache_size   : 500   # cache this many queries; use @_query(cache:true, ...) to cache result
            concurrent_warn : 500
        @setMaxListeners(10000)  # because of a potentially large number of changefeeds
        @_state = 'init'
        @_debug = opts.debug
        i = opts.host.indexOf(':')
        if i != -1
            @_host = opts.host.slice(0, i)
            @_port = parseInt(opts.host.slice(i+1))
        else
            @_host = opts.host
            @_port = 5432
        @_concurrent_warn = opts.concurrent_warn
        @_user = opts.user<commit_msg>reduce cache<commit_after>###
PostgreSQL -- basic queries and database interface

COPYRIGHT : (c) 2017 SageMath, Inc.
LICENSE   : AGPLv3
###

QUERY_ALERT_THRESH_MS=5000

EventEmitter = require('events')

fs      = require('fs')
async   = require('async')
pg      = require('pg')

winston = require('winston')
winston.remove(winston.transports.Console)
winston.add(winston.transports.Console, {level: 'debug', timestamp:true, colorize:true})

misc_node = require('smc-util-node/misc_node')

{defaults} = misc = require('smc-util/misc')
required = defaults.required

{SCHEMA, client_db} = require('smc-util/schema')

read_password_from_disk = ->
    filename = (process.env.SMC_ROOT ? '.') + '/data/secrets/postgres'
    try
        #winston.debug("Loading password from '#{filename}'")
        return fs.readFileSync(filename).toString().trim()
    catch
        #winston.debug("NO PASSWORD FILE!")
        # no password file
        return

class exports.PostgreSQL extends EventEmitter    # emits a 'connect' event whenever we successfully connect to the database.
    constructor: (opts) ->
        opts = defaults opts,
            host         : process.env['PGHOST'] ? 'localhost'    # or 'hostname:port'
            database     : process.env['SMC_DB'] ? 'smc'
            user         : process.env['PGUSER'] ? 'smc'
            debug        : true
            connect      : true
            password     : <PASSWORD>
            pool         : undefined   # IGNORED for now.
            cache_expiry : 3000  # expire cached queries after this many milliseconds
                                 # keep this very short; it's just meant to reduce impact of a bunch of
                                 # identical permission checks in a single user query.
            cache_size   : 100   # cache this many queries; use @_query(cache:true, ...) to cache result
            concurrent_warn : 500
        @setMaxListeners(10000)  # because of a potentially large number of changefeeds
        @_state = 'init'
        @_debug = opts.debug
        i = opts.host.indexOf(':')
        if i != -1
            @_host = opts.host.slice(0, i)
            @_port = parseInt(opts.host.slice(i+1))
        else
            @_host = opts.host
            @_port = 5432
        @_concurrent_warn = opts.concurrent_warn
        @_user = opts.user
{'fraction_non_alphanumeric': 0.110397946084724, 'fraction_numerical': 0.013478818998716302, 'mean_word_length': 2.2108516483516483, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4311204', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1344, 'n_words': 356}