<commit_before>package edu.berkeley.MetadataRepo;


import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * A class that connects to a MongoDB database and handles all interactions with the database
 */
public class DatabaseController
{

    private static MongoClient mongoClient = new MongoClient( "localhost" );
    private static MongoDatabase database = mongoClient.getDatabase("test");

    public static void dump()
    {
        MongoCollection<Document> collection = database.getCollection("testData");
        for (Document d : collection.find())
            System.out.println(d.toJson());
    }

    public static void commit(String file, String jsonMetadata)
    {
        MongoCollection<Document> collection = database.getCollection("testData");

        // Find a document with the given name
        Document fdoc = new Document("file", file);
        FindIterable<Document> found = collection.find(fdoc);

        if (found.iterator().hasNext())
        {
            // If a document is found, it should be the only one
            Document doc = found.iterator().next();
            ArrayList<Document> metadataList  = (ArrayList<Document>) doc.get("metadata");
            Document metadata = Document.parse(jsonMetadata);
            metadata.append("timestamp", new Date());
            metadataList.add(metadata);

            // Updates the metadata
            collection.updateOne(fdoc, new Document("$set", new Document("metadata", metadataList)));
        }
        else
        {
            // Document not found, creating new document
            Document doc = new Document();
            doc.append("file", file);
            ArrayList<Document> metadataList = new ArrayList<Document>();
            Document metadata = Document.parse(jsonMetadata);
            metadata.append("timestamp", new Date());
            metadataList.add(metadata);
            doc.append("metadata", metadataList);

            // Inserts the new document to the db
            collection.insertOne(doc);
        }
    }

    public static void show(String file)
    {
    }

    /** Using O(n) time to search. Assumes the metadata is at most one degree nested */
    public static void find(String keyWord, String time)
    {
        boolean checkTime = false;
        long startTime = 0;
        long endTime = 0;
        long compareTime = 0;
        Date date;
//        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf  = new SimpleDateFormat("MM/dd/yy");

        if (time != "None") {
            checkTime = true;
            try {
                date = sdf.parse(time);
            } catch (ParseException e) {
                System.out.println("Time should be in MM/dd/yy format.");
                return;
            }
            startTime = date.getTime();
            endTime = startTime + 864000000;
        }
        MongoCollection<Document> collection = database.getCollection("testData");
        for (Document d : collection.find()) {
            ArrayList<Document> metadataList = (ArrayList<Document>) d.get("metadata");
            for (int i = 0; i < metadataList.size(); i++ ) {
                if (checkTime) {
                    compareTime = ((Date) metadataList.get(i).get("timestamp")).getTime();<commit_msg>implemented basic version of show command<commit_after>package edu.berkeley.MetadataRepo;


import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * A class that connects to a MongoDB database and handles all interactions with the database
 */
public class DatabaseController
{

    private static MongoClient mongoClient = new MongoClient( "localhost" );
    private static MongoDatabase database = mongoClient.getDatabase("test");

    public static void dump()
    {
        MongoCollection<Document> collection = database.getCollection("testData");
        for (Document d : collection.find())
            System.out.println(d.toJson());
    }

    public static void commit(String file, String jsonMetadata)
    {
        MongoCollection<Document> collection = database.getCollection("testData");

        // Find a document with the given name
        Document fdoc = new Document("file", file);
        FindIterable<Document> found = collection.find(fdoc);

        if (found.iterator().hasNext())
        {
            // If a document is found, it should be the only one
            Document doc = found.iterator().next();
            ArrayList<Document> metadataList  = (ArrayList<Document>) doc.get("metadata");
            Document metadata = Document.parse(jsonMetadata);
            metadata.append("timestamp", new Date());
            metadataList.add(metadata);

            // Updates the metadata
            collection.updateOne(fdoc, new Document("$set", new Document("metadata", metadataList)));
        }
        else
        {
            // Document not found, creating new document
            Document doc = new Document();
            doc.append("file", file);
            ArrayList<Document> metadataList = new ArrayList<Document>();
            Document metadata = Document.parse(jsonMetadata);
            metadata.append("timestamp", new Date());
            metadataList.add(metadata);
            doc.append("metadata", metadataList);

            // Inserts the new document to the db
            collection.insertOne(doc);
        }
    }

    public static void show(String file)
    {
        MongoCollection<Document> collection = database.getCollection("testData");

        // Find a document with the given name
        Document fdoc = new Document("file", file);
        FindIterable<Document> found = collection.find(fdoc);

        if (found.iterator().hasNext()) {
            // If a document is found, it should be the only one
            Document doc = found.iterator().next();
            ArrayList<Document> metadataList  = (ArrayList<Document>) doc.get("metadata");

            System.out.println("Metadata for " + file + ":");
            for (Document d : metadataList)
                System.out.println(d.toJson());
        }
        else
        {
            System.out.println("No file with that name.");
        }

    }

    /** Using O(n) time to search. Assumes the metadata is at most one degree nested */
    public static void find(String keyWord, String time)
    {
        boolean checkTime = false;
        long startTime = 0;
        long endTime = 0;
        long compareTime = 0;
        Date date;
//        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf  = new SimpleDateFormat("MM/dd/yy");

        if (time != "None") {
            checkTime = true;
            try {
                date = sdf.parse(time);
            } catch (ParseException e) {
                System.out.println("Time should be in MM/dd/yy format.");
                return;
            }
            startTime = date.getTime();
            endTime = startTime + 864000000;
        }
        MongoCollection<Document> collection = database.getCollection("testData");
        for (Document d : collection.find()) {
            ArrayList<Document> metadataList = (ArrayList<Document>) d.get("metadata");
            for (int i = 0; i < metadataList.size(); i++ ) {
                if (checkTime) {
                    compareTime = ((Date) metadataList.get(i).get("timestamp")).getTime();
{'fraction_non_alphanumeric': 0.10885045778229908, 'fraction_numerical': 0.003306205493387589, 'mean_word_length': 2.310185185185185, 'pattern_counts': {'":': 1, '<': 33, '<?xml version=': 0, '>': 31, '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': '4157472', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1938, 'n_words': 558}