Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
K
key_value-server
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
amir.yosef
key_value-server
Commits
921ae604
Commit
921ae604
authored
Aug 13, 2024
by
amir.yosef
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Applying Commands Response correctly
parent
988c6bf3
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
23 additions
and
57 deletions
+23
-57
CommandHandler.java
src/command/CommandHandler.java
+1
-2
CommandInvoker.java
src/command/CommandInvoker.java
+2
-3
EchoCommand.java
src/command/EchoCommand.java
+3
-9
GetCommand.java
src/command/GetCommand.java
+5
-14
PingCommand.java
src/command/PingCommand.java
+2
-10
SetCommand.java
src/command/SetCommand.java
+3
-9
UnknownCommand.java
src/command/UnknownCommand.java
+2
-9
ClientCommandHandler.java
src/handlers/ClientCommandHandler.java
+3
-1
Storage.java
src/storage/Storage.java
+1
-0
StorageManager.java
src/storage/StorageManager.java
+1
-0
No files found.
src/command/CommandHandler.java
View file @
921ae604
package
command
;
import
java.io.IOException
;
import
java.io.OutputStream
;
public
interface
CommandHandler
{
void
execute
(
OutputStream
os
)
throws
IOException
;
byte
[]
execute
(
)
throws
IOException
;
}
src/command/CommandInvoker.java
View file @
921ae604
package
command
;
import
java.io.IOException
;
import
java.io.OutputStream
;
public
class
CommandInvoker
{
public
static
void
invoke
(
CommandHandler
command
,
OutputStream
os
)
throws
IOException
{
command
.
execute
(
os
);
public
static
byte
[]
invoke
(
CommandHandler
command
)
throws
IOException
{
return
command
.
execute
(
);
}
}
src/command/EchoCommand.java
View file @
921ae604
...
...
@@ -2,23 +2,17 @@ package command;
import
util.Response
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.util.List
;
public
class
EchoCommand
implements
CommandHandler
{
private
final
List
<
String
>
args
;
public
EchoCommand
(
List
<
String
>
args
)
{
this
.
args
=
args
;
}
@Override
public
void
execute
(
OutputStream
os
)
{
try
{
os
.
write
(
Response
.
getResponse
(
args
.
getFirst
()));
os
.
flush
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
public
byte
[]
execute
()
{
return
(
Response
.
getResponse
(
args
.
getFirst
()));
}
}
src/command/GetCommand.java
View file @
921ae604
...
...
@@ -3,8 +3,6 @@ package command;
import
storage.Storage
;
import
util.Response
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.util.List
;
...
...
@@ -17,19 +15,12 @@ public class GetCommand implements CommandHandler {
}
@Override
public
void
execute
(
OutputStream
os
)
{
public
byte
[]
execute
(
)
{
try
{
String
response
=
storage
.
get
(
args
.
getFirst
().
toLowerCase
());
if
(
response
==
null
||
response
.
isEmpty
()
||
response
.
isBlank
())
{
os
.
write
(
"$-1\r\n"
.
getBytes
());
os
.
flush
();
return
;
}
os
.
write
(
Response
.
getResponse
(
response
));
os
.
flush
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
return
(
"$-1\r\n"
.
getBytes
());
}
return
(
Response
.
getResponse
(
response
));
}
}
src/command/PingCommand.java
View file @
921ae604
...
...
@@ -3,18 +3,10 @@ package command;
import
model.Command
;
import
java.io.IOException
;
import
java.io.OutputStream
;
public
class
PingCommand
implements
CommandHandler
{
@Override
public
void
execute
(
OutputStream
os
)
{
try
{
os
.
write
((
"+"
+
Command
.
PONG
.
getValue
()
+
"\r\n"
).
getBytes
());
os
.
flush
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
public
byte
[]
execute
()
{
return
((
"+"
+
Command
.
PONG
.
getValue
()
+
"\r\n"
).
getBytes
());
}
}
\ No newline at end of file
src/command/SetCommand.java
View file @
921ae604
...
...
@@ -3,8 +3,6 @@ package command;
import
model.Command
;
import
storage.Storage
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -18,7 +16,7 @@ public class SetCommand implements CommandHandler {
}
@Override
public
void
execute
(
OutputStream
os
)
{
public
byte
[]
execute
(
)
{
Map
<
String
,
String
>
commandsMap
=
new
HashMap
<>();
...
...
@@ -40,11 +38,7 @@ public class SetCommand implements CommandHandler {
storage
.
save
(
args
.
getFirst
().
toLowerCase
(),
value
);
}
try
{
os
.
write
(
"+OK\r\n"
.
getBytes
());
os
.
flush
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
return
(
"+OK\r\n"
.
getBytes
());
}
}
src/command/UnknownCommand.java
View file @
921ae604
package
command
;
import
java.io.IOException
;
import
java.io.OutputStream
;
public
class
UnknownCommand
implements
CommandHandler
{
@Override
public
void
execute
(
OutputStream
os
)
{
try
{
os
.
write
((
"-"
+
"Unknown Command"
+
"\r\n"
).
getBytes
());
os
.
flush
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
public
byte
[]
execute
()
{
return
((
"-"
+
"Unknown Command"
+
"\r\n"
).
getBytes
());
}
}
src/handlers/ClientCommandHandler.java
View file @
921ae604
...
...
@@ -26,7 +26,9 @@ public class ClientCommandHandler {
commands
.
removeFirst
();
CommandHandler
commandProcessor
=
factory
.
getCommand
(
command
,
commands
);
try
{
CommandInvoker
.
invoke
(
commandProcessor
,
os
);
byte
[]
result
=
CommandInvoker
.
invoke
(
commandProcessor
);
os
.
write
(
result
);
os
.
flush
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
...
...
src/storage/Storage.java
View file @
921ae604
...
...
@@ -62,6 +62,7 @@ public class Storage {
timeToExpiration
.
remove
(
key
);
currentTimeForKey
.
remove
(
key
);
}
void
runCachePolicy
()
{
storage
.
runMaintenance
();
}
...
...
src/storage/StorageManager.java
View file @
921ae604
...
...
@@ -14,6 +14,7 @@ public class StorageManager {
this
.
scheduler
=
Executors
.
newSingleThreadScheduledExecutor
();
scheduler
.
scheduleAtFixedRate
(
this
::
performMaintenance
,
2
,
2
,
TimeUnit
.
MINUTES
);
}
private
void
performMaintenance
()
{
storage
.
runCachePolicy
();
System
.
out
.
println
(
"Maintenance performed at: "
+
Date
.
from
(
java
.
time
.
Clock
.
systemUTC
().
instant
()));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment