PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Algorithm/SpatialIndex/Storage.pm

https://github.com/tsee/algorithm-spatialindex
Perl | 171 lines | 114 code | 57 blank | 0 comment | 10 complexity | e769c615f95b46e9ea2b6ed91d422417 MD5 | raw file
  1. package Algorithm::SpatialIndex::Storage;
  2. use 5.008001;
  3. use strict;
  4. use warnings;
  5. use Carp qw(croak);
  6. require Algorithm::SpatialIndex::Strategy;
  7. use Scalar::Util 'weaken';
  8. use Class::XSAccessor {
  9. getters => [qw(
  10. index
  11. no_of_subnodes
  12. bucket_class
  13. )],
  14. };
  15. sub new {
  16. my $class = shift;
  17. my %opt = @_;
  18. my $ext_opt = $opt{opt}||{};
  19. my $self = bless {
  20. bucket_class => defined($ext_opt->{bucket_class}) ? $ext_opt->{bucket_class} : 'Algorithm::SpatialIndex::Bucket',
  21. %opt,
  22. } => $class;
  23. weaken($self->{index});
  24. my $bucket_class = $self->bucket_class;
  25. if (not $bucket_class =~ /::/) {
  26. $bucket_class = "Algorithm::SpatialIndex::Bucket::$bucket_class";
  27. $self->{bucket_class} = $bucket_class;
  28. }
  29. eval "require $bucket_class; 1;" or do {
  30. my $err = $@ || "Zombie error";
  31. die "Could not load bucket implementation '$bucket_class': $err"
  32. };
  33. my $strategy = $self->index->strategy;
  34. $self->{no_of_subnodes} = $strategy->no_of_subnodes;
  35. $self->init() if $self->can('init');
  36. return $self;
  37. }
  38. sub fetch_node {
  39. croak("Not implemented in base class");
  40. }
  41. sub store_node {
  42. croak("Not implemented in base class");
  43. }
  44. sub fetch_bucket {
  45. croak("Not implemented in base class");
  46. }
  47. sub delete_bucket {
  48. croak("Not implemented in base class");
  49. }
  50. sub store_bucket {
  51. croak("Not implemented in base class");
  52. }
  53. sub get_option {
  54. croak("Not implemented in base class");
  55. }
  56. sub set_option {
  57. croak("Not implemented in base class");
  58. }
  59. 1;
  60. __END__
  61. =head1 NAME
  62. Algorithm::SpatialIndex::Storage - Base class for storage backends
  63. =head1 SYNOPSIS
  64. use Algorithm::SpatialIndex;
  65. my $idx = Algorithm::SpatialIndex->new(
  66. storage => 'Memory', # or others
  67. );
  68. =head1 DESCRIPTION
  69. =head1 METHODS
  70. =head2 new
  71. Constructor. Called by the L<Algorithm::SpatialIndex>
  72. constructor. You probably do not need to call or implement this.
  73. Calls your C<init> method if available.
  74. =head2 init
  75. If your subcass implements this, it will be called on the
  76. fresh object in the constructor.
  77. =head2 fetch_node
  78. Fetch a node from storage by node id.
  79. Has to be implemented in a subclass.
  80. =head2 store_node
  81. Store the provided node. Assigns a new ID to it if it has none.
  82. Returns the (potentially new) node id.
  83. Note that general id handling is the task of the storage backend.
  84. Users or strategies should not set node ids.
  85. Has to be implemented in a subclass.
  86. =head2 set_option
  87. Takes a key/value pair for a tree property/option to be
  88. stored.
  89. Has to be implemented in a subclass.
  90. =head2 get_option
  91. Takes a key for a tree property/option to be
  92. fetched from storage.
  93. Has to be implemented in a subclass.
  94. =head2 fetch_bucket
  95. Takes a node id as argument and returns the bucket for this
  96. node (or undef on failure).
  97. Has to be implemented in a subclass.
  98. =head2 store_bucket
  99. Takes a bucket object (with assigned node id)
  100. and stores the bucket
  101. as the bucket for this node id.
  102. Has to be implemented in a subclass.
  103. =head2 delete_bucket
  104. Removes the given bucket (or bucket/node id) from the
  105. storage.
  106. Has to be implemented in a subclass.
  107. =head1 AUTHOR
  108. Steffen Mueller, E<lt>smueller@cpan.orgE<gt>
  109. =head1 COPYRIGHT AND LICENSE
  110. Copyright (C) 2010, 2011 by Steffen Mueller
  111. This library is free software; you can redistribute it and/or modify
  112. it under the same terms as Perl itself, either Perl version 5.10.1 or,
  113. at your option, any later version of Perl 5 you may have available.
  114. =cut