|
| 1 | +package tablestore |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/binary" |
| 6 | + |
| 7 | + "github.com/aliyun/aliyun-tablestore-go-sdk/tablestore" |
| 8 | +) |
| 9 | + |
| 10 | +type mockClient struct { |
| 11 | + tablestore.TableStoreClient |
| 12 | + |
| 13 | + data map[string][]byte |
| 14 | +} |
| 15 | + |
| 16 | +func (m *mockClient) DeleteRow(request *tablestore.DeleteRowRequest) (*tablestore.DeleteRowResponse, error) { |
| 17 | + var key string |
| 18 | + for _, col := range request.DeleteRowChange.PrimaryKey.PrimaryKeys { |
| 19 | + if col.ColumnName == stateKey { |
| 20 | + key = col.Value.(string) |
| 21 | + |
| 22 | + break |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + delete(m.data, key) |
| 27 | + |
| 28 | + return nil, nil |
| 29 | +} |
| 30 | + |
| 31 | +func (m *mockClient) GetRow(request *tablestore.GetRowRequest) (*tablestore.GetRowResponse, error) { |
| 32 | + var key string |
| 33 | + for _, col := range request.SingleRowQueryCriteria.PrimaryKey.PrimaryKeys { |
| 34 | + if col.ColumnName == stateKey { |
| 35 | + key = col.Value.(string) |
| 36 | + |
| 37 | + break |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + val := m.data[key] |
| 42 | + |
| 43 | + resp := &tablestore.GetRowResponse{ |
| 44 | + Columns: []*tablestore.AttributeColumn{{ |
| 45 | + ColumnName: stateValue, |
| 46 | + Value: val, |
| 47 | + }}, |
| 48 | + } |
| 49 | + |
| 50 | + return resp, nil |
| 51 | +} |
| 52 | + |
| 53 | +func (m *mockClient) UpdateRow(req *tablestore.UpdateRowRequest) (*tablestore.UpdateRowResponse, error) { |
| 54 | + change := req.UpdateRowChange |
| 55 | + |
| 56 | + var val []byte |
| 57 | + var key string |
| 58 | + |
| 59 | + for _, col := range change.PrimaryKey.PrimaryKeys { |
| 60 | + if col.ColumnName == stateKey { |
| 61 | + key = col.Value.(string) |
| 62 | + |
| 63 | + break |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + for _, col := range change.Columns { |
| 68 | + if col.ColumnName == stateValue { |
| 69 | + buf := &bytes.Buffer{} |
| 70 | + binary.Write(buf, binary.BigEndian, col.Value) |
| 71 | + val = buf.Bytes() |
| 72 | + |
| 73 | + break |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + m.data[key] = val |
| 78 | + |
| 79 | + return nil, nil |
| 80 | +} |
| 81 | + |
| 82 | +func (m *mockClient) BatchGetRow(request *tablestore.BatchGetRowRequest) (*tablestore.BatchGetRowResponse, error) { |
| 83 | + resp := &tablestore.BatchGetRowResponse{ |
| 84 | + TableToRowsResult: map[string][]tablestore.RowResult{}, |
| 85 | + } |
| 86 | + |
| 87 | + for _, criteria := range request.MultiRowQueryCriteria { |
| 88 | + tableRes := resp.TableToRowsResult[criteria.TableName] |
| 89 | + if tableRes == nil { |
| 90 | + tableRes = []tablestore.RowResult{} |
| 91 | + } |
| 92 | + for _, keys := range criteria.PrimaryKey { |
| 93 | + for _, key := range keys.PrimaryKeys { |
| 94 | + if key.ColumnName == stateKey { |
| 95 | + pk := key.Value.(string) |
| 96 | + |
| 97 | + if m.data[pk] == nil { |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + value := m.data[key.Value.(string)] |
| 102 | + tableRes = append(tableRes, tablestore.RowResult{ |
| 103 | + TableName: criteria.TableName, |
| 104 | + Columns: []*tablestore.AttributeColumn{ |
| 105 | + { |
| 106 | + ColumnName: stateValue, |
| 107 | + Value: value, |
| 108 | + }, |
| 109 | + }, |
| 110 | + PrimaryKey: tablestore.PrimaryKey{ |
| 111 | + PrimaryKeys: []*tablestore.PrimaryKeyColumn{ |
| 112 | + { |
| 113 | + ColumnName: stateKey, |
| 114 | + Value: key.Value, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }) |
| 119 | + resp.TableToRowsResult[criteria.TableName] = tableRes |
| 120 | + |
| 121 | + break |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return resp, nil |
| 128 | +} |
| 129 | + |
| 130 | +func (m *mockClient) BatchWriteRow(request *tablestore.BatchWriteRowRequest) (*tablestore.BatchWriteRowResponse, error) { |
| 131 | + resp := &tablestore.BatchWriteRowResponse{} |
| 132 | + for _, changes := range request.RowChangesGroupByTable { |
| 133 | + for _, change := range changes { |
| 134 | + switch inst := change.(type) { |
| 135 | + case *tablestore.UpdateRowChange: |
| 136 | + var pk string |
| 137 | + for _, col := range inst.PrimaryKey.PrimaryKeys { |
| 138 | + if col.ColumnName == stateKey { |
| 139 | + pk = col.Value.(string) |
| 140 | + |
| 141 | + break |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + for _, col := range inst.Columns { |
| 146 | + if col.ColumnName == stateValue { |
| 147 | + buf := &bytes.Buffer{} |
| 148 | + binary.Write(buf, binary.BigEndian, col.Value) |
| 149 | + m.data[pk] = buf.Bytes() |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + case *tablestore.DeleteRowChange: |
| 154 | + for _, col := range inst.PrimaryKey.PrimaryKeys { |
| 155 | + if col.ColumnName == stateKey { |
| 156 | + delete(m.data, col.Value.(string)) |
| 157 | + |
| 158 | + break |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return resp, nil |
| 166 | +} |
0 commit comments