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



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

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



31
32
33
34
35
36
37
# File 'lib/xchan/bytes.rb', line 31

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



46
47
48
49
50
51
52
# File 'lib/xchan/bytes.rb', line 46

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



59
60
61
62
63
64
65
# File 'lib/xchan/bytes.rb', line 59

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



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

def size
  read(@io).size
end

#closevoid

This method returns an undefined value.

Close the underlying IO



78
79
80
# File 'lib/xchan/bytes.rb', line 78

def close
  @io.close
end