Add history after each entry update for DatabaseV4

This commit is contained in:
J-Jamet
2019-09-19 14:20:28 +02:00
parent 116643a45a
commit 5ad3f62de5
4 changed files with 41 additions and 33 deletions

View File

@@ -33,11 +33,17 @@ class UpdateEntryRunnable constructor(
: ActionNodeDatabaseRunnable(context, database, finishRunnable, save) { : ActionNodeDatabaseRunnable(context, database, finishRunnable, save) {
// Keep backup of original values in case save fails // Keep backup of original values in case save fails
private var mBackupEntry: EntryVersioned? = null private var mBackupEntryHistory: EntryVersioned? = null
override fun nodeAction() { override fun nodeAction() {
mBackupEntry = database.addHistoryBackupTo(mOldEntry) mNewEntry.touch(modified = true, touchParents = true)
mOldEntry.touch(modified = true, touchParents = true)
mBackupEntryHistory = EntryVersioned(mOldEntry)
// Create an entry history (an entry history don't have history)
mNewEntry.addEntryToHistory(EntryVersioned(mOldEntry, copyHistory = false))
database.removeOldestHistory(mNewEntry)
// Update entry with new values // Update entry with new values
mOldEntry.updateWith(mNewEntry) mOldEntry.updateWith(mNewEntry)
} }
@@ -45,7 +51,7 @@ class UpdateEntryRunnable constructor(
override fun nodeFinish(result: Result): ActionNodeValues { override fun nodeFinish(result: Result): ActionNodeValues {
if (!result.isSuccess) { if (!result.isSuccess) {
// If we fail to save, back out changes to global structure // If we fail to save, back out changes to global structure
mBackupEntry?.let { mBackupEntryHistory?.let {
mOldEntry.updateWith(it) mOldEntry.updateWith(it)
} }
} }

View File

@@ -702,12 +702,9 @@ class Database {
} }
} }
fun addHistoryBackupTo(entry: EntryVersioned): EntryVersioned { fun removeOldestHistory(entry: EntryVersioned) {
val backupEntry = EntryVersioned(entry)
entry.addBackupToHistory() // Remove oldest history if more than max items or max memory
// Remove oldest backup if more than max items or max memory
pwDatabaseV4?.let { pwDatabaseV4?.let {
val history = entry.getHistory() val history = entry.getHistory()
@@ -721,12 +718,12 @@ class Database {
val maxSize = it.historyMaxSize val maxSize = it.historyMaxSize
if (maxSize >= 0) { if (maxSize >= 0) {
while (true) { while (true) {
var histSize: Long = 0 var historySize: Long = 0
for (backup in history) { for (entryHistory in history) {
histSize += backup.getSize() historySize += entryHistory.getSize()
} }
if (histSize > maxSize) { if (historySize > maxSize) {
entry.removeOldestEntryFromHistory() entry.removeOldestEntryFromHistory()
} else { } else {
break break
@@ -734,8 +731,6 @@ class Database {
} }
} }
} }
return backupEntry
} }
companion object : SingletonHolder<Database>(::Database) { companion object : SingletonHolder<Database>(::Database) {

View File

@@ -15,26 +15,26 @@ class EntryVersioned : NodeVersioned, PwEntryInterface<GroupVersioned> {
var pwEntryV4: PwEntryV4? = null var pwEntryV4: PwEntryV4? = null
private set private set
fun updateWith(entry: EntryVersioned) { fun updateWith(entry: EntryVersioned, copyHistory: Boolean = true) {
entry.pwEntryV3?.let { entry.pwEntryV3?.let {
this.pwEntryV3?.updateWith(it) this.pwEntryV3?.updateWith(it)
} }
entry.pwEntryV4?.let { entry.pwEntryV4?.let {
this.pwEntryV4?.updateWith(it) this.pwEntryV4?.updateWith(it, copyHistory)
} }
} }
/** /**
* Use this constructor to copy an Entry with exact same values * Use this constructor to copy an Entry with exact same values
*/ */
constructor(entry: EntryVersioned) { constructor(entry: EntryVersioned, copyHistory: Boolean = true) {
if (entry.pwEntryV3 != null) { if (entry.pwEntryV3 != null) {
this.pwEntryV3 = PwEntryV3() this.pwEntryV3 = PwEntryV3()
} }
if (entry.pwEntryV4 != null) { if (entry.pwEntryV4 != null) {
this.pwEntryV4 = PwEntryV4() this.pwEntryV4 = PwEntryV4()
} }
updateWith(entry) updateWith(entry, copyHistory)
} }
constructor(entry: PwEntryV3) { constructor(entry: PwEntryV3) {
@@ -258,18 +258,6 @@ class EntryVersioned : NodeVersioned, PwEntryInterface<GroupVersioned> {
pwEntryV4?.stopToManageFieldReferences() pwEntryV4?.stopToManageFieldReferences()
} }
fun addBackupToHistory() {
pwEntryV4?.let {
val entryHistory = PwEntryV4()
entryHistory.updateWith(it)
it.addEntryToHistory(entryHistory)
}
}
fun removeOldestEntryFromHistory() {
pwEntryV4?.removeOldestEntryFromHistory()
}
fun getHistory(): ArrayList<EntryVersioned> { fun getHistory(): ArrayList<EntryVersioned> {
val history = ArrayList<EntryVersioned>() val history = ArrayList<EntryVersioned>()
val entryV4History = pwEntryV4?.history ?: ArrayList() val entryV4History = pwEntryV4?.history ?: ArrayList()
@@ -279,6 +267,20 @@ class EntryVersioned : NodeVersioned, PwEntryInterface<GroupVersioned> {
return history return history
} }
fun addEntryToHistory(entry: EntryVersioned) {
entry.pwEntryV4?.let {
pwEntryV4?.addEntryToHistory(it)
}
}
fun removeAllHistory() {
pwEntryV4?.removeAllHistory()
}
fun removeOldestEntryFromHistory() {
pwEntryV4?.removeOldestEntryFromHistory()
}
fun getSize(): Long { fun getSize(): Long {
return pwEntryV4?.size ?: 0L return pwEntryV4?.size ?: 0L
} }

View File

@@ -129,7 +129,7 @@ class PwEntryV4 : PwEntry<PwGroupV4, PwEntryV4>, PwNodeV4Interface {
* Update with deep copy of each entry element * Update with deep copy of each entry element
* @param source * @param source
*/ */
fun updateWith(source: PwEntryV4) { fun updateWith(source: PwEntryV4, copyHistory: Boolean = true) {
super.updateWith(source) super.updateWith(source)
iconCustom = PwIconCustom(source.iconCustom) iconCustom = PwIconCustom(source.iconCustom)
usageCount = source.usageCount usageCount = source.usageCount
@@ -146,7 +146,8 @@ class PwEntryV4 : PwEntry<PwGroupV4, PwEntryV4>, PwNodeV4Interface {
overrideURL = source.overrideURL overrideURL = source.overrideURL
autoType = AutoType(source.autoType) autoType = AutoType(source.autoType)
history.clear() history.clear()
history.addAll(source.history) if (copyHistory)
history.addAll(source.history)
url = source.url url = source.url
additional = source.additional additional = source.additional
tags = source.tags tags = source.tags
@@ -287,6 +288,10 @@ class PwEntryV4 : PwEntry<PwGroupV4, PwEntryV4>, PwNodeV4Interface {
history.add(entry) history.add(entry)
} }
fun removeAllHistory() {
history.clear()
}
fun removeOldestEntryFromHistory() { fun removeOldestEntryFromHistory() {
var min: Date? = null var min: Date? = null
var index = -1 var index = -1