Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
5fe2ee30
Unverified
提交
5fe2ee30
authored
1月 15, 2018
作者:
Noel Grandin
提交者:
GitHub
1月 15, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #763 from katzyn/DataUtils
Add DataUtils.getMapName()
上级
e59716c0
e7e6a8a9
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
101 行增加
和
25 行删除
+101
-25
DataUtils.java
h2/src/main/org/h2/mvstore/DataUtils.java
+79
-21
MVStore.java
h2/src/main/org/h2/mvstore/MVStore.java
+1
-1
TestDataUtils.java
h2/src/test/org/h2/test/store/TestDataUtils.java
+21
-3
没有找到文件。
h2/src/main/org/h2/mvstore/DataUtils.java
浏览文件 @
5fe2ee30
...
@@ -613,6 +613,40 @@ public final class DataUtils {
...
@@ -613,6 +613,40 @@ public final class DataUtils {
}
}
}
}
/**
* @param buff output buffer, should be empty
* @param s parsed string
* @param i offset to parse from
* @param size stop offset (exclusive)
* @return new offset
*/
private
static
int
parseMapValue
(
StringBuilder
buff
,
String
s
,
int
i
,
int
size
)
{
while
(
i
<
size
)
{
char
c
=
s
.
charAt
(
i
++);
if
(
c
==
','
)
{
break
;
}
else
if
(
c
==
'\"'
)
{
while
(
i
<
size
)
{
c
=
s
.
charAt
(
i
++);
if
(
c
==
'\\'
)
{
if
(
i
==
size
)
{
throw
DataUtils
.
newIllegalStateException
(
DataUtils
.
ERROR_FILE_CORRUPT
,
"Not a map: {0}"
,
s
);
}
c
=
s
.
charAt
(
i
++);
}
else
if
(
c
==
'\"'
)
{
break
;
}
buff
.
append
(
c
);
}
}
else
{
buff
.
append
(
c
);
}
}
return
i
;
}
/**
/**
* Parse a key-value pair list.
* Parse a key-value pair list.
*
*
...
@@ -622,6 +656,7 @@ public final class DataUtils {
...
@@ -622,6 +656,7 @@ public final class DataUtils {
*/
*/
public
static
HashMap
<
String
,
String
>
parseMap
(
String
s
)
{
public
static
HashMap
<
String
,
String
>
parseMap
(
String
s
)
{
HashMap
<
String
,
String
>
map
=
New
.
hashMap
();
HashMap
<
String
,
String
>
map
=
New
.
hashMap
();
StringBuilder
buff
=
new
StringBuilder
();
for
(
int
i
=
0
,
size
=
s
.
length
();
i
<
size
;)
{
for
(
int
i
=
0
,
size
=
s
.
length
();
i
<
size
;)
{
int
startKey
=
i
;
int
startKey
=
i
;
i
=
s
.
indexOf
(
':'
,
i
);
i
=
s
.
indexOf
(
':'
,
i
);
...
@@ -630,33 +665,56 @@ public final class DataUtils {
...
@@ -630,33 +665,56 @@ public final class DataUtils {
DataUtils
.
ERROR_FILE_CORRUPT
,
"Not a map: {0}"
,
s
);
DataUtils
.
ERROR_FILE_CORRUPT
,
"Not a map: {0}"
,
s
);
}
}
String
key
=
s
.
substring
(
startKey
,
i
++);
String
key
=
s
.
substring
(
startKey
,
i
++);
StringBuilder
buff
=
new
StringBuilder
();
i
=
parseMapValue
(
buff
,
s
,
i
,
size
);
while
(
i
<
size
)
{
map
.
put
(
key
,
buff
.
toString
());
char
c
=
s
.
charAt
(
i
++);
buff
.
setLength
(
0
);
if
(
c
==
','
)
{
}
break
;
return
map
;
}
else
if
(
c
==
'\"'
)
{
}
while
(
i
<
size
)
{
c
=
s
.
charAt
(
i
++);
/**
if
(
c
==
'\\'
)
{
* Parse a name from key-value pair list.
if
(
i
==
size
)
{
*
throw
DataUtils
.
newIllegalStateException
(
* @param s the list
DataUtils
.
ERROR_FILE_CORRUPT
,
* @return value of name item, or {@code null}
"Not a map: {0}"
,
s
);
* @throws IllegalStateException if parsing failed
}
*/
public
static
String
getMapName
(
String
s
)
{
for
(
int
i
=
0
,
size
=
s
.
length
();
i
<
size
;)
{
int
startKey
=
i
;
i
=
s
.
indexOf
(
':'
,
i
);
if
(
i
<
0
)
{
throw
DataUtils
.
newIllegalStateException
(
DataUtils
.
ERROR_FILE_CORRUPT
,
"Not a map: {0}"
,
s
);
}
if
(
i
++
-
startKey
==
4
&&
s
.
regionMatches
(
startKey
,
"name"
,
0
,
4
))
{
StringBuilder
buff
=
new
StringBuilder
();
i
=
parseMapValue
(
buff
,
s
,
i
,
size
);
return
buff
.
toString
();
}
else
{
while
(
i
<
size
)
{
char
c
=
s
.
charAt
(
i
++);
if
(
c
==
','
)
{
break
;
}
else
if
(
c
==
'\"'
)
{
while
(
i
<
size
)
{
c
=
s
.
charAt
(
i
++);
c
=
s
.
charAt
(
i
++);
}
else
if
(
c
==
'\"'
)
{
if
(
c
==
'\\'
)
{
break
;
if
(
i
==
size
)
{
throw
DataUtils
.
newIllegalStateException
(
DataUtils
.
ERROR_FILE_CORRUPT
,
"Not a map: {0}"
,
s
);
}
c
=
s
.
charAt
(
i
++);
}
else
if
(
c
==
'\"'
)
{
break
;
}
}
}
buff
.
append
(
c
);
}
}
}
else
{
buff
.
append
(
c
);
}
}
}
}
map
.
put
(
key
,
buff
.
toString
());
}
}
return
map
;
return
null
;
}
}
/**
/**
...
...
h2/src/main/org/h2/mvstore/MVStore.java
浏览文件 @
5fe2ee30
...
@@ -2462,7 +2462,7 @@ public final class MVStore {
...
@@ -2462,7 +2462,7 @@ public final class MVStore {
public
synchronized
String
getMapName
(
int
id
)
{
public
synchronized
String
getMapName
(
int
id
)
{
checkOpen
();
checkOpen
();
String
m
=
meta
.
get
(
MVMap
.
getMapKey
(
id
));
String
m
=
meta
.
get
(
MVMap
.
getMapKey
(
id
));
return
m
==
null
?
null
:
DataUtils
.
parseMap
(
m
).
get
(
"name"
);
return
m
==
null
?
null
:
DataUtils
.
getMapName
(
m
);
}
}
/**
/**
...
...
h2/src/test/org/h2/test/store/TestDataUtils.java
浏览文件 @
5fe2ee30
...
@@ -99,16 +99,34 @@ public class TestDataUtils extends TestBase {
...
@@ -99,16 +99,34 @@ public class TestDataUtils extends TestBase {
DataUtils
.
appendMap
(
buff
,
"c"
,
"1,2"
);
DataUtils
.
appendMap
(
buff
,
"c"
,
"1,2"
);
DataUtils
.
appendMap
(
buff
,
"d"
,
"\"test\""
);
DataUtils
.
appendMap
(
buff
,
"d"
,
"\"test\""
);
DataUtils
.
appendMap
(
buff
,
"e"
,
"}"
);
DataUtils
.
appendMap
(
buff
,
"e"
,
"}"
);
assertEquals
(
":,a:1,b:\",\",c:\"1,2\",d:\"\\\"test\\\"\",e:}"
,
buff
.
toString
());
DataUtils
.
appendMap
(
buff
,
"name"
,
"1:1\","
);
String
encoded
=
buff
.
toString
();
assertEquals
(
":,a:1,b:\",\",c:\"1,2\",d:\"\\\"test\\\"\",e:},name:\"1:1\\\",\""
,
encoded
);
HashMap
<
String
,
String
>
m
=
DataUtils
.
parseMap
(
buff
.
toString
()
);
HashMap
<
String
,
String
>
m
=
DataUtils
.
parseMap
(
encoded
);
assertEquals
(
6
,
m
.
size
());
assertEquals
(
7
,
m
.
size
());
assertEquals
(
""
,
m
.
get
(
""
));
assertEquals
(
""
,
m
.
get
(
""
));
assertEquals
(
"1"
,
m
.
get
(
"a"
));
assertEquals
(
"1"
,
m
.
get
(
"a"
));
assertEquals
(
","
,
m
.
get
(
"b"
));
assertEquals
(
","
,
m
.
get
(
"b"
));
assertEquals
(
"1,2"
,
m
.
get
(
"c"
));
assertEquals
(
"1,2"
,
m
.
get
(
"c"
));
assertEquals
(
"\"test\""
,
m
.
get
(
"d"
));
assertEquals
(
"\"test\""
,
m
.
get
(
"d"
));
assertEquals
(
"}"
,
m
.
get
(
"e"
));
assertEquals
(
"}"
,
m
.
get
(
"e"
));
assertEquals
(
"1:1\","
,
m
.
get
(
"name"
));
assertEquals
(
"1:1\","
,
DataUtils
.
getMapName
(
encoded
));
buff
.
setLength
(
0
);
DataUtils
.
appendMap
(
buff
,
"1"
,
"1"
);
DataUtils
.
appendMap
(
buff
,
"name"
,
"2"
);
DataUtils
.
appendMap
(
buff
,
"3"
,
"3"
);
encoded
=
buff
.
toString
();
assertEquals
(
"2"
,
DataUtils
.
parseMap
(
encoded
).
get
(
"name"
));
assertEquals
(
"2"
,
DataUtils
.
getMapName
(
encoded
));
buff
.
setLength
(
0
);
DataUtils
.
appendMap
(
buff
,
"name"
,
"xx"
);
encoded
=
buff
.
toString
();
assertEquals
(
"xx"
,
DataUtils
.
parseMap
(
encoded
).
get
(
"name"
));
assertEquals
(
"xx"
,
DataUtils
.
getMapName
(
encoded
));
}
}
private
void
testMapRandomized
()
{
private
void
testMapRandomized
()
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论