blob: 1bed0d15e14ba3bb1a6a0f41d9c4c73e1b1b711a [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040016package model
17
18import "fmt"
19
20type Transaction struct {
21 proxy *Proxy
22 txid string
23}
24
25func NewTransaction(proxy *Proxy, txid string) *Transaction {
26 tx := &Transaction{
27 proxy: proxy,
28 txid: txid,
29 }
30 return tx
31}
Stephane Barbarieec0919b2018-09-05 14:14:29 -040032func (t *Transaction) Get(path string, depth int, deep bool) Revision {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040033 if t.txid == "" {
34 fmt.Errorf("closed transaction")
35 return nil
36 }
37 // TODO: need to review the return values at the different layers!!!!!
Stephane Barbarieec0919b2018-09-05 14:14:29 -040038 return t.proxy.Get(path, depth, deep, t.txid).(Revision)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040039}
Stephane Barbarieec0919b2018-09-05 14:14:29 -040040func (t *Transaction) Update(path string, data interface{}, strict bool) Revision {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040041 if t.txid == "" {
42 fmt.Errorf("closed transaction")
43 return nil
44 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -040045 return t.proxy.Update(path, data, strict, t.txid).(Revision)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040046}
Stephane Barbarieec0919b2018-09-05 14:14:29 -040047func (t *Transaction) Add(path string, data interface{}) Revision {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040048 if t.txid == "" {
49 fmt.Errorf("closed transaction")
50 return nil
51 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -040052 return t.proxy.Add(path, data, t.txid).(Revision)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040053}
Stephane Barbarieec0919b2018-09-05 14:14:29 -040054func (t *Transaction) Remove(path string) Revision {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040055 if t.txid == "" {
56 fmt.Errorf("closed transaction")
57 return nil
58 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -040059 return t.proxy.Remove(path, t.txid).(Revision)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040060}
61func (t *Transaction) Cancel() {
62 t.proxy.cancelTransaction(t.txid)
63 t.txid = ""
64}
65func (t *Transaction) Commit() {
66 t.proxy.commitTransaction(t.txid)
67 t.txid = ""
68}