Skip to content

Commit d7e11db

Browse files
committed
Migrate to wvlet-log
1 parent 853444e commit d7e11db

21 files changed

Lines changed: 600 additions & 458 deletions

File tree

build.sbt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ lazy val larrayScala = Project(
105105
},
106106
libraryDependencies ++= Seq(
107107
// Add dependent jars here
108-
"org.xerial" %% "xerial-core" % "3.6.0",
108+
"org.wvlet" %% "wvlet-log" % "1.1",
109109
snappy % "test",
110110
junit,
111111
"org.iq80.snappy" % "snappy" % "0.3" % "test",
@@ -124,11 +124,12 @@ lazy val larrayBuffer = Project(
124124
settings = buildSettings ++ Seq(
125125
description := "LArray off-heap buffer library",
126126
crossPaths := false,
127+
scalaVersion := SCALA_VERSION,
127128
autoScalaLibrary := false,
128129
libraryDependencies ++= Seq(
129-
"org.scalatest" %% "scalatest" % "3.0.1" % "test",
130130
"org.xerial.java" % "xerial-core" % "2.1",
131-
"org.xerial" %% "xerial-core" % "3.6.0" % "test"
131+
"org.scalatest" %% "scalatest" % "3.0.1" % "test",
132+
"org.wvlet" %% "wvlet-log" % "1.1" % "test"
132133
)
133134
)
134135
)

larray-buffer/src/main/java/xerial/larray/buffer/DefaultMemoryAllocator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
*/
1818
public class DefaultMemoryAllocator implements MemoryAllocator {
1919

20-
private Logger logger = Logger.getLogger(DefaultMemoryAllocator.class);
21-
20+
private static Logger logger = Logger.getLogger(DefaultMemoryAllocator.class);
2221

2322
// Table from address -> MemoryReference
2423
private Map<Long, MemoryReference> allocatedMemoryReferences = new ConcurrentHashMap<Long, MemoryReference>();

larray-buffer/src/main/java/xerial/larray/buffer/UnsafeUtil.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/**
1010
* Helper methods for using sun.misc.Unsafe.
11+
*
1112
* @author Taro L. Saito
1213
*/
1314
public class UnsafeUtil {
@@ -17,9 +18,11 @@ public static Unsafe getUnsafe() {
1718
Field f = Unsafe.class.getDeclaredField("theUnsafe");
1819
f.setAccessible(true);
1920
return Unsafe.class.cast(f.get(null));
20-
} catch (NoSuchFieldException e) {
21+
}
22+
catch(NoSuchFieldException e) {
2123
throw new IllegalStateException("sun.misc.Unsafe is not available in this JVM");
22-
} catch (IllegalAccessException e) {
24+
}
25+
catch(IllegalAccessException e) {
2326
throw new IllegalStateException("sun.misc.Unsafe is not available in this JVM");
2427
}
2528
}
@@ -29,9 +32,11 @@ public static Unsafe getUnsafe() {
2932
private static Constructor<?> findDirectByteBufferConstructor() {
3033
try {
3134
return Class.forName("java.nio.DirectByteBuffer").getDeclaredConstructor(Long.TYPE, Integer.TYPE, Object.class);
32-
} catch (ClassNotFoundException e) {
35+
}
36+
catch(ClassNotFoundException e) {
3337
throw new IllegalStateException(String.format("Failed to find java.nio.DirectByteBuffer: $s", e.getMessage()));
34-
} catch (NoSuchMethodException e) {
38+
}
39+
catch(NoSuchMethodException e) {
3540
throw new IllegalStateException(String.format("Failed to find constructor f java.nio.DirectByteBuffer: $s", e.getMessage()));
3641
}
3742
}
@@ -44,9 +49,9 @@ private static Constructor<?> findDirectByteBufferConstructor() {
4449
*
4550
* @param addr
4651
* @param size
47-
* @param att object holding the underlying memory to attach to the buffer.
48-
* This will prevent the garbage collection of the memory area that's
49-
* associated with the new <code>DirectByteBuffer</code>
52+
* @param att object holding the underlying memory to attach to the buffer.
53+
* This will prevent the garbage collection of the memory area that's
54+
* associated with the new <code>DirectByteBuffer</code>
5055
* @return
5156
*/
5257
public static ByteBuffer newDirectByteBuffer(long addr, int size, Object att) {
@@ -55,7 +60,8 @@ public static ByteBuffer newDirectByteBuffer(long addr, int size, Object att) {
5560
try {
5661
b = dbbCC.newInstance(new Long(addr), new Integer(size), att);
5762
return ByteBuffer.class.cast(b);
58-
} catch (Exception e) {
63+
}
64+
catch(Exception e) {
5965
throw new IllegalStateException(String.format("Failed to create DirectByteBuffer: %s", e.getMessage()));
6066
}
6167
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
//--------------------------------------
16+
//
17+
// DataUnit.scala
18+
// Since: 2012/12/07 1:38 PM
19+
//
20+
//--------------------------------------
21+
22+
package xerial.larray
23+
24+
/**
25+
* Translators of data sizes
26+
*
27+
* @author Taro L. Saito
28+
*/
29+
object DataUnit {
30+
31+
/**
32+
* Convert the given byte size into human readable format like 1024 -> 1K
33+
* @param byteSize
34+
* @return string representation of the byte size
35+
*/
36+
def toHumanReadableFormat(byteSize:Long) : String = {
37+
// kilo, mega, giga, tera, peta, exa, zetta, yotta
38+
val unitName = Seq("", "K", "M", "G", "T", "P", "E", "Z", "Y")
39+
40+
def loop(index:Int, v:Long) : (Long, String) = {
41+
if(index >= unitName.length)
42+
(byteSize, "")
43+
val next = v >> 10L
44+
if(next == 0L)
45+
(v, unitName(index))
46+
else
47+
loop(index+1, next)
48+
}
49+
50+
val (prefix, unit) = if(byteSize > 0)
51+
loop(0, byteSize)
52+
else
53+
loop(0, -byteSize) match { case (p, u) => (-p, u)}
54+
s"$prefix$unit"
55+
}
56+
57+
// implicit class DataSize(size:Long) {
58+
// def K = size * KB
59+
// def M = size * MB
60+
// def G = size * GB
61+
// def P = size * PB
62+
// }
63+
64+
// data size unit
65+
val KB = 1L << 10
66+
val MB = 1L << 20
67+
val GB = 1L << 30
68+
val PB = 1L << 40
69+
70+
}
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
11
package xerial.larray
22

33
import org.scalatest._
4-
import xerial.core.io.Resource
5-
import xerial.core.util.Timer
6-
import xerial.core.log.Logger
4+
import wvlet.log.LogFormatter.SourceCodeLogFormatter
5+
import wvlet.log.{LogSupport, Logger}
6+
import wvlet.log.io.{ResourceReader, StopWatch, Timer}
7+
78
import scala.language.implicitConversions
89

910
/**
1011
* @author Taro L. Saito
1112
*/
12-
trait LArraySpec extends WordSpec with Matchers with Resource with Timer with Logger
13-
with BeforeAndAfterAll with BeforeAndAfter with BeforeAndAfterEach with GivenWhenThen {
13+
trait LArraySpec
14+
extends WordSpec
15+
with Matchers
16+
with ResourceReader
17+
with Timer
18+
with LogSupport
19+
with BeforeAndAfterAll
20+
with BeforeAndAfter
21+
with BeforeAndAfterEach
22+
with GivenWhenThen {
1423

1524
implicit def toTag(t:String) = Tag(t)
1625

26+
Logger.setDefaultFormatter(SourceCodeLogFormatter)
27+
28+
override protected def beforeAll(): Unit = {
29+
// Run LogLevel scanner (log-test.properties or log.properties in classpath) every 1 minute
30+
Logger.scheduleLogLevelScan
31+
super.beforeAll()
32+
}
33+
34+
override protected def afterAll(): Unit = {
35+
Logger.stopScheduledLogLevelScan
36+
super.afterAll()
37+
}
1738
}

larray-buffer/src/test/scala/xerial/larray/buffer/LBufferTest.scala

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
package xerial.larray.buffer
99

1010
import java.nio.ByteBuffer
11-
import xerial.larray.LArraySpec
12-
import scala.util.Random
13-
import xerial.core.util.DataUnit
1411

12+
import xerial.larray.{DataUnit, LArraySpec}
1513

1614
/**
1715
* @author Taro L. Saito
@@ -69,34 +67,34 @@ class LBufferTest extends LArraySpec {
6967

7068
info("start buffer allocation test")
7169

72-
time("single-thread allocation", repeat=10) {
73-
block("without zero-filling", repeat=R) {
70+
time("single-thread allocation", repeat=10, blockRepeat = R) {
71+
block("without zero-filling") {
7472
for(i <- range) yield {
7573
new LBuffer(S)
7674
}
7775
}
7876

79-
block("with zero-filling", repeat=R) {
77+
block("with zero-filling") {
8078
for(i <- range) yield {
8179
val m = new LBuffer(S)
8280
m.clear()
8381
m
8482
}
8583
}
8684

87-
block("java array", repeat=R) {
85+
block("java array") {
8886
for(i <- range) yield {
8987
new Array[Byte](S)
9088
}
9189
}
9290

93-
block("byte buffer", repeat=R) {
91+
block("byte buffer") {
9492
for(i <- range) yield {
9593
ByteBuffer.allocate(S)
9694
}
9795
}
9896

99-
block("direct byte buffer", repeat=R) {
97+
block("direct byte buffer") {
10098
for(i <- range) yield {
10199
ByteBuffer.allocateDirect(S)
102100
}
@@ -114,34 +112,34 @@ class LBufferTest extends LArraySpec {
114112

115113
info("start buffer allocation test")
116114

117-
time("concurrent allocation", repeat=10) {
118-
block("without zero-filling", repeat=R) {
115+
time("concurrent allocation", repeat=10, blockRepeat = R) {
116+
block("without zero-filling") {
119117
for(i <- range) yield {
120118
new LBuffer(S)
121119
}
122120
}
123121

124-
block("with zero-filling", repeat=R) {
122+
block("with zero-filling") {
125123
for(i <- range) yield {
126124
val m = new LBuffer(S)
127125
m.clear()
128126
m
129127
}
130128
}
131129

132-
block("java array", repeat=R) {
130+
block("java array") {
133131
for(i <- range) yield {
134132
new Array[Byte](S)
135133
}
136134
}
137135

138-
block("byte buffer", repeat=R) {
136+
block("byte buffer") {
139137
for(i <- range) yield {
140138
ByteBuffer.allocate(S)
141139
}
142140
}
143141

144-
block("direct byte buffer", repeat=R) {
142+
block("direct byte buffer") {
145143
for(i <- range) yield {
146144
ByteBuffer.allocateDirect(S)
147145
}

larray/src/main/scala/xerial/larray/LArray.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
package xerial.larray
2424

2525
import scala.reflect.ClassTag
26-
import xerial.core.log.Logger
27-
import java.nio.{ByteOrder, ByteBuffer}
26+
import java.nio.{ByteBuffer, ByteOrder}
2827
import java.nio.channels.{FileChannel, WritableByteChannel}
28+
2929
import sun.nio.ch.DirectBuffer
30-
import java.io.{FileInputStream, FileOutputStream, File}
30+
import java.io.{File, FileInputStream, FileOutputStream}
31+
32+
import wvlet.log.LogSupport
3133
import xerial.larray.buffer.{Memory, MemoryAllocator}
3234
import xerial.larray.mmap.MMapMode
3335

@@ -742,7 +744,7 @@ trait RawByteArray[A] extends LArray[A] {
742744
}
743745

744746

745-
private[larray] trait UnsafeArray[T] extends RawByteArray[T] with Logger {
747+
private[larray] trait UnsafeArray[T] extends RawByteArray[T] with LogSupport {
746748
self: LArray[T] =>
747749
private[larray] def m: Memory
748750
def address = m.address

0 commit comments

Comments
 (0)