<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/>.
*/

package ed.lang.ruby;

import java.io.IOException;

import org.jruby.*;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.*;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

import ed.db.DBBase;
import ed.db.DBCollection;
import ed.js.*;
import ed.js.engine.Scope;
import static ed.lang.ruby.RubyObjectWrapper.toJS;

public class GridFS {

    public static void save(RubyJSObjectWrapper rscope, RubyJSObjectWrapper rdb, RubyObject gridFile) {
        final Scope scope = (Scope)rscope.getJSObject();
        final Ruby runtime = gridFile.getRuntime();
        final ThreadContext context = runtime.getCurrentContext();

        DBBase db = (DBBase)rdb.getJSObject();
        DBCollection _files = (DBCollection)db.get("_files");
        RubyObject gridFileName = (RubyObject)gridFile.instance_variable_get(context, runtime.newString("@name"));
        String name = gridFileName.toString();
        String str = gridFile.callMethod(context, "read", JSFunctionWrapper.EMPTY_IRUBY_OBJECT_ARRAY, Block.NULL_BLOCK).toString();
        RubyHash metadata = (RubyHash)gridFile.instance_variable_get(context, runtime.newString("@metadata"));

        try {
            final JSInputFile f = new JSInputFile(null, null, str);
            f.set("filename", name);
            metadata.visitAll(new RubyHash.Visitor() {
                    public void visit(IRubyObject key, IRubyObject value) {
                        if (!value.toString().startsWith("_"))
                            f.set(toJS(scope, key), toJS(scope, value));
                    }
                });

            remove(rscope, rdb, name);
            _files.save(scope, f);
        }
        catch (IOException e) {
            System.err.println("GridFS.save: " + e);
            RaiseException.createNativeRaiseException(runtime, e);
        }
    }

    public static void remove(RubyJSObjectWrapper rscope, RubyJSObjectWrapper rdb, String fileName) {
        Scope scope = (Scope)rscope.getJSObject();
        DBBase base = (DBBase)rdb.getJSObject();

        JSObjectBase criteria = new JSObjectBase();
        criteria.set("filename", fileName.toString());
        JSDBFile f = (JSDBFile)base.getCollectionFromString("_files").findOne(criteria);
        if (f != null)
            f.remove();
    }
}
<commit_msg>Comments and minor code changes for readability.<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/>.
*/

package ed.lang.ruby;

import java.io.IOException;

import org.jruby.*;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.*;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

import ed.db.DBBase;
import ed.db.DBCollection;
import ed.js.*;
import ed.js.engine.Scope;
import static ed.lang.ruby.RubyObjectWrapper.toJS;

public class GridFS {

    public static void save(RubyJSObjectWrapper rscope, RubyJSObjectWrapper rdb, RubyObject gridFile) {
        final Scope scope = (Scope)rscope.getJSObject();
        final Ruby runtime = gridFile.getRuntime();
        final ThreadContext context = runtime.getCurrentContext();

        DBBase db = (DBBase)rdb.getJSObject();
        DBCollection _files = db.getCollectionFromString("_files");
        RubyObject gridFileName = (RubyObject)gridFile.instance_variable_get(context, runtime.newString("@name"));
        String name = gridFileName.toString();
        String str = gridFile.callMethod(context, "read", JSFunctionWrapper.EMPTY_IRUBY_OBJECT_ARRAY, Block.NULL_BLOCK).toString();
        RubyHash metadata = (RubyHash)gridFile.instance_variable_get(context, runtime.newString("@metadata"));

        try {
            final JSInputFile f = new JSInputFile(null, null, str);
            f.set("filename", name);
            metadata.visitAll(new RubyHash.Visitor() {
                    public void visit(IRubyObject key, IRubyObject value) {
                        if (!value.toString().startsWith("_"))
                            f.set(toJS(scope, key), toJS(scope, value));
                    }
                });

            remove(rscope, rdb, name);
            _files.save(scope, f);
        }
        catch (IOException e) {
            System.err.println("GridFS.save: " + e);
            RaiseException.createNativeRaiseException(runtime, e);
        }
    }

    public static void remove(RubyJSObjectWrapper rscope, RubyJSObjectWrapper rdb, String fileName) {
        Scope scope = (Scope)rscope.getJSObject();
        DBBase db = (DBBase)rdb.getJSObject();

        JSObjectBase criteria = new JSObjectBase();
        criteria.set("filename", fileName.toString());
        JSDBFile f = (JSDBFile)db.getCollectionFromString("_files").findOne(criteria);
        if (f != null)
            f.remove();         // Removes the chunks, too
    }
}

{'fraction_non_alphanumeric': 0.10355987055016182, 'fraction_numerical': 0.0022653721682847896, 'mean_word_length': 2.914502849905003, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5304575', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1711, 'n_words': 480}