github.com/maypok86/otter@v1.2.1/internal/core/task.go (about) 1 // Copyright (c) 2023 Alexey Mayshev. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package core 16 17 import ( 18 "github.com/maypok86/otter/internal/generated/node" 19 ) 20 21 // reason represents the reason for writing the item to the cache. 22 type reason uint8 23 24 const ( 25 addReason reason = iota + 1 26 deleteReason 27 updateReason 28 clearReason 29 closeReason 30 ) 31 32 // task is a set of information to update the cache: 33 // node, reason for write, difference after node cost change, etc. 34 type task[K comparable, V any] struct { 35 n node.Node[K, V] 36 old node.Node[K, V] 37 writeReason reason 38 } 39 40 // newAddTask creates a task to add a node to policies. 41 func newAddTask[K comparable, V any](n node.Node[K, V]) task[K, V] { 42 return task[K, V]{ 43 n: n, 44 writeReason: addReason, 45 } 46 } 47 48 // newDeleteTask creates a task to delete a node from policies. 49 func newDeleteTask[K comparable, V any](n node.Node[K, V]) task[K, V] { 50 return task[K, V]{ 51 n: n, 52 writeReason: deleteReason, 53 } 54 } 55 56 // newUpdateTask creates a task to update the node in the policies. 57 func newUpdateTask[K comparable, V any](n, oldNode node.Node[K, V]) task[K, V] { 58 return task[K, V]{ 59 n: n, 60 old: oldNode, 61 writeReason: updateReason, 62 } 63 } 64 65 // newClearTask creates a task to clear policies. 66 func newClearTask[K comparable, V any]() task[K, V] { 67 return task[K, V]{ 68 writeReason: clearReason, 69 } 70 } 71 72 // newCloseTask creates a task to clear policies and stop all goroutines. 73 func newCloseTask[K comparable, V any]() task[K, V] { 74 return task[K, V]{ 75 writeReason: closeReason, 76 } 77 } 78 79 // node returns the node contained in the task. If node was not specified, it returns nil. 80 func (t *task[K, V]) node() node.Node[K, V] { 81 return t.n 82 } 83 84 // oldNode returns the old node contained in the task. If old node was not specified, it returns nil. 85 func (t *task[K, V]) oldNode() node.Node[K, V] { 86 return t.old 87 } 88 89 // isAdd returns true if this is an add task. 90 func (t *task[K, V]) isAdd() bool { 91 return t.writeReason == addReason 92 } 93 94 // isDelete returns true if this is a delete task. 95 func (t *task[K, V]) isDelete() bool { 96 return t.writeReason == deleteReason 97 } 98 99 // isUpdate returns true if this is an update task. 100 func (t *task[K, V]) isUpdate() bool { 101 return t.writeReason == updateReason 102 } 103 104 // isClear returns true if this is a clear task. 105 func (t *task[K, V]) isClear() bool { 106 return t.writeReason == clearReason 107 } 108 109 // isClose returns true if this is a close task. 110 func (t *task[K, V]) isClose() bool { 111 return t.writeReason == closeReason 112 }