简体   繁体   中英

How to make a data structure which can hold 100 most recent record_values basis on timestamp?

I am getting data for a particular user id from 14 tables as shown below. As part of data, I am extracting user_id , record_name and record_value and then I get timestamp from record_name (by splitting on it) and then populate my TreeMap with key as timestamp and value as record_value .

After that I am extracting 100 most recent record_value from valueTimestampMap and then populating it in my recordValueHolder LinkedList.

In my case 100 most recent means by looking at the timestamp not the way they are coming.

Below is my code -

public List<String> getData(String userId) {

    List<String> recordValueHolder = new LinkedList<String>();
    Map<Long, String> valueTimestampMap = new TreeMap<Long, String>(Collections.reverseOrder());

    for (int tableNumber = 0; tableNumber < 14; tableNumber++) {

        String sql = "select * from table_" + tableNumber + " where user_id='" + userId + "';";

        SimpleStatement query = new SimpleStatement(sql);
        query.setConsistencyLevel(ConsistencyLevel.QUORUM);
        ResultSet res = session.execute(query);

        Iterator<Row> rows = res.iterator();
        while (rows.hasNext()) {
            Row r = rows.next();

            String user_id = r.getString("user_id"); // get user id
            String record_name = r.getString("record_name"); // get record name
            String record_value = r.getString("record_value"); // get record value

            long timestamp = Long.parseLong(record_name.split("\\.")[1]);

            // populate my tree map
            valueTimestampMap.put(timestamp, record_value);
        }
    }

    // now extract 100 most recent record_value since
    // valueTimestampMap is already sorted basis on key in
    // descending order
    for (Map.Entry<Long, String> entry : valueTimestampMap.entrySet()) {
        if (recordValueHolder.size() > 99)
            break;
        recordValueHolder.add(entry.getValue());
    }       

    return recordValueHolder;
}

I am sorting TreeMap in descending order of the keys by using Collections.reverseOrder() so that I have most recent timestamps at the top and then I can simply extract 100 most recent record_value from it and that's what my above code does.

Problem Statement:-

I have 100 most recent record_value in recordValueHolder List. Now I also need to find out which tableNumber each record_value out of 100 came from and what was the record_name for that record_value as well?

So I was thinking to make a data structure something like below which can hold 100 most recent record_value along with their tableNumber, record_name and timestamp as well.

public class RecordValueTimestampTableHolder {

    private long timestamp;
    private String recordName;
    private String recordValue;
    private Integer tableNumber;

    // setters and getters

}

So the size of List<RecordValueTimestampTableHolder> should be 100. Is this possible to do with my current setup? I am not able to understand how to make this work?

Now my return data type of getData method will change and instead of returning List<String> , now it will return List<RecordValueTimestampTableHolder> which will have 100 most recent record_values along with other values as well.

Instead of using a TreeMap<Long, String> , use TreeMap<Long, RecordValueTimestampTableHolder>

Instead of using

        valueTimestampMap.put(timestamp, record_value);

use:

        valueTimestampMap.put(timestamp, new RecordValueTimestampTableHolder(timestamp, record_name, record_value, tableNumber));

Of course, this means you will have to add a constructor to RecordValueTimestampTableHolder that accepts the four parameters and assigns them to the internal fields.

As you said recordValueHolder will have to be defined as a List<RecordValueTimestampTableHolder> and this will also have to be the return type from this method.

Filling it will be exactly like you fill it now. Though personally I'd use valueTimestampMap.values() to iterate.

int i = 0;
for (RecordValueTimestampTableHolder item : valueTimestampMap.values()) {
    recordValueHolder.add(item);
    if (++i == 100)
        break;
}  

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