MCPcopy Create free account
hub / github.com/ByteStorage/FlyDB / XDel

Method XDel

structure/stream.go:263–310  ·  view source on GitHub ↗

XDel deletes a message from a stream. Returns true if the message was deleted. Returns false if the message was not deleted. Returns the number of messages in the stream after deletion. Parameters: name: The name of the stream. ids: The ID of the message to delete. Returns: bool: Indicates wh

(name string, ids string)

Source from the content-addressed store, hash-verified

261// - It returns false if the message was not found in the stream.
262// - It returns the number of messages in the stream after deletion.
263func (s *StreamStructure) XDel(name string, ids string) (bool, int, error) {
264 // Get the stream
265 encodedStreams, err := s.db.Get([]byte(name))
266 if err != nil {
267 return false, len(s.streams.Messages), err
268 }
269
270 // Decode the streams
271 if err = s.decodeStreams(encodedStreams, s.streams); err != nil {
272 return false, len(s.streams.Messages), err
273 }
274
275 // Get the messages
276 messages := s.streams.Messages
277
278 // Create a new slice of StreamMessage
279 var result []*StreamMessage
280
281 // Get the messages
282 found := false
283 for _, msg := range messages {
284 if msg.Id != ids {
285 result = append(result, msg)
286 } else {
287 found = true
288 }
289 }
290
291 if !found {
292 return false, len(s.streams.Messages), nil
293 }
294
295 // Set the messages
296 s.streams.Messages = result
297
298 // Encode the streams
299 encodedStreams, err = s.encodeStreams(s.streams)
300 if err != nil {
301 return false, len(s.streams.Messages), err
302 }
303
304 // Set the stream
305 if err = s.db.Put([]byte(s.streams.Name), encodedStreams); err != nil {
306 return false, len(s.streams.Messages), err
307 }
308
309 return true, len(s.streams.Messages), nil
310}
311
312// XLen returns the number of elements in a given stream.
313// It takes the name of the stream as an argument and returns the number of elements in the stream.

Callers 1

TestStreamStructure_XDelFunction · 0.80

Calls 4

decodeStreamsMethod · 0.95
encodeStreamsMethod · 0.95
GetMethod · 0.65
PutMethod · 0.65

Tested by 1

TestStreamStructure_XDelFunction · 0.64