Class: Chan::Bytes

Inherits:
Object
  • Object
show all
Defined in:
lib/xchan/bytes.rb

Overview

Chan::Bytes represents a collection of byte counts for each object stored on a channel. When an object is written to a channel, the collection increases in size, and when an object is read from a channel, the collection decreases in size.

Instance Method Summary collapse

Constructor Details

#initialize(tmpdir) ⇒ Chan::Bytes

Parameters:

  • tmpdir (String)

    Directory where temporary files are stored



17
18
19
20
21
# File 'lib/xchan/bytes.rb', line 17

def initialize(tmpdir)
  @io = Chan.temporary_file(%w[bytes .json], tmpdir:)
  @io.sync = true
  write(@io, [])
end

Instance Method Details

#unshift(len) ⇒ void

This method returns an undefined value.

Adds a count to the start of the collection

Parameters:

  • len (Integer)

    The bytesize of an object



28
29
30
31
32
33
34
# File 'lib/xchan/bytes.rb', line 28

def unshift(len)
  return 0 if len.nil? || len.zero?
  bytes = read(@io)
  bytes.unshift(len)
  write(@io, bytes)
  len
end

#push(len) ⇒ void

This method returns an undefined value.

Adds a count to the end of the collection

Parameters:

  • len (Integer)

    The bytesize of an object



41
42
43
44
45
46
47
# File 'lib/xchan/bytes.rb', line 41

def push(len)
  return 0 if len.nil? || len.zero?
  bytes = read(@io)
  bytes.push(len)
  write(@io, bytes)
  len
end

#shiftInteger

Removes a count from the start of the collection

Returns:

  • (Integer)

    Returns the removed byte count



53
54
55
56
57
58
59
# File 'lib/xchan/bytes.rb', line 53

def shift
  bytes = read(@io)
  return 0 if bytes.size.zero?
  len = bytes.shift
  write(@io, bytes)
  len
end

#sizeInteger

Returns the number of objects in the collection

Returns:

  • (Integer)

    Returns the number of objects in the collection



64
65
66
# File 'lib/xchan/bytes.rb', line 64

def size
  read(@io).size
end

#closevoid

This method returns an undefined value.

Close the underlying IO



71
72
73
# File 'lib/xchan/bytes.rb', line 71

def close
  @io.close
end