<commit_before># Copyright (C) 2008 10gen Inc.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License, version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
# for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

require 'stringio'

# A GridFile file lives in the database. GridFiles can be retrieved by name.
#
# GridFiles have attributes. You can read/write them using [] and []=. One
# interesting one is 'contentType', which tells Babble how to serve the file.
#
# Example:
#
#   GridFile.open("myfile", 'w') { |f|
#     f['contentType'] = 'text/plain'  # or f.contentType = 'text/plain'
#     f['creator'] = '<NAME>'
#     f.puts "Hello, GridFS!"
#   }
#   GridFile.open("myfile", 'r') { |f|
#     puts f.read
#     puts f['contentType']            # or f.contentType
#     puts f['creator']
#   }
#   # => Hello, GridFS!
#   # => text/plain
#   # => <NAME>
#   GridFile.exist?("myfile")
#   # => true
#   GridFile.delete("myfile")
#
# A GridFile is a StringIO that reads from the database when it is created and
# writes to the database when it is closed.
#
# TODO: allow retrieval by _id, return _id on close, expose _id, more modes,
# perhaps use delegation instead of inheritance.
class GridFile < StringIO

  RESERVED_KEYS = %w(_ns _id filename contentType length chunkSize next uploadDate _save _update)

  @@connection = nil

  class << self                 # Class methods

    def connection
      @@connection ||= $db
      raise "connection not defined" unless @@connection
      @@connection
    end
    def connection=(val)
      @@connection = val
    end

    # Reads a GridFile from the database and returns it, or +nil+ if is not
    # found.
    def find(name)              # :nodoc:
      connection['_files'].findOne({:filename => name})
    end

    # Opens a GridFile with the given mode. If a block is given then the file
    # is passed in to the block and is closed at the end of the block.
    def open(name, mode)
      grid_file = GridFile.new(name, mode)
      if block_given?
        begin
          yield grid_file
        ensure
          grid_file.close if grid_file
        end
      else
        grid_file
      end
    end
<commit_msg>whitespace<commit_after># Copyright (C) 2008 10gen Inc.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License, version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
# for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

require 'stringio'

# A GridFile file lives in the database. GridFiles can be retrieved by name.
#
# GridFiles have attributes. You can read/write them using [] and []=. One
# interesting one is 'contentType', which tells Babble how to serve the file.
#
# Example:
#
#   GridFile.open("myfile", 'w') { |f|
#     f['contentType'] = 'text/plain'  # or f.contentType = 'text/plain'
#     f['creator'] = '<NAME>'
#     f.puts "Hello, GridFS!"
#   }
#   GridFile.open("myfile", 'r') { |f|
#     puts f.read
#     puts f['contentType']            # or f.contentType
#     puts f['creator']
#   }
#   # => Hello, GridFS!
#   # => text/plain
#   # => <NAME>
#   GridFile.exist?("myfile")
#   # => true
#   GridFile.delete("myfile")
#
# A GridFile is a StringIO that reads from the database when it is created and
# writes to the database when it is closed.
#
# TODO: allow retrieval by _id, return _id on close, expose _id, more modes,
# perhaps use delegation instead of inheritance.
class GridFile < StringIO

  RESERVED_KEYS = %w(_ns _id filename contentType length chunkSize next uploadDate _save _update)

  @@connection = nil

  class << self                 # Class methods

    def connection
      @@connection ||= $db
      raise "connection not defined" unless @@connection
      @@connection
    end

    def connection=(val)
      @@connection = val
    end

    # Reads a GridFile from the database and returns it, or +nil+ if is not
    # found.
    def find(name)              # :nodoc:
      connection['_files'].findOne({:filename => name})
    end

    # Opens a GridFile with the given mode. If a block is given then the file
    # is passed in to the block and is closed at the end of the block.
    def open(name, mode)
      grid_file = GridFile.new(name, mode)
      if block_given?
        begin
          yield grid_file
        ensure
          grid_file.close if grid_file
        end
      else
        grid_file
      end
    end

{'fraction_non_alphanumeric': 0.09604302727621974, 'fraction_numerical': 0.002689204763734153, 'mean_word_length': 2.817448680351906, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4924229', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1506, 'n_words': 632}