AddVersion adds newVersion as the current version to the in memory representation of the HLV.
(newVersion Version)
| 384 | |
| 385 | // AddVersion adds newVersion as the current version to the in memory representation of the HLV. |
| 386 | func (hlv *HybridLogicalVector) AddVersion(newVersion Version) error { |
| 387 | |
| 388 | // check if this is the first time we're adding a source - version pair |
| 389 | if hlv.SourceID == "" { |
| 390 | hlv.Version = newVersion.Value |
| 391 | hlv.SourceID = newVersion.SourceID |
| 392 | return nil |
| 393 | } |
| 394 | |
| 395 | // If the new version is older than an existing version for the same source, return error |
| 396 | existingValueForSource, found := hlv.GetValue(newVersion.SourceID) |
| 397 | if found && existingValueForSource > newVersion.Value { |
| 398 | return fmt.Errorf("attempting to add new version vector entry with a value that is less than the existing value for the same source. New version: %v, Existing HLV: %v", newVersion, hlv) |
| 399 | } |
| 400 | |
| 401 | // Move existing mv to pv before adding the new version |
| 402 | hlv.InvalidateMV() |
| 403 | |
| 404 | // If the new version has the same source as existing cv, we just update the cv value |
| 405 | if newVersion.SourceID == hlv.SourceID { |
| 406 | hlv.Version = newVersion.Value |
| 407 | return nil |
| 408 | } |
| 409 | |
| 410 | // If we get here this is a new version from a different sourceID. Need to move existing cv to pv and update cv |
| 411 | if hlv.PreviousVersions == nil { |
| 412 | hlv.PreviousVersions = make(HLVVersions) |
| 413 | } |
| 414 | |
| 415 | hlv.PreviousVersions[hlv.SourceID] = hlv.Version |
| 416 | |
| 417 | // If new version source already existed in PV, need to remove it |
| 418 | delete(hlv.PreviousVersions, newVersion.SourceID) |
| 419 | hlv.Version = newVersion.Value |
| 420 | hlv.SourceID = newVersion.SourceID |
| 421 | return nil |
| 422 | } |
| 423 | |
| 424 | // InvalidateMV will move all merge versions to PV, except merge version entries that share a source with cv |
| 425 | func (hlv *HybridLogicalVector) InvalidateMV() { |