@@ -120,20 +120,10 @@ func (sl *SkipList) Insert(key, value []byte, ttl *time.Duration) {
120120
121121 // Traverse the skip list to find the position of the key
122122 for i := sl .level ; i >= 0 ; i -- {
123- for current .forward [i ] != nil && string (current .forward [i ].key ) < string (key ) {
124- if current .forward [i ].IsExpired () {
125- //sl.Delete(current.forward[i].key)
126- // We mark as tombstone and delete later
127- current .forward [i ].ttl = nil
128-
129- // Subtraction of the size of the old value
130- sl .size -= current .forward [i ].Size ()
131- // Mark as tombstone
132- current .forward [i ].value = []byte (TOMBSTONE_VALUE )
133-
134- // Add the size of the tombstone
135- sl .size += current .forward [i ].Size ()
136-
123+ for current .forward [i ] != nil && (string (current .forward [i ].key ) < string (key ) || bytes .Equal (current .forward [i ].value , []byte (TOMBSTONE_VALUE ))) {
124+ if current .forward [i ].IsExpired () || bytes .Equal (current .forward [i ].value , []byte (TOMBSTONE_VALUE )) {
125+ // Skip nodes with tombstone values
126+ current = current .forward [i ]
137127 } else {
138128 current = current .forward [i ]
139129 }
@@ -181,19 +171,10 @@ func (sl *SkipList) Delete(key []byte) {
181171 current := sl .header
182172
183173 for i := sl .level ; i >= 0 ; i -- {
184- for current .forward [i ] != nil && string (current .forward [i ].key ) < string (key ) {
185- if current .forward [i ].IsExpired () {
186- //sl.Delete(current.forward[i].key)
187- // We mark as tombstone and delete later
188- current .forward [i ].ttl = nil
189-
190- // Subtraction of the size of the old value
191- sl .size -= current .forward [i ].Size ()
192- // Mark as tombstone
193- current .forward [i ].value = []byte (TOMBSTONE_VALUE )
194-
195- // Add the size of the tombstone
196- sl .size += current .forward [i ].Size ()
174+ for current .forward [i ] != nil && (string (current .forward [i ].key ) < string (key ) || bytes .Equal (current .forward [i ].value , []byte (TOMBSTONE_VALUE ))) {
175+ if current .forward [i ].IsExpired () || bytes .Equal (current .forward [i ].value , []byte (TOMBSTONE_VALUE )) {
176+ // Skip nodes with tombstone values
177+ current = current .forward [i ]
197178 } else {
198179 current = current .forward [i ]
199180 }
@@ -222,18 +203,10 @@ func (sl *SkipList) Delete(key []byte) {
222203func (sl * SkipList ) Search (key []byte ) ([]byte , bool ) {
223204 current := sl .header
224205 for i := sl .level ; i >= 0 ; i -- {
225- for current .forward [i ] != nil && string (current .forward [i ].key ) < string (key ) {
206+ for current .forward [i ] != nil && ( string (current .forward [i ].key ) < string (key ) || bytes . Equal ( current . forward [ i ]. value , [] byte ( TOMBSTONE_VALUE )) ) {
226207 if current .forward [i ].IsExpired () || bytes .Equal (current .forward [i ].value , []byte (TOMBSTONE_VALUE )) {
227- // Mark as tombstone and delete later
228- current .forward [i ].ttl = nil
229-
230- // Subtraction of the size of the old value
231- sl .size -= current .forward [i ].Size ()
232- // Mark as tombstone
233- current .forward [i ].value = []byte (TOMBSTONE_VALUE )
234-
235- // Add the size of the tombstone
236- sl .size += current .forward [i ].Size ()
208+ // Skip nodes with tombstone values
209+ current = current .forward [i ]
237210 } else {
238211 current = current .forward [i ]
239212 }
@@ -242,16 +215,6 @@ func (sl *SkipList) Search(key []byte) ([]byte, bool) {
242215 current = current .forward [0 ]
243216 if current != nil && string (current .key ) == string (key ) {
244217 if current .IsExpired () || bytes .Equal (current .value , []byte (TOMBSTONE_VALUE )) {
245- // Mark as tombstone and delete later
246- current .ttl = nil
247-
248- // Subtraction of the size of the old value
249- sl .size -= current .Size ()
250- // Mark as tombstone
251- current .value = []byte (TOMBSTONE_VALUE )
252-
253- // Add the size of the tombstone
254- sl .size += current .Size ()
255218 return nil , false
256219 }
257220 return current .value , true
0 commit comments