package com.benchmark;


import com.benchmark.util.DateUtility;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;

import java.sql.SQLException;

import java.util.Random;
import java.util.UUID;


public class Mongo
{
    private DB db;
    private DBCollection collection;
    MongoClient mongoClient;

    public static void main(String[] args)
    {
        new Mongo();
    }

    public Mongo()
    {
        connect();
        selectTest();
    }

    private void connect()
    {
        try
        {
            // To connect to mongodb server
            mongoClient = new MongoClient("localhost", 27017);
            // Now connect to your databases
            db = mongoClient.getDB("test");
            System.out.println("Connected to database successfully"); //DB ye baglandik, yoksa da otomatik olarak bu isimle bir db yaratildi.
            //            boolean auth = db.authenticate("mustafa", "benchmark".toCharArray());
            //            System.out.println("Authentication: " + auth);
        }
        catch (Exception e)
        {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }

    }

    private void createTable()
    {
        try
        {
            db.getCollection("test").drop();
            collection = db.getCollection("test");
            System.out.println("Collection created successfully");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void insertBenchmarkVeri()
    {
        int x = 0;
        BasicDBObject document;
        Random generator = new Random();
        String c, d, e;
        int ayniKayitLimit = 50000; // select yapildiginda donecek kayit sayisi
        int nrCount = 0;
        long startTime, totalTime;
        try
        {
            startTime = System.currentTimeMillis();
            System.out.println("startTime for insert operation in ms: " + startTime + " sys date: " + DateUtility.getToday());
            for (int i = 0; i < 1000000; i++)
            {
                if (i % 10000 == 0)
                {
                    System.gc();
                }
                final String randomString = UUID.randomUUID().toString();
                c = randomString.substring(0, 5);
                d = randomString.substring(5, 10);
                e = randomString.substring(10, 15);
                if (ayniKayitLimit >= nrCount)
                {
                    nrCount++;
                    document = new BasicDBObject();
                    document.put("test1", "" + c + "");
                    document.put("test2", "" + d + "");
                    document.put("test3", "" + e + "");
                    document.put("test4", 12345);
                    collection.insert(document);
                } else
                {
                    x = generator.nextInt(9999);
                    document = new BasicDBObject();
                    document.put("test1", "" + c + "");
                    document.put("test2", "" + d + "");
                    document.put("test3", "" + e + "");
                    document.put("test4", "" + x + "");
                    collection.insert(document);
                }
            }
            totalTime = DateUtility.getSecondsBetween(startTime, System.currentTimeMillis());
            System.out.println("totalTime of insert operation in second: " + totalTime);
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }

    private void selectTest()
    {
        long startTime, totalTime;
        try
        {
            collection = db.getCollection("test");
            BasicDBObject query;
            DBCursor cursor;
            query = new BasicDBObject();
            query.put("test4", 12345);
            startTime = System.currentTimeMillis();
            cursor = collection.find(query);
            totalTime = System.currentTimeMillis() - startTime;
            System.out.println("Select time: " + totalTime + " ms");
            startTime = System.currentTimeMillis();
            int count = 0;
            while (cursor.hasNext())
            {
                count++;
                cursor.next();
            }
            totalTime = System.currentTimeMillis() - startTime;
            System.out.println("Fetch time of select operation in ms: " + totalTime);
            System.out.println("Rows of select count: " + count);
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }
}
