Optimize DatabaseManager.update(UpdateQuery) to Avoid Full Entity Rewrite
The current update is implemented as a default method, primarily to ensure compatibility across all NoSQL types. However, this comes with significant performance drawbacks:
- It fetches the existing entity from the database
- Deserializes it to Java
- Updates the field(s) in memory
- Then saves the entire object back to the database
This process introduces unnecessary round-trips and serialization overhead, especially for document and column-family databases that support native update operations (e.g., $set, SET, etc.).
The goal is provide a custom update() implementation per database driver to take advantage of database-native operations whenever possible — avoiding read/deserialize steps when only partial updates are needed.
📊 Current vs. Target Flow
❌ Current Implementation (Default in Java)
flowchart TD
subgraph Current Default Flow
A1[UpdateQuery] --> B1[Select matching entities]
B1 --> C1[Apply in-memory changes]
C1 --> D1[Re-insert entire entity]
end
subgraph Optimized Driver Flow
A2[UpdateQuery] --> B2[Translate to native DB update command]
B2 --> C2[Execute at DB layer]
end
- Reduces latency
- Fewer serialization/deserialization steps
- Better alignment with database-native update capabilities
- Enables atomic updates in databases that support them
🛠️ Implementation per database
The code to be overwritten on those drivers
- [ ] Override
updatein MongoDB driver to use$set - [ ] Override
updatein Cassandra driver to useUPDATE ... SET - [ ] Override
updatein DynamoDB driver usingUpdateItem - [ ] Override
updatein Couchbase usingmutateIn - [ ] Override
updatein Neo4j usingSET - [ ] Override
updatein ArangoDB usingupdate() - [ ] Override
updatein CouchDB (if native support allows it)
As the next step, break compatibility and move it to a non-default method.
@dearrudam I will move this one to you as well, so you can enhance the database on your time.