Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajoute LOCK et UNLOCK #16

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/Server/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public static Command Create(string command, Database db)
"SET" => new SetCommand(db),
"GET" => new GetCommand(db),
"DEL" => new DelCommand(db),
"LOCK" => new LockCommand(db),
"UNLOCK" => new UnlockCommand(db),
_ => new UnknownCommand(db),
};
}
Expand Down Expand Up @@ -120,4 +122,54 @@ public override Item execute(params string[] args)
_db.Del(args[0]);
return new SimpleString("OK");
}
}

public class LockCommand : Command
{
public LockCommand(Database db) : base(db) { }

public override Item execute(params string[] args)
{
if (args.Length != 1)
return new SimpleError("Expected 1 argument");

_db.Lock();
var mut = _db.Get(args[0]);
if (mut == null || mut == "f")
_db.Set(args[0], "l");
_db.Unlock();

switch (mut)
{
case null:
return new SimpleString("OK");
case "f":
return new SimpleString("OK");
case "l":
return new SimpleError("Key is already locked");
default:
return new SimpleError("Key is already set");
}
}
}

public class UnlockCommand : Command
{
public UnlockCommand(Database db) : base(db) { }

public override Item execute(params string[] args)
{
if (args.Length != 1)
return new SimpleError("Expected 1 argument");

_db.Lock();
var mut = _db.Get(args[0]);
if (mut == "l") _db.Set(args[0], "f");
_db.Unlock();

if (mut != "l" && mut != "f")
return new SimpleError("Key is not a lock");

return new SimpleString("OK");
}
}