-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support other charsets #85
Open
takuros
wants to merge
4
commits into
chriskite:next
Choose a base branch
from
takuros:next
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ test.db | |
test.tch | ||
*.kch | ||
rdoc | ||
*.gem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
== 0.7.2-fork / 2014-04-05 | ||
|
||
* Minor enhancements | ||
|
||
* Support other charsets | ||
|
||
== 0.7.2 / 2012-05-30 | ||
|
||
* Bug fixes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# coding: utf-8 | ||
|
||
begin | ||
require 'mysql2' | ||
rescue LoadError | ||
puts "You need the mysql2 gem to use Anemone::Storage::MySQL" | ||
exit | ||
end | ||
|
||
module Anemone | ||
module Storage | ||
class MySQL | ||
|
||
def initialize(opts = {}) | ||
host = opts[:host] || 'localhost' | ||
username = opts[:username] || 'crawler' | ||
password = opts[:password] || 'anemone_pass' | ||
database = opts[:database] || 'anemone' | ||
@db = Mysql2::Client.new(:host => #{host}, :username => #{username}, :password => #{password}, :database => #{database}) | ||
create_schema | ||
end | ||
|
||
def [](url) | ||
value = @db.query("SELECT data FROM anemone_storage WHERE page_key = '#{get_hash_value(url)}'").first['data'] | ||
if value | ||
Marshal.load(value) | ||
end | ||
end | ||
|
||
def []=(url, value) | ||
data = Marshal.dump(value) | ||
key = get_hash_value(url) | ||
if has_key?(url) | ||
@db.query("UPDATE anemone_storage SET page_data = '#{data}' WHERE page_key = '#{key}'") | ||
else | ||
@db.query("INSERT INTO anemone_storage (page_key, page_data) VALUES('#{key}', '#{data}')") | ||
end | ||
end | ||
|
||
def delete(url) | ||
page = self[url] | ||
@db.query("DELETE FROM anemone_storage WHERE page_key = '#{get_hash_value(url)}'") | ||
page | ||
end | ||
|
||
def each | ||
@db.execute("SELECT page_key, page_data FROM anemone_storage ORDER BY id") do |row| | ||
value = Marshal.load(row[1]) | ||
yield row[0], value | ||
end | ||
end | ||
|
||
def merge!(hash) | ||
hash.each { |key, value| self[key] = value } | ||
self | ||
end | ||
|
||
def size | ||
@db.query('SELECT COUNT(*) FROM anemone_storage') | ||
end | ||
|
||
def keys | ||
@db.query("SELECT page_key FROM anemone_storage ORDER BY id").map{|t| t[0]} | ||
end | ||
|
||
def has_key?(url) | ||
key = get_hash_value(url) | ||
result = @db.query("SELECT count(id) FROM anemone_storage WHERE page_key = '#{key}'") | ||
if result.first['count(id)'] > 0 | ||
return true | ||
else | ||
return false | ||
end | ||
end | ||
|
||
def close | ||
@db.close | ||
end | ||
|
||
private | ||
|
||
def create_schema | ||
@db.query <<SQL | ||
create table if not exists anemone_storage ( | ||
id int(11) NOT NULL auto_increment, | ||
page_key varchar(255), | ||
page_data BLOB, | ||
PRIMARY KEY (id), | ||
key (page_key) | ||
) DEFAULT CHARSET=utf8; | ||
SQL | ||
end | ||
|
||
def load_page(hash) | ||
BINARY_FIELDS.each do |field| | ||
hash[field] = hash[field].to_s | ||
end | ||
Page.from_hash(hash) | ||
end | ||
|
||
def get_hash_value(key) | ||
Digest::SHA1.hexdigest(key) | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
begin | ||
require 'aws-sdk' | ||
rescue LoadError | ||
puts "You need the sqlite3 gem to use Anemone::Storage::SQLite3" | ||
exit | ||
end | ||
|
||
module Anemone | ||
module Storage | ||
class S3 | ||
|
||
def initialize(bucket,key,secret) | ||
AWS.config( | ||
:access_key_id => ENV['AWS_ACCESS_KEY']', | ||
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'] | ||
) | ||
@s3 = AWS::S3.new | ||
@bucket = @s3.buckets[bucket] | ||
end | ||
|
||
def [](url) | ||
@bucket.objects[url2hash(url)].read | ||
end | ||
|
||
def []=(url, value) | ||
object = @bucket.objects[url2hash(url)] | ||
object.write(value) | ||
end | ||
|
||
def delete(url) | ||
@bucket.objects.delete(url2hash(url)) | ||
end | ||
|
||
def each | ||
#TODO | ||
end | ||
|
||
def merge!(hash) | ||
#TODO | ||
end | ||
|
||
def size | ||
#TODO | ||
end | ||
|
||
def keys | ||
#TODO | ||
end | ||
|
||
def has_key?(url) | ||
#TODO | ||
end | ||
|
||
def close | ||
#TODO | ||
end | ||
|
||
private | ||
|
||
def url2hash(url) | ||
Digest::SHA1.digest(url) | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
$:.unshift(File.dirname(__FILE__)) | ||
require 'spec_helper' | ||
|
||
require 'anemone/storage/mysql.rb | ||
|
||
module Anemone | ||
describe Storage do | ||
|
||
describe ".MySQL" do | ||
it "returns a MySQL adapter" do | ||
store = Anemone::Storage.MySQL | ||
store.should be_an_instance_of(Anemone::Storage::MySQL) | ||
store.close | ||
end | ||
end | ||
|
||
module Storage | ||
shared_examples_for "storage engine" do | ||
|
||
before(:each) do | ||
@url = SPEC_DOMAIN | ||
@page = Page.new(URI(@url)) | ||
end | ||
|
||
it "should implement [] and []=" do | ||
@store.should respond_to(:[]) | ||
@store.should respond_to(:[]=) | ||
|
||
@store[@url] = @page | ||
@store[@url].url.should == URI(@url) | ||
end | ||
|
||
it "should implement has_key?" do | ||
@store.should respond_to(:has_key?) | ||
|
||
@store[@url] = @page | ||
@store.has_key?(@url).should == true | ||
|
||
@store.has_key?('missing').should == false | ||
end | ||
|
||
it "should implement delete" do | ||
@store.should respond_to(:delete) | ||
|
||
@store[@url] = @page | ||
@store.delete(@url).url.should == @page.url | ||
@store.has_key?(@url).should == false | ||
end | ||
|
||
it "should implement keys" do | ||
@store.should respond_to(:keys) | ||
|
||
urls = [SPEC_DOMAIN, SPEC_DOMAIN + 'test', SPEC_DOMAIN + 'another'] | ||
pages = urls.map { |url| Page.new(URI(url)) } | ||
urls.zip(pages).each { |arr| @store[arr[0]] = arr[1] } | ||
|
||
(@store.keys - urls).should == [] | ||
end | ||
|
||
it "should implement each" do | ||
@store.should respond_to(:each) | ||
|
||
urls = [SPEC_DOMAIN, SPEC_DOMAIN + 'test', SPEC_DOMAIN + 'another'] | ||
pages = urls.map { |url| Page.new(URI(url)) } | ||
urls.zip(pages).each { |arr| @store[arr[0]] = arr[1] } | ||
|
||
result = {} | ||
@store.each { |k, v| result[k] = v } | ||
(result.keys - urls).should == [] | ||
(result.values.map { |page| page.url.to_s } - urls).should == [] | ||
end | ||
|
||
it "should implement merge!, and return self" do | ||
@store.should respond_to(:merge!) | ||
|
||
hash = {SPEC_DOMAIN => Page.new(URI(SPEC_DOMAIN)), | ||
SPEC_DOMAIN + 'test' => Page.new(URI(SPEC_DOMAIN + 'test'))} | ||
merged = @store.merge! hash | ||
hash.each { |key, value| @store[key].url.to_s.should == key } | ||
|
||
merged.should === @store | ||
end | ||
|
||
it "should correctly deserialize nil redirect_to when loading" do | ||
@page.redirect_to.should be_nil | ||
@store[@url] = @page | ||
@store[@url].redirect_to.should be_nil | ||
end | ||
end | ||
|
||
describe PStore do | ||
it_should_behave_like "storage engine" | ||
|
||
before(:each) do | ||
@test_file = 'test.pstore' | ||
File.delete @test_file rescue nil | ||
@store = Anemone::Storage.PStore(@test_file) | ||
end | ||
|
||
after(:all) do | ||
File.delete @test_file rescue nil | ||
end | ||
end | ||
|
||
describe MySQL do | ||
it_should_behave_like "storage engine" | ||
|
||
before(:each) do | ||
@test_file = 'test.db' | ||
File.delete @test_file rescue nil | ||
@store = Anemone::Storage.SQLite3(@test_file) | ||
end | ||
|
||
after(:each) do | ||
@store.close | ||
end | ||
|
||
after(:all) do | ||
File.delete @test_file rescue nil | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i cant find db.execute method