MCPcopy Create free account
hub / github.com/dbProjectRED/redimo.go / LINSERT

Method LINSERT

lists.go:259–312  ·  view source on GitHub ↗

LINSERT inserts the given element on the given side of the pivot element.

(key string, side LSide, pivot, element Value)

Source from the content-addressed store, hash-verified

257
258// LINSERT inserts the given element on the given side of the pivot element.
259func (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
314func (c Client) listNodeAtPivot(key string, pivot Value, side LSide) (node listNode, found bool, err error) {
315 node, found, err = c.listFindEnd(key, side)

Callers 1

TestListValueBasedCRUDFunction · 0.80

Calls 15

listNodeAtPivotMethod · 0.95
LPUSHXMethod · 0.95
RPUSHXMethod · 0.95
listGetByAddressMethod · 0.95
setPrevMethod · 0.95
setNextMethod · 0.95
putActionMethod · 0.95
listCountDeltaActionMethod · 0.95
LLENMethod · 0.95
isHeadMethod · 0.80
isTailMethod · 0.80
prevMethod · 0.80

Tested by 1

TestListValueBasedCRUDFunction · 0.64