Class: VK::BOOK

Inherits:
O
  • Object
show all
Defined in:
lib/valkey/objects.rb

Instance Attribute Summary

Attributes inherited from O

#key

Instance Method Summary collapse

Methods inherited from O

#delete!, #expire, #initialize

Constructor Details

This class inherits a constructor from VK::O

Instance Method Details

#[](i) ⇒ Object



713
714
715
716
717
718
719
720
# File 'lib/valkey/objects.rb', line 713

def [] i			
	if VK.redis.call("SMISMEMBER", _set, i.to_s)[0] == 0
		VK.redis.call("HSET", _index, i.to_s, length.to_s);
		VK.redis.call("HSET", _sec, length.to_s, i.to_s);
		VK.redis.call("SADD", _set, i)
	end
	return VK.redis.call("HGET", _index, i.to_s).to_i;
end

#_embedObject



688
689
690
# File 'lib/valkey/objects.rb', line 688

def _embed
		%[#{key}-embed]
end

#_indexObject



682
683
684
# File 'lib/valkey/objects.rb', line 682

def _index
		%[#{key}-index]
end

#_secObject



685
686
687
# File 'lib/valkey/objects.rb', line 685

def _sec
		%[#{key}-sec]
end

#_setObject



679
680
681
# File 'lib/valkey/objects.rb', line 679

def _set
		%[#{key}-sset]
end

#embedObject



705
706
707
# File 'lib/valkey/objects.rb', line 705

def embed
	return KNN.new(vectors, :distance_measure => :tanimoto_coefficient)
end

#entryObject



697
698
699
# File 'lib/valkey/objects.rb', line 697

def entry
	Hash.new { |h,k| VK.redis.call("HGET", _sec, k) }
end

#lengthObject



691
692
693
# File 'lib/valkey/objects.rb', line 691

def length
  VK.redis.call("SCARD", _set)
end

#near(g, *n) ⇒ Object



708
709
710
711
712
# File 'lib/valkey/objects.rb', line 708

def near g, *n
	 return embed.nearest_neighbours(VK.embed(g)).map { |e|
		{ index: e[0], distance: e[1], entry: entry[e[0]] }
	 }.sort_by { |ee| ee[:distance] }
end

#value(&b) ⇒ Object



694
695
696
# File 'lib/valkey/objects.rb', line 694

def value &b
	VK.redis.call("HGETALL", _index).each_pair { |e, i| b.call(i.to_i, e) }
end

#vectorsObject



700
701
702
703
704
# File 'lib/valkey/objects.rb', line 700

def vectors
	a = []
	value { |i, e| a << [ i, VK.embed(e) ].flatten }		
	return a
end