StrIdx 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Makefile +1 -0
- data/README.md +16 -3
- data/demo.cpp +130 -48
- data/exe/stridx.rb +124 -5
- data/flist.txt +0 -5550
- data/rubyext/extconf.rb +1 -1
- data/rubyext/ruby_interf.cpp +95 -6
- data/runserver.rb +22 -0
- data/server.rb +8 -2
- data/stridx.gemspec +1 -5
- data/stridx.hpp +431 -227
- data/thread_pool.hpp +20 -5
- data/unittest.cpp +58 -16
- metadata +3 -3
data/rubyext/extconf.rb
CHANGED
data/rubyext/ruby_interf.cpp
CHANGED
@@ -48,7 +48,32 @@ VALUE StringIndexWaitUntilDone(VALUE self) {
|
|
48
48
|
((StrIdx::StringIndex *)data)->waitUntilDone();
|
49
49
|
return self;
|
50
50
|
}
|
51
|
-
|
51
|
+
|
52
|
+
VALUE StringIndexFindNum(VALUE self, VALUE str, VALUE _limit) {
|
53
|
+
VALUE ret;
|
54
|
+
std::string s1 = StringValueCStr(str);
|
55
|
+
|
56
|
+
void *data;
|
57
|
+
TypedData_Get_Struct(self, int, &str_idx_type, data);
|
58
|
+
StrIdx::StringIndex *idx = (StrIdx::StringIndex *)data;
|
59
|
+
|
60
|
+
int limit = NUM2INT(_limit);
|
61
|
+
|
62
|
+
ret = rb_ary_new();
|
63
|
+
const std::vector<std::pair<float, int>> &results = idx->findSimilar(s1);
|
64
|
+
int i = 0;
|
65
|
+
for (const auto &res : results) {
|
66
|
+
VALUE arr = rb_ary_new();
|
67
|
+
rb_ary_push(arr, INT2NUM(res.second));
|
68
|
+
rb_ary_push(arr, DBL2NUM(res.first));
|
69
|
+
rb_ary_push(ret, arr);
|
70
|
+
i++;
|
71
|
+
if (i >= limit) {
|
72
|
+
break;
|
73
|
+
}
|
74
|
+
}
|
75
|
+
return ret;
|
76
|
+
}
|
52
77
|
|
53
78
|
VALUE StringIndexFind(VALUE self, VALUE str) {
|
54
79
|
VALUE ret;
|
@@ -59,8 +84,8 @@ VALUE StringIndexFind(VALUE self, VALUE str) {
|
|
59
84
|
StrIdx::StringIndex *idx = (StrIdx::StringIndex *)data;
|
60
85
|
|
61
86
|
ret = rb_ary_new();
|
62
|
-
const std::vector<std::pair<float, int>> &results = idx->findSimilar(s1
|
63
|
-
int limit =
|
87
|
+
const std::vector<std::pair<float, int>> &results = idx->findSimilar(s1);
|
88
|
+
int limit = 40;
|
64
89
|
int i = 0;
|
65
90
|
for (const auto &res : results) {
|
66
91
|
VALUE arr = rb_ary_new();
|
@@ -75,6 +100,57 @@ VALUE StringIndexFind(VALUE self, VALUE str) {
|
|
75
100
|
return ret;
|
76
101
|
}
|
77
102
|
|
103
|
+
VALUE StringIndexFindFilesAndDirs(VALUE self, VALUE str) {
|
104
|
+
VALUE ret;
|
105
|
+
std::string s1 = StringValueCStr(str);
|
106
|
+
|
107
|
+
void *data;
|
108
|
+
TypedData_Get_Struct(self, int, &str_idx_type, data);
|
109
|
+
StrIdx::StringIndex *idx = (StrIdx::StringIndex *)data;
|
110
|
+
|
111
|
+
ret = rb_ary_new();
|
112
|
+
const std::vector<std::pair<float, std::string>> &results = idx->findFilesAndDirectories(s1);
|
113
|
+
int limit = 40;
|
114
|
+
int i = 0;
|
115
|
+
for (const auto &res : results) {
|
116
|
+
VALUE arr = rb_ary_new();
|
117
|
+
rb_ary_push(arr, rb_str_new_cstr(res.second.c_str()));
|
118
|
+
rb_ary_push(arr, DBL2NUM(res.first));
|
119
|
+
rb_ary_push(ret, arr);
|
120
|
+
i++;
|
121
|
+
if (i >= limit) {
|
122
|
+
break;
|
123
|
+
}
|
124
|
+
}
|
125
|
+
return ret;
|
126
|
+
}
|
127
|
+
|
128
|
+
VALUE StringIndexFindDirs(VALUE self, VALUE str) {
|
129
|
+
VALUE ret;
|
130
|
+
std::string s1 = StringValueCStr(str);
|
131
|
+
|
132
|
+
void *data;
|
133
|
+
TypedData_Get_Struct(self, int, &str_idx_type, data);
|
134
|
+
StrIdx::StringIndex *idx = (StrIdx::StringIndex *)data;
|
135
|
+
|
136
|
+
ret = rb_ary_new();
|
137
|
+
const std::vector<std::pair<float, std::string>> &results =
|
138
|
+
idx->findFilesAndDirectories(s1, false, true);
|
139
|
+
int limit = 40;
|
140
|
+
int i = 0;
|
141
|
+
for (const auto &res : results) {
|
142
|
+
VALUE arr = rb_ary_new();
|
143
|
+
rb_ary_push(arr, rb_str_new_cstr(res.second.c_str()));
|
144
|
+
rb_ary_push(arr, DBL2NUM(res.first));
|
145
|
+
rb_ary_push(ret, arr);
|
146
|
+
i++;
|
147
|
+
if (i >= limit) {
|
148
|
+
break;
|
149
|
+
}
|
150
|
+
}
|
151
|
+
return ret;
|
152
|
+
}
|
153
|
+
|
78
154
|
VALUE StringIndexSetDirSeparator(VALUE self, VALUE str) {
|
79
155
|
char c = '/';
|
80
156
|
if (TYPE(str) == T_STRING) {
|
@@ -95,6 +171,17 @@ VALUE StringIndexSetDirSeparator(VALUE self, VALUE str) {
|
|
95
171
|
return self;
|
96
172
|
}
|
97
173
|
|
174
|
+
VALUE StringIndexSetDirWeight(VALUE self, VALUE d) {
|
175
|
+
if (TYPE(d) == T_FLOAT) {
|
176
|
+
double c_float = NUM2DBL(rb_funcall(d, rb_intern("to_f"), 0));
|
177
|
+
void *data;
|
178
|
+
TypedData_Get_Struct(self, int, &str_idx_type, data);
|
179
|
+
StrIdx::StringIndex *idx = (StrIdx::StringIndex *)data;
|
180
|
+
idx->setDirWeight(c_float);
|
181
|
+
}
|
182
|
+
return self;
|
183
|
+
}
|
184
|
+
|
98
185
|
void Init_stridx(void) {
|
99
186
|
|
100
187
|
VALUE mStrIdx = rb_define_module("StrIdx");
|
@@ -104,10 +191,12 @@ void Init_stridx(void) {
|
|
104
191
|
rb_define_method(classStringIndex, "add", StringIndexAddSegments, 2);
|
105
192
|
rb_define_method(classStringIndex, "waitUntilDone", StringIndexWaitUntilDone, 0);
|
106
193
|
rb_define_method(classStringIndex, "find", StringIndexFind, 1);
|
107
|
-
|
194
|
+
rb_define_method(classStringIndex, "findNum", StringIndexFindNum, 2);
|
195
|
+
rb_define_method(classStringIndex, "setDirWeight", StringIndexSetDirWeight, 1);
|
196
|
+
rb_define_method(classStringIndex, "findFilesAndDirs", StringIndexFindFilesAndDirs, 1);
|
197
|
+
rb_define_method(classStringIndex, "findDirs", StringIndexFindDirs, 1);
|
198
|
+
|
108
199
|
rb_define_method(classStringIndex, "setDirSeparator", StringIndexSetDirSeparator, 1);
|
109
|
-
|
110
|
-
|
111
200
|
}
|
112
201
|
|
113
202
|
} // End extern "C"
|
data/runserver.rb
CHANGED
@@ -1,7 +1,29 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Add cur dir to load path
|
2
4
|
$:.unshift File.dirname(__FILE__)
|
3
5
|
|
6
|
+
def kill_signal
|
7
|
+
puts "\nShutting down..."
|
8
|
+
File.delete(File.expand_path("~/.stridx/sock"))
|
9
|
+
end
|
10
|
+
|
11
|
+
# https://gist.github.com/sauloperez/6592971
|
12
|
+
# Trap ^C
|
13
|
+
Signal.trap("INT") {
|
14
|
+
kill_signal
|
15
|
+
exit
|
16
|
+
}
|
17
|
+
|
18
|
+
# Trap `Kill `
|
19
|
+
Signal.trap("TERM") {
|
20
|
+
kill_signal
|
21
|
+
exit
|
22
|
+
}
|
23
|
+
|
4
24
|
require "server.rb"
|
5
25
|
# StrIdx::Server.start ARGV, daemonize: true
|
6
26
|
StrIdx::Server.start ARGV
|
7
27
|
|
28
|
+
|
29
|
+
|
data/server.rb
CHANGED
@@ -33,6 +33,7 @@ module StrIdx
|
|
33
33
|
def initialize(dir_list, daemonize: false)
|
34
34
|
idx = StrIdx::StringIndex.new
|
35
35
|
idx.setDirSeparator("/")
|
36
|
+
idx.setDirWeight(0.85) # Lower scores for directory matches
|
36
37
|
|
37
38
|
t = Time.new
|
38
39
|
|
@@ -86,8 +87,12 @@ module StrIdx
|
|
86
87
|
# puts "Received from client: #{data}"
|
87
88
|
if data.match(/^find:(.*)/)
|
88
89
|
query = Regexp.last_match(1)
|
89
|
-
|
90
|
-
|
90
|
+
# TODO: not sure which is best as default:
|
91
|
+
# res = idx.find(query)
|
92
|
+
# res = idx.findDirs(query)
|
93
|
+
res = idx.findFilesAndDirs(query)
|
94
|
+
# response = res.collect { |x| flist[x[0]] }.join("\n")
|
95
|
+
response = res.collect { |x| "/"+x[0] }.join("\n")
|
91
96
|
|
92
97
|
# Send a response back to the client
|
93
98
|
client.puts response
|
@@ -98,6 +103,7 @@ module StrIdx
|
|
98
103
|
}
|
99
104
|
|
100
105
|
t.join
|
106
|
+
File.delete(sockfn)
|
101
107
|
end
|
102
108
|
end
|
103
109
|
end
|
data/stridx.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "StrIdx"
|
3
|
-
spec.version = "0.1.
|
3
|
+
spec.version = "0.1.6"
|
4
4
|
spec.authors = ["Sami Sieranoja"]
|
5
5
|
spec.email = ["sami.sieranoja@gmail.com"]
|
6
6
|
|
@@ -13,10 +13,6 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
14
14
|
f.match(%r{^(refcode|spec|features)/})
|
15
15
|
end
|
16
|
-
# spec.files << "thread_pool.hpp"
|
17
|
-
# spec.files << "exe/stridx.rb"
|
18
|
-
# spec.files << "server.rb"
|
19
|
-
# spec.files << "stridx-tty.rb"
|
20
16
|
|
21
17
|
spec.bindir = "exe"
|
22
18
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|