/[suikacvs]/webroot/swe/lib/SWE/Object/Graph.pm
Suika

Contents of /webroot/swe/lib/SWE/Object/Graph.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Mon Mar 16 07:40:04 2009 UTC (17 years ago) by wakaba
Branch: MAIN
Changes since 1.2: +1 -1 lines
++ swe/lib/SWE/Object/ChangeLog	16 Mar 2009 07:39:22 -0000
2009-03-16  Wakaba  <wakaba@suika.fam.cx>

	* Repository.pm: New module.

++ swe/lib/suikawiki/ChangeLog	16 Mar 2009 07:39:54 -0000
2009-03-16  Wakaba  <wakaba@suika.fam.cx>

	* main.pl: Moved relatedness detection method into its own module.

1 wakaba 1.1 package SWE::Object::Graph;
2     use strict;
3     use warnings;
4    
5     sub new ($%) {
6     my $class = shift;
7     my $self = bless {@_}, $class;
8    
9     return $self;
10     }
11    
12 wakaba 1.3 sub db { $_[0]->{db} }
13 wakaba 1.1
14     use constant EMPTY_NODE_RATIO => 0.2;
15     use constant INITIAL_DEGREE => 5;
16    
17     sub add_nodes ($$) {
18     my ($self, $new_doc_number) = @_;
19    
20     ## TODO: graph lock
21    
22     my $global_prop_db = $self->db->global_prop;
23    
24     my $last_node_index = ${$global_prop_db->get_data ('lastnodeindex') || \ 0};
25     my $max_node_index = int ($last_node_index + $new_doc_number / (1 - EMPTY_NODE_RATIO)) + 1;
26    
27     if ($last_node_index < $max_node_index) {
28     my $new_edges = {};
29    
30     for my $index1 ($last_node_index + 1 .. $max_node_index) {
31     for (1 .. INITIAL_DEGREE) {
32     my $index2 = int rand $index1;
33    
34     $new_edges->{$index1}->{$index2} = 1;
35     $new_edges->{$index2}->{$index1} = 1;
36     }
37     }
38    
39     my $graph_prop_db = $self->db->graph_prop;
40     for my $index1 (keys %$new_edges) {
41     my $node = $graph_prop_db->get_data ($index1);
42     my $edges = $node->{neighbors} ||= {};
43     for my $index2 (keys %{$new_edges->{$index1}}) {
44     $edges->{$index2} = 1;
45     }
46     $graph_prop_db->set_data ($index1 => $node);
47     }
48    
49     $global_prop_db->set_data (lastnodeindex => \$max_node_index);
50     }
51    
52     return ($last_node_index + 1 .. $max_node_index);
53     } # add_nodes
54    
55     sub create_node ($$) {
56     my ($self, $doc_id) = @_;
57    
58     ## TODO: docid lock
59    
60     my ($node_id) = $self->add_nodes (1);
61 wakaba 1.2
62     require SWE::Object::Node;
63     my $node = SWE::Object::Node->new (db => $self->db);
64     $node->create (id => $node_id);
65     $node->prop->{ids}->{$doc_id} = 1;
66     $node->save_prop;
67    
68     return $node;
69     } # create_node
70    
71     sub get_node_by_id ($$) {
72     my ($self, $node_id) = @_;
73    
74     ## TODO: cache
75 wakaba 1.1
76 wakaba 1.2 require SWE::Object::Node;
77     my $node = SWE::Object::Node->new (db => $self->db);
78     $node->load (id => $node_id);
79 wakaba 1.1
80 wakaba 1.2 return $node;
81     } # get_node_by_id
82 wakaba 1.1
83     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24