LINSERT inserts the given element on the given side of the pivot element.
(key string, side LSide, pivot, element Value)
| 257 | |
| 258 | // LINSERT inserts the given element on the given side of the pivot element. |
| 259 | func (c Client) LINSERT(key string, side LSide, pivot, element Value) (newLength int64, done bool, err error) { |
| 260 | var actions []dynamodb.TransactWriteItem |
| 261 | |
| 262 | pivotNode, found, err := c.listNodeAtPivot(key, pivot, Left) |
| 263 | if err != nil || !found { |
| 264 | return newLength, false, err |
| 265 | } |
| 266 | |
| 267 | switch { |
| 268 | case pivotNode.isHead() && side == Left: |
| 269 | _, err = c.LPUSHX(key, element) |
| 270 | done = true |
| 271 | case pivotNode.isTail() && side == Right: |
| 272 | _, err = c.RPUSHX(key, element) |
| 273 | done = true |
| 274 | default: |
| 275 | otherNode, ok, err := c.listGetByAddress(key, pivotNode.prev(side)) |
| 276 | if err != nil || !ok { |
| 277 | return newLength, false, fmt.Errorf("could not find or load required node %v: %w", pivotNode, err) |
| 278 | } |
| 279 | |
| 280 | newNode := listNode{ |
| 281 | key: key, |
| 282 | address: ulid.MustNew(ulid.Now(), rand.Reader).String(), |
| 283 | value: ReturnValue{element.ToAV()}, |
| 284 | } |
| 285 | newNode.setPrev(side, otherNode.address) |
| 286 | newNode.setNext(side, pivotNode.address) |
| 287 | |
| 288 | actions = append(actions, otherNode.updateSideAction(side.otherSide(), newNode.address, c)) |
| 289 | actions = append(actions, pivotNode.updateSideAction(side, newNode.address, c)) |
| 290 | actions = append(actions, newNode.putAction(c)) |
| 291 | actions = append(actions, c.listCountDeltaAction(key, 1)) |
| 292 | } |
| 293 | |
| 294 | if err != nil { |
| 295 | return newLength, done, err |
| 296 | } |
| 297 | |
| 298 | if len(actions) > 0 { |
| 299 | _, err = c.ddbClient.TransactWriteItemsRequest(&dynamodb.TransactWriteItemsInput{ |
| 300 | TransactItems: actions, |
| 301 | }).Send(context.TODO()) |
| 302 | if err != nil { |
| 303 | return newLength, done, err |
| 304 | } |
| 305 | |
| 306 | done = true |
| 307 | } |
| 308 | |
| 309 | newLength, err = c.LLEN(key) |
| 310 | |
| 311 | return newLength, done, err |
| 312 | } |
| 313 | |
| 314 | func (c Client) listNodeAtPivot(key string, pivot Value, side LSide) (node listNode, found bool, err error) { |
| 315 | node, found, err = c.listFindEnd(key, side) |