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
c9425153
Commit
c9425153
authored
Aug 13, 2024
by
amir.yosef
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make the Invoker independent from command's args
parent
10acd291
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
36 additions
and
25 deletions
+36
-25
CommandHandler.java
src/command/CommandHandler.java
+1
-2
CommandInvoker.java
src/command/CommandInvoker.java
+2
-3
EchoCommand.java
src/command/EchoCommand.java
+6
-1
GetCommand.java
src/command/GetCommand.java
+5
-1
PingCommand.java
src/command/PingCommand.java
+1
-3
SetCommand.java
src/command/SetCommand.java
+9
-4
UnknownCommand.java
src/command/UnknownCommand.java
+1
-2
CommandFactory.java
src/factory/CommandFactory.java
+6
-4
Factory.java
src/factory/Factory.java
+3
-1
ClientCommandHandler.java
src/handlers/ClientCommandHandler.java
+2
-4
No files found.
src/command/CommandHandler.java
View file @
c9425153
...
@@ -2,8 +2,7 @@ package command;
...
@@ -2,8 +2,7 @@ package command;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.util.List
;
public
interface
CommandHandler
{
public
interface
CommandHandler
{
void
execute
(
List
<
String
>
args
,
OutputStream
os
)
throws
IOException
;
void
execute
(
OutputStream
os
)
throws
IOException
;
}
}
src/command/CommandInvoker.java
View file @
c9425153
...
@@ -2,10 +2,9 @@ package command;
...
@@ -2,10 +2,9 @@ package command;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.util.List
;
public
class
CommandInvoker
{
public
class
CommandInvoker
{
public
static
void
invoke
(
CommandHandler
command
,
List
<
String
>
CommandAndArgs
,
OutputStream
os
)
throws
IOException
{
public
static
void
invoke
(
CommandHandler
command
,
OutputStream
os
)
throws
IOException
{
command
.
execute
(
CommandAndArgs
,
os
);
command
.
execute
(
os
);
}
}
}
}
src/command/EchoCommand.java
View file @
c9425153
...
@@ -7,8 +7,13 @@ import java.io.OutputStream;
...
@@ -7,8 +7,13 @@ 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
;
public
EchoCommand
(
List
<
String
>
args
)
{
this
.
args
=
args
;
}
@Override
@Override
public
void
execute
(
List
<
String
>
args
,
OutputStream
os
)
{
public
void
execute
(
OutputStream
os
)
{
try
{
try
{
os
.
write
(
Response
.
getResponse
(
args
.
getFirst
()));
os
.
write
(
Response
.
getResponse
(
args
.
getFirst
()));
os
.
flush
();
os
.
flush
();
...
...
src/command/GetCommand.java
View file @
c9425153
...
@@ -10,10 +10,14 @@ import java.util.List;
...
@@ -10,10 +10,14 @@ import java.util.List;
public
class
GetCommand
implements
CommandHandler
{
public
class
GetCommand
implements
CommandHandler
{
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
List
<
String
>
args
;
public
GetCommand
(
List
<
String
>
args
)
{
this
.
args
=
args
;
}
@Override
@Override
public
void
execute
(
List
<
String
>
args
,
OutputStream
os
)
{
public
void
execute
(
OutputStream
os
)
{
try
{
try
{
String
response
=
storage
.
get
(
args
.
getFirst
().
toLowerCase
());
String
response
=
storage
.
get
(
args
.
getFirst
().
toLowerCase
());
...
...
src/command/PingCommand.java
View file @
c9425153
...
@@ -5,13 +5,11 @@ import model.Command;
...
@@ -5,13 +5,11 @@ import model.Command;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.util.List
;
public
class
PingCommand
implements
CommandHandler
{
public
class
PingCommand
implements
CommandHandler
{
@Override
@Override
public
void
execute
(
List
<
String
>
args
,
OutputStream
os
)
{
public
void
execute
(
OutputStream
os
)
{
System
.
out
.
printf
(
"Processing PING args %s%n"
,
args
);
try
{
try
{
os
.
write
((
"+"
+
Command
.
PONG
.
getValue
()
+
"\r\n"
).
getBytes
());
os
.
write
((
"+"
+
Command
.
PONG
.
getValue
()
+
"\r\n"
).
getBytes
());
os
.
flush
();
os
.
flush
();
...
...
src/command/SetCommand.java
View file @
c9425153
...
@@ -10,10 +10,15 @@ import java.util.List;
...
@@ -10,10 +10,15 @@ import java.util.List;
import
java.util.Map
;
import
java.util.Map
;
public
class
SetCommand
implements
CommandHandler
{
public
class
SetCommand
implements
CommandHandler
{
private
final
Storage
redisStorage
=
Storage
.
getInstance
();
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
List
<
String
>
args
;
public
SetCommand
(
List
<
String
>
args
)
{
this
.
args
=
args
;
}
@Override
@Override
public
void
execute
(
List
<
String
>
args
,
OutputStream
os
)
{
public
void
execute
(
OutputStream
os
)
{
Map
<
String
,
String
>
commandsMap
=
new
HashMap
<>();
Map
<
String
,
String
>
commandsMap
=
new
HashMap
<>();
...
@@ -27,12 +32,12 @@ public class SetCommand implements CommandHandler {
...
@@ -27,12 +32,12 @@ public class SetCommand implements CommandHandler {
if
(
expiration
!=
null
)
{
if
(
expiration
!=
null
)
{
try
{
try
{
Long
expirationTime
=
Long
.
parseLong
(
expiration
);
Long
expirationTime
=
Long
.
parseLong
(
expiration
);
redisS
torage
.
save
(
args
.
getFirst
().
toLowerCase
(),
value
,
expirationTime
);
s
torage
.
save
(
args
.
getFirst
().
toLowerCase
(),
value
,
expirationTime
);
}
catch
(
NumberFormatException
e
)
{
}
catch
(
NumberFormatException
e
)
{
System
.
out
.
println
(
"NumberFormatException: "
+
e
.
getMessage
());
System
.
out
.
println
(
"NumberFormatException: "
+
e
.
getMessage
());
}
}
}
else
{
}
else
{
redisS
torage
.
save
(
args
.
getFirst
().
toLowerCase
(),
value
);
s
torage
.
save
(
args
.
getFirst
().
toLowerCase
(),
value
);
}
}
try
{
try
{
...
...
src/command/UnknownCommand.java
View file @
c9425153
...
@@ -2,11 +2,10 @@ package command;
...
@@ -2,11 +2,10 @@ package command;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.util.List
;
public
class
UnknownCommand
implements
CommandHandler
{
public
class
UnknownCommand
implements
CommandHandler
{
@Override
@Override
public
void
execute
(
List
<
String
>
args
,
OutputStream
os
)
{
public
void
execute
(
OutputStream
os
)
{
try
{
try
{
os
.
write
((
"-"
+
"Unknown Command"
+
"\r\n"
).
getBytes
());
os
.
write
((
"-"
+
"Unknown Command"
+
"\r\n"
).
getBytes
());
os
.
flush
();
os
.
flush
();
...
...
src/factory/CommandFactory.java
View file @
c9425153
...
@@ -3,6 +3,8 @@ package factory;
...
@@ -3,6 +3,8 @@ package factory;
import
command.*
;
import
command.*
;
import
model.Command
;
import
model.Command
;
import
java.util.List
;
public
class
CommandFactory
implements
Factory
{
public
class
CommandFactory
implements
Factory
{
private
static
final
class
FactoryHolder
{
private
static
final
class
FactoryHolder
{
...
@@ -18,12 +20,12 @@ public class CommandFactory implements Factory {
...
@@ -18,12 +20,12 @@ public class CommandFactory implements Factory {
}
}
@Override
@Override
public
CommandHandler
getCommand
(
Command
command
)
{
public
CommandHandler
getCommand
(
Command
command
,
List
<
String
>
args
)
{
return
switch
(
command
)
{
return
switch
(
command
)
{
case
PING
->
new
PingCommand
();
case
PING
->
new
PingCommand
();
case
ECHO
->
new
EchoCommand
();
case
ECHO
->
new
EchoCommand
(
args
);
case
SET
->
new
SetCommand
();
case
SET
->
new
SetCommand
(
args
);
case
GET
->
new
GetCommand
();
case
GET
->
new
GetCommand
(
args
);
// case REPLCONF -> new ReplConfCommand(replicaReceiver);
// case REPLCONF -> new ReplConfCommand(replicaReceiver);
// case PSYNC -> new FullResyncCommandProcessor(replicaSender);
// case PSYNC -> new FullResyncCommandProcessor(replicaSender);
// case WAIT -> new WaitCommandProcessor(replicaSender, replicaReceiver);
// case WAIT -> new WaitCommandProcessor(replicaSender, replicaReceiver);
...
...
src/factory/Factory.java
View file @
c9425153
...
@@ -4,6 +4,8 @@ package factory;
...
@@ -4,6 +4,8 @@ package factory;
import
command.CommandHandler
;
import
command.CommandHandler
;
import
model.Command
;
import
model.Command
;
import
java.util.List
;
public
interface
Factory
{
public
interface
Factory
{
CommandHandler
getCommand
(
Command
command
);
CommandHandler
getCommand
(
Command
command
,
List
<
String
>
args
);
}
}
src/handlers/ClientCommandHandler.java
View file @
c9425153
...
@@ -24,13 +24,11 @@ public class ClientCommandHandler {
...
@@ -24,13 +24,11 @@ public class ClientCommandHandler {
public
void
execute
()
{
public
void
execute
()
{
Command
command
=
CommandUtil
.
getCommand
(
commands
.
getFirst
());
Command
command
=
CommandUtil
.
getCommand
(
commands
.
getFirst
());
commands
.
removeFirst
();
commands
.
removeFirst
();
CommandHandler
commandProcessor
=
factory
.
getCommand
(
command
);
CommandHandler
commandProcessor
=
factory
.
getCommand
(
command
,
commands
);
try
{
try
{
CommandInvoker
.
invoke
(
commandProcessor
,
commands
,
os
);
CommandInvoker
.
invoke
(
commandProcessor
,
os
);
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
}
}
}
}
}
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