Class: Fairy::PGroupBy::DirectMergeSortBuffer::CachedBuffer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/fairy/node/p-group-by.rb

Instance Method Summary collapse

Constructor Details

#initialize(njob, io) ⇒ CachedBuffer

Returns a new instance of CachedBuffer.



1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
# File 'lib/fairy/node/p-group-by.rb', line 1044

def initialize(njob, io)
  @njob = njob
  @io = io
  io.open

  @cache = []
  @cache_pv = 0

  @eof = false

  read_buffer
  @key = @njob.hash_key(@cache.first)
end

Instance Method Details

#each_by_same_key(&block) ⇒ Object



1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
# File 'lib/fairy/node/p-group-by.rb', line 1070

def each_by_same_key(&block)
  if @cache.size <= @cache_pv
    read_buffer
    return if @cache.empty?
  end
  
  while @njob.hash_key(@cache[@cache_pv]) == @key
    block.call @cache[@cache_pv]
    @cache_pv += 1

    if @cache.size <= @cache_pv
      read_buffer
      return if @cache.empty?
    end
  end
  @key = @njob.hash_key(@cache[@cache_pv])
end

#eof?Boolean

Returns:

  • (Boolean)


1062
1063
1064
# File 'lib/fairy/node/p-group-by.rb', line 1062

def eof?
  @eof
end

#keyObject



1066
1067
1068
# File 'lib/fairy/node/p-group-by.rb', line 1066

def key
  @key
end

#read_bufferObject



1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
# File 'lib/fairy/node/p-group-by.rb', line 1105

def read_buffer
  io = @io.io
  begin
    @cache = Marshal.load(io)
  rescue EOFError
    @eof = true
    @cache = []
  rescue ArgumentError
    Log::debug(self, "MARSHAL ERROR OCCURED!!")
    io.seek(-1024, IO::SEEK_CUR)
    buf = io.read(2048)
    Log::debugf(self, "File Contents: %s", buf)
    raise
  end
#	  @key = @njob.hash_key(@cache.first)
  @cache_pv = 0
end

#shift_valuesObject



1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
# File 'lib/fairy/node/p-group-by.rb', line 1088

def shift_values
  if @cache.empty?
    read_buffer
    return nil if @cache.empty?
  end

  idx = @cache.index{|v| @njob.hash_key(v) != @key}
  if idx
    vv = @cache.slice!(0, idx)
    @key = @njob.hash_key(@cache.first)
  else
    vv = @cache
    @cache = []
  end
  vv
end