Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
StackExchange.Redis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
tsai
StackExchange.Redis
Commits
fb02b2f7
Commit
fb02b2f7
authored
Mar 11, 2018
by
Nick Craver
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup: RedisKey
parent
b1b65583
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
87 deletions
+66
-87
RedisKey.cs
StackExchange.Redis/StackExchange/Redis/RedisKey.cs
+66
-87
No files found.
StackExchange.Redis/StackExchange/Redis/RedisKey.cs
View file @
fb02b2f7
...
@@ -13,14 +13,12 @@ public struct RedisKey : IEquatable<RedisKey>
...
@@ -13,14 +13,12 @@ public struct RedisKey : IEquatable<RedisKey>
private
readonly
object
keyValue
;
// always either a string or a byte[]
private
readonly
object
keyValue
;
// always either a string or a byte[]
internal
RedisKey
(
byte
[]
keyPrefix
,
object
keyValue
)
internal
RedisKey
(
byte
[]
keyPrefix
,
object
keyValue
)
{
{
this
.
keyPrefix
=
(
keyPrefix
!=
null
&&
keyPrefix
.
Length
==
0
)
?
null
:
keyPrefix
;
this
.
keyPrefix
=
keyPrefix
?.
Length
==
0
?
null
:
keyPrefix
;
this
.
keyValue
=
keyValue
;
this
.
keyValue
=
keyValue
;
}
}
internal
RedisKey
AsPrefix
()
internal
RedisKey
AsPrefix
()
=>
new
RedisKey
((
byte
[])
this
,
null
);
{
return
new
RedisKey
((
byte
[])
this
,
null
);
}
internal
bool
IsNull
=>
keyPrefix
==
null
&&
keyValue
==
null
;
internal
bool
IsNull
=>
keyPrefix
==
null
&&
keyValue
==
null
;
internal
bool
IsEmpty
internal
bool
IsEmpty
...
@@ -40,96 +38,86 @@ internal bool IsEmpty
...
@@ -40,96 +38,86 @@ internal bool IsEmpty
/// <summary>
/// <summary>
/// Indicate whether two keys are not equal
/// Indicate whether two keys are not equal
/// </summary>
/// </summary>
public
static
bool
operator
!=(
RedisKey
x
,
RedisKey
y
)
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
{
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
return
!(
x
==
y
);
public
static
bool
operator
!=(
RedisKey
x
,
RedisKey
y
)
=>
!(
x
==
y
);
}
/// <summary>
/// <summary>
/// Indicate whether two keys are not equal
/// Indicate whether two keys are not equal
/// </summary>
/// </summary>
public
static
bool
operator
!=(
string
x
,
RedisKey
y
)
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
{
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
return
!(
x
==
y
);
public
static
bool
operator
!=(
string
x
,
RedisKey
y
)
=>
!(
x
==
y
);
}
/// <summary>
/// <summary>
/// Indicate whether two keys are not equal
/// Indicate whether two keys are not equal
/// </summary>
/// </summary>
public
static
bool
operator
!=(
byte
[]
x
,
RedisKey
y
)
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
{
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
return
!(
x
==
y
);
public
static
bool
operator
!=(
byte
[]
x
,
RedisKey
y
)
=>
!(
x
==
y
);
}
/// <summary>
/// <summary>
/// Indicate whether two keys are not equal
/// Indicate whether two keys are not equal
/// </summary>
/// </summary>
public
static
bool
operator
!=(
RedisKey
x
,
string
y
)
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
{
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
return
!(
x
==
y
);
public
static
bool
operator
!=(
RedisKey
x
,
string
y
)
=>
!(
x
==
y
);
}
/// <summary>
/// <summary>
/// Indicate whether two keys are not equal
/// Indicate whether two keys are not equal
/// </summary>
/// </summary>
public
static
bool
operator
!=(
RedisKey
x
,
byte
[]
y
)
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
{
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
return
!(
x
==
y
);
public
static
bool
operator
!=(
RedisKey
x
,
byte
[]
y
)
=>
!(
x
==
y
);
}
/// <summary>
/// <summary>
/// Indicate whether two keys are equal
/// Indicate whether two keys are equal
/// </summary>
/// </summary>
public
static
bool
operator
==(
RedisKey
x
,
RedisKey
y
)
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
{
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
return
CompositeEquals
(
x
.
keyPrefix
,
x
.
keyValue
,
y
.
keyPrefix
,
y
.
keyValue
);
public
static
bool
operator
==(
RedisKey
x
,
RedisKey
y
)
=>
CompositeEquals
(
x
.
keyPrefix
,
x
.
keyValue
,
y
.
keyPrefix
,
y
.
keyValue
);
}
/// <summary>
/// <summary>
/// Indicate whether two keys are equal
/// Indicate whether two keys are equal
/// </summary>
/// </summary>
public
static
bool
operator
==(
string
x
,
RedisKey
y
)
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
{
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
return
CompositeEquals
(
null
,
x
,
y
.
keyPrefix
,
y
.
keyValue
);
public
static
bool
operator
==(
string
x
,
RedisKey
y
)
=>
CompositeEquals
(
null
,
x
,
y
.
keyPrefix
,
y
.
keyValue
);
}
/// <summary>
/// <summary>
/// Indicate whether two keys are equal
/// Indicate whether two keys are equal
/// </summary>
/// </summary>
public
static
bool
operator
==(
byte
[]
x
,
RedisKey
y
)
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
{
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
return
CompositeEquals
(
null
,
x
,
y
.
keyPrefix
,
y
.
keyValue
);
public
static
bool
operator
==(
byte
[]
x
,
RedisKey
y
)
=>
CompositeEquals
(
null
,
x
,
y
.
keyPrefix
,
y
.
keyValue
);
}
/// <summary>
/// <summary>
/// Indicate whether two keys are equal
/// Indicate whether two keys are equal
/// </summary>
/// </summary>
public
static
bool
operator
==(
RedisKey
x
,
string
y
)
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
{
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
return
CompositeEquals
(
x
.
keyPrefix
,
x
.
keyValue
,
null
,
y
);
public
static
bool
operator
==(
RedisKey
x
,
string
y
)
=>
CompositeEquals
(
x
.
keyPrefix
,
x
.
keyValue
,
null
,
y
);
}
/// <summary>
/// <summary>
/// Indicate whether two keys are equal
/// Indicate whether two keys are equal
/// </summary>
/// </summary>
public
static
bool
operator
==(
RedisKey
x
,
byte
[]
y
)
/// <param name="x">The first <see cref="RedisChannel"/> to compare.</param>
{
/// <param name="y">The second <see cref="RedisChannel"/> to compare.</param>
return
CompositeEquals
(
x
.
keyPrefix
,
x
.
keyValue
,
null
,
y
);
public
static
bool
operator
==(
RedisKey
x
,
byte
[]
y
)
=>
CompositeEquals
(
x
.
keyPrefix
,
x
.
keyValue
,
null
,
y
);
}
/// <summary>
/// <summary>
/// See Object.Equals
/// See Object.Equals
/// </summary>
/// </summary>
/// <param name="obj">The <see cref="RedisKey"/> to compare to.</param>
public
override
bool
Equals
(
object
obj
)
public
override
bool
Equals
(
object
obj
)
{
{
if
(
obj
is
RedisKey
)
if
(
obj
is
RedisKey
other
)
{
{
var
other
=
(
RedisKey
)
obj
;
return
CompositeEquals
(
keyPrefix
,
keyValue
,
other
.
keyPrefix
,
other
.
keyValue
);
return
CompositeEquals
(
this
.
keyPrefix
,
this
.
keyValue
,
other
.
keyPrefix
,
other
.
keyValue
);
}
}
if
(
obj
is
string
||
obj
is
byte
[])
if
(
obj
is
string
||
obj
is
byte
[])
{
{
return
CompositeEquals
(
this
.
keyPrefix
,
this
.
keyValue
,
null
,
obj
);
return
CompositeEquals
(
keyPrefix
,
keyValue
,
null
,
obj
);
}
}
return
false
;
return
false
;
}
}
...
@@ -137,14 +125,12 @@ public override bool Equals(object obj)
...
@@ -137,14 +125,12 @@ public override bool Equals(object obj)
/// <summary>
/// <summary>
/// Indicate whether two keys are equal
/// Indicate whether two keys are equal
/// </summary>
/// </summary>
public
bool
Equals
(
RedisKey
other
)
/// <param name="other">The <see cref="RedisKey"/> to compare to.</param>
{
public
bool
Equals
(
RedisKey
other
)
=>
CompositeEquals
(
keyPrefix
,
keyValue
,
other
.
keyPrefix
,
other
.
keyValue
);
return
CompositeEquals
(
this
.
keyPrefix
,
this
.
keyValue
,
other
.
keyPrefix
,
other
.
keyValue
);
}
private
static
bool
CompositeEquals
(
byte
[]
keyPrefix0
,
object
keyValue0
,
byte
[]
keyPrefix1
,
object
keyValue1
)
private
static
bool
CompositeEquals
(
byte
[]
keyPrefix0
,
object
keyValue0
,
byte
[]
keyPrefix1
,
object
keyValue1
)
{
{
if
(
RedisValue
.
Equals
(
keyPrefix0
,
keyPrefix1
))
if
(
RedisValue
.
Equals
(
keyPrefix0
,
keyPrefix1
))
{
{
if
(
keyValue0
==
keyValue1
)
return
true
;
// ref equal
if
(
keyValue0
==
keyValue1
)
return
true
;
// ref equal
if
(
keyValue0
==
null
||
keyValue1
==
null
)
return
false
;
// null vs non-null
if
(
keyValue0
==
null
||
keyValue1
==
null
)
return
false
;
// null vs non-null
...
@@ -161,7 +147,7 @@ private static bool CompositeEquals(byte[] keyPrefix0, object keyValue0, byte[]
...
@@ -161,7 +147,7 @@ private static bool CompositeEquals(byte[] keyPrefix0, object keyValue0, byte[]
/// </summary>
/// </summary>
public
override
int
GetHashCode
()
public
override
int
GetHashCode
()
{
{
int
chk0
=
keyPrefix
==
null
?
0
:
RedisValue
.
GetHashCode
(
this
.
keyPrefix
),
int
chk0
=
keyPrefix
==
null
?
0
:
RedisValue
.
GetHashCode
(
keyPrefix
),
chk1
=
keyValue
is
string
?
keyValue
.
GetHashCode
()
:
RedisValue
.
GetHashCode
((
byte
[])
keyValue
);
chk1
=
keyValue
is
string
?
keyValue
.
GetHashCode
()
:
RedisValue
.
GetHashCode
((
byte
[])
keyValue
);
return
unchecked
((
17
*
chk0
)
+
chk1
);
return
unchecked
((
17
*
chk0
)
+
chk1
);
...
@@ -170,15 +156,9 @@ public override int GetHashCode()
...
@@ -170,15 +156,9 @@ public override int GetHashCode()
/// <summary>
/// <summary>
/// Obtains a string representation of the key
/// Obtains a string representation of the key
/// </summary>
/// </summary>
public
override
string
ToString
()
public
override
string
ToString
()
=>
((
string
)
this
)
??
"(null)"
;
{
return
((
string
)
this
)
??
"(null)"
;
}
internal
RedisValue
AsRedisValue
()
internal
RedisValue
AsRedisValue
()
=>
(
byte
[])
this
;
{
return
(
byte
[])
this
;
}
internal
void
AssertNotNull
()
internal
void
AssertNotNull
()
{
{
...
@@ -186,35 +166,38 @@ internal void AssertNotNull()
...
@@ -186,35 +166,38 @@ internal void AssertNotNull()
}
}
/// <summary>
/// <summary>
/// Create a
key from a String
/// Create a
<see cref="RedisKey"/> from a <see cref="string"/>.
/// </summary>
/// </summary>
/// <param name="key">The string to get a key from.</param>
public
static
implicit
operator
RedisKey
(
string
key
)
public
static
implicit
operator
RedisKey
(
string
key
)
{
{
if
(
key
==
null
)
return
default
(
RedisKey
);
if
(
key
==
null
)
return
default
(
RedisKey
);
return
new
RedisKey
(
null
,
key
);
return
new
RedisKey
(
null
,
key
);
}
}
/// <summary>
/// <summary>
/// Create a
key from a Byte[]
/// Create a
<see cref="RedisKey"/> from a <see cref="T:byte[]"/>.
/// </summary>
/// </summary>
/// <param name="key">The byte array to get a key from.</param>
public
static
implicit
operator
RedisKey
(
byte
[]
key
)
public
static
implicit
operator
RedisKey
(
byte
[]
key
)
{
{
if
(
key
==
null
)
return
default
(
RedisKey
);
if
(
key
==
null
)
return
default
(
RedisKey
);
return
new
RedisKey
(
null
,
key
);
return
new
RedisKey
(
null
,
key
);
}
}
/// <summary>
/// <summary>
/// Obtain the
key as a Byte[]
/// Obtain the
<see cref="RedisKey"/> as a <see cref="T:byte[]"/>.
/// </summary>
/// </summary>
public
static
implicit
operator
byte
[](
RedisKey
key
)
/// <param name="key">The key to get a byte array for.</param>
{
public
static
implicit
operator
byte
[]
(
RedisKey
key
)
=>
ConcatenateBytes
(
key
.
keyPrefix
,
key
.
keyValue
,
null
);
return
ConcatenateBytes
(
key
.
keyPrefix
,
key
.
keyValue
,
null
);
}
/// <summary>
/// <summary>
/// Obtain the key as a
String
/// Obtain the key as a
<see cref="string"/>.
/// </summary>
/// </summary>
/// <param name="key">The key to get a string for.</param>
public
static
implicit
operator
string
(
RedisKey
key
)
public
static
implicit
operator
string
(
RedisKey
key
)
{
{
byte
[]
arr
;
byte
[]
arr
;
if
(
key
.
keyPrefix
==
null
)
if
(
key
.
keyPrefix
==
null
)
{
{
if
(
key
.
keyValue
==
null
)
return
null
;
if
(
key
.
keyValue
==
null
)
return
null
;
...
@@ -235,12 +218,13 @@ internal void AssertNotNull()
...
@@ -235,12 +218,13 @@ internal void AssertNotNull()
{
{
return
BitConverter
.
ToString
(
arr
);
return
BitConverter
.
ToString
(
arr
);
}
}
}
}
/// <summary>
/// <summary>
/// Concatenate two keys
/// Concatenate two keys
/// </summary>
/// </summary>
/// <param name="x">The first <see cref="RedisKey"/> to add.</param>
/// <param name="y">The second <see cref="RedisKey"/> to add.</param>
[
Obsolete
]
[
Obsolete
]
public
static
RedisKey
operator
+(
RedisKey
x
,
RedisKey
y
)
public
static
RedisKey
operator
+(
RedisKey
x
,
RedisKey
y
)
{
{
...
@@ -249,7 +233,7 @@ internal void AssertNotNull()
...
@@ -249,7 +233,7 @@ internal void AssertNotNull()
internal
static
RedisKey
WithPrefix
(
byte
[]
prefix
,
RedisKey
value
)
internal
static
RedisKey
WithPrefix
(
byte
[]
prefix
,
RedisKey
value
)
{
{
if
(
prefix
==
null
||
prefix
.
Length
==
0
)
return
value
;
if
(
prefix
==
null
||
prefix
.
Length
==
0
)
return
value
;
if
(
value
.
keyPrefix
==
null
)
return
new
RedisKey
(
prefix
,
value
.
keyValue
);
if
(
value
.
keyPrefix
==
null
)
return
new
RedisKey
(
prefix
,
value
.
keyValue
);
if
(
value
.
keyValue
==
null
)
return
new
RedisKey
(
prefix
,
value
.
keyPrefix
);
if
(
value
.
keyValue
==
null
)
return
new
RedisKey
(
prefix
,
value
.
keyPrefix
);
...
@@ -275,13 +259,12 @@ internal static byte[] ConcatenateBytes(byte[] a, object b, byte[] c)
...
@@ -275,13 +259,12 @@ internal static byte[] ConcatenateBytes(byte[] a, object b, byte[] c)
:
((
byte
[])
b
).
Length
),
:
((
byte
[])
b
).
Length
),
cLen
=
c
?.
Length
??
0
;
cLen
=
c
?.
Length
??
0
;
byte
[]
result
=
new
byte
[
aLen
+
bLen
+
cLen
];
var
result
=
new
byte
[
aLen
+
bLen
+
cLen
];
if
(
aLen
!=
0
)
Buffer
.
BlockCopy
(
a
,
0
,
result
,
0
,
aLen
);
if
(
aLen
!=
0
)
Buffer
.
BlockCopy
(
a
,
0
,
result
,
0
,
aLen
);
if
(
bLen
!=
0
)
if
(
bLen
!=
0
)
{
{
if
(
b
is
string
)
if
(
b
is
string
s
)
{
{
string
s
=
(
string
)
b
;
Encoding
.
UTF8
.
GetBytes
(
s
,
0
,
s
.
Length
,
result
,
aLen
);
Encoding
.
UTF8
.
GetBytes
(
s
,
0
,
s
.
Length
,
result
,
aLen
);
}
}
else
else
...
@@ -299,10 +282,8 @@ internal static byte[] ConcatenateBytes(byte[] a, object b, byte[] c)
...
@@ -299,10 +282,8 @@ internal static byte[] ConcatenateBytes(byte[] a, object b, byte[] c)
/// Avoids some allocations if possible, repeated Prepend/Appends make
/// Avoids some allocations if possible, repeated Prepend/Appends make
/// it less possible.
/// it less possible.
/// </summary>
/// </summary>
public
RedisKey
Prepend
(
RedisKey
p
)
/// <param name="prefix">The prefix to prepend.</param>
{
public
RedisKey
Prepend
(
RedisKey
prefix
)
=>
WithPrefix
(
prefix
,
this
);
return
WithPrefix
(
p
,
this
);
}
/// <summary>
/// <summary>
/// Appends p to this RedisKey, returning a new RedisKey.
/// Appends p to this RedisKey, returning a new RedisKey.
...
@@ -310,9 +291,7 @@ public RedisKey Prepend(RedisKey p)
...
@@ -310,9 +291,7 @@ public RedisKey Prepend(RedisKey p)
/// Avoids some allocations if possible, repeated Prepend/Appends make
/// Avoids some allocations if possible, repeated Prepend/Appends make
/// it less possible.
/// it less possible.
/// </summary>
/// </summary>
public
RedisKey
Append
(
RedisKey
p
)
/// <param name="suffix">The suffix to append.</param>
{
public
RedisKey
Append
(
RedisKey
suffix
)
=>
WithPrefix
(
this
,
suffix
);
return
WithPrefix
(
this
,
p
);
}
}
}
}
}
\ No newline at end of file
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