Skip to content

Commit 1c2aed9

Browse files
committedMar 24, 2014
iqdb changes
1 parent 6713673 commit 1c2aed9

File tree

5 files changed

+77
-30
lines changed

5 files changed

+77
-30
lines changed
 

‎README

+8
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,11 @@ correct.
3636
debug your Nginx configuration file.
3737

3838
4) Check all log files.
39+
40+
=== IQDB Integration
41+
42+
In order to enable IQDB integration, you must compile and run the IQDB server somewhere (preferably on the local server). There are two Danbooru configuration settings that you must then set: iqdb_hostname_and_port and iqdb_file.
43+
44+
You must then populate the initial database. There is a fix script called 028_iqdb_import.rb to do this for you.
45+
46+
From then on, all new uploads will asynchronously create two tasks: one to update the IQDB database through the server interface (which updates the in-memory representation), and another to the command interface (which updates the disk representation). Expunging posts will also update the database.

‎app/logical/iqdb/command.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Iqdb
2+
class Command
3+
attr_reader :database
4+
5+
def initialize(database)
6+
@database = database
7+
end
8+
9+
def process(&block)
10+
IO.popen("iqdb #{database}", "w", &block)
11+
end
12+
13+
def add(post)
14+
hex = post.id.to_s(16)
15+
process do |io|
16+
io.puts "add 0 #{hex} :#{post.preview_file_path}"
17+
io.puts "quit"
18+
end
19+
end
20+
21+
def remove(post_id)
22+
hex = post_id.to_s(16)
23+
process do |io|
24+
io.puts "remove 0 #{hex}"
25+
io.puts "quit"
26+
end
27+
end
28+
end
29+
end

‎app/logical/iqdb/server.rb

+18-23
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,7 @@ class Server
55
FLAG_WIDTH_AS_SET = 0x08
66
FLAG_DISCARD_COMMON_COEFFS = 0x16
77

8-
attr_reader :hostname, :port
9-
10-
def self.import(database)
11-
IO.popen("iqdb #{database}", "w") do |io|
12-
Post.find_each do |post|
13-
puts "Adding #{post.id}"
14-
io.puts "#{post.id.to_s(16)} :#{post.preview_file_path}"
15-
end
16-
end
17-
end
18-
19-
def self.add(database, image_id, filename)
20-
image_id_hex = image_id.to_s(16)
21-
`iqdb add #{database} #{image_id_hex} :#{filename}`
22-
end
23-
24-
def self.remove(database, image_id)
25-
image_id_hex = image_id.to_s(16)
26-
`iqdb remove 0 #{image_id_hex} #{database}`
27-
end
8+
attr_reader :hostname, :port, :socket
289

2910
def initialize(hostname, port)
3011
@hostname = hostname
@@ -36,7 +17,7 @@ def open
3617
end
3718

3819
def close
39-
@socket.close
20+
socket.close
4021
end
4122

4223
def request
@@ -46,9 +27,23 @@ def request
4627
close
4728
end
4829

49-
def query(dbid, results, filename, flags = FLAG_DISCARD_COMMON_COEFFS)
30+
def add(post)
31+
request do
32+
hex = post.id.to_s(16)
33+
socket.puts "add 0 #{hex} :#{post.preview_file_path}"
34+
end
35+
end
36+
37+
def remove(post_id)
38+
request do
39+
hext = post_id.to_s(16)
40+
socket.puts "remove 0 #{hex}"
41+
end
42+
end
43+
44+
def query(results, filename, flags = FLAG_DISCARD_COMMON_COEFFS)
5045
request do
51-
@socket.puts "query #{dbid} #{flags} #{results} #{filename}"
46+
socket.puts "query 0 #{flags} #{results} #{filename}"
5247
responses = Responses::Collection.new(@socket.read)
5348
end
5449
end

‎app/models/post.rb

+21-6
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class SearchError < Exception ; end
99
after_save :create_version
1010
after_save :update_parent_on_save
1111
after_save :apply_post_metatags, :on => :create
12-
# after_save :update_iqdb, :on => :create
13-
# after_destroy :remove_iqdb
12+
# after_save :update_iqdb_async, :on => :create
13+
# after_destroy :remove_iqdb_async
1414
before_save :merge_old_changes
1515
before_save :normalize_tags
1616
before_save :update_tag_post_counts
@@ -1272,17 +1272,31 @@ def parse_pixiv_id
12721272
end
12731273

12741274
module IqdbMethods
1275-
def update_iqdb
1275+
extend ActiveSupport::Concern
1276+
1277+
module ClassMethods
1278+
def remove_iqdb(post_id)
1279+
Iqdb::Server.new(*Danbooru.config.iqdb_hostname_and_port).remove(post_id)
1280+
Iqdb::Command.new(Danbooru.config.iqdb_file).remove(post_id)
1281+
end
1282+
end
1283+
1284+
def update_iqdb_async
12761285
Danbooru.config.all_server_hosts.each do |host|
1277-
Iqdb::Server.delay(:queue => host).add(Danbooru.config.iqdb_file, id, preview_file_path)
1286+
delay(:queue => host).update_iqdb
12781287
end
12791288
end
12801289

1281-
def remove_iqdb
1290+
def remove_iqdb_async
12821291
Danbooru.config.all_server_hosts.each do |host|
1283-
Iqdb::Server.delay(:queue => host).remove(Danbooru.config.iqdb_file, id)
1292+
Post.delay(:queue => host).remove_iqdb(id)
12841293
end
12851294
end
1295+
1296+
def update_iqdb
1297+
Iqdb::Server.new(*Danbooru.config.iqdb_hostname_and_port).add(self)
1298+
Iqdb::Command.new(Danbooru.config.iqdb_file).add(self)
1299+
end
12861300
end
12871301

12881302
include FileMethods
@@ -1303,6 +1317,7 @@ def remove_iqdb
13031317
include ApiMethods
13041318
extend SearchMethods
13051319
include PixivMethods
1320+
include IqdbMethods
13061321

13071322
def visible?
13081323
return false if !Danbooru.config.can_user_see_post?(CurrentUser.user, self)

‎script/fixes/029_iqdb_import.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment'))
44

5-
Iqdb::Server.import("/var/www/danbooru2/shared/iqdb.db")
5+
Iqdb::Server.import(Danbooru.config.iqdb_file)

0 commit comments

Comments
 (0)
Please sign in to comment.