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 answerOK. If the record is locked by a different user, change nothing and answerLOCKED.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 answertrue, orfalseif there is no such field. If the record is locked by a different user, change nothing and answerLOCKED; this check happens before looking at the field. Deleting a record's last field removes the record together with its lock.LOCK key user: answerINVALIDif the record does not exist,ACQUIREDif the record is unlocked or already locked byuser, andWAITif another user holds the lock.UNLOCK key user: answerINVALIDif the record does not exist or another user holds the lock. Otherwise answerRELEASED, removing the lock ifuserholds it; an unlocked record also answersRELEASED.
SET on a key with no record creates the record.
- 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:
SETanswersOKandGET a xreturns the value1; missing records or fields give an empty string. The firstDELETEremoves the last field ofa, so the record disappears and the secondDELETEfinds no field and answersfalse.
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.