Class: RiakRest::QueryLink

Inherits:
Object
  • Object
show all
Defined in:
lib/riakrest/core/query_link.rb

Overview

Represents a link used to query linked objects using Jiak’s link/step map-reduce facility. Links are established using a JiakLink and queried using a QueryLink. The structures are very similar but significantly different. A JiakLink is [bucket,key,tag] and a QueryLink is [bucket,tag,acc]. The accumulator field of a QueryLink allows for accumulating intermediate link/step results. Since RiakRest auto-inflates data returned via Jiak link/step processing, the accumulation of intermediate results is not supported by RiakRest. The setting of the acc is implemented for future consideration but should not be used.

Usage

<code>

link = QueryLink.new('people','parent')
link = QueryLink.new('blogs',nil)

</code>

Constant Summary collapse

ANY =

Jiak wildcard character (erlang atom)

'_'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ QueryLink

:call-seq:

QueryLink.new(*args)  -> QueryLink

Create a link from argument array. Missing, nil, or empty string values are set to QueryLink::ANY.

Examples

The following create QueryLinks with the shown equivalent array structure: <code>

QueryLink.new                       # => ['_','_','_']
QueryLink.new 'b'                   # => ['b','_','_']
QueryLink.new 'b','t'               # => ['b','t','_']
QueryLink.new 'b','t','a'           # => ['b','t','a']

QueryLink.new []                    # => ['_','_','_']
QueryLink.new ['b']                 # => ['b','_','_']
QueryLink.new ['b','t']             # => ['b','t','_']
QueryLink.new ['b','t','a']         # => ['b','t','a']

QueryLink.new ['',nil,' ']          # => ['_','_','_']

</code>

Passing another QueryLink as an argument makes a copy of that link. Passing a JiakBucket in the first (bucket) position uses the name field of that JiakBucket.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/riakrest/core/query_link.rb', line 52

def initialize(*args)
  case args.size
  when 0
    bucket = tag = acc = ANY
  when 1
    if args[0].is_a? String
      bucket = args[0]
      tag = acc = ANY
    elsif args[0].is_a? QueryLink
      bucket, tag, acc = args[0].bucket, args[0].tag, args[0].acc
    elsif args[0].is_a? Array
      bucket, tag, acc = args[0][0], args[0][1], args[0][2]
    else
      raise QueryLinkException, "argument error"
    end
  when 2
    bucket, tag = args[0], args[1]
    acc = ANY
  when 3
    bucket, tag, acc = args[0], args[1], args[2]
  else
    raise QueryLinkException, "too many arguments, (#{args.size} for 3)"
  end
  
  @bucket, @tag, @acc  = transform_args(bucket,tag,acc)
end

Instance Attribute Details

#accObject

Returns the value of attribute acc.



21
22
23
# File 'lib/riakrest/core/query_link.rb', line 21

def acc
  @acc
end

#bucketObject

Returns the value of attribute bucket.



21
22
23
# File 'lib/riakrest/core/query_link.rb', line 21

def bucket
  @bucket
end

#tagObject

Returns the value of attribute tag.



21
22
23
# File 'lib/riakrest/core/query_link.rb', line 21

def tag
  @tag
end

Instance Method Details

#==(other) ⇒ Object

:call-seq:

link == other -> true or false

Equality – QueryLinks are equal if they contain the same attribute values.



118
119
120
121
122
# File 'lib/riakrest/core/query_link.rb', line 118

def ==(other)
  (@bucket == other.bucket &&
   @tag    == other.tag    &&
   @acc    == other.acc) rescue false
end

#eql?(other) ⇒ Boolean

:call-seq:

link.eql?(other) -> true or false

Returns true if other is a QueryLink with the same attribute values.



129
130
131
132
133
134
# File 'lib/riakrest/core/query_link.rb', line 129

def eql?(other)
  other.is_a?(QueryLink) &&
    @bucket.eql?(other.bucket) &&
    @tag.eql?(other.tag) &&
    @acc.eql?(other.acc)
end

#for_uriObject

:call-seq:

link.for_uri  -> URI encoded string

URI represent this QueryLink, i.e, a string suitable for inclusion in an URI.



109
110
111
# File 'lib/riakrest/core/query_link.rb', line 109

def for_uri
  URI.encode([@bucket,@tag,@acc].join(','))
end

#hashObject

:nodoc:



136
137
138
# File 'lib/riakrest/core/query_link.rb', line 136

def hash    # :nodoc:
  @bucket.name.hash + @tag.hash + @acc.hash
end

#to_sObject

String representation of this QueryLink.



141
142
143
# File 'lib/riakrest/core/query_link.rb', line 141

def to_s
  "[#{bucket},#{tag},#{acc}]"
end