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
Hide 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
;
package
command
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
public
interface
CommandHandler
{
public
interface
CommandHandler
{
void
execute
(
OutputStream
os
)
throws
IOException
;
byte
[]
execute
(
)
throws
IOException
;
}
}
src/command/CommandInvoker.java
View file @
921ae604
package
command
;
package
command
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
public
class
CommandInvoker
{
public
class
CommandInvoker
{
public
static
void
invoke
(
CommandHandler
command
,
OutputStream
os
)
throws
IOException
{
public
static
byte
[]
invoke
(
CommandHandler
command
)
throws
IOException
{
command
.
execute
(
os
);
return
command
.
execute
(
);
}
}
}
}
src/command/EchoCommand.java
View file @
921ae604
...
@@ -2,23 +2,17 @@ package command;
...
@@ -2,23 +2,17 @@ package command;
import
util.Response
;
import
util.Response
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.util.List
;
import
java.util.List
;
public
class
EchoCommand
implements
CommandHandler
{
public
class
EchoCommand
implements
CommandHandler
{
private
final
List
<
String
>
args
;
private
final
List
<
String
>
args
;
public
EchoCommand
(
List
<
String
>
args
)
{
public
EchoCommand
(
List
<
String
>
args
)
{
this
.
args
=
args
;
this
.
args
=
args
;
}
}
@Override
@Override
public
void
execute
(
OutputStream
os
)
{
public
byte
[]
execute
()
{
try
{
return
(
Response
.
getResponse
(
args
.
getFirst
()));
os
.
write
(
Response
.
getResponse
(
args
.
getFirst
()));
os
.
flush
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
}
}
src/command/GetCommand.java
View file @
921ae604
...
@@ -3,8 +3,6 @@ package command;
...
@@ -3,8 +3,6 @@ package command;
import
storage.Storage
;
import
storage.Storage
;
import
util.Response
;
import
util.Response
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.util.List
;
import
java.util.List
;
...
@@ -17,19 +15,12 @@ public class GetCommand implements CommandHandler {
...
@@ -17,19 +15,12 @@ public class GetCommand implements CommandHandler {
}
}
@Override
@Override
public
void
execute
(
OutputStream
os
)
{
public
byte
[]
execute
(
)
{
try
{
String
response
=
storage
.
get
(
args
.
getFirst
().
toLowerCase
());
String
response
=
storage
.
get
(
args
.
getFirst
().
toLowerCase
());
if
(
response
==
null
||
response
.
isEmpty
()
||
response
.
isBlank
())
{
if
(
response
==
null
||
response
.
isEmpty
()
||
response
.
isBlank
())
{
return
(
"$-1\r\n"
.
getBytes
());
os
.
write
(
"$-1\r\n"
.
getBytes
());
os
.
flush
();
return
;
}
os
.
write
(
Response
.
getResponse
(
response
));
os
.
flush
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
return
(
Response
.
getResponse
(
response
));
}
}
}
}
src/command/PingCommand.java
View file @
921ae604
...
@@ -3,18 +3,10 @@ package command;
...
@@ -3,18 +3,10 @@ package command;
import
model.Command
;
import
model.Command
;
import
java.io.IOException
;
import
java.io.OutputStream
;
public
class
PingCommand
implements
CommandHandler
{
public
class
PingCommand
implements
CommandHandler
{
@Override
@Override
public
void
execute
(
OutputStream
os
)
{
public
byte
[]
execute
()
{
try
{
return
((
"+"
+
Command
.
PONG
.
getValue
()
+
"\r\n"
).
getBytes
());
os
.
write
((
"+"
+
Command
.
PONG
.
getValue
()
+
"\r\n"
).
getBytes
());
os
.
flush
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
}
}
\ No newline at end of file
src/command/SetCommand.java
View file @
921ae604
...
@@ -3,8 +3,6 @@ package command;
...
@@ -3,8 +3,6 @@ package command;
import
model.Command
;
import
model.Command
;
import
storage.Storage
;
import
storage.Storage
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -18,7 +16,7 @@ public class SetCommand implements CommandHandler {
...
@@ -18,7 +16,7 @@ public class SetCommand implements CommandHandler {
}
}
@Override
@Override
public
void
execute
(
OutputStream
os
)
{
public
byte
[]
execute
(
)
{
Map
<
String
,
String
>
commandsMap
=
new
HashMap
<>();
Map
<
String
,
String
>
commandsMap
=
new
HashMap
<>();
...
@@ -40,11 +38,7 @@ public class SetCommand implements CommandHandler {
...
@@ -40,11 +38,7 @@ public class SetCommand implements CommandHandler {
storage
.
save
(
args
.
getFirst
().
toLowerCase
(),
value
);
storage
.
save
(
args
.
getFirst
().
toLowerCase
(),
value
);
}
}
try
{
return
(
"+OK\r\n"
.
getBytes
());
os
.
write
(
"+OK\r\n"
.
getBytes
());
os
.
flush
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
}
}
src/command/UnknownCommand.java
View file @
921ae604
package
command
;
package
command
;
import
java.io.IOException
;
import
java.io.OutputStream
;
public
class
UnknownCommand
implements
CommandHandler
{
public
class
UnknownCommand
implements
CommandHandler
{
@Override
@Override
public
void
execute
(
OutputStream
os
)
{
public
byte
[]
execute
()
{
try
{
return
((
"-"
+
"Unknown Command"
+
"\r\n"
).
getBytes
());
os
.
write
((
"-"
+
"Unknown Command"
+
"\r\n"
).
getBytes
());
os
.
flush
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
}
}
src/handlers/ClientCommandHandler.java
View file @
921ae604
...
@@ -26,7 +26,9 @@ public class ClientCommandHandler {
...
@@ -26,7 +26,9 @@ public class ClientCommandHandler {
commands
.
removeFirst
();
commands
.
removeFirst
();
CommandHandler
commandProcessor
=
factory
.
getCommand
(
command
,
commands
);
CommandHandler
commandProcessor
=
factory
.
getCommand
(
command
,
commands
);
try
{
try
{
CommandInvoker
.
invoke
(
commandProcessor
,
os
);
byte
[]
result
=
CommandInvoker
.
invoke
(
commandProcessor
);
os
.
write
(
result
);
os
.
flush
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
}
...
...
src/storage/Storage.java
View file @
921ae604
...
@@ -62,6 +62,7 @@ public class Storage {
...
@@ -62,6 +62,7 @@ public class Storage {
timeToExpiration
.
remove
(
key
);
timeToExpiration
.
remove
(
key
);
currentTimeForKey
.
remove
(
key
);
currentTimeForKey
.
remove
(
key
);
}
}
void
runCachePolicy
()
{
void
runCachePolicy
()
{
storage
.
runMaintenance
();
storage
.
runMaintenance
();
}
}
...
...
src/storage/StorageManager.java
View file @
921ae604
...
@@ -14,6 +14,7 @@ public class StorageManager {
...
@@ -14,6 +14,7 @@ public class StorageManager {
this
.
scheduler
=
Executors
.
newSingleThreadScheduledExecutor
();
this
.
scheduler
=
Executors
.
newSingleThreadScheduledExecutor
();
scheduler
.
scheduleAtFixedRate
(
this
::
performMaintenance
,
2
,
2
,
TimeUnit
.
MINUTES
);
scheduler
.
scheduleAtFixedRate
(
this
::
performMaintenance
,
2
,
2
,
TimeUnit
.
MINUTES
);
}
}
private
void
performMaintenance
()
{
private
void
performMaintenance
()
{
storage
.
runCachePolicy
();
storage
.
runCachePolicy
();
System
.
out
.
println
(
"Maintenance performed at: "
+
Date
.
from
(
java
.
time
.
Clock
.
systemUTC
().
instant
()));
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