Class: Sequence

Inherits:
Object
  • Object
show all
Defined in:
backend/app/model/sequence.rb

Constant Summary collapse

QUEUE =

DEPRECATED: this is dead code since ArchivesSpace v2

java.util.concurrent.ArrayBlockingQueue.new(1024)

Class Method Summary collapse

Class Method Details

._handle_get(request, initialised_sequences) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'backend/app/model/sequence.rb', line 19

def self._handle_get(request, initialised_sequences)
  sequence = request[:sequence]
  DB.open(true) do |db|
    if !initialised_sequences[sequence]
      initialised_sequences[sequence] = true

      DB.attempt {
        db[:sequence].insert(:sequence_name => sequence.to_s, :value => 0)
        request[:result].complete(0)
        return
      }.and_if_constraint_fails {
        # Sequence is already defined, which is fine
      }
    end

    # If we make it to here, the sequence already exists and needs to be incremented
    1000.times do
      old_value = db[:sequence].filter(:sequence_name => sequence.to_s).get(:value)
      updated_count = db[:sequence].filter(:sequence_name => sequence.to_s, :value => old_value).
                        update(:value => old_value + 1)

      if updated_count == 0
        # Need to retry
        sleep(0.01)
      elsif updated_count == 1
        request[:result].complete(old_value + 1)
        return
      else
        Log.error("Unrecognised response from SQL update when generating next element in sequence '#{sequence}': #{updated_count}")
      end
    end

    Log.error("Gave up trying to generate a sequence number for: '#{sequence}'")
    request[:result].completeExceptionally(java.lang.RuntimeException.new("Gave up trying to generate a sequence number for: '#{sequence}'"))
  end
end

.get(sequence) ⇒ Object



13
14
15
16
17
# File 'backend/app/model/sequence.rb', line 13

def self.get(sequence)
  result = java.util.concurrent.CompletableFuture.new
  QUEUE.add({action: :get, sequence: sequence, result: result})
  result.get
end

.init(sequence, value) ⇒ Object



6
7
8
9
10
11
# File 'backend/app/model/sequence.rb', line 6

def self.init(sequence, value)
  result = java.util.concurrent.CompletableFuture.new
  QUEUE.add({action: :init, sequence: sequence, value: value, result: result})
  result.get
  nil
end