Created
February 13, 2012 21:20
-
-
Save asmuth/1820592 to your computer and use it in GitHub Desktop.
bp ultrasimple social graph (wip)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# the "abstract" node | |
class BPGraph::Node | |
# initialize an instance with id of the correct subclass from a node_key | |
def self.load(node_key) | |
end | |
def initialize(node_id) | |
@node_class ||= :node | |
@node_id = node_id | |
end | |
# generates e.g. "bpgraph_dev:user_node:436923" | |
def redis_key(*append) | |
[BPGraph.redis_prefix, node_key, append].flatten.join(":") | |
end | |
# generates e.g. "user_node:436923" | |
def node_key | |
[@node_class, @node_id].join(":") | |
end | |
# simple key/value store - we use it to save profile-picture url and first/ | |
# lastname from facebook without creating a model and record in mysql | |
def prop_get(key) | |
BPGraph.redis.hget(redis_key(:props), key) | |
end | |
# simple key/value store - create or overwrite a key with value | |
def prop_store!(key, value) | |
BPGraph.redis.hset(redis_key(:props), key, value) | |
end | |
private | |
# find all connected node_ids by edge_type (:donor, :visitor, :friend...) | |
def find_edges(edge_type) | |
BPGraph.redis.smembers(redis_key(:edge, edge_type)) | |
end | |
# find all connected nodes as objects by edge_type (UserNode, ProjectNode...) | |
def find_edges!(edge_type) | |
find_edges(edge_type).map{ |node_key| self.class.load(node_key) } | |
end | |
# store a new connection to another node with edge_type (:donor, :friend...) | |
def store_edge!(edge_type, node) | |
BPGraph.redis.sadd(redis_key(:edge, edge_type), node.node_key) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment