-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a optimized database #271
Add a optimized database #271
Conversation
57ec133
to
79f643b
Compare
@viveksahu26 same here is this ready for review |
Yeah, this one too. |
@viveksahu26 this appears to be too complex. Lets discuss this |
Yeah sure @riteshnoronha !! So, basically originally we were fetching records from database in 3 ways:
For example, to get all In the new changes, we have simplified it to Map data structure. As it has the functionality to store all values for a particular key. func (d *db) addRecord(r *record) {
d.records = append(d.records, r)
} But, when we are adding any record, we are adding it in 3 ways:
// addRecord adds a single record to the database
func (d *db) addRecord(r *record) {
d.mu.Lock()
defer d.mu.Unlock()
// store record using a key
d.keyRecords[r.check_key] = append(d.keyRecords[r.check_key], r)
// store record using a id
d.idRecords[r.id] = append(d.idRecords[r.id], r)
if d.idRecords[r.id] == nil {
d.keyIdRecords[r.id] = make(map[int][]*record)
}
// store record using a key and id
d.keyIdRecords[r.id][r.check_key] = append(d.keyIdRecords[r.id][r.check_key], r)
d.allIds[r.id] = struct{}{}
} We are adding in this way, so that at the time of fetching these records from any any type of key, whether it be Now coming to the concept of
Here we are using
Each time when write operation is being done to db, then we want to make sure that no other process or threads or goroutine is trying to write. That's why we are locking the operation. And once written it is unlocked. And then next threads or goroutine waiting in a queue will perform the writing operation.
and Below Mutex is used in case when multiple process or threads or goroutine is trying to read the database, in that
Let me know if you have any question ? |
We dont use go-routines currently. Lets keep it even simpler for this release. |
Signed-off-by: Vivek Kumar Sahu <[email protected]>
Signed-off-by: Vivek Kumar Sahu <[email protected]>
Signed-off-by: Vivek Kumar Sahu <[email protected]>
b974d56
to
a1ebab4
Compare
Removed Mutex stuffs or any related to goroutines. |
Closes issue: #265
This pull request introduces an optimized version of the db package to improve the performance of our database operations. As well as, benchmark tests been added to measure the performance and efficiency of the new implementation.
Changes modifies are:
check_key
.id
.check_key
andid
.Storing records in map is highly optimized version at the retrieval time, which result into O(1) time complexity. Whereas, original db uses loops to retrieve records and that results into O(n) time complexity.
Use of Mutex concept:
Apart from that added a benchmark test, to measure the performance of key database operations::
check_key
.id
.check_key
andid
.Implementation of goroutines for parallel tasking:- Goroutines are functions or methods that run concurrently with other functions or methods. They are created using the go keyword. Whereas channels are used to send and receive values, allowing goroutines to synchronize their operations.sync.RWMutex
will make ensure that concurrent access to the database is handled correctly.