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
f74874d4
Commit
f74874d4
authored
Aug 20, 2024
by
amir.yosef
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding Validator class for arguments
parent
62cf7a72
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
18 deletions
+78
-18
CommandValidator.java
src/command/CommandValidator.java
+56
-0
GetCommand.java
src/command/GetCommand.java
+4
-1
SetCommand.java
src/command/SetCommand.java
+18
-17
No files found.
src/command/CommandValidator.java
0 → 100644
View file @
f74874d4
package
command
;
import
model.Command
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
import
java.util.stream.IntStream
;
public
class
CommandValidator
{
private
CommandValidator
()
{
}
private
static
final
class
InstanceHolder
{
private
static
final
CommandValidator
instance
=
new
CommandValidator
();
}
public
static
CommandValidator
getInstance
()
{
return
InstanceHolder
.
instance
;
}
public
boolean
validateSetCommand
(
List
<
String
>
args
)
{
if
(
args
==
null
||
args
.
isEmpty
())
{
return
false
;
}
if
(
args
.
size
()
<
2
)
{
return
false
;
}
if
(
args
.
size
()
>
2
)
{
Map
<
String
,
String
>
commandsMap
=
IntStream
.
iterate
(
2
,
i
->
i
<
args
.
size
(),
i
->
i
+
2
)
.
boxed
()
.
collect
(
Collectors
.
toMap
(
i
->
args
.
get
(
i
).
toLowerCase
(),
i
->
args
.
get
(
i
+
1
),
(
a
,
b
)
->
b
));
String
expiration
=
commandsMap
.
get
(
Command
.
PX
.
getValue
().
toLowerCase
());
if
(
expiration
!=
null
)
{
try
{
Long
.
parseLong
(
expiration
);
}
catch
(
NumberFormatException
e
)
{
return
false
;
}
}
}
return
true
;
}
public
boolean
validateGetCommand
(
List
<
String
>
args
)
{
return
args
!=
null
&&
args
.
size
()
==
1
;
}
}
src/command/GetCommand.java
View file @
f74874d4
...
@@ -9,6 +9,7 @@ import java.util.List;
...
@@ -9,6 +9,7 @@ import java.util.List;
public
class
GetCommand
implements
CommandExecutable
<
byte
[]>
{
public
class
GetCommand
implements
CommandExecutable
<
byte
[]>
{
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
List
<
String
>
args
;
private
final
List
<
String
>
args
;
private
final
CommandValidator
validator
=
CommandValidator
.
getInstance
();
public
GetCommand
(
List
<
String
>
args
)
{
public
GetCommand
(
List
<
String
>
args
)
{
this
.
args
=
args
;
this
.
args
=
args
;
...
@@ -16,7 +17,9 @@ public class GetCommand implements CommandExecutable<byte[]> {
...
@@ -16,7 +17,9 @@ public class GetCommand implements CommandExecutable<byte[]> {
@Override
@Override
public
byte
[]
execute
()
{
public
byte
[]
execute
()
{
if
(!
validator
.
validateGetCommand
(
args
))
{
return
"wrong args"
.
getBytes
();
}
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
());
return
(
"$-1\r\n"
.
getBytes
());
...
...
src/command/SetCommand.java
View file @
f74874d4
...
@@ -11,6 +11,7 @@ import java.util.stream.IntStream;
...
@@ -11,6 +11,7 @@ import java.util.stream.IntStream;
public
class
SetCommand
implements
CommandExecutable
<
byte
[]>
{
public
class
SetCommand
implements
CommandExecutable
<
byte
[]>
{
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
Storage
storage
=
Storage
.
getInstance
();
private
final
List
<
String
>
args
;
private
final
List
<
String
>
args
;
private
final
CommandValidator
validator
=
CommandValidator
.
getInstance
();
public
SetCommand
(
List
<
String
>
args
)
{
public
SetCommand
(
List
<
String
>
args
)
{
this
.
args
=
args
;
this
.
args
=
args
;
...
@@ -18,26 +19,26 @@ public class SetCommand implements CommandExecutable<byte[]> {
...
@@ -18,26 +19,26 @@ public class SetCommand implements CommandExecutable<byte[]> {
@Override
@Override
public
byte
[]
execute
()
{
public
byte
[]
execute
()
{
if
(!
args
.
isEmpty
())
{
if
(!
validator
.
validateSetCommand
(
args
))
{
Map
<
String
,
String
>
commandsMap
=
IntStream
.
iterate
(
2
,
i
->
i
<
args
.
size
(),
i
->
i
+
2
)
return
"wrong args"
.
getBytes
();
.
boxed
()
}
.
collect
(
Collectors
.
toMap
(
i
->
args
.
get
(
i
).
toLowerCase
(),
i
->
args
.
get
(
i
+
1
),
(
a
,
b
)
->
b
));
String
value
=
args
.
get
(
1
);
Map
<
String
,
String
>
commandsMap
=
IntStream
.
iterate
(
2
,
i
->
i
<
args
.
size
(),
i
->
i
+
2
)
String
expiration
=
commandsMap
.
get
(
Command
.
PX
.
getValue
().
toLowerCase
());
.
boxed
()
if
(
expiration
!=
null
)
{
.
collect
(
Collectors
.
toMap
(
i
->
args
.
get
(
i
).
toLowerCase
(),
i
->
args
.
get
(
i
+
1
),
(
a
,
b
)
->
b
));
try
{
Long
expirationTime
=
Long
.
parseLong
(
expiration
);
String
value
=
args
.
get
(
1
);
storage
.
save
(
args
.
getFirst
().
toLowerCase
(),
value
,
expirationTime
);
String
expiration
=
commandsMap
.
get
(
Command
.
PX
.
getValue
().
toLowerCase
());
}
catch
(
NumberFormatException
e
)
{
if
(
expiration
!=
null
)
{
System
.
out
.
println
(
"NumberFormatException: "
+
e
.
getMessage
());
try
{
}
Long
expirationTime
=
Long
.
parseLong
(
expiration
);
}
else
{
storage
.
save
(
args
.
getFirst
().
toLowerCase
(),
value
,
expirationTime
);
storage
.
save
(
args
.
getFirst
().
toLowerCase
(),
value
);
}
catch
(
NumberFormatException
e
)
{
System
.
out
.
println
(
"NumberFormatException: "
+
e
.
getMessage
());
}
}
return
(
"+OK\r\n"
.
getBytes
());
}
else
{
}
else
{
return
"wrong args"
.
getBytes
(
);
storage
.
save
(
args
.
getFirst
().
toLowerCase
(),
value
);
}
}
return
(
"+OK\r\n"
.
getBytes
());
}
}
}
}
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