How to temporarily store parts of data? - java

I have parts of data the coming not by order. Let me explain :
User with id 1 sending data ("phone number") but in the same time user with id 78 sending another data ("home address").
All the incoming data have the same receiver.
When all set of the data per id is set the data would be sent to mysql database and deleted from temporarily storage.
Each user id need to fill 6 different information before the complete data is sent.
So the question is how to to store the temporary data parts using their personal id (without mixing them up) and only then I have all the parts I will proceed to the next task ?
Should I use arrayList or something different ?
Edit (answering the duplicate suggestion ): my question is a bit different and the answer there not helping me at all !

You could, as you say, store it in some structure in memory. I'd pick some method which isn't tied to an Activity, otherwise you need to track the activity lifecycle. E.g. you could have a HashMap to map entity name (i.e. name, address, etc) to its value (or in case you're storing multiple instances of a single entity, go with some kind of a Data class suggested in the other answer). Personally, I find this method most cumbersome/cluttered, because you need to either divorce your storage from your Activity or handle all activity changes in order not to lose state.
Second approach is using SharedPreferences. Make a separate prefs file for that purpose, obtain them (getSharedPreferences("partial_data_dl", MODE_PRIVATE)), store each column as one field in the prefs, and clear them before commiting them to the database. It's easier than storing everything in memory and shouldn't be noticeably slower.
Third is the obvious one: why don't you update the database record as the data comes in? First time insert a new record with only one column populated, every other issue an update query to add value for the new column. This is admittedly the heaviest solution and might not work for your use case, but I'd give it a try and test it out. As the saying goes, premature optimization is the root of all evil.

you can create object 'Data'
class Data {
private param1,param2,...,param6;
public Data(){
}
/* PARAMS getters */
/* PARAMS setters */
public void insert(){
//insert to database
}
}
then use HashMap to update the data :
HashMap<Integer, Data> map = new HashMap<Integer, Data>();
and every time you recive some data and id check the type of the data (which param in the Data object) and update it in the map
Data toUpdate = map.get(id);
toUpdate.setParam...
you can make a listener for each time you set some param to indicate that all the data had been set and ready to be inserted

Related

How should I struct my Firebase data (base- & movement-data)?

I've created an android app with Java and I use a Firebase realtime-database. Via REST, I get JSON-data for cities. "Data" means:
base data like the city-id, name or population. They rarely change. This is represented in my model class "BaseData".
(movement) data which changes every day. The class is called "CityData".
BaseData is currently part of the CityData:
public class CityData
{
private BaseData baseData;
private int cases;
//...
}
At this time, I can only request data for today. When I save a CityData-object in my db, it looks like this:
My next step: Save the data for the last 7 days. But: Firebase would also save the base data every day. Can I prevent this (=> only save when not exists or update when something changes)? Otherwise, I have to change the data struct. But which is the best possibility?
My first idea: Create two seperate paths like this:
But then, I have to extract BaseData from CityData and join them every time, for example when I need the city name, which is often. Is this the best way? It's my first time with NoSql, so every help would be nice!

Monitoring changes in SQL db using Java

I want to check for changes in a SQL database but not sure what the best approach would be.
Currently what I do is I create an Object with column (key, value) information and store that into an ArrayList. I then copy that ArrayList and make a new one called my "cache".
Now, this is what I'm doing: every X ms I am grabbing all the data from the SQL db and then comparing that to my cached local copy. If there is anything different e.g. a value has been updated, inserted or deleted, I have a listener to notify me.
Is this a bad approach? Should I be doing a "SELECT * FROM table" query or is there another way I can get the information I want. I also want to be able to see the specific data that has been modified e.g. the row, the column and value.
Note: The database is generic.

Mixed list of new/updated objects: how to efficiently store them to the DB?

OK, so let's say I have a list that contains the following types of objects:
Objects that are already stored in the database (have the same PK),
and are the same as in the database, not modified
Objects that are already stored in the database (have the same PK), and are modified in regards to the stored ones, so they need to be updated
Objects that don't yet exist in the database, and are about to be saved
Such list of objects is being sent as a JSON to the web-service, and the web-service now has to communicate to the database, and decide what objects to store, update or ignore.
My question is how to do this effectively?
One idea is to iterate the list, and for every object's PK make a query to the database, and check if the object in the database is non-existent, the same, or modified. And then choose the action based on that information.
What bothers me with that approach is a whole lot of queries to the database, just to save some objects. What if only 1 of 100 should really be saved? It is so ineffective.
Is there any better way to do that?
You can send the whole list to DB (MYSQL) and do upsert :
INSERT ... ON DUPLICATE KEY UPDATE

Persistent Data Object Implementation

I have a page where a user can edit a lot of information, right now about 100 lines worth of DDLs and a text area, I want to update a data object after each change so that I only have to save to the database the changed rows instead of updating every row.
i.e. when the DDL value changes or when the text area data has changed (this is done in a pop up so that it will only be changed when 'Ok' is clicked) it will be stored into an array holding each updated row as an object. When the user hits save, it will only save the rows that were changed.
Right now im using AJAX so that its making a HTTPRequest, getting the array from the session and adding a new entry with the new value. Unfortunately I believe the page is stepping on itself at times and not keeping the data correct. I'm not sure why, but was wondering what would be the best way of implementing this, and if this is a good way of doing this.
Would a Java bean or anything else be better to represent the data object?
Would not accessing and storing in the session be faster and prevent this?
Java bean is very good for this purpose (as compared to java Map).
As I understand you want to call UPDATE only for items that has change, the best would be to implement equals() for that java bean class.
You have to store old values in session or anywhere else on server, to be able to determine what have change.
Anyway, you'll have to loop and do compare for each object:
if (!prevValue.equals(currValue)) {
DAO.update(currValue);
}

Is it advisable to store some information (meta-data) about a content in the id (or key) of that content?

It is advisable to store some information(meta-data) about a content in the Id(or key) of that content ?
In other words, I am using a time based UUIDs as the Ids (or key) for some content stored in the database. My application first accesses the list of all such Ids(or keys) of the content (from the database) and then accessed the corresponding content(from the database). These Ids are actually UUIDs(time based). My idea is to store some extra information about the content, in the Ids itself, so that the my software can access this meta-content without accessing the entire content from the database again.
My application context is a website using Java technology and Cassandra database.
So my question is,
whether I should do so ? I am concerned since lots of processing may be required (at the time of presentation of data to user) in order to retrieve the meta data from the ids of the content!! Thus it may be instead better to retrieve it from database then getting it through processing of the Id of that content.
If suggested then , How should I implement that in an efficient manner ? I was thinking of following way :-
Id of a content = 'Timebased UUID' + 'UserId'
where, 'timebasedUUID' is the generated ID based on the timestamp when that content was added by a user & 'userId' represents the Id of the user who put that content.
so my example Id would look something like this:- e4c0b9c0-a633-15a0-ac78-001b38952a49(TimeUUID) -- ff7405dacd2b(UserId)
How should I extract this userId from the above id of the content, in most efficient manner?
Is there a better approach to store meta information in the Ids ?
I hate to say it since you seem to have put a lot of thought into this but I would say this is not advisable. Storing data like this sounds like a good idea at first but ends up causing problems because you will have many unexpected issues reading and saving the data. It's best to keep separate data as separate variables and columns.
If you are really interested in accessing meta-content with out main content I would make two column families. One family has the meta-content and the other the larger main content and both share the same ID key. I don't know much about Cassandra but this seems to be the recommended way to do this sort of thing.
I should note that I don't think that all this will be necessary. Unless the users are storing very large amounts of information their size should be trivial and your retrievals of them should remain quick
I agree with AmaDaden. Mixing IDs and data is the first step on a path that leads to a world of suffering. In particular, you will eventually find a situation where the business logic requires the data part to change and the database logic requires the ID not to change. Off the cuff, in your example, there might suddenly be a requirement for a user to be able to merge two accounts to a single user id. If user id is just data, this should be a trivial update. If it's part of the ID, you need to find and update all references to that id.

Categories