Created
January 26, 2022 16:53
-
-
Save trozet/44dc1cf5a3bf2e9a738d54091f04ef31 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()} | |
} | |
dbModel := t.Model | |
Loop: | |
for { | |
var filteredRows []ovsdb.Row | |
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 | |
} | |
} | |
} | |
if foundMatch { | |
resultRow, err := m.NewRow(info) | |
if err != nil { | |
panic(err) | |
} | |
filteredRows = append(filteredRows, resultRow) | |
} | |
} | |
if until == "==" && len(filteredRows) > 0 { | |
return ovsdb.OperationResult{ | |
Rows: filteredRows, | |
} | |
} else if until == "!=" && len(filteredRows) == 0 { | |
return ovsdb.OperationResult{} | |
} | |
if timeout != nil { | |
if time.Since(start) > time.Duration(*timeout) * time.Second { | |
break Loop | |
} | |
} | |
time.Sleep(200 * time.Millisecond) | |
} | |
e := ovsdb.TimedOut{} | |
return ovsdb.OperationResult{Error: e.Error()} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment