简体   繁体   中英

Get all the data from the database first time and if any entries got changed then print out only the change

I am working on a project in which I need to print out the data from the database. Let's take an example, suppose in my database, I have below entries only-

Hello 1.0.0
World 1.0.0

Then my Java method that will make a call to the database will return me a map of above data.

My map will have above data as below-

Key as Hello, Value as 1.0.0
Key as World, Value as 1.0.0

Suppose, I started my program for the first time, then it will print out like this with the below code I have, which is perfectly fine.

{Hello=1.0.0, World=1.0.0}

And then I am running background thread every 2 seconds that will make a call to the database and get all the data again from the database. And every two seconds, it will print out the same thing- (and my code is working fine according to that)

{Hello=1.0.0, World=1.0.0}

Now what is interesting is, suppose my application is running and somebody changed the database entry like this-

Hello 1.0.1
World 1.0.0

meaning, version got changed for Hello and now it is 1.0.1 and as my application is running, so now what it will print out basis on my below code? Something like this-

{Hello=1.0.1, World=1.0.0}

But that's not what I am looking for. It should print out like below, meaning only the entry that got changed

{Hello=1.0.1}

I hope the questions is clear enough. Below is my code that I have so far. It only prints out all the entry from the database every time which is not what I am looking for.

I want to print out everything only when the program is getting started for the first time but after if it is started, it should print out only the things that have changed or any new entry.

Below is my code:-

public class Graph {

    public static Map<String, String> bundleList = new LinkedHashMap<String, String>();

    public static void main(String[] args) {

        getAttributesFromDatabase();

        printOutBundleInformation();

        loggingAfterEveryXMilliseconds();

    }

    private static void printOutBundleInformation() {
        System.out.println(bundleList);     
    }

    private static void getAttributesFromDatabase() {

        Map<String, String> bundleInformation = new LinkedHashMap<String, String>();

        bundleInformation = getFromDatabase();

        if(!bundleInformation.isEmpty()) {
            bundleList = bundleInformation;
        }
    }

    private static Map<String, String> getFromDatabase() {

        Map<String, String> hello = new LinkedHashMap<String, String>();

        // In actual scenario, I will have a database call here

        hello.put("Hello", "1.0.0");
        hello.put("World", "1.0.0");

        return hello;
    }


    private static void loggingAfterEveryXMilliseconds() {
        new Thread() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ex) {

                    }
                    getAttributesFromDatabase();
                    printOutBundleInformation();
                }
            }
        }.start();
    }
}

Any help will be appreciated on this.

You need to add extra column in the table called last modified(date with time). Every time querying to the database you pass the last access time(for example getFromDatabase(last access time) ). You need to keep your last access time in you program. In the query you need to verify the last access time against the last modified field.

MapDifference.html#entriesDiffering() might be what you need.

Quick and dirty:

public class Graph {

    public static Map<String, String> bundleList = new LinkedHashMap<String, String>();

    private static Map<String, String> oldBundleList = new LinkedHashMap<String, String>();

    // ...

    private static void getAttributesFromDatabase() {
        Map<String, String> bundleInformation = new LinkedHashMap<String, String>();
        bundleInformation = getFromDatabase();
        if(!bundleInformation.isEmpty()) {
            oldBundleList = bundleList;
            bundleList = bundleInformation;
        }
    }

    // ...

    private static void loggingAfterEveryXMilliseconds() {
        new Thread() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ex) {

                    }
                    getAttributesFromDatabase();
                    final Map<String, ValueDifference<String>> entriesDiffering = Maps.difference(oldBundleList, bundleList).entriesDiffering();
                    if (!entriesDiffering.isEmpty()) {
                       for (String key : entriesDiffering.keySet()) {
                           System.out.println("{" + key + "=" + bundleList.get(key) + "}");
                       } 
                    }
                    // printOutBundleInformation();
                }
            }
        }.start();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM