db-calc-storesize 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use DBI;
  4. sub do_error($) {
  5. # exit(1);
  6. }
  7. if(@ARGV != 4) {
  8. print "Usage: $0 <mysqluser> <mysqlpass> <mysqldb> <db-hostname>\n";
  9. exit(1);
  10. }
  11. my ($user,$pass,$dbname,$dbhost) = @ARGV;
  12. my $db = DBI->connect("dbi:mysql:$dbname;host=$dbhost", $user, $pass);
  13. if (!defined($db)) {
  14. print "did not connect to mysql\n";
  15. exit(1);
  16. }
  17. sub getsize {
  18. my ($hierarchyid) = @_;
  19. my $sth = $db->prepare("SELECT hierarchy.id, properties.val_ulong, hierarchy.type FROM hierarchy LEFT JOIN properties ON properties.hierarchyid=hierarchy.id AND properties.tag=0x0e08 AND properties.type=0x0003 WHERE hierarchy.parent=". $hierarchyid . " AND hierarchy.flags & 0x400 = 0");
  20. $sth->execute();
  21. my @row;
  22. my $totalsize = 0;
  23. while(@row = $sth->fetchrow_array()) {
  24. if($row[2] == 5 && defined($row[1])) {
  25. $totalsize += $row[1];
  26. } elsif($row[2] == 3) {
  27. $totalsize += getsize($row[0]);
  28. }
  29. }
  30. return $totalsize;
  31. }
  32. my $res;
  33. my $sth;
  34. my $rows;
  35. $res = $db->do("set character_set_client=utf8;");
  36. if(!$res) {
  37. do_error(1);
  38. }
  39. $res = $db->do("set character_set_connection=utf8;");
  40. if(!$res) {
  41. do_error(1);
  42. }
  43. $res = $db->do("set character_set_results=utf8;");
  44. if(!$res) {
  45. do_error(1);
  46. }
  47. my @row;
  48. @row = $db->selectrow_array("SELECT major FROM versions ORDER BY databaserevision DESC LIMIT 1")
  49. or die $DBI::errstr;
  50. if (scalar(@row) == 0) {
  51. print "Unable to determain version of database\n";
  52. exit(1);
  53. }
  54. my $version = $row[0];
  55. print "Calculating store sizes..\n";
  56. # Add PR_SEARCH_KEY for all objects
  57. $sth = $db->prepare("SELECT hierarchy_id, users.id, user_name, val_longint FROM stores JOIN users ON users.id = stores.user_id JOIN properties ON hierarchy_id=properties.hierarchyid WHERE properties.tag=0x0e08 AND properties.type=0x0014 order by val_longint");
  58. if(!$sth) {
  59. do_error(1);
  60. }
  61. $sth->execute();
  62. while(@row = $sth->fetchrow_array()) {
  63. my $size = getsize($row[0]);
  64. if($row[3] == $size) {
  65. print "Size of store for user " . $row[1] ."(".$row[2].") is correct at " . $size . "\n";
  66. } else {
  67. print "Size of store for user " . $row[1] ."(".$row[2].") was ".$row[3].", now is " . $size . "\n";
  68. $db->do("DELETE FROM properties WHERE hierarchyid = " . $row[0] . " AND tag=0x0e08 AND (type=0x0003 OR type=0x0014)");
  69. if ($version < 7) {
  70. $db->do("INSERT INTO properties (storeid, hierarchyid, tag, type, val_longint) VALUES(" . $row[0]. ", " . $row[0] . ", 0x0e08, 0x0014, $size)");
  71. } else {
  72. $db->do("INSERT INTO properties (hierarchyid, tag, type, val_longint) VALUES(" . $row[0] . ", 0x0e08, 0x0014, $size)");
  73. }
  74. }
  75. }
  76. print "done.\n";