Firestore lecture operations count - java

I would like to understand how Firestore works for counting read operations.
firestore lecture operations
I have a database and an application for mobile devices so to make Firestore go offline, nothing needs to be done.
When my application is opened the user receives data from a collection of | n | documents.
I wanted to understand if a user who opens the application with the internet will perform read operations from the database even if there are no changes in the database?
Or every time it accesses if there are no changes, no read operations are added since the data still resides in the cache?

if a user who opens the application with the internet on will perform read operations from the database even if there are no changes in the database?
If you are using get() call, the Firestore SDK will always try to get the data from the server, no matter if the data is changed or not. You'll be able to use the data from the cache, only if the device goes offline. However, if you trying to listen for changes in real-time using a SnapShotListener, according to the official documentation:
The initial state can come from the server directly, or from a local cache. If there is a state available in a local cache, the query snapshot will be initially populated with the cached data, then updated with the server's data when the client has caught up with the server's state.
In other words, if the server says that you have no new added/updated/deleted documents, you get the data from the cache. However, there is something else you should take care regarding the duration of time you can get the data from cache:
Also, if the listener is disconnected for more than 30 minutes (for example, if the user goes offline), you will be charged for reads as if you had issued a brand-new query.
Another possible charge might also come from:
There is a minimum charge of one document read for each query that you perform, even if the query returns no results.
But all these charges are normal according to how is Firestore designed.

Related

Retrieve data and wait time to check if something new appeared in database

I would like to retrieve data from my in-memory H2 database via rest endpoint using Spring and Java8. I have 2 endpoints: one to retrieve data, and second one to add data to database.
How can I achieve something like it is described below in easiest way? I am not sure what solution can be better I thought about JMS Queue or CompletableFuture (if it is possible). It should work for few users, they will call to retrieve data saved under their id number.
Scenario:
User calls rest-endpoint to retrieve data.
If data is present in database then it is retrieved and returned to user.
If data is not present in database then connection is hold for 60 seconds and if during that time something will appear in database (added via endpoint to add new data) then data will be returned.
If data is not present in database and new data won’t appear in 60 seconds then endpoint returns no content.
There were multiple ways of doing that and if requirements is clear then i suggest below two approaches.
Approach 1:
Find and retrieve if data available without waiting.
If data not available set resource id and retrieveTime in header and respond to consumer.
Based resource id you can be ready with data if available.
In this way you can sure about your endpoint service time always consistent and ideally it shouldn't be more than 3 seconds.
Approach 2.
if data not available then out sleep in 60 seconds (not in database connection scope) and then again try with same thread.
Don't need any queue or ansyc process here.
Here your loosing resources and service time would take more.
Apart from other approaches, if your systems using eventing then use eventing approach when there is record persistent then send event to consumer (all database has the feature to send event to source system today).

Both online & offline, load cached data if there is no change in firebase database reference

In my android app, I'm using firebase database. For offline work, the following code is used :
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Data in a reference will remain same. While offline, data in that reference loads without any delay, but while online the same data takes time(around 8 seconds) to get loaded.
As there is no change in data in that reference, I want those data to be loaded without delay (may be from cache). How can I do this?
What I want is it'll load data from cache, but if there is any update in the database reference, only then it'll load data from online
So this is what Firebase does. The whole idea is that you are synchronized with Firebase servers and once new data is added your onDataChange() is triggered with a DataSnapshot object that contains the new data. If you don't want this feature you can explicitly tell Firebase to goOffline():
Manually disconnect the Firebase Database client from the server and disable automatic reconnection.
So you can use this feature, if this is what you need it.

Android: Best way to store large amount of sensor datas over long time

I'm fairly new to Android-Development and I got a general question about How-To:
My App gets Sensor-Data from Step-Detector (Detected steps gets added up).
Now I need to store those Steps (which will be a lot of Data).
The steps should be stored like this:
If Todays
steps are stored on per Hour basis.
Else
steps are stored on per Day basis
SharedPreferences falls out of this as it only stores KeyValues.
But can SQLite handle this? Or is there any other way?
A future feature could be to sync those data with a Server.
I mean this could end up in thousands of Entries, and the app will also support other large data sets which need to get stored in similar way.
Try using Realm noSql database for it. The point is, you can save entire database on sd card as separate file for each day and process it later. It is native and work very fast with large amount of data. You can process all your readings later on - open database, transform readings (perhaps interpolate values for older to shring data in size) and then upload it to the cloud and delete database file.
But, anyways, a database is just implementation details, consider abstracting out all your operations so you can replace db later on.
As far as I know, sqLite stores all tables in a single file, so you will need column for a date and all records will be stored in single table. Realm is more flexible for this task.
SQL Lite can be used , it will be there as long as your application exist in the device, however if you want you can use Cloud Service, Azure provides simple and easy to use App Service , which have easy tables , in which you can directly call the APIs and internally it takes care of making connection and inserting the data into table.You can use Free Tier of App Service to test the concept.

Is checksum a good way to see if table has been modified in MySQL?

I'm currently developing an application in Java that connects to a MySQL database using JDBC, and displays records in jTable. The application is going to be run by more than one user at a time and I'm trying to implement a way to see if the table has been modified. EG if user one modifies a column such as stock level, and then user two tries to access the same record tries to change it based on level before user one interacts.
At the moment I'm storing the checksum of the table that's being displayed as a variable and when a user tries to modify a record it will do a check whether the stored checksum is the same as the one generated before the edit.
As I'm new to this I'm not sure if this a correct way to do it or not; as I have no experience in this matter.
Calculating the checksum of an entire table seems like a very heavy-handed solution and definitely something that wouldn't scale in the long term. There are multiple ways of handling this but the core theme is to do as little work as possible to ensure that you can scale as the number of users increase. Imagine implementing the checksum based solution on table with million rows continuously updated by hundreds of users!
One of the solutions (which requires minimal re-work) would be to "check" the stock name against which the value is updated. In the background, you'll fire across a query to the table to see if the data for "that particular stock" has been updated after the table was populated. If yes, you can warn the user or mark the updated cell as dirty to indicate that that value has changed. The problem here is that the query won't be fired off till the user tries to save the updated value. Or you could poll the database to avoid that but again hardly an efficient solution.
As a more robust solution, I would recommend using a database which implements native "push notifications" to all the connected clients. Redis is a NoSQL database which comes to mind for this.
Another tried and tested technique would be to forgo direct database connection and use a middleware layer like a messaging queue (e.g. RabbitMQ). Message queues enable design of systems which communicate using message. So for e.g. every update the stock value in the JTable would be sent across as a message to an "update database queue". Once the update is done, a message would be sent across to a "update notification queue" to which all clients would be connected. This will enable all of them to know that the value of a given stock has been updated and act accordingly. The advantage to this solution is that you get to keep your existing stack (Java, MySQL) and can implement notifications without polling the DB and killing it.
Checksum is a way to see if data has changed.
Anyway I would suggest you store a column "last_update_date", this column is supposed to be always updated at every update of the record.
So you juste have to store this date (precision date time) and do the check with that.
You can also add a column version number : a simple counter incremented by 1 at each update.
Note:
You can add a trigger on update for updating last_update_date, it should be 100% reliable, maybe you don't need a trigger if you control all updates.
When using in network communication:
A checksum is a count of the number of bits in a transmission unit
that is included with the unit so that the receiver can check to see
whether the same number of bits arrived. If the counts match, it's
assumed that the complete transmission was received.
So it can be translated to check 2 objects are different, your approach is correct.

Database access and battery friendly apps

I'd like to save statistics about the usage of the ad networks in my app. Basically I want to maintain the count of the number of times an ad network is requested, makes an impression, or fails in the request each day.
My initial idea is have an SQLite database with the following columns
[ day , networkId, requests, impressions, fails]
But I'm considering the following performance/battery drain possible issue:
Since I use 3 ad network (1 main and 2 as backfill) the worst case is:
Network A requested
Network A fails
Network B requested
Network B fails
Network C requested
Network C success.
All within ~4 seconds. If each one of this points make an access to the database for updating the corresponding row, the overhead is significantly? Should I implement some kind of cache in memory and bulk insert the values (seizing also the pair of [request+success] or [request+fail] making a single update?) or can I use directly the database
you can make use of the object model technigue...by this whenever u get a change in data you can write to the objects...and at a certain time interval you can wrtite the data in the object model to the sql database...
the only problem I see is if ur activity is killed...or stop the object model will get empty...you may avoid this by writing the data to database when there is a chance of ur activity getting finished or killed.

Categories

Resources