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) {
// Keep backup of original values in case save fails
private var mBackupEntry: EntryVersioned? = null
private var mBackupEntryHistory: EntryVersioned? = null
override fun nodeAction() {
mBackupEntry = database.addHistoryBackupTo(mOldEntry)
mOldEntry.touch(modified = true, touchParents = true)
mNewEntry.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
mOldEntry.updateWith(mNewEntry)
}
@@ -45,7 +51,7 @@ class UpdateEntryRunnable constructor(
override fun nodeFinish(result: Result): ActionNodeValues {
if (!result.isSuccess) {
// If we fail to save, back out changes to global structure
mBackupEntry?.let {
mBackupEntryHistory?.let {
mOldEntry.updateWith(it)
}
}

View File

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

View File

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

View File

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