<commit_before>use v6;

#BEGIN {
#  @*INC.unshift('/home/marcel/Languages/Perl6/Projects/BSON/lib');
#}

use MongoDB;
use MongoDB::Database;
use BSON::EDCTools;

package MongoDB {

  #-----------------------------------------------------------------------------
  #
  class MongoDB::Connection {

    has IO::Socket::INET $!sock;
    has Exception $.status = Nil;

    #---------------------------------------------------------------------------
    #
    submethod BUILD ( Str :$host = 'localhost', Int :$port = 27017 ) {

      # Try block used because IO::Socket::INET throws an exception when things
      # go wrong. This is not nessesary because there is no risc of data loss
      #
      try {
        if ? $!sock {
          $!sock.close;
          $!sock = IO::Socket::INET;
        }

        $!status = Nil;
        $!sock .= new( :$host, :$port);
        CATCH {
          default {
            $!status = X::MongoDB.new(
              :error-text("Failed to connect to $host at $port"),
              :oper-name<connect>
              :severity(MongoDB::Severity::Error)
            );
          }
        }
      }

      $MongoDB::version = self.version unless ? $!status;
    }

    #---------------------------------------------------------------------------
    #
    method _send ( Buf:D $b, Bool $has_response --> Any ) {
      $!sock.write($b);

      # some calls do not expect response
      #
      return unless $has_response;

      # check response size
      #
      my $index = 0;
      my Buf $l = $!sock.read(4);
      my Int $w = decode_int32( $l.list, $index) - 4;

      # receive remaining response bytes from socket
      #
      return $l ~ $!sock.read($w);
    }

    #---------------------------------------------------------------------------
    #
    method database ( Str:D $name --> MongoDB::Database ) {
      return MongoDB::Database.new(
        :connection(self),
        :name($name)
      );
    }

    #---------------------------------------------------------------------------
    # List databases using MongoDB db.runCommand({listDatabases: 1});
    #
    method list_databases ( --> Array ) {
      my $database = self.database('admin');
      my Pair @req = listDatabases => 1;
      my Hash $doc = $database.run_command(@req);

      if $doc<ok>.Bool == False {
        die X::MongoDB.new(
          error-text => $doc<errmsg>,
          error-code => $doc<code>,
          oper-name => 'list_databases',
          oper-data => @req.perl,
          collection-ns => 'admin.$cmd',
          severity => MongoDB::Severity::Error
        );
      }

      return @($doc<databases>);
    }<commit_msg>set status instead of throwing<commit_after>use v6;

#BEGIN {
#  @*INC.unshift('/home/marcel/Languages/Perl6/Projects/BSON/lib');
#}

use MongoDB;
use MongoDB::Database;
use BSON::EDCTools;

package MongoDB {

  #-----------------------------------------------------------------------------
  #
  class MongoDB::Connection {

    has IO::Socket::INET $!sock;
    has Exception $.status = Nil;

    #---------------------------------------------------------------------------
    #
    submethod BUILD ( Str :$host = 'localhost', Int :$port = 27017 ) {

      # Try block used because IO::Socket::INET throws an exception when things
      # go wrong. This is not nessesary because there is no risc of data loss
      #
      try {
        if ? $!sock {
          $!sock.close;
          $!sock = IO::Socket::INET;
        }

        $!status = Nil;
        $!sock .= new( :$host, :$port);
        CATCH {
          default {
            $!status = X::MongoDB.new(
              :error-text("Failed to connect to $host at $port"),
              :oper-name<connect>
              :severity(MongoDB::Severity::Error)
            );
          }
        }
      }

      $MongoDB::version = self.version unless ? $!status;
    }

    #---------------------------------------------------------------------------
    #
    method _send ( Buf:D $b, Bool $has_response --> Any ) {
      $!sock.write($b);

      # some calls do not expect response
      #
      return unless $has_response;

      # check response size
      #
      my $index = 0;
      my Buf $l = $!sock.read(4);
      my Int $w = decode_int32( $l.list, $index) - 4;

      # receive remaining response bytes from socket
      #
      return $l ~ $!sock.read($w);
    }

    #---------------------------------------------------------------------------
    #
    method database ( Str:D $name --> MongoDB::Database ) {
      return MongoDB::Database.new(
        :connection(self),
        :name($name)
      );
    }

    #---------------------------------------------------------------------------
    # List databases using MongoDB db.runCommand({listDatabases: 1});
    #
    method list_databases ( --> Array ) {
      my $database = self.database('admin');
      my Pair @req = listDatabases => 1;
      my Hash $doc = $database.run_command(@req);

      if $doc<ok>.Bool == False {
        $!status = X::MongoDB.new(
          error-text => $doc<errmsg>,
          error-code => $doc<code>,
          oper-name => 'list_databases',
          oper-data => @req.perl,
          collection-ns => 'admin.$cmd',
          severity => MongoDB::Severity::Error
        );
      }

      return @($doc<databases>);
    }
{'fraction_non_alphanumeric': 0.2664041994750656, 'fraction_numerical': 0.005249343832020997, 'mean_word_length': 1.2644312393887946, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, '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': '5869831', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1476, 'n_words': 352}