Meta questions

Record Database With Locks

MetaEasy

An in-memory database stores records. A record is addressed by a key and holds named fields, each with a string value. A record can also be locked exclusively by one user.

Part 1: Records, fields and locks

You are given a list of queries. Each query is a list of strings: a command followed by its arguments. Process the queries in order and return one answer string per query.

  • SET key field value user: store or replace the field and answer OK. If the record is locked by a different user, change nothing and answer LOCKED.
  • GET key field: answer the field's value, or "" if the record or field does not exist. Locks never block reads.
  • DELETE key field user: remove the field and answer true, or false if there is no such field. If the record is locked by a different user, change nothing and answer LOCKED; this check happens before looking at the field. Deleting a record's last field removes the record together with its lock.
  • LOCK key user: answer INVALID if the record does not exist, ACQUIRED if the record is unlocked or already locked by user, and WAIT if another user holds the lock.
  • UNLOCK key user: answer INVALID if the record does not exist or another user holds the lock. Otherwise answer RELEASED, removing the lock if user holds it; an unlocked record also answers RELEASED.

SET on a key with no record creates the record.

  1. Example 1
    queries
    [["SET","a","x","1","u1"],["GET","a","x"],["GET","a","y"],["GET","b","x"],["DELETE","a","x","u1"],["DELETE","a","x","u1"],["GET","a","x"]]
    Output
    ["OK","1","","","true","false",""]

    Why: SET answers OK and GET a x returns the value 1; missing records or fields give an empty string. The first DELETE removes the last field of a, so the record disappears and the second DELETE finds no field and answers false.

Constraints

  • 1 <= len(queries) <= 10^5
  • Each query is ['SET', key, field, value, user], ['GET', key, field], ['DELETE', key, field, user], ['LOCK', key, user] or ['UNLOCK', key, user]
  • 1 <= len(key), len(field), len(value), len(user) <= 20
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question