Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

MySQL アダプタの追加 #24

Merged
merged 10 commits into from

1 participant

@hsbt
Owner

るびまで使っている MySQL アダプタをサルベージして、mysql.so を直接叩くのではなく、sequel を使って書き直しています。

@hsbt hsbt merged commit a4bd9bf into master

1 check passed

Details default The Travis build passed
@hsbt hsbt deleted the mysql branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 21, 2013
  1. @hsbt
Commits on Mar 22, 2013
  1. @hsbt

    added sequel

    hsbt authored
  2. @hsbt

    rewrite with sequel

    hsbt authored
  3. @hsbt

    oops

    hsbt authored
  4. @hsbt

    support page_backup

    hsbt authored
  5. @hsbt

    support to set attribute value

    hsbt authored
  6. @hsbt

    support rdb: no test:<

    hsbt authored
  7. @hsbt

    rename

    hsbt authored
  8. @hsbt

    tiny

    hsbt authored
  9. @hsbt

    more rename

    hsbt authored
This page is out of date. Refresh to see the latest.
Showing with 301 additions and 4 deletions.
  1. +3 −0  Gemfile
  2. +8 −4 Gemfile.lock
  3. +216 −0 hiki/db/rdb.rb
  4. +74 −0 hiki/repos/rdb.rb
View
3  Gemfile
@@ -3,6 +3,9 @@ source 'https://rubygems.org'
gem 'rack'
gem 'thin'
+gem 'sequel'
+gem 'mysql2'
+
group :development do
gem 'foreman'
end
View
12 Gemfile.lock
@@ -12,13 +12,14 @@ GEM
ffi (~> 1.0, >= 1.0.11)
daemons (1.1.9)
eventmachine (1.0.3)
- ffi (1.4.0)
+ ffi (1.5.0)
foreman (0.62.0)
thor (>= 0.13.6)
json (1.7.7)
mime-types (1.21)
- multi_json (1.7.0)
- nokogiri (1.5.6)
+ multi_json (1.7.1)
+ mysql2 (0.3.11)
+ nokogiri (1.5.9)
rack (1.5.2)
rack-test (0.6.2)
rack (>= 1.0)
@@ -29,6 +30,7 @@ GEM
multi_json (~> 1.0)
rubyzip
websocket (~> 1.0.4)
+ sequel (3.45.0)
test-unit (2.5.4)
test-unit-capybara (1.0.2)
capybara (>= 1.1.2)
@@ -39,7 +41,7 @@ GEM
test-unit-rr (1.0.2)
rr (>= 1.0.2)
test-unit (>= 2.5.2)
- thin (1.5.0)
+ thin (1.5.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
@@ -54,7 +56,9 @@ PLATFORMS
DEPENDENCIES
capybara (< 2)
foreman
+ mysql2
rack
+ sequel
test-unit
test-unit-capybara
test-unit-notify
View
216 hiki/db/rdb.rb
@@ -0,0 +1,216 @@
+# $Id: flatfile.rb,v 1.23 2005/11/01 14:21:00 yanagita Exp $
+# Copyright (C) 2007 Kazuhiko <kazuhiko@fdiary.net>
+
+require "hiki/storage"
+require "hiki/util"
+require "hiki/db/tmarshal"
+require "sequel"
+
+module Hiki
+ class HikiDB_rdb < HikiDBBase
+ attr_reader :db, :wiki
+
+ def initialize(conf)
+ @conf = conf
+ @db = Sequel.connect(ENV['DATABASE_URL'] || @conf.database_url)
+ @wiki = @conf.database_wiki
+ @conf.repos.db = self
+ @cache = {}
+ end
+
+ def close_db
+ true
+ end
+
+ def store(page, body, md5, update_timestamp = true)
+ if exist?(page)
+ return nil if md5 != md5hex(page)
+ if update_timestamp
+ backup(page)
+ end
+ end
+
+ last_modified = Time::now
+
+ revisions = @db[:page_backup].where(wiki: @wiki, name: page).select(:revision).to_a.map{|record| record[:revision]}
+ revision = if revisions.empty?
+ 1
+ else
+ max(revisions)
+ end
+ @db[:page_backup].insert(body: body, last_modified: last_modified, wiki: @wiki, name: page, revision: revision)
+
+ record = @db[:page].where(wiki: @wiki, name: page)
+ if record.first
+ record.update(body: body, last_modified: last_modified)
+ else
+ @db[:page].insert(body: body, last_modified: last_modified, wiki: @wiki, name: page, count: 0)
+ end
+
+ @cache[page] = body
+ true
+ end
+
+ def unlink(page)
+ @db[:page].where(wiki: @wiki, name: page).delete
+ end
+
+ def load(page)
+ return @cache[page] if @cache.has_key?(page)
+
+ if res = @db[:page].where(wiki: @wiki, name: page).select(:body).first
+ @cache[page] = res[:body]
+ else
+ @cache[page] = nil
+ end
+ @cache[page]
+ end
+
+ def load_backup(page)
+ @db[:page_backup].where(wiki: @wiki, name: page).order(:revision).limit(1, 1).to_a.first
+ end
+
+ def save(page, src, md5)
+ raise "DB#save is obsoleted. Please use Plugin#save instead."
+ end
+
+ def exist?(page)
+ return page_info.find{|i| i.to_a[0][0] == page} ? true : false
+ end
+
+ def pages
+ @db[:page].where(wiki: @wiki).select(:name).to_a.map{|page| page[:name]}
+ end
+
+ def info(page)
+ res = page_info.find{|i| i.to_a[0][0] == page}.to_a[0][1] rescue nil
+ if res
+ return res
+ else
+ return default
+ end
+ end
+
+ def page_info
+ @info_db ||= @db[:page].where(wiki: @wiki).select(:name, :title, :last_modified, :keyword, :references, :editor, :freeze, :count).to_a.map{|page| {page[:name] => make_info_hash(page)}}
+ end
+
+ def set_attribute(page, attr)
+ attr.each do |attribute, value|
+ attribute = attribute.to_s.chomp
+ case value
+ when Array
+ value = value.join("\n")
+ when TrueClass
+ value = 1
+ when FalseClass
+ value = 0
+ end
+ @db[:page].where(wiki: @wiki, name: page).update(attribute => value)
+ unless %w(references count freeze).include?(attribute)
+ @db[:page_backup].where(wiki: @wiki, name: page).order(:revision).limite(1).update(attribute => value)
+ end
+ end
+ end
+
+ def get_attribute(page, attribute)
+ return info(page)[attribute]
+ end
+
+ def select
+ result = []
+ page_info.each do |e|
+ name, info = e.to_a.first
+ result << name if yield(info)
+ end
+ result
+ end
+
+ def increment_hitcount(page)
+ @db[:page].where(wiki: @wiki, name: page).update(count: count + 1)
+ end
+
+ def get_hitcount(page)
+ get_attribute(page, :count)
+ end
+
+ def freeze_page (page, freeze)
+ set_attribute(page, [[:freeze, freeze]])
+ end
+
+ def is_frozen? (page)
+ get_attribute(page, :freeze)
+ end
+
+ def set_last_update (page, t)
+ set_attribute(page, [[:last_modified, t]])
+ end
+
+ def get_last_update(page)
+ get_attribute(page, :last_modified)
+ end
+
+ def set_references(page, r)
+ set_attribute(page, [[:references, r]])
+ end
+
+ def get_references(page)
+ ref = []
+ page_info.each do |a|
+ r = a.values[0][:references]
+ if String === r # for compatibility
+ r = r.split(",")
+ set_references(a.keys[0], r)
+ end
+ ref << a.keys[0] if r.include?(page)
+ end
+ ref
+ end
+
+ private
+
+ def create_missing_dirs
+ [@pages_path, @backup_path].each {|d|
+ FileUtils.mkdir_p(d) unless FileTest.exist?(d)
+ }
+ end
+
+ def default
+ { :count => 0,
+ :last_modified => Time::now,
+ :freeze => false,
+ :references => [],
+ :keyword => [],
+ :title => "",
+ }
+ end
+
+ def make_info_hash(hash)
+ {
+ :title => hash[:title] || "",
+ :last_modified => make_time(hash[:last_modified]),
+ :keyword => (hash[:keyword] || "").split(/\n/),
+ :references => (hash[:references] || "").split(/\n/),
+ :editor => hash[:editor],
+ :freeze => (hash[:freeze] == 1),
+ :count => hash[:count],
+ }
+ end
+
+ def make_time(time)
+ if time
+ Time.local(time.year, time.month, time.day, time.hour, time.min, time.sec)
+ else
+ Time.now
+ end
+ end
+
+ def textdir(s)
+ File::join(@pages_path, s.escape).untaint
+ end
+
+ def backupdir(s)
+ File::join(@backup_path, s.escape).untaint
+ end
+ end
+end
View
74 hiki/repos/rdb.rb
@@ -0,0 +1,74 @@
+# $Id: svn.rb,v 1.14 2005/09/11 10:10:30 fdiary Exp $
+# Copyright (C) 2003, Koichiro Ohba <koichiro@meadowy.org>
+# Copyright (C) 2003, Yasuo Itabashi <yasuo_itabashi{@}hotmail.com>
+# You can distribute this under GPL.
+
+require "hiki/repos/default"
+require 'sequel'
+
+module Hiki
+ class HikifarmReposRdb < HikifarmReposBase
+ def initialize(root, data_root)
+ @data_root = data_root
+ @db = Sequel.connect(root)
+ end
+
+ def setup
+ # do nothing
+ end
+
+ def imported?(wiki)
+ true
+ end
+
+ def import(wiki)
+ Dir["#{@data_root}/#{wiki}/text/*"].each do |f|
+ if File.file?(f.untaint)
+ @db[:page_backup].insert(wiki: wiki, name: File.basename(f), body: File.read(f), last_modified: File.mtime(f), revision: 1)
+ @db[:page].insert(wiki: wiki, name: File.basename(f), body: File.read(f), last_modified: File.mtime(f))
+ end
+ end
+ end
+
+ def update(wiki)
+ # do nothing
+ end
+ end
+
+ class ReposRdb < ReposBase
+ attr_writer :db
+
+ def initialize(root, data_path)
+ # do nothing
+ end
+
+ def commit(page, msg = default_msg)
+ # do nothing
+ end
+
+ def delete(page, msg = default_msg)
+ # do nothing
+ end
+
+ def get_revision(page, revision)
+ record = @db.db[:page_backup].where(wiki: @db.wiki, name: page, revision: revision).select(:body).first
+ if record && record[:body]
+ record[:body]
+ else
+ ""
+ end
+ end
+
+ def revisions(page)
+ records = @db.db[:page_backup].where(wiki: @db.wiki, name: page).order(:revision).select(:revision, :last_modified, :editor)
+ records.map do |record|
+ [
+ record[:revision],
+ "%04d/%02d/%02d %02d:%02d:%02d" % [record[:last_modified].year, record[:last_modified].month, record[:last_modified].day, record[:last_modified].hour, record[:last_modified].min, record[:last_modified].sec],
+ nil,
+ record[:editor] || "Anonymous"
+ ]
+ end
+ end
+ end
+end
Something went wrong with that request. Please try again.