Class: Nanite::RedisTagStore

Inherits:
Object
  • Object
show all
Defined in:
lib/nanite/redis_tag_store.rb

Overview

Implementation of a tag store on top of Redis For a nanite with the identity ‘nanite-foobar’, we store the following:

s-nanite-foobar: { /foo/bar, /foo/nik } # a SET of the provided services tg-nanite-foobar: { foo-42, customer-12 } # a SET of the tags for this agent

Also we do an inverted index for quick lookup of agents providing a certain service, so for each service the agent provides, we add the nanite to a SET of all the nanites that provide said service:

foo/bar: { nanite-foobar, nanite-nickelbag, nanite-another } # redis SET

We do that same thing for tags:

some-tag: { nanite-foobar, nanite-nickelbag, nanite-another } # redis SET

This way we can do a lookup of what nanites provide a set of services and tags based on redis SET intersection:

nanites_for(‘/gems/list’, ‘some-tag’)

> returns an array of nanites that provide the intersection of these two service tags

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ RedisTagStore

Initialize tag store with given redis handle



30
31
32
# File 'lib/nanite/redis_tag_store.rb', line 30

def initialize(redis)
  @redis = redis
end

Instance Method Details

#delete(nanite) ⇒ Object

Delete services and tags for given agent



56
57
58
59
# File 'lib/nanite/redis_tag_store.rb', line 56

def delete(nanite)
  delete_elems(nanite, "s-#{nanite}", 'naniteservices')
  delete_elems(nanite, "tg-#{nanite}", 'nanitestags')
end

#nanites_ids_for(request) ⇒ Object

Retrieve nanites implementing given service and exposing given tags



72
73
74
75
76
77
78
# File 'lib/nanite/redis_tag_store.rb', line 72

def nanites_ids_for(request)
  keys = request.tags ? request.tags.dup : []
  keys << request.type if request.type
  keys.compact!
  return {} if keys.empty?
  log_redis_error { @redis.set_intersect(keys) }
end

#services(nanite) ⇒ Object

Services implemented by given agent



62
63
64
# File 'lib/nanite/redis_tag_store.rb', line 62

def services(nanite)
  @redis.set_members("s-#{nanite}")
end

#store(nanite, services, tags) ⇒ Object

Store services and tags for given agent



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nanite/redis_tag_store.rb', line 35

def store(nanite, services, tags)
  services = nil if services.compact.empty?
  tags = nil if tags.compact.empty?
  log_redis_error do
    if services
      obsolete_services = @redis.set_members("s-#{nanite}") - services
      update_elems(nanite, services, obsolete_services, "s-#{nanite}", 'naniteservices')
    end
    if tags
      obsolete_tags = @redis.set_members("tg-#{nanite}") - tags
      update_elems(nanite, tags, obsolete_tags, "tg-#{nanite}", 'nanitestags')
    end
  end
end

#tags(nanite) ⇒ Object

Tags exposed by given agent



67
68
69
# File 'lib/nanite/redis_tag_store.rb', line 67

def tags(nanite)
  @redis.set_members("tg-#{nanite}")
end

#update(nanite, new_tags, obsolete_tags) ⇒ Object

Update tags for given agent



51
52
53
# File 'lib/nanite/redis_tag_store.rb', line 51

def update(nanite, new_tags, obsolete_tags)
  update_elems(nanite, new_tags, obsolete_tags, "tg-#{nanite}", 'nanitestags')
end