Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
jobily
Questdb
提交
e5a821cb
Q
Questdb
项目概览
jobily
/
Questdb
9 个月 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Q
Questdb
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
e5a821cb
编写于
9月 04, 2023
作者:
A
Andrei Pechkurov
提交者:
GitHub
9月 04, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
chore(core): introduce internal off-heap data structures (#3700)
上级
07538073
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
527 addition
and
107 deletion
+527
-107
core/src/main/java/io/questdb/std/DirectIntList.java
core/src/main/java/io/questdb/std/DirectIntList.java
+184
-0
core/src/main/java/io/questdb/std/DirectLongList.java
core/src/main/java/io/questdb/std/DirectLongList.java
+19
-20
core/src/test/java/io/questdb/test/std/DirectIntListTest.java
.../src/test/java/io/questdb/test/std/DirectIntListTest.java
+190
-0
core/src/test/java/io/questdb/test/std/DirectLongListTest.java
...src/test/java/io/questdb/test/std/DirectLongListTest.java
+128
-81
core/src/test/java/io/questdb/test/std/VectTest.java
core/src/test/java/io/questdb/test/std/VectTest.java
+6
-6
未找到文件。
core/src/main/java/io/questdb/std/DirectIntList.java
0 → 100644
浏览文件 @
e5a821cb
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2023 QuestDB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package
io.questdb.std
;
import
io.questdb.cairo.Reopenable
;
import
io.questdb.log.Log
;
import
io.questdb.log.LogFactory
;
import
io.questdb.std.str.CharSink
;
import
java.io.Closeable
;
public
class
DirectIntList
implements
Mutable
,
Closeable
,
Reopenable
{
private
static
final
Log
LOG
=
LogFactory
.
getLog
(
DirectIntList
.
class
);
private
final
long
initialCapacity
;
private
final
int
memoryTag
;
private
long
address
;
private
long
capacity
;
private
long
limit
;
private
long
pos
;
public
DirectIntList
(
long
capacity
,
int
memoryTag
)
{
this
.
memoryTag
=
memoryTag
;
this
.
capacity
=
(
capacity
*
Integer
.
BYTES
);
this
.
address
=
Unsafe
.
malloc
(
this
.
capacity
,
memoryTag
);
this
.
pos
=
address
;
this
.
limit
=
pos
+
this
.
capacity
;
this
.
initialCapacity
=
this
.
capacity
;
}
public
void
add
(
int
x
)
{
ensureCapacity
();
assert
pos
<
limit
;
Unsafe
.
getUnsafe
().
putInt
(
pos
,
x
);
pos
+=
Integer
.
BYTES
;
}
public
final
void
addAll
(
DirectIntList
that
)
{
long
thatSize
=
that
.
pos
-
that
.
address
;
if
(
limit
-
pos
<
thatSize
)
{
setCapacityBytes
(
this
.
capacity
+
thatSize
-
(
limit
-
pos
));
}
Vect
.
memcpy
(
this
.
pos
,
that
.
address
,
thatSize
);
this
.
pos
+=
thatSize
;
}
// clear without "zeroing" memory
public
void
clear
()
{
pos
=
address
;
}
public
void
clear
(
int
b
)
{
zero
(
b
);
pos
=
address
;
}
@Override
public
void
close
()
{
if
(
address
!=
0
)
{
Unsafe
.
free
(
address
,
capacity
,
memoryTag
);
address
=
0
;
limit
=
0
;
pos
=
0
;
capacity
=
0
;
}
}
public
int
get
(
long
p
)
{
return
Unsafe
.
getUnsafe
().
getInt
(
address
+
(
p
<<
2
));
}
// base address of native memory
public
long
getAddress
()
{
return
address
;
}
// capacity in INTs
public
long
getCapacity
()
{
return
capacity
/
Integer
.
BYTES
;
}
@Override
public
void
reopen
()
{
if
(
address
==
0
)
{
resetCapacity
();
}
}
public
void
resetCapacity
()
{
setCapacityBytes
(
initialCapacity
);
}
public
void
set
(
long
p
,
int
v
)
{
assert
p
>=
0
&&
p
<=
(
limit
-
address
)
>>
2
;
Unsafe
.
getUnsafe
().
putInt
(
address
+
(
p
<<
2
),
v
);
}
// desired capacity in INTs (not count of bytes)
public
void
setCapacity
(
long
capacity
)
{
assert
capacity
>
0
;
setCapacityBytes
(
capacity
*
Integer
.
BYTES
);
}
public
void
setPos
(
long
p
)
{
assert
p
*
Integer
.
BYTES
<=
capacity
;
pos
=
address
+
p
*
Integer
.
BYTES
;
}
public
void
shrink
(
long
newCapacity
)
{
// deallocates memory but keeps reusable
if
(
newCapacity
<
capacity
)
{
setCapacityBytes
(
newCapacity
<<
2
);
}
}
public
long
size
()
{
return
(
int
)
((
pos
-
address
)
/
Integer
.
BYTES
);
}
@Override
public
String
toString
()
{
CharSink
sb
=
Misc
.
getThreadLocalBuilder
();
sb
.
put
(
'['
);
final
int
maxElementsToPrint
=
1000
;
// Do not try to print too much, it can hang IntelliJ debugger.
for
(
int
i
=
0
,
n
=
(
int
)
Math
.
min
(
maxElementsToPrint
,
size
());
i
<
n
;
i
++)
{
if
(
i
>
0
)
{
sb
.
put
(
','
).
put
(
' '
);
}
sb
.
put
(
get
(
i
));
}
if
(
size
()
>
maxElementsToPrint
)
{
sb
.
put
(
", .. "
);
}
sb
.
put
(
']'
);
return
sb
.
toString
();
}
public
void
zero
(
int
v
)
{
Vect
.
memset
(
address
,
pos
-
address
,
v
);
}
// desired capacity in bytes (not count of INT values)
private
void
setCapacityBytes
(
long
capacity
)
{
if
(
this
.
capacity
!=
capacity
)
{
final
long
oldCapacity
=
this
.
capacity
;
final
long
oldSize
=
this
.
pos
-
this
.
address
;
this
.
capacity
=
capacity
;
long
address
=
Unsafe
.
realloc
(
this
.
address
,
oldCapacity
,
capacity
,
memoryTag
);
this
.
address
=
address
;
this
.
limit
=
address
+
capacity
;
this
.
pos
=
Math
.
min
(
this
.
limit
,
address
+
oldSize
);
LOG
.
debug
().
$
(
"resized [old="
).
$
(
oldCapacity
).
$
(
", new="
).
$
(
this
.
capacity
).
$
(
']'
).
$
();
}
}
void
ensureCapacity
()
{
if
(
pos
<
limit
)
{
return
;
}
setCapacityBytes
(
capacity
*
2
);
}
}
core/src/main/java/io/questdb/std/DirectLongList.java
浏览文件 @
e5a821cb
...
...
@@ -40,13 +40,12 @@ public class DirectLongList implements Mutable, Closeable, Reopenable {
private
long
capacity
;
private
long
limit
;
private
long
pos
;
private
long
start
;
public
DirectLongList
(
long
capacity
,
int
memoryTag
)
{
this
.
memoryTag
=
memoryTag
;
this
.
capacity
=
(
capacity
*
Long
.
BYTES
);
this
.
address
=
Unsafe
.
malloc
(
this
.
capacity
,
memoryTag
);
this
.
start
=
this
.
pos
=
address
;
this
.
pos
=
address
;
this
.
limit
=
pos
+
this
.
capacity
;
this
.
initialCapacity
=
this
.
capacity
;
}
...
...
@@ -58,31 +57,31 @@ public class DirectLongList implements Mutable, Closeable, Reopenable {
pos
+=
Long
.
BYTES
;
}
public
final
void
add
(
DirectLongList
that
)
{
long
thatSize
=
that
.
pos
-
that
.
start
;
public
final
void
add
All
(
DirectLongList
that
)
{
long
thatSize
=
that
.
pos
-
that
.
address
;
if
(
limit
-
pos
<
thatSize
)
{
setCapacityBytes
(
this
.
capacity
+
thatSize
-
(
limit
-
pos
));
}
Vect
.
memcpy
(
this
.
pos
,
that
.
start
,
thatSize
);
Vect
.
memcpy
(
this
.
pos
,
that
.
address
,
thatSize
);
this
.
pos
+=
thatSize
;
}
public
long
binarySearch
(
long
value
,
int
scanDir
)
{
final
long
high
=
(
pos
-
start
)
/
8
;
final
long
high
=
(
pos
-
address
)
/
8
;
if
(
high
>
0
)
{
return
Vect
.
binarySearch64Bit
(
start
,
value
,
0
,
high
-
1
,
scanDir
);
return
Vect
.
binarySearch64Bit
(
address
,
value
,
0
,
high
-
1
,
scanDir
);
}
return
-
1
;
}
// clear without "zeroing" memory
public
void
clear
()
{
pos
=
start
;
pos
=
address
;
}
public
void
clear
(
long
b
)
{
zero
(
b
);
pos
=
start
;
pos
=
address
;
}
@Override
...
...
@@ -90,7 +89,6 @@ public class DirectLongList implements Mutable, Closeable, Reopenable {
if
(
address
!=
0
)
{
Unsafe
.
free
(
address
,
capacity
,
memoryTag
);
address
=
0
;
start
=
0
;
limit
=
0
;
pos
=
0
;
capacity
=
0
;
...
...
@@ -98,7 +96,7 @@ public class DirectLongList implements Mutable, Closeable, Reopenable {
}
public
long
get
(
long
p
)
{
return
Unsafe
.
getUnsafe
().
getLong
(
start
+
(
p
<<
3
));
return
Unsafe
.
getUnsafe
().
getLong
(
address
+
(
p
<<
3
));
}
// base address of native memory
...
...
@@ -136,18 +134,19 @@ public class DirectLongList implements Mutable, Closeable, Reopenable {
}
public
void
set
(
long
p
,
long
v
)
{
assert
p
>=
0
&&
p
<=
(
limit
-
start
)
>>
3
;
Unsafe
.
getUnsafe
().
putLong
(
start
+
(
p
<<
3
),
v
);
assert
p
>=
0
&&
p
<=
(
limit
-
address
)
>>
3
;
Unsafe
.
getUnsafe
().
putLong
(
address
+
(
p
<<
3
),
v
);
}
// desired capacity in LONGs (not count of bytes)
public
void
setCapacity
(
long
capacity
)
{
assert
capacity
>
0
;
setCapacityBytes
(
capacity
*
Long
.
BYTES
);
}
public
void
setPos
(
long
p
)
{
assert
p
*
Long
.
BYTES
<=
capacity
;
pos
=
start
+
p
*
Long
.
BYTES
;
pos
=
address
+
p
*
Long
.
BYTES
;
}
public
void
shrink
(
long
newCapacity
)
{
...
...
@@ -158,7 +157,7 @@ public class DirectLongList implements Mutable, Closeable, Reopenable {
}
public
long
size
()
{
return
(
int
)
((
pos
-
start
)
/
Long
.
BYTES
);
return
(
int
)
((
pos
-
address
)
/
Long
.
BYTES
);
}
public
void
sortAsUnsigned
()
{
...
...
@@ -168,7 +167,7 @@ public class DirectLongList implements Mutable, Closeable, Reopenable {
@Override
public
String
toString
()
{
CharSink
sb
=
Misc
.
getThreadLocalBuilder
();
sb
.
put
(
'
{
'
);
sb
.
put
(
'
[
'
);
final
int
maxElementsToPrint
=
1000
;
// Do not try to print too much, it can hang IntelliJ debugger.
for
(
int
i
=
0
,
n
=
(
int
)
Math
.
min
(
maxElementsToPrint
,
size
());
i
<
n
;
i
++)
{
if
(
i
>
0
)
{
...
...
@@ -179,24 +178,24 @@ public class DirectLongList implements Mutable, Closeable, Reopenable {
if
(
size
()
>
maxElementsToPrint
)
{
sb
.
put
(
", .. "
);
}
sb
.
put
(
'
}
'
);
sb
.
put
(
'
]
'
);
return
sb
.
toString
();
}
public
void
zero
(
long
v
)
{
Vect
.
memset
(
start
,
pos
-
start
,
(
int
)
v
);
Vect
.
memset
(
address
,
pos
-
address
,
(
int
)
v
);
}
// desired capacity in bytes (not count of LONG values)
private
void
setCapacityBytes
(
long
capacity
)
{
if
(
this
.
capacity
!=
capacity
)
{
final
long
oldCapacity
=
this
.
capacity
;
final
long
oldSize
=
this
.
pos
-
this
.
address
;
this
.
capacity
=
capacity
;
long
address
=
Unsafe
.
realloc
(
this
.
address
,
oldCapacity
,
capacity
,
memoryTag
);
this
.
pos
=
address
+
(
this
.
pos
-
this
.
start
);
this
.
address
=
address
;
this
.
start
=
address
;
this
.
limit
=
address
+
capacity
;
this
.
pos
=
Math
.
min
(
this
.
limit
,
address
+
oldSize
);
LOG
.
debug
().
$
(
"resized [old="
).
$
(
oldCapacity
).
$
(
", new="
).
$
(
this
.
capacity
).
$
(
']'
).
$
();
}
}
...
...
core/src/test/java/io/questdb/test/std/DirectIntListTest.java
0 → 100644
浏览文件 @
e5a821cb
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2023 QuestDB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package
io.questdb.test.std
;
import
io.questdb.log.Log
;
import
io.questdb.log.LogFactory
;
import
io.questdb.std.DirectIntList
;
import
io.questdb.std.MemoryTag
;
import
io.questdb.std.Unsafe
;
import
org.junit.Assert
;
import
org.junit.Test
;
public
class
DirectIntListTest
{
private
static
final
Log
LOG
=
LogFactory
.
getLog
(
DirectIntListTest
.
class
);
@Test
public
void
testAddAll
()
{
try
(
DirectIntList
list
=
new
DirectIntList
(
256
,
MemoryTag
.
NATIVE_DEFAULT
);
DirectIntList
list2
=
new
DirectIntList
(
256
,
MemoryTag
.
NATIVE_DEFAULT
)
)
{
final
int
N
=
100
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
list
.
add
(
i
);
list2
.
add
(
N
+
i
);
}
list
.
addAll
(
list2
);
Assert
.
assertEquals
(
256
,
list
.
getCapacity
());
Assert
.
assertEquals
(
2
*
N
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
size
();
i
++)
{
Assert
.
assertEquals
(
i
,
list
.
get
(
i
));
}
}
}
@Test
public
void
testAddAllExpand
()
{
try
(
DirectIntList
list
=
new
DirectIntList
(
128
,
MemoryTag
.
NATIVE_DEFAULT
);
DirectIntList
list2
=
new
DirectIntList
(
128
,
MemoryTag
.
NATIVE_DEFAULT
)
)
{
final
int
N
=
100
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
list
.
add
(
i
);
list2
.
add
(
N
+
i
);
}
list
.
addAll
(
list2
);
Assert
.
assertEquals
(
200
,
list
.
getCapacity
());
// 128 + 100 - 28
Assert
.
assertEquals
(
2
*
N
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
size
();
i
++)
{
Assert
.
assertEquals
(
i
,
list
.
get
(
i
));
}
}
}
@Test
public
void
testCapacityAndSize
()
{
// use logger so that static memory allocation happens before our control measurement
LOG
.
info
().
$
(
"testCapacityAndSize"
).
$
();
long
expected
=
Unsafe
.
getMemUsed
();
try
(
DirectIntList
list
=
new
DirectIntList
(
1024
,
MemoryTag
.
NATIVE_DEFAULT
))
{
Assert
.
assertEquals
(
1024
,
list
.
getCapacity
());
list
.
setCapacity
(
2048
);
Assert
.
assertEquals
(
2048
,
list
.
getCapacity
());
// verify that extend also shrinks capacity
list
.
setCapacity
(
1024
);
Assert
.
assertEquals
(
1024
,
list
.
getCapacity
());
Assert
.
assertEquals
(
0
,
list
.
size
());
long
addr
=
list
.
getAddress
();
Unsafe
.
getUnsafe
().
putLong
(
addr
,
42
);
Assert
.
assertEquals
(
42
,
list
.
get
(
0
));
for
(
int
i
=
0
;
i
<
list
.
getCapacity
();
i
++)
{
list
.
add
(
i
);
}
for
(
long
i
=
0
;
i
<
list
.
size
();
i
++)
{
Assert
.
assertEquals
(
i
,
list
.
get
(
i
));
}
list
.
clear
(
0
);
Assert
.
assertEquals
(
0
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
getCapacity
();
i
++)
{
Assert
.
assertEquals
(
0
,
list
.
get
(
i
));
}
list
.
setPos
(
42
);
Assert
.
assertEquals
(
42
,
list
.
size
());
list
.
clear
();
Assert
.
assertEquals
(
0
,
list
.
size
());
}
Assert
.
assertEquals
(
expected
,
Unsafe
.
getMemUsed
());
}
@Test
public
void
testResizeMemLeak
()
{
// use logger so that static memory allocation happens before our control measurement
LOG
.
info
().
$
(
"testResizeMemLeak"
).
$
();
long
expected
=
Unsafe
.
getMemUsed
();
try
(
DirectIntList
list
=
new
DirectIntList
(
1024
,
MemoryTag
.
NATIVE_DEFAULT
))
{
for
(
int
i
=
0
;
i
<
1_000_000
;
i
++)
{
list
.
add
(
i
);
}
}
Assert
.
assertEquals
(
expected
,
Unsafe
.
getMemUsed
());
}
@Test
public
void
testSet
()
{
try
(
DirectIntList
list
=
new
DirectIntList
(
32
,
MemoryTag
.
NATIVE_DEFAULT
))
{
final
int
N
=
100
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
list
.
add
(
i
);
}
Assert
.
assertEquals
(
128
,
list
.
getCapacity
());
Assert
.
assertEquals
(
N
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
size
();
i
++)
{
Assert
.
assertEquals
(
i
,
list
.
get
(
i
));
}
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
list
.
set
(
i
,
N
-
i
);
}
Assert
.
assertEquals
(
128
,
list
.
getCapacity
());
Assert
.
assertEquals
(
N
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
size
();
i
++)
{
Assert
.
assertEquals
(
N
-
i
,
list
.
get
(
i
));
}
}
}
@Test
public
void
testShrink
()
{
try
(
DirectIntList
list
=
new
DirectIntList
(
32
,
MemoryTag
.
NATIVE_DEFAULT
))
{
final
int
N
=
100
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
list
.
add
(
i
);
}
Assert
.
assertEquals
(
128
,
list
.
getCapacity
());
Assert
.
assertEquals
(
N
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
size
();
i
++)
{
Assert
.
assertEquals
(
i
,
list
.
get
(
i
));
}
list
.
shrink
(
N
);
Assert
.
assertEquals
(
N
,
list
.
getCapacity
());
Assert
.
assertEquals
(
N
,
list
.
size
());
list
.
shrink
(
16
);
Assert
.
assertEquals
(
16
,
list
.
getCapacity
());
Assert
.
assertEquals
(
16
,
list
.
size
());
}
}
@Test
public
void
testToString
()
{
try
(
DirectIntList
list
=
new
DirectIntList
(
1001
,
MemoryTag
.
NATIVE_DEFAULT
))
{
final
int
N
=
1000
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
list
.
add
(
i
);
}
String
str1
=
list
.
toString
();
list
.
add
(
1001
);
String
str2
=
list
.
toString
();
Assert
.
assertEquals
(
str1
.
substring
(
0
,
str1
.
length
()
-
1
)
+
", .. ]"
,
str2
);
}
}
}
core/src/test/java/io/questdb/test/std/DirectLongListTest.java
浏览文件 @
e5a821cb
...
...
@@ -43,7 +43,7 @@ public class DirectLongListTest {
TestUtils
.
assertMemoryLeak
(()
->
{
try
(
DirectLongList
list
=
new
DirectLongList
(
256
,
MemoryTag
.
NATIVE_LONG_LIST
))
{
final
int
N
=
100
;
for
(
int
i
=
0
;
i
<
N
;
++
i
)
{
for
(
int
i
=
0
;
i
<
N
;
i
++
)
{
list
.
add
((
100
-
i
-
1
)
/
10
);
list
.
add
((
100
-
i
-
1
));
}
...
...
@@ -68,7 +68,7 @@ public class DirectLongListTest {
TestUtils
.
assertMemoryLeak
(()
->
{
try
(
DirectLongList
list
=
new
DirectLongList
(
size
,
MemoryTag
.
NATIVE_LONG_LIST
))
{
long
[]
longList
=
new
long
[
size
];
for
(
int
i
=
0
;
i
<
size
;
++
i
)
{
for
(
int
i
=
0
;
i
<
size
;
i
++
)
{
int
rnd1
=
Math
.
abs
(
rnd
.
nextInt
()
%
(
range
));
int
rnd2
=
Math
.
abs
(
rnd
.
nextShort
());
...
...
@@ -97,41 +97,43 @@ public class DirectLongListTest {
}
@Test
public
void
testAddList
()
{
DirectLongList
list
=
new
DirectLongList
(
256
,
MemoryTag
.
NATIVE_LONG_LIST
);
DirectLongList
list2
=
new
DirectLongList
(
256
,
MemoryTag
.
NATIVE_LONG_LIST
);
final
int
N
=
100
;
for
(
int
i
=
0
;
i
<
N
;
++
i
)
{
list
.
add
(
i
);
list2
.
add
(
N
+
i
);
}
list
.
add
(
list2
);
Assert
.
assertEquals
(
256
,
list
.
getCapacity
());
Assert
.
assertEquals
(
2
*
N
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
size
();
++
i
)
{
Assert
.
assertEquals
(
i
,
list
.
get
(
i
));
public
void
testAddAll
()
{
try
(
DirectLongList
list
=
new
DirectLongList
(
256
,
MemoryTag
.
NATIVE_LONG_LIST
);
DirectLongList
list2
=
new
DirectLongList
(
256
,
MemoryTag
.
NATIVE_LONG_LIST
)
)
{
final
int
N
=
100
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
list
.
add
(
i
);
list2
.
add
(
N
+
i
);
}
list
.
addAll
(
list2
);
Assert
.
assertEquals
(
256
,
list
.
getCapacity
());
Assert
.
assertEquals
(
2
*
N
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
size
();
i
++)
{
Assert
.
assertEquals
(
i
,
list
.
get
(
i
));
}
}
list
.
close
();
list2
.
close
();
}
@Test
public
void
testAddListExpand
()
{
DirectLongList
list
=
new
DirectLongList
(
128
,
MemoryTag
.
NATIVE_LONG_LIST
);
DirectLongList
list2
=
new
DirectLongList
(
128
,
MemoryTag
.
NATIVE_LONG_LIST
);
final
int
N
=
100
;
for
(
int
i
=
0
;
i
<
N
;
++
i
)
{
list
.
add
(
i
);
list2
.
add
(
N
+
i
);
}
list
.
add
(
list2
);
Assert
.
assertEquals
(
200
,
list
.
getCapacity
());
//128 + 100 - 28
Assert
.
assertEquals
(
2
*
N
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
size
();
++
i
)
{
Assert
.
assertEquals
(
i
,
list
.
get
(
i
));
public
void
testAddAllExpand
()
{
try
(
DirectLongList
list
=
new
DirectLongList
(
128
,
MemoryTag
.
NATIVE_LONG_LIST
);
DirectLongList
list2
=
new
DirectLongList
(
128
,
MemoryTag
.
NATIVE_LONG_LIST
)
)
{
final
int
N
=
100
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
list
.
add
(
i
);
list2
.
add
(
N
+
i
);
}
list
.
addAll
(
list2
);
Assert
.
assertEquals
(
200
,
list
.
getCapacity
());
// 128 + 100 - 28
Assert
.
assertEquals
(
2
*
N
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
size
();
i
++)
{
Assert
.
assertEquals
(
i
,
list
.
get
(
i
));
}
}
list
.
close
();
list2
.
close
();
}
@Test
...
...
@@ -158,35 +160,35 @@ public class DirectLongListTest {
// use logger so that static memory allocation happens before our control measurement
LOG
.
info
().
$
(
"testCapacityAndSize"
).
$
();
long
expected
=
Unsafe
.
getMemUsed
();
DirectLongList
list
=
new
DirectLongList
(
1024
,
MemoryTag
.
NATIVE_LONG_LIST
);
Assert
.
assertEquals
(
1024
,
list
.
getCapacity
());
list
.
setCapacity
(
2048
);
Assert
.
assertEquals
(
2048
,
list
.
getCapacity
());
// verify that extend also shrinks capacity
list
.
setCapacity
(
1024
);
Assert
.
assertEquals
(
1024
,
list
.
getCapacity
());
Assert
.
assertEquals
(
0
,
list
.
size
());
long
addr
=
list
.
getAddress
();
Unsafe
.
getUnsafe
().
putLong
(
addr
,
42
);
Assert
.
assertEquals
(
42
,
list
.
get
(
0
));
for
(
long
i
=
0
;
i
<
list
.
getCapacity
();
++
i
)
{
list
.
add
(
i
);
}
for
(
long
i
=
0
;
i
<
list
.
size
();
++
i
)
{
Assert
.
assertEquals
(
i
,
list
.
get
(
i
));
}
list
.
clear
(
0
);
Assert
.
assertEquals
(
0
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
getCapacity
();
++
i
)
{
Assert
.
assertEquals
(
0
,
list
.
get
(
i
));
try
(
DirectLongList
list
=
new
DirectLongList
(
1024
,
MemoryTag
.
NATIVE_LONG_LIST
))
{
Assert
.
assertEquals
(
1024
,
list
.
getCapacity
());
list
.
setCapacity
(
2048
);
Assert
.
assertEquals
(
2048
,
list
.
getCapacity
());
// verify that extend also shrinks capacity
list
.
setCapacity
(
1024
);
Assert
.
assertEquals
(
1024
,
list
.
getCapacity
());
Assert
.
assertEquals
(
0
,
list
.
size
());
long
addr
=
list
.
getAddress
();
Unsafe
.
getUnsafe
().
putLong
(
addr
,
42
);
Assert
.
assertEquals
(
42
,
list
.
get
(
0
));
for
(
long
i
=
0
;
i
<
list
.
getCapacity
();
i
++)
{
list
.
add
(
i
);
}
for
(
long
i
=
0
;
i
<
list
.
size
();
i
++)
{
Assert
.
assertEquals
(
i
,
list
.
get
(
i
));
}
list
.
clear
(
0
);
Assert
.
assertEquals
(
0
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
getCapacity
();
i
++)
{
Assert
.
assertEquals
(
0
,
list
.
get
(
i
));
}
list
.
setPos
(
42
);
Assert
.
assertEquals
(
42
,
list
.
size
());
list
.
clear
();
Assert
.
assertEquals
(
0
,
list
.
size
());
}
list
.
setPos
(
42
);
Assert
.
assertEquals
(
42
,
list
.
size
());
list
.
clear
();
Assert
.
assertEquals
(
0
,
list
.
size
());
list
.
close
();
//release memory
Assert
.
assertEquals
(
expected
,
Unsafe
.
getMemUsed
());
}
...
...
@@ -205,48 +207,93 @@ public class DirectLongListTest {
@Test
public
void
testSearch
()
{
DirectLongList
list
=
new
DirectLongList
(
256
,
MemoryTag
.
NATIVE_LONG_LIST
);
final
int
N
=
100
;
for
(
int
i
=
0
;
i
<
N
;
++
i
)
{
list
.
add
(
i
);
try
(
DirectLongList
list
=
new
DirectLongList
(
256
,
MemoryTag
.
NATIVE_LONG_LIST
))
{
final
int
N
=
100
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
list
.
add
(
i
);
}
Assert
.
assertEquals
(
N
/
2
,
list
.
scanSearch
(
N
/
2
,
0
,
list
.
size
()));
Assert
.
assertEquals
(
N
/
2
,
list
.
binarySearch
(
N
/
2
,
BinarySearch
.
SCAN_UP
));
}
Assert
.
assertEquals
(
N
/
2
,
list
.
scanSearch
(
N
/
2
,
0
,
list
.
size
()));
Assert
.
assertEquals
(
N
/
2
,
list
.
binarySearch
(
N
/
2
,
BinarySearch
.
SCAN_UP
));
list
.
close
();
}
@Test
public
void
testSearchWithDups
()
{
DirectLongList
list
=
new
DirectLongList
(
256
,
MemoryTag
.
NATIVE_LONG_LIST
);
final
int
N
=
100
;
// 0,0,0,2,2,2,4,4,4,6,6,6...
for
(
int
i
=
0
;
i
<
N
;
++
i
)
{
list
.
add
(
2
*
(
i
/
3
));
try
(
DirectLongList
list
=
new
DirectLongList
(
256
,
MemoryTag
.
NATIVE_LONG_LIST
))
{
final
int
N
=
100
;
// 0,0,0,2,2,2,4,4,4,6,6,6...
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
list
.
add
(
2
*
(
i
/
3
));
}
// existing
Assert
.
assertEquals
(
2
,
list
.
binarySearch
(
0
,
BinarySearch
.
SCAN_DOWN
));
Assert
.
assertEquals
(
0
,
list
.
binarySearch
(
0
,
BinarySearch
.
SCAN_UP
));
// non-existing
Assert
.
assertEquals
(
3
,
-
list
.
binarySearch
(
1
,
BinarySearch
.
SCAN_DOWN
)
-
1
);
Assert
.
assertEquals
(
3
,
-
list
.
binarySearch
(
1
,
BinarySearch
.
SCAN_UP
)
-
1
);
}
}
@Test
public
void
testSet
()
{
try
(
DirectLongList
list
=
new
DirectLongList
(
32
,
MemoryTag
.
NATIVE_DEFAULT
))
{
final
int
N
=
100
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
list
.
add
(
i
);
}
Assert
.
assertEquals
(
128
,
list
.
getCapacity
());
Assert
.
assertEquals
(
N
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
size
();
i
++)
{
Assert
.
assertEquals
(
i
,
list
.
get
(
i
));
}
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
list
.
set
(
i
,
N
-
i
);
}
Assert
.
assertEquals
(
128
,
list
.
getCapacity
());
Assert
.
assertEquals
(
N
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
size
();
i
++)
{
Assert
.
assertEquals
(
N
-
i
,
list
.
get
(
i
));
}
}
// existing
Assert
.
assertEquals
(
2
,
list
.
binarySearch
(
0
,
BinarySearch
.
SCAN_DOWN
));
Assert
.
assertEquals
(
0
,
list
.
binarySearch
(
0
,
BinarySearch
.
SCAN_UP
));
}
// non-existing
Assert
.
assertEquals
(
3
,
-
list
.
binarySearch
(
1
,
BinarySearch
.
SCAN_DOWN
)
-
1
);
Assert
.
assertEquals
(
3
,
-
list
.
binarySearch
(
1
,
BinarySearch
.
SCAN_UP
)
-
1
);
@Test
public
void
testShrink
()
{
try
(
DirectLongList
list
=
new
DirectLongList
(
32
,
MemoryTag
.
NATIVE_DEFAULT
))
{
final
int
N
=
100
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
list
.
add
(
i
);
}
Assert
.
assertEquals
(
128
,
list
.
getCapacity
());
Assert
.
assertEquals
(
N
,
list
.
size
());
for
(
long
i
=
0
;
i
<
list
.
size
();
i
++)
{
Assert
.
assertEquals
(
i
,
list
.
get
(
i
));
}
list
.
shrink
(
N
);
Assert
.
assertEquals
(
N
,
list
.
getCapacity
());
Assert
.
assertEquals
(
N
,
list
.
size
());
list
.
close
();
list
.
shrink
(
16
);
Assert
.
assertEquals
(
16
,
list
.
getCapacity
());
Assert
.
assertEquals
(
16
,
list
.
size
());
}
}
@Test
public
void
testToString
()
{
try
(
DirectLongList
list
=
new
DirectLongList
(
1001
,
MemoryTag
.
NATIVE_LONG_LIST
))
{
final
int
N
=
1000
;
for
(
int
i
=
0
;
i
<
N
;
++
i
)
{
for
(
int
i
=
0
;
i
<
N
;
i
++
)
{
list
.
add
(
i
);
}
String
str1
=
list
.
toString
();
list
.
add
(
1001
);
String
str2
=
list
.
toString
();
Assert
.
assertEquals
(
str1
.
substring
(
0
,
str1
.
length
()
-
1
)
+
", ..
}
"
,
str2
);
Assert
.
assertEquals
(
str1
.
substring
(
0
,
str1
.
length
()
-
1
)
+
", ..
]
"
,
str2
);
}
}
...
...
core/src/test/java/io/questdb/test/std/VectTest.java
浏览文件 @
e5a821cb
...
...
@@ -314,14 +314,14 @@ public class VectTest {
for
(
int
i
=
0
;
i
<
srcLen
;
i
++)
{
src
.
set
(
i
,
(
i
+
1
)
*
10
);
}
Assert
.
assertEquals
(
"
{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
"
,
src
.
toString
());
Assert
.
assertEquals
(
"
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
"
,
src
.
toString
());
index
.
setPos
(
indexLen
*
2
);
for
(
int
i
=
0
;
i
<
indexLen
*
2
;
i
+=
2
)
{
index
.
set
(
i
,
(
i
+
1
)
*
10L
);
index
.
set
(
i
+
1
,
i
/
2
);
}
Assert
.
assertEquals
(
"
{10, 0, 30, 1, 50, 2, 70, 3, 90, 4, 110, 5, 130, 6, 150, 7, 170, 8, 190, 9}
"
,
index
.
toString
());
Assert
.
assertEquals
(
"
[10, 0, 30, 1, 50, 2, 70, 3, 90, 4, 110, 5, 130, 6, 150, 7, 170, 8, 190, 9]
"
,
index
.
toString
());
long
mergedCount
=
Vect
.
mergeDedupTimestampWithLongIndexAsc
(
src
.
getAddress
(),
...
...
@@ -350,10 +350,10 @@ public class VectTest {
for
(
int
i
=
0
;
i
<
srcLen
;
i
++)
{
src
.
set
(
i
,
(
i
+
1
)
*
10
);
}
Assert
.
assertEquals
(
"
{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
"
,
src
.
toString
());
Assert
.
assertEquals
(
"
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
"
,
src
.
toString
());
index
.
setPos
(
indexLen
*
2
);
Assert
.
assertEquals
(
"
{}
"
,
index
.
toString
());
Assert
.
assertEquals
(
"
[]
"
,
index
.
toString
());
long
mergedCount
=
Vect
.
mergeDedupTimestampWithLongIndexAsc
(
src
.
getAddress
(),
...
...
@@ -446,14 +446,14 @@ public class VectTest {
for
(
int
i
=
0
;
i
<
srcLen
;
i
++)
{
src
.
set
(
i
,
(
i
+
1
)
/
2
*
2
*
10
);
}
Assert
.
assertEquals
(
"
{0, 20, 20, 40, 40, 60, 60, 80, 80, 100}
"
,
src
.
toString
());
Assert
.
assertEquals
(
"
[0, 20, 20, 40, 40, 60, 60, 80, 80, 100]
"
,
src
.
toString
());
index
.
setPos
(
indexLen
*
2
);
for
(
int
i
=
0
;
i
<
indexLen
*
2
;
i
+=
2
)
{
index
.
set
(
i
,
(
4
+
i
/
2
*
2
)
*
10
);
index
.
set
(
i
+
1
,
i
/
2
);
}
Assert
.
assertEquals
(
"
{40, 0, 60, 1, 80, 2, 100, 3, 120, 4}
"
,
index
.
toString
());
Assert
.
assertEquals
(
"
[40, 0, 60, 1, 80, 2, 100, 3, 120, 4]
"
,
index
.
toString
());
long
mergedCount
=
Vect
.
mergeDedupTimestampWithLongIndexAsc
(
src
.
getAddress
(),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录