Class: Neo4j::Server::CypherTransaction

Inherits:
Object
  • Object
show all
Includes:
Resource, Transaction::Instance
Defined in:
lib/neo4j-server/cypher_transaction.rb

Overview

The CypherTransaction object lifecycle is as follows:

  • It is initialized with the transactional endpoint URL and the connection object to use for communication. It does not communicate with the server to create this.

  • The first query within the transaction sets the commit and execution addresses, :commit_url and :query_url.

  • At any time, ‘failure` can be called to mark a transaction failed and trigger a rollback upon closure.

  • ‘close` is called to end the transaction. It calls `commit` or `delete`.

If a transaction is created and then closed without performing any queries, an OpenStruct is returned that behaves like a successfully closed query.

Constant Summary collapse

ROW_REST =
%w(row REST)

Instance Attribute Summary collapse

Attributes included from Resource

#resource_data, #resource_url

Instance Method Summary collapse

Methods included from Resource

#convert_from_json_value, #expect_response_code!, #handle_response_error!, #init_resource_data, #resource_headers, #resource_url_id, #response_exception, #wrap_resource

Methods included from Transaction::Instance

#acquire_read_lock, #acquire_write_lock, #autoclosed!, #autoclosed?, #close, #expired?, #failed?, #mark_expired, #mark_failed, #pop_nested!, #push_nested!, #register_instance, #transient_failures_autoclose?

Constructor Details

#initialize(url, session_connection) ⇒ CypherTransaction

Returns a new instance of CypherTransaction.



16
17
18
19
20
# File 'lib/neo4j-server/cypher_transaction.rb', line 16

def initialize(url, session_connection)
  @base_url = url
  @connection = session_connection
  register_instance
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



14
15
16
# File 'lib/neo4j-server/cypher_transaction.rb', line 14

def base_url
  @base_url
end

#commit_urlObject (readonly)

Returns the value of attribute commit_url.



14
15
16
# File 'lib/neo4j-server/cypher_transaction.rb', line 14

def commit_url
  @commit_url
end

#connectionObject (readonly)

Returns the value of attribute connection.



14
15
16
# File 'lib/neo4j-server/cypher_transaction.rb', line 14

def connection
  @connection
end

#query_urlObject (readonly)

Returns the value of attribute query_url.



14
15
16
# File 'lib/neo4j-server/cypher_transaction.rb', line 14

def query_url
  @query_url
end

Instance Method Details

#_query(cypher_query, params = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/neo4j-server/cypher_transaction.rb', line 23

def _query(cypher_query, params = nil)
  fail 'Transaction expired, unable to perform query' if expired?
  statement = {statement: cypher_query, parameters: params, resultDataContents: ROW_REST}
  body = {statements: [statement]}

  response = @query_url ? query(body) : start(body)

  create_cypher_response(response)
end

#commitObject



54
55
56
57
58
# File 'lib/neo4j-server/cypher_transaction.rb', line 54

def commit
  return empty_response if !@commit_url || expired?

  request(:post, @commit_url, 200, nil, resource_headers)
end

#deleteObject



48
49
50
51
52
# File 'lib/neo4j-server/cypher_transaction.rb', line 48

def delete
  return empty_response if !@commit_url || expired?

  request(:delete, @query_url, 200, nil, resource_headers)
end

#query(body) ⇒ Object



44
45
46
# File 'lib/neo4j-server/cypher_transaction.rb', line 44

def query(body)
  request(:post, @query_url, 200, body)
end

#start(body) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/neo4j-server/cypher_transaction.rb', line 33

def start(body)
  request(:post, @base_url, 201, body).tap do |response|
    @commit_url = response.body[:commit]
    @query_url = response.headers[:Location]

    fail "NO ENDPOINT URL #{connection} : HEAD: #{response.headers.inspect}" if !@query_url || @query_url.empty?

    init_resource_data(response.body, @base_url)
  end
end