Created
January 26, 2022 16:31
-
-
Save trozet/1f2ccbb05ab97766fec5ffb7bd9d8a24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (t *Transaction) Wait(database, table string, timeout *int, where []ovsdb.Condition, columns []string, until string, rows []ovsdb.Row) ovsdb.OperationResult { | |
start := time.Now() | |
if until != "!=" || until != "==" { | |
e := ovsdb.NotSupported{} | |
return ovsdb.OperationResult{Error: e.Error()} | |
} | |
var results []ovsdb.Row | |
dbModel := t.Model | |
for { | |
foundRows, err := t.rowsFromTransactionCacheAndDatabase(table, where) | |
if err != nil { | |
panic(err) | |
} | |
m := dbModel.Mapper | |
for _, row := range foundRows { | |
info, err := dbModel.NewModelInfo(row) | |
if err != nil { | |
panic(err) | |
} | |
foundMatch := true | |
for _, column := range columns { | |
for _, r := range rows { | |
i, err := dbModel.NewModelInfo(r) | |
if err != nil { | |
panic(err) | |
} | |
x, err := i.FieldByColumn(column) | |
if err != nil { | |
continue | |
} | |
y, err := info.FieldByColumn(column) | |
if err != nil { | |
continue | |
} | |
if !reflect.DeepEqual(x, y) { | |
foundMatch = false | |
} | |
} | |
} | |
// positive search and we found the first match | |
if until == "==" && foundMatch { | |
resultRow, err := m.NewRow(info) | |
if err != nil { | |
panic(err) | |
} | |
return ovsdb.OperationResult{ | |
Rows: []ovsdb.Row{resultRow}, | |
} | |
} | |
} | |
if timeout != nil { | |
if time.Since(start) > time.Duration(*timeout) * time.Second { | |
e := ovsdb.TimedOut{} | |
return ovsdb.OperationResult{Error: e.Error()} | |
} | |
} | |
time.Sleep(200 * time.Millisecond) | |
} | |
e := ovsdb.NotSupported{} | |
return ovsdb.OperationResult{Error: e.Error()} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment