<commit_before>package uk.me.graphe.server.database;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


import uk.me.graphe.server.database.dbitems.*;
import uk.me.graphe.shared.Edge;
import uk.me.graphe.shared.Vertex;
import uk.me.graphe.shared.graphmanagers.OTGraphManager2d;
import uk.me.graphe.shared.graphmanagers.OTGraphManager2dImpl;
import uk.me.graphe.shared.graphmanagers.OTGraphManagerFactory;
import uk.me.graphe.shared.jsonwrapper.JSONException;
import uk.me.graphe.shared.jsonwrapper.JSONImplHolder;
import uk.me.graphe.shared.jsonwrapper.JSONObject;
import uk.me.graphe.shared.messages.Message;
import uk.me.graphe.shared.messages.MessageFactory;
import uk.me.graphe.shared.messages.operations.CompositeOperation;
import uk.me.graphe.shared.messages.operations.GraphOperation;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.query.Query;
import com.google.code.morphia.query.UpdateOperations;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;

public class DatabaseImpl implements Database{
    
    private Mongo mMongo;
    private Morphia mMorphia = new Morphia();
    private Datastore mData;
    DBCollection mCollection;
    
    public DatabaseImpl () {
        try {
            mMongo = new Mongo();
        } catch (UnknownHostException e) {
            return;
        }
        mData = mMorphia.createDatastore(mMongo, "graphs");
        mCollection = mMongo.getDB("graphs").getCollection("OTGraphManager2dStore");
    }

    @Override
    public void delete(int key) {
        mData.delete(mData.createQuery(OTGraphManager2dStore.class).filter("id =", key));
    }
    
    @Override
    public void clean() {
        mCollection.drop();
    }

    @Override
    public int size() {
        return (int) mCollection.getCount();
    }
    
    @Override
    public OTGraphManager2d retrieve(int key) {
        //  Extract OtGraphManagerStore from DB
        List<OTGraphManager2dStore> retrieves = mData.find(OTGraphManager2dStore.class, "id =", key).asList();
        if (retrieves == null || retrieves.size() != 1)
            return null;
        OTGraphManager2dStore retrieve = retrieves.get(0);
        List<GraphOperation> operations = new ArrayList<GraphOperation>();
        List<JSONObject> objects = new LinkedList<JSONObject>();
        List<Message> messages = null;
        if (retrieve.getmOps() == null)
            return OTGraphManagerFactory.newInstance(key);
        for (String s : retrieve.getmOps()) {
            s = s.substring(s.indexOf('{'), s.lastIndexOf('}')+1);
            String st = "";
            for (int i=0; i < s.length(); i++)
                if (s.charAt(i) != '\\')
                    st += s.charAt(i);
            JSONObject item = parseItem(st);
            objects.add(item);
        }
        try {
            messages = MessageFactory.makeOperationsFromJson(objects);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OTGraphManager2d toReturn = OTGraphManagerFactory.newInstance(key);
        for (Message item : messages) {
            // Store all operations as local, map to server in restoreState()
            toReturn.applyOperation((GraphOperation) item);
        }
        System.out.println("Returning graph: " + toReturn.getGraphId());
        return toReturn;<commit_msg>Added some output to database and fix bug with retrieval method<commit_after>package uk.me.graphe.server.database;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


import uk.me.graphe.server.database.dbitems.*;
import uk.me.graphe.shared.Edge;
import uk.me.graphe.shared.Vertex;
import uk.me.graphe.shared.graphmanagers.OTGraphManager2d;
import uk.me.graphe.shared.graphmanagers.OTGraphManager2dImpl;
import uk.me.graphe.shared.graphmanagers.OTGraphManagerFactory;
import uk.me.graphe.shared.jsonwrapper.JSONException;
import uk.me.graphe.shared.jsonwrapper.JSONImplHolder;
import uk.me.graphe.shared.jsonwrapper.JSONObject;
import uk.me.graphe.shared.messages.Message;
import uk.me.graphe.shared.messages.MessageFactory;
import uk.me.graphe.shared.messages.operations.CompositeOperation;
import uk.me.graphe.shared.messages.operations.GraphOperation;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.query.Query;
import com.google.code.morphia.query.UpdateOperations;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;

public class DatabaseImpl implements Database{
    
    private Mongo mMongo;
    private Morphia mMorphia = new Morphia();
    private Datastore mData;
    DBCollection mCollection;
    
    public DatabaseImpl () {
        try {
            mMongo = new Mongo();
        } catch (UnknownHostException e) {
            return;
        }
        mData = mMorphia.createDatastore(mMongo, "graphs");
        mCollection = mMongo.getDB("graphs").getCollection("OTGraphManager2dStore");
    }

    @Override
    public void delete(int key) {
        mData.delete(mData.createQuery(OTGraphManager2dStore.class).filter("id =", key));
    }
    
    @Override
    public void clean() {
        mCollection.drop();
    }

    @Override
    public int size() {
        return (int) mCollection.getCount();
    }
    
    @Override
    public OTGraphManager2d retrieve(int key) {
        //  Extract OtGraphManagerStore from DB
        List<OTGraphManager2dStore> retrieves = mData.find(OTGraphManager2dStore.class, "id =", key).asList();
        if (retrieves == null || retrieves.size() != 1)
            return null;
        OTGraphManager2dStore retrieve = retrieves.get(0);
        List<GraphOperation> operations = new ArrayList<GraphOperation>();
        List<JSONObject> objects = new LinkedList<JSONObject>();
        List<Message> messages = null;
        if (retrieve.getmOps() == null)
            return OTGraphManagerFactory.newInstance(key);
        for (String s : retrieve.getmOps()) {
            do {
                String toStrip = s.substring(s.indexOf('{'), s.indexOf('}')+1);
                String st = "";
                for (int i=0; i < toStrip.length(); i++)
                    if (toStrip.charAt(i) != '\\')
                        st += toStrip.charAt(i);
                JSONObject item = parseItem(st);
                System.out.println("Parsing: " + st +" to " + item);
                objects.add(item);
                s = s.substring(s.indexOf('}') + 1);
            } while (s.indexOf('{') != -1);
        }
        try {
            messages = MessageFactory.makeOperationsFromJson(objects);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OTGraphManager2d toReturn = OTGraphManagerFactory.newInstance(key);
        for (Message item : messages) {
            // Store all operations as local, map to server in restoreState()
            System.out.println("Applying operation:" + item.toJson() +" to graph");
            toReturn.applyOperation((GraphOperation) item);
        }
        System.out.println("Returning graph: " + toReturn.getGraphId());
        return toReturn;
{'fraction_non_alphanumeric': 0.11686879823594266, 'fraction_numerical': 0.003858875413450937, 'mean_word_length': 2.813452443510247, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '366224', 'n_tokens_mistral': 2197, 'n_tokens_neox': 2146, 'n_words': 414}