Hi, Sorry for the late reply. Le 10 oct. 06, ? 02:27, Harlan Crystal a ?crit :> I''m trying to use your "better nested set" plugin.? I''ve only > accessed the database through the "children" and "move_to_child_of" > function --- and it has managed to put the lft and rgt columns into an > inconsistant state. > > For instance, there are negative numbers in the lft/rgt columns in > some cases.? In others, there are overlapping ranges for sets which > unrelated.? In other cases, the value of lft is greater than the value > of rgt on the same row. > > > +----+----------------------+-----------+-----+-----+ > | id | name???????????????? | parent_id | lft | rgt | > +----+----------------------+-----------+-----+-----+ > |? 6 | Battery Park City??? |??????? 32 |?? 8 |?? 9 | > |? 7 | Chelsea????????????? |??????? 34 |? 10 |? 28 | > |? 8 | Chinatown??????????? |??????? 32 |?? 6 |?? 7 | > |? 9 | East Village???????? |??????? 32 |? -4 |? -3 | > | 10 | Financial/Battery Pk |??????? 32 |? -2 |? -1 | > | 11 | Flatiron???????????? |??????? 34 |? 31 |? 32 | > | 12 | Gramercy???????????? |??????? 34 |?? 3 |?? 4 | > | 13 | Harlem?????????????? |??????? 33 |? 45 |? 46 | > | 14 | Hells Kitchen??????? |??????? 34 |? 39 |? 40 | > | 15 | Little Italy???????? |??????? 32 |?? 0 |?? 1 | > | 16 | Lower East Side????? |??????? 32 |?? 2 |?? 5 | > | 17 | Meatpacking District |??????? 34 |? 29 |? 30 | > | 18 | Midtown East???????? |??????? 34 |? 33 |? 34 | > | 19 | Midtown West???????? |??????? 34 |? 11 |? 12 | > | 20 | Morningside Hts????? |??????? 33 |? 53 |? 54 | > | 21 | Murray Hill/Kips Bay |??????? 34 |? -9 |? 11 | > | 22 | Nolita?????????????? |??????? 32 |? -8 |? -7 | > | 23 | Other??????????????? |??????? 32 |? 42 |? 43 | > | 24 | Soho???????????????? |??????? 35 |? 43 |? 12 | > | 25 | Times Square???????? |??????? 34 |? 35 |? 36 | > | 26 | Tribeca????????????? |??????? 32 |? -6 |? -5 | > | 27 | Union Square???????? |??????? 34 |? 37 |? 38 | > | 28 | Upper East Side????? |??????? 33 |? 47 |? 48 | > | 29 | Upper West Side????? |??????? 33 |? 49 |? 50 | > | 30 | Wash Hts/Inwood????? |??????? 33 |? 51 |? 52 | > | 31 | West Village???????? |??????? 35 |?? 3 |?? 4 | > | 32 | Manhattan??????????? |????? NULL |?? 1 |? 56 | > | 33 | Uptown?????????????? |??????? 32 |? 44 |? 55 | > | 34 | Midtown????????????? |??????? 32 |? 10 |? 41 | > | 35 | Downtown???????????? |??????? 32 |?? 2 |? -9 | > +----+----------------------+-----------+-----+-----+ >Obviously there''s a problem. Could you send us, to betternestedset-talk at rubyforge.org, the code you use to add or update your entries ? and the original consistent data you had when starting editing ? Jean-Christophe Michel -- symetrie.com Better Nested Set for rails: http://opensource.symetrie.com/trac/better_nested_set
Hi Harlan, Are you resaving any of these objects to the database manually? It''s very easy to cause inconsistencies with better_nested_set if you don''t reload the parent, lft, and rgt values before saving the object. The better_nested_set plugin does direct sql commands (to gain the benefits of a nested_set implementation), so object values can change out from under you. -Tim On 10/18/06, Jean-Christophe Michel <jc.michel at symetrie.com> wrote:> Hi, > > Sorry for the late reply. > > Le 10 oct. 06, ? 02:27, Harlan Crystal a ?crit : > > I''m trying to use your "better nested set" plugin. I''ve only > > accessed the database through the "children" and "move_to_child_of" > > function --- and it has managed to put the lft and rgt columns into an > > inconsistant state. > > > > For instance, there are negative numbers in the lft/rgt columns in > > some cases. In others, there are overlapping ranges for sets which > > unrelated. In other cases, the value of lft is greater than the value > > of rgt on the same row. > > > > > > +----+----------------------+-----------+-----+-----+ > > | id | name | parent_id | lft | rgt | > > +----+----------------------+-----------+-----+-----+ > > | 6 | Battery Park City | 32 | 8 | 9 | > > | 7 | Chelsea | 34 | 10 | 28 | > > | 8 | Chinatown | 32 | 6 | 7 | > > | 9 | East Village | 32 | -4 | -3 | > > | 10 | Financial/Battery Pk | 32 | -2 | -1 | > > | 11 | Flatiron | 34 | 31 | 32 | > > | 12 | Gramercy | 34 | 3 | 4 | > > | 13 | Harlem | 33 | 45 | 46 | > > | 14 | Hells Kitchen | 34 | 39 | 40 | > > | 15 | Little Italy | 32 | 0 | 1 | > > | 16 | Lower East Side | 32 | 2 | 5 | > > | 17 | Meatpacking District | 34 | 29 | 30 | > > | 18 | Midtown East | 34 | 33 | 34 | > > | 19 | Midtown West | 34 | 11 | 12 | > > | 20 | Morningside Hts | 33 | 53 | 54 | > > | 21 | Murray Hill/Kips Bay | 34 | -9 | 11 | > > | 22 | Nolita | 32 | -8 | -7 | > > | 23 | Other | 32 | 42 | 43 | > > | 24 | Soho | 35 | 43 | 12 | > > | 25 | Times Square | 34 | 35 | 36 | > > | 26 | Tribeca | 32 | -6 | -5 | > > | 27 | Union Square | 34 | 37 | 38 | > > | 28 | Upper East Side | 33 | 47 | 48 | > > | 29 | Upper West Side | 33 | 49 | 50 | > > | 30 | Wash Hts/Inwood | 33 | 51 | 52 | > > | 31 | West Village | 35 | 3 | 4 | > > | 32 | Manhattan | NULL | 1 | 56 | > > | 33 | Uptown | 32 | 44 | 55 | > > | 34 | Midtown | 32 | 10 | 41 | > > | 35 | Downtown | 32 | 2 | -9 | > > +----+----------------------+-----------+-----+-----+ > > > > Obviously there''s a problem. Could you send us, to > betternestedset-talk at rubyforge.org, > the code you use to add or update your entries ? and the original > consistent data you had when starting editing ? > > > > Jean-Christophe Michel > -- > symetrie.com > > Better Nested Set for rails: > http://opensource.symetrie.com/trac/better_nested_set > > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk >
Hi Tim, Le 18 oct. 06, ? 14:21, Tim Olsen a ?crit :> Are you resaving any of these objects to the database manually? It''s > very easy to cause inconsistencies with better_nested_set if you don''t > reload the parent, lft, and rgt values before saving the object. The > better_nested_set plugin does direct sql commands (to gain the > benefits of a nested_set implementation), so object values can change > out from under you.In order to prevent any inconsistencies, we could try to add a class variable datetime that would be compared to an instance var datetime: @@updated would be set by move_to_* methods @last_reloaded would be set by the autoreload method, and checkreload would be called before any read or write change to instance data, and would call autoreload if necessary. would probably be a bit heavy... Maybe a consistency_check method would be a good start... Would check that [all left]+[all right] === (1..(node count)*2).to_a Jean-Christophe Michel -- symetrie.com Better Nested Set for rails: http://opensource.symetrie.com/trac/better_nested_set
I think such a strategy may accidentally overwrite any changes to other attributes that are not related to the nested set representation. Also, I''m not sure if we can depend 100% on the precision of datetime. In addition, I''m not sure how well this would work in the face of concurrency where class variables are not necessarily shared across different invocations of Ruby by mongrel or FastCGI. And then there''s the case where Rails is split across several machines, where the only shared data is the session store and the database. I think a possibly better approach would be to assume that we always write to the better_nested_set attributes via direct SQL. Then we can add a before_update callback that reloads just those attributes. The number of SQL queries to do a save remains constant. I think having a consistency check is an excellent idea. We could provide an option to turn it on or off. We should put the check at the end of every one of our methods that does a SQL update or insert. To stay consistent, we can also call the consistency check inside an after_save callback. What do you think? Cheers, Tim On 10/18/06, Jean-Christophe Michel <jc.michel at symetrie.com> wrote:> Hi Tim, > > Le 18 oct. 06, ? 14:21, Tim Olsen a ?crit : > > Are you resaving any of these objects to the database manually? It''s > > very easy to cause inconsistencies with better_nested_set if you don''t > > reload the parent, lft, and rgt values before saving the object. The > > better_nested_set plugin does direct sql commands (to gain the > > benefits of a nested_set implementation), so object values can change > > out from under you. > > In order to prevent any inconsistencies, we could try to add a class > variable datetime that would be compared to an instance var datetime: > > @@updated would be set by move_to_* methods > @last_reloaded would be set by the > autoreload method, and > checkreload would be called before any read or write change to instance > data, and would call autoreload if necessary. > > > would probably be a bit heavy... > > Maybe a > consistency_check method would be a good start... > Would check that [all left]+[all right] === (1..(node count)*2).to_a > > Jean-Christophe Michel > -- > symetrie.com > > Better Nested Set for rails: > http://opensource.symetrie.com/trac/better_nested_set > > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk >