Class: Parse::Vector
- Inherits:
-
Object
- Object
- Parse::Vector
- Includes:
- Enumerable
- Defined in:
- lib/parse/model/vector.rb
Overview
Wraps a dense numeric embedding stored on a Parse object. Backs the
:vector property data type and the embed DSL. The value is just
an array of Floats — Parse::Vector adds dimension awareness,
finite-value validation, and JSON serialization helpers so the
provider/index plumbing can rely on a single concrete shape.
Constant Summary collapse
- MAX_DIMENSIONS =
Maximum dimensions a Parse::Vector will accept. Atlas Vector Search caps individual vector indexes at 8192 dims as of MongoDB 7.0; we keep some headroom but still refuse pathological inputs that would blow up memory.
16384
Instance Attribute Summary collapse
-
#values ⇒ Array<Float>
readonly
The underlying float array.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Equality by element-wise comparison.
-
#as_json ⇒ Array<Float>
MongoDB / Parse server store this as a plain BSON array.
-
#dimensions ⇒ Integer
(also: #length, #size)
Number of dimensions.
- #each(&block) ⇒ Object
- #hash ⇒ Object
-
#initialize(values) ⇒ Vector
constructor
A new instance of Vector.
-
#to_a ⇒ Array<Float>
The underlying float array.
-
#to_json(*opts) ⇒ String
JSON representation.
Constructor Details
#initialize(values) ⇒ Vector
Returns a new instance of Vector.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/parse/model/vector.rb', line 38 def initialize(values) values = values.values if values.is_a?(Parse::Vector) unless values.is_a?(Array) raise ArgumentError, "[Parse::Vector] expected Array, got #{values.class}." end if values.length > MAX_DIMENSIONS raise ArgumentError, "[Parse::Vector] refusing #{values.length}-dim vector; max #{MAX_DIMENSIONS}." end @values = values.map do |x| unless x.is_a?(Numeric) && x.respond_to?(:finite?) && x.finite? raise ArgumentError, "[Parse::Vector] all elements must be finite Numeric (got #{x.inspect})." end x.to_f end.freeze end |
Instance Attribute Details
#values ⇒ Array<Float> (readonly)
Returns the underlying float array.
34 35 36 |
# File 'lib/parse/model/vector.rb', line 34 def values @values end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Returns equality by element-wise comparison.
84 85 86 87 88 89 90 |
# File 'lib/parse/model/vector.rb', line 84 def ==(other) case other when Parse::Vector then @values == other.values when Array then @values == other else false end end |
#as_json ⇒ Array<Float>
MongoDB / Parse server store this as a plain BSON array.
70 71 72 |
# File 'lib/parse/model/vector.rb', line 70 def as_json(*) @values end |
#dimensions ⇒ Integer Also known as: length, size
Returns number of dimensions.
57 58 59 |
# File 'lib/parse/model/vector.rb', line 57 def dimensions @values.length end |
#each(&block) ⇒ Object
79 80 81 |
# File 'lib/parse/model/vector.rb', line 79 def each(&block) @values.each(&block) end |
#hash ⇒ Object
93 94 95 |
# File 'lib/parse/model/vector.rb', line 93 def hash @values.hash end |
#to_a ⇒ Array<Float>
Returns the underlying float array.
64 65 66 |
# File 'lib/parse/model/vector.rb', line 64 def to_a @values.dup end |
#to_json(*opts) ⇒ String
Returns JSON representation.
75 76 77 |
# File 'lib/parse/model/vector.rb', line 75 def to_json(*opts) @values.to_json(*opts) end |