|
| 1 | +package db |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + e "github.com/ulfox/dby/errors" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + ire = "index error" |
| 12 | +) |
| 13 | + |
| 14 | +// state struct used by dby storage |
| 15 | +type state struct { |
| 16 | + data, buffer []interface{} |
| 17 | + lib map[string]int |
| 18 | + ad int |
| 19 | +} |
| 20 | + |
| 21 | +// newStateFactory for creating a new v3 State |
| 22 | +func newStateFactory() *state { |
| 23 | + s := state{ |
| 24 | + data: make([]interface{}, 0), |
| 25 | + buffer: make([]interface{}, 0), |
| 26 | + lib: make(map[string]int), |
| 27 | + } |
| 28 | + return &s |
| 29 | +} |
| 30 | + |
| 31 | +// Clear for clearing the v3 state |
| 32 | +func (c *state) Clear() { |
| 33 | + c.data, c.buffer, c.lib = nil, nil, nil |
| 34 | + |
| 35 | + c.data = make([]interface{}, 0) |
| 36 | + c.buffer = make([]interface{}, 0) |
| 37 | + c.lib = make(map[string]int) |
| 38 | +} |
| 39 | + |
| 40 | +// SetAD for setting new Active Document index |
| 41 | +func (c *state) SetAD(i int) error { |
| 42 | + if err := c.IndexInRange(i); err != nil { |
| 43 | + return wrapErr(err) |
| 44 | + } |
| 45 | + c.ad = i |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +// GetAD returns the current active document index |
| 50 | +func (c *state) GetAD() int { |
| 51 | + return c.ad |
| 52 | +} |
| 53 | + |
| 54 | +// PushData for appending data to the data array |
| 55 | +func (c *state) PushData(d interface{}) { |
| 56 | + c.data = append(c.data, d) |
| 57 | +} |
| 58 | + |
| 59 | +// PushBuffer for appending data to the buffer array |
| 60 | +func (c *state) PushBuffer(d interface{}) { |
| 61 | + c.buffer = append(c.buffer, d) |
| 62 | +} |
| 63 | + |
| 64 | +// GetAllData returns the data array |
| 65 | +func (c *state) GetAllData() []interface{} { |
| 66 | + return c.data |
| 67 | +} |
| 68 | + |
| 69 | +// GetAllBuffer returns the buffer array |
| 70 | +func (c *state) GetAllBuffer() []interface{} { |
| 71 | + return c.buffer |
| 72 | +} |
| 73 | + |
| 74 | +// GetData returns the data in the c.ad index from the data array |
| 75 | +func (c *state) GetData() interface{} { |
| 76 | + data, _ := c.GetDataFromIndex(c.GetAD()) |
| 77 | + return data |
| 78 | +} |
| 79 | + |
| 80 | +// GetDataFromIndex returns the i'th element from the data array |
| 81 | +func (c *state) GetDataFromIndex(i int) (interface{}, error) { |
| 82 | + if err := c.IndexInRange(i); err != nil { |
| 83 | + return nil, wrapErr(err) |
| 84 | + } |
| 85 | + return c.data[i], nil |
| 86 | +} |
| 87 | + |
| 88 | +// SetData sets to input value the data in the c.ad index from the data array |
| 89 | +func (c *state) SetData(v interface{}) error { |
| 90 | + return c.SetDataFromIndex(v, c.GetAD()) |
| 91 | +} |
| 92 | + |
| 93 | +// SetDataFromIndex sets to input value the i'th element from the data array |
| 94 | +func (c *state) SetDataFromIndex(v interface{}, i int) error { |
| 95 | + if err := c.IndexInRange(i); err != nil { |
| 96 | + return wrapErr(err) |
| 97 | + } |
| 98 | + c.data[i] = v |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +// GetBufferFromIndex returns the i'th element from the buffer array |
| 103 | +func (c *state) GetBufferFromIndex(i int) (interface{}, error) { |
| 104 | + if err := c.IndexInRange(i); err != nil { |
| 105 | + return nil, wrapErr(err) |
| 106 | + } |
| 107 | + return c.data[i], nil |
| 108 | +} |
| 109 | + |
| 110 | +// IndexInRange check if index is within data array range |
| 111 | +func (c *state) IndexInRange(i int) error { |
| 112 | + if len(c.data)-1 >= i { |
| 113 | + return nil |
| 114 | + } |
| 115 | + return fmt.Errorf(ire) |
| 116 | +} |
| 117 | + |
| 118 | +// Lib returns the lib map |
| 119 | +func (c *state) Lib() map[string]int { |
| 120 | + return c.lib |
| 121 | +} |
| 122 | + |
| 123 | +// AddDoc for adding a document to the lib map |
| 124 | +func (c *state) AddDoc(k string, i int) error { |
| 125 | + if err := c.IndexInRange(i); err != nil { |
| 126 | + return wrapErr(err) |
| 127 | + } |
| 128 | + c.lib[k] = i |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +// LibIndex returns the index for a given doc name |
| 133 | +func (c *state) LibIndex(doc string) (int, bool) { |
| 134 | + i, exists := c.lib[strings.ToLower(doc)] |
| 135 | + return i, exists |
| 136 | +} |
| 137 | + |
| 138 | +// RemoveDocName removes a doc from the lib |
| 139 | +func (c *state) RemoveDocName(i int) error { |
| 140 | + if err := c.IndexInRange(i); err != nil { |
| 141 | + return wrapErr(err) |
| 142 | + } |
| 143 | + for k, v := range c.lib { |
| 144 | + if v == i { |
| 145 | + delete(c.lib, k) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +// DeleteData for deleting the i'th element from the data array |
| 153 | +func (c *state) DeleteData(i int) error { |
| 154 | + if err := c.IndexInRange(i); err != nil { |
| 155 | + return wrapErr(err) |
| 156 | + } |
| 157 | + |
| 158 | + if err := c.RemoveDocName(i); err != nil { |
| 159 | + return e.WrapErr(err) |
| 160 | + } |
| 161 | + |
| 162 | + c.data[i] = nil |
| 163 | + c.data = append(c.data[:i], c.data[i+1:]...) |
| 164 | + |
| 165 | + if c.ad == i { |
| 166 | + if c.ad > 0 { |
| 167 | + c.ad = c.ad - 1 |
| 168 | + } else { |
| 169 | + c.ad = 0 |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return nil |
| 174 | +} |
| 175 | + |
| 176 | +// CopyBufferToData for copying buffer array over data array |
| 177 | +func (c *state) CopyBufferToData() { |
| 178 | + copy(c.data, c.buffer) |
| 179 | +} |
| 180 | + |
| 181 | +// UnsetDataArray for deleting all data. This sets data = nil |
| 182 | +func (c *state) UnsetDataArray() { |
| 183 | + c.data = nil |
| 184 | +} |
| 185 | + |
| 186 | +// DeleteAllData calls PurgeAllData first and then creates a new empty array |
| 187 | +func (c *state) DeleteAllData() { |
| 188 | + c.UnsetDataArray() |
| 189 | + c.data = make([]interface{}, 0) |
| 190 | +} |
| 191 | + |
| 192 | +// UnsetBufferArray This sets buffer = nil |
| 193 | +func (c *state) UnsetBufferArray() { |
| 194 | + c.buffer = nil |
| 195 | +} |
| 196 | + |
| 197 | +// DeleteBuffer deletes the data from the buffer array |
| 198 | +func (c *state) DeleteBuffer() { |
| 199 | + c.UnsetBufferArray() |
| 200 | + c.buffer = append(c.buffer, 0) |
| 201 | +} |
| 202 | + |
| 203 | +// ClearLib removes all keys from the lib map |
| 204 | +func (c *state) ClearLib() { |
| 205 | + c.lib = nil |
| 206 | + c.lib = make(map[string]int) |
| 207 | +} |
0 commit comments