package org.weiboad.ragnar.server.storage;

import org.rocksdb.RocksDBException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.weiboad.ragnar.server.config.FieryConfig;
import org.weiboad.ragnar.server.util.DateTimeHelper;
import org.weiboad.ragnar.server.util.FileUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Scope("singleton")

public class DBManage {

    private ConcurrentHashMap<Long, DBSharder> dbSharderList;

    private Logger log;

    @Autowired
    FieryConfig fieryConfig;

    public DBManage() {
        log = LoggerFactory.getLogger(DBManage.class);
        dbSharderList = new ConcurrentHashMap<>();
        log.info("DB Manager init...");
    }

    public DBSharder getDB(Long timestamp) {
        try {

            Long timeshard = DateTimeHelper.getTimesMorning(timestamp);

            //log.info("timestamp:" + timestamp + " shard:" + timeshard + " befor:" + DateTimeHelper.getBeforeDay(fieryConfig.getKeepdataday()));
            if (timeshard > DateTimeHelper.getBeforeDay(fieryConfig.getKeepdataday())
                    && timeshard <= DateTimeHelper.getCurrentTime()) {

                if (dbSharderList.containsKey(timeshard)) {
                    return dbSharderList.get(timeshard);
                }

                dbSharderList.put(timeshard, new DBSharder(fieryConfig.getDbpath(), timeshard));
                return dbSharderList.get(timeshard);

            } else {
                log.info("out of the date:" + timeshard);
                return null;
            }

        } catch (RocksDBException e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return null;
        }

    }


    public HashMap<String, String> getDBFolderList() {
        HashMap<String, String> dblist = new HashMap<>();
        try {
            dblist = FileUtil.subFolderList(fieryConfig.getDbpath());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dblist;
    }


    //remove the old db
    @Scheduled(fixedRate = 5000)
    public void Refresh() {
        for (Map.Entry<Long, DBSharder> e : dbSharderList.entrySet()) {
            //check if the ragnarlog is expire
            if (e.getKey() < DateTimeHelper.getBeforeDay(fieryConfig.getKeepdataday())) {
                log.info("Remove DB Name:" + e.getKey() + " expireday:" + fieryConfig.getKeepdataday() + " expiredate:" + DateTimeHelper
                        .getBeforeDay(fieryConfig.getKeepdataday()));
                e.getValue().close();
                FileUtil.deleteDir(fieryConfig.getDbpath() + "/" + e.getKey());
                dbSharderList.remove(e.getKey());
            }
        }
    }
}

@@ -512,16 +512,44 @@
 eUtil;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -2389,24 +2389,576 @@
 Refresh() %7B%0A
+%0A        ArrayList%3CLong%3E removeMap = new ArrayList%3C%3E();%0A%0A        //folder walker%0A        HashMap%3CString, String%3E folderList = this.getDBFolderList();%0A        for (Map.Entry%3CString, String%3E folderItem : folderList.entrySet()) %7B%0A            try %7B%0A                Long foldertimestamp = Long.parseLong(folderItem.getKey());%0A                if (foldertimestamp %3C DateTimeHelper.getBeforeDay(fieryConfig.getKeepdataday())) %7B%0A                    removeMap.add(foldertimestamp);%0A                %7D%0A            %7D catch (Exception e) %7B%0A%0A            %7D%0A        %7D%0A%0A
         for 
@@ -3150,32 +3150,142 @@
 taday())) %7B%0A    
+            removeMap.add(e.getKey());%0A            %7D%0A        %7D%0A%0A        for (Long removeUrlKey : removeMap) %7B%0A
             log.
@@ -3309,26 +3309,28 @@
 ame:%22 + 
-e.get
+removeUrl
 Key
-()
  + %22 exp
@@ -3423,20 +3423,16 @@
         
-    
 .getBefo
@@ -3461,32 +3461,33 @@
 eepdataday()));%0A
+%0A
                 
@@ -3486,111 +3486,109 @@
     
-    e.getValue().close();%0A                FileUtil.deleteDir(fieryConfig.getDbpath() + %22/%22 + e.getKey()
+if (dbSharderList.containsKey(removeUrlKey)) %7B%0A                dbSharderList.get(removeUrlKey).close(
 );%0A 
@@ -3623,26 +3623,28 @@
 .remove(
-e.get
+removeUrl
 Key
-()
 );%0A     
@@ -3643,32 +3643,111 @@
 );%0A            %7D
+%0A%0A            FileUtil.deleteDir(fieryConfig.getDbpath() + %22/%22 + removeUrlKey);
 %0A        %7D%0A    %7D
@@ -3737,20 +3737,21 @@
 ;%0A        %7D%0A
+%0A
     %7D%0A%7D%0A

add ugly way to clean up the folder