Module: Ccp::Kvs::Tokyo::StateMachine

Includes:
TokyoCabinet
Included in:
Cabinet
Defined in:
lib/ccp/kvs/tokyo/state_machine.rb

Constant Summary collapse

CLOSED =

state machine

1
READABLE =
2
WRITABLE =
3

Instance Method Summary collapse

Instance Method Details

#C!Object



27
28
29
30
31
32
33
34
# File 'lib/ccp/kvs/tokyo/state_machine.rb', line 27

def C!
  case state
  when CLOSED   ; # NOP
  when READABLE,
       WRITABLE ; @db.close; @state = CLOSED
  else          ; raise "unknown state: #{state}"
  end
end

#closeObject



23
24
25
# File 'lib/ccp/kvs/tokyo/state_machine.rb', line 23

def close
  C!
end

#open(mode) ⇒ Object



18
19
20
21
# File 'lib/ccp/kvs/tokyo/state_machine.rb', line 18

def open(mode)
  Pathname(@source.to_s).parent.mkpath
  @db.open(@source.to_s, mode) or tokyo_error!("%s#open(%s,%s): " % [self.class, @source, mode])
end

#R(&block) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/ccp/kvs/tokyo/state_machine.rb', line 54

def R(&block)
  case state
  when CLOSED   ; begin; R!(); yield; ensure; close; end
  when READABLE ; yield
  when WRITABLE ; yield
  else          ; raise "unknown state: #{state}"
  end
end

#R!Object



36
37
38
39
40
41
42
43
# File 'lib/ccp/kvs/tokyo/state_machine.rb', line 36

def R!
  case state
  when CLOSED   ; open(HDB::OREADER); @state = READABLE
  when READABLE ; # NOP
  when WRITABLE ; # NOP
  else          ; raise "unknown state: #{state}"
  end
end

#stateObject



14
15
16
# File 'lib/ccp/kvs/tokyo/state_machine.rb', line 14

def state
  @state || CLOSED
end

#touchObject



72
73
74
# File 'lib/ccp/kvs/tokyo/state_machine.rb', line 72

def touch
  W() {}
end

#W(&block) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/ccp/kvs/tokyo/state_machine.rb', line 63

def W(&block)
  case state
  when CLOSED   ; begin; W!(); yield; ensure; close; end
  when READABLE ; raise "reopen from read to write is not permitted"
  when WRITABLE ; yield
  else          ; raise "unknown state: #{state}"
  end
end

#W!Object



45
46
47
48
49
50
51
52
# File 'lib/ccp/kvs/tokyo/state_machine.rb', line 45

def W!
  case state
  when CLOSED   ; open(HDB::OCREAT | HDB::OWRITER); @state = WRITABLE
  when READABLE ; C!; W!()
  when WRITABLE ; # NOP
  else          ; raise "unknown state: #{state}"
  end
end