Class: Diametric::Transactor

Inherits:
Object
  • Object
show all
Includes:
ServiceBase
Defined in:
lib/diametric/transactor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ServiceBase

included

Constructor Details

#initialize(conf = "datomic_version.yml", dest = "vendor/datomic") ⇒ Transactor

Returns a new instance of Transactor


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/diametric/transactor.rb', line 19

def initialize(conf="datomic_version.yml", dest="vendor/datomic")
  @conf = conf
  @dest = dest
  datomic_type, datomic_version = Transactor.datomic_version(conf)
  datomic_path = "datomic-#{datomic_type}-#{datomic_version}"
  if Pathname.new(dest).relative?
    @datomic_home = File.join(File.dirname(__FILE__), "../..", dest, datomic_path)
  else
    @datomic_home = File.join(dest, datomic_path)
  end
  #@datomic_version_no = Transactor.datomic_version_no(@datomic_version)
  @hostname = nil
  @port = nil
  @pid = nil
end

Instance Attribute Details

#datomic_homeObject

Returns the value of attribute datomic_home


16
17
18
# File 'lib/diametric/transactor.rb', line 16

def datomic_home
  @datomic_home
end

#datomic_versionObject

Returns the value of attribute datomic_version


16
17
18
# File 'lib/diametric/transactor.rb', line 16

def datomic_version
  @datomic_version
end

#datomic_version_noObject

Returns the value of attribute datomic_version_no


16
17
18
# File 'lib/diametric/transactor.rb', line 16

def datomic_version_no
  @datomic_version_no
end

#db_aliasObject

Returns the value of attribute db_alias


17
18
19
# File 'lib/diametric/transactor.rb', line 17

def db_alias
  @db_alias
end

#hostObject

Returns the value of attribute host


17
18
19
# File 'lib/diametric/transactor.rb', line 17

def host
  @host
end

#pidObject

Returns the value of attribute pid


16
17
18
# File 'lib/diametric/transactor.rb', line 16

def pid
  @pid
end

#portObject

Returns the value of attribute port


17
18
19
# File 'lib/diametric/transactor.rb', line 17

def port
  @port
end

#uriObject

Returns the value of attribute uri


17
18
19
# File 'lib/diametric/transactor.rb', line 17

def uri
  @uri
end

Class Method Details

.datomic_command(datomic_home) ⇒ Object


9
10
11
12
13
# File 'lib/diametric/transactor.rb', line 9

def datomic_command(datomic_home)
  classpath = datomic_classpath(datomic_home)
  java_opts='-XX:NewRatio=4 -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:CMSInitiatingOccupancyFraction=60 -XX:+UseCMSInitiatingOccupancyOnly -XX:+CMSScavengeBeforeRemark'
  command = ["java -server -Xmx1g -Xms1g", java_opts, "-cp", classpath, "clojure.main", "--main datomic.launcher"].flatten.join(" ")
end

Instance Method Details

#properties(props) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/diametric/transactor.rb', line 57

def properties(props)
  File.readlines(props).each do |line|
    m = /^(host=)(.+)/.match(line)
    if m && m[2]
      @hostname = m[2]
    end
    m = /^(port=)(\d+)/.match(line)
    if m && m[2]
      @port = m[2]
    end
  end
end

#ready?Boolean

Returns:


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/diametric/transactor.rb', line 70

def ready?
  tmp_database = "datomic:free://#{@hostname}:#{port}/tmp_database-#{SecureRandom.uuid}"
  while true
    begin
      if Diametric::Persistence::Peer.create_database(tmp_database)
        Diametric::Persistence::Peer.delete_database(tmp_database)
        return true
      end
    rescue
      sleep 1
      redo
    end
  end
  true
end

#start(props) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/diametric/transactor.rb', line 35

def start(props)
  return if @pid
  Transactor.download(@conf, @dest)
  command = Transactor.datomic_command(@datomic_home)

  unless File.exists?(props)
    puts "Transactor property file #{props} doesn't exist."
    return
  end
  properties(props)
  tmp_pid = spawn("#{command} #{props}")
  if ready?
    @pid = tmp_pid
  end
  @pid
end

#stopObject


52
53
54
55
# File 'lib/diametric/transactor.rb', line 52

def stop
  Process.kill("HUP", @pid) if @pid
  @pid = nil
end