SHARE
TWEET

Untitled

a guest Feb 11th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # creates a uniquely-named file
  2. new_path = do_work()
  3.  
  4. old_path = database.query('select old path')
  5. unlink(old_path)
  6.  
  7. database.query('insert new path')
  8.    
  9. CREATE TABLE paths (
  10.     used_flag boolean not null default 'f',
  11.     when_entered timestamptz not null default current_timestamp,
  12.     data text not null
  13. );
  14.  
  15. INSERT INTO paths (data) VALUES
  16. ('aa'),('bb'),('cc'),('dd');
  17.    
  18. SESSION 1             SESSION2         SESSION 3
  19.  
  20. BEGIN;
  21.                                        BEGIN;
  22.  
  23. UPDATE      paths
  24.     SET     used_flag = TRUE
  25.     WHERE   used_flag = FALSE
  26.     RETURNING data;
  27.  
  28.                       BEGIN;
  29.  
  30.                       INSERT INTO
  31.                       paths(data)
  32.                       VALUES
  33.                       ('ee'),('ff');      
  34.  
  35.                       COMMIT;              
  36.                                        UPDATE      paths
  37.                                            SET     used_flag = TRUE
  38.                                            WHERE   used_flag = FALSE
  39.                                            RETURNING data;
  40.  
  41.  
  42.                       BEGIN;
  43.  
  44.                       INSERT INTO
  45.                       paths(data)
  46.                       VALUES
  47.                       ('gg'),('hh');      
  48.  
  49.                       COMMIT;        
  50.  
  51. COMMIT;
  52.    
  53. UPDATE      paths
  54.     SET     used_flag = TRUE
  55.     WHERE entry_id = (
  56.         SELECT entry_id
  57.         FROM paths
  58.         WHERE used_flag = FALSE
  59.         ORDER BY when_entered
  60.         LIMIT 1
  61.     )
  62.     AND used_flag = FALSE
  63.     RETURNING data;
  64.    
  65. UPDATE      paths
  66.     SET     used_flag = TRUE
  67.     WHERE entry_id = (
  68.         SELECT min(entry_id)
  69.         FROM paths
  70.         WHERE used_flag = FALSE
  71.     )
  72.     AND used_flag = FALSE
  73.     RETURNING data;
  74.    
  75. old_path = database.query('
  76.     UPDATE      paths
  77.         SET     used_flag = TRUE
  78.         WHERE   used_flag = FALSE
  79.         RETURNS data');
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top