lsh 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/lsh/storage/redis_backend.rb +43 -15
  2. metadata +2 -2
@@ -28,16 +28,24 @@ module LSH
28
28
  def initialize(params = { :redis => { :host => '127.0.0.1', :port => 6379 }, :data_dir => 'data' })
29
29
  @redis = Redis.new(params[:redis])
30
30
  @data_dir = params[:data_dir]
31
- Dir.mkdir(@data_dir) unless File.exists?(@data_dir)
31
+ unless File.exists?(@data_dir)
32
+ Dir.mkdir(@data_dir)
33
+ Dir.mkdir(File.join(@data_dir, 'projections'))
34
+ end
32
35
  end
33
36
 
34
37
  def reset!
35
38
  @redis.flushall
36
- Dir.foreach(@data_dir) {|f| File.delete(File.join(@data_dir, f)) if f != '.' and f != '..' and f.end_with?('.dat')}
39
+ delete_dat_files_in_dir(@data_dir)
40
+ delete_dat_files_in_dir(File.join(@data_dir, 'projections'))
41
+ end
42
+
43
+ def delete_dat_files_in_dir(dir)
44
+ Dir.foreach(dir) {|f| File.delete(File.join(dir, f)) if f != '.' and f != '..' and f.end_with?('.dat')}
37
45
  end
38
46
 
39
47
  def has_index?
40
- projections and parameters and number_of_buckets > 0
48
+ parameters and projections and number_of_buckets > 0
41
49
  end
42
50
 
43
51
  def number_of_buckets
@@ -45,15 +53,31 @@ module LSH
45
53
  end
46
54
 
47
55
  def projections=(projections)
48
- @redis.set "projections", projections.to_json
56
+ # Saving the projections to disk
57
+ # (too slow to serialize and store in Redis for
58
+ # large number of dimensions/projections)
59
+ projections.each_with_index do |projection, i|
60
+ projection.each_with_index do |vector, j|
61
+ vector.save(File.join(@data_dir, 'projections', "vector_#{i}_#{j}.dat"))
62
+ end
63
+ end
49
64
  end
50
65
 
51
66
  def projections
52
- begin
53
- @projections ||= JSON.parse(@redis.get "projections")
54
- rescue TypeError
55
- nil
56
- end
67
+ return unless parameters
68
+ @projections ||= (
69
+ projections = []
70
+ parameters[:number_of_independent_projections].times do |i|
71
+ vectors = []
72
+ parameters[:number_of_random_vectors].times do |j|
73
+ v = MathUtil.zeros(parameters[:dim])
74
+ v.load(File.join(@data_dir, 'projections', "vector_#{i}_#{j}.dat"))
75
+ vectors << v
76
+ end
77
+ projections << vectors
78
+ end
79
+ projections
80
+ )
57
81
  end
58
82
 
59
83
  def parameters=(parms)
@@ -62,12 +86,16 @@ module LSH
62
86
  end
63
87
 
64
88
  def parameters
65
- @parms ||= (
66
- parms = JSON.parse(@redis.get "parameters")
67
- parms.keys.each { |k| parms[k.to_sym] = parms[k]; parms.delete(k) }
68
- parms[:window] = Float::INFINITY if parms[:window] == 'Infinity'
69
- parms
70
- )
89
+ begin
90
+ @parms ||= (
91
+ parms = JSON.parse(@redis.get "parameters")
92
+ parms.keys.each { |k| parms[k.to_sym] = parms[k]; parms.delete(k) }
93
+ parms[:window] = Float::INFINITY if parms[:window] == 'Infinity'
94
+ parms
95
+ )
96
+ rescue TypeError
97
+ nil
98
+ end
71
99
  end
72
100
 
73
101
  def create_new_bucket
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lsh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-28 00:00:00.000000000 Z
12
+ date: 2012-12-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gsl