Skip to content

Commit

Permalink
chore: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
yankun1992 committed Sep 19, 2024
1 parent ab8d575 commit 8d504a2
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 144 deletions.
1 change: 0 additions & 1 deletion buffer/src/cc/otavia/buffer/Buffer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import java.lang.{Double as JDouble, Float as JFloat, Long as JLong, Short as JS
import java.nio.ByteBuffer
import java.nio.channels.{FileChannel, ReadableByteChannel, WritableByteChannel}
import java.nio.charset.{Charset, StandardCharsets}
import java.util.UUID
import scala.language.unsafeNulls

/** A wrapper of [[java.nio.ByteBuffer]], with separate reader and writer offsets.
Expand Down
4 changes: 2 additions & 2 deletions core/src/cc/otavia/core/actor/AbstractActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ private[core] abstract class AbstractActor[M <: Call] extends FutureDispatcher w
final private[core] def switchState(stack: Stack, stackYield: StackYield): Unit =
if (!stackYield.completed) {
val oldState = stack.state
val newState = stack.getNextState()
val newState = stack.getNextState
if (oldState != newState) {
stack.setState(newState) // change the stack to new state.
this.recycleStackState(oldState) // recycle old state if enable.
this.recycleStackState(oldState) // recycle old state if enabled.
}
assert(stack.hasUncompletedPromise, s"has no future to wait for $stack")
} else {
Expand Down
9 changes: 4 additions & 5 deletions core/src/cc/otavia/core/actor/Actor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package cc.otavia.core.actor

import cc.otavia.common.SystemPropertyUtil
import cc.otavia.core.actor.Actor.*
import cc.otavia.core.address.Address
import cc.otavia.core.message.*
Expand Down Expand Up @@ -85,28 +84,28 @@ trait Actor[+M <: Call] {
// method for receive message

/** receive notice message by this method, the method will be call when this actor instance receive notice message
* @param notice
* @param envelope
* notice message receive by this actor instance
*/
private[core] def receiveNotice(envelope: Envelope[?]): Unit

/** receive ask message by this method, the method will be call when this actor instance receive ask message
*
* @param ask
* @param envelope
* ask message received by this actor instance
*/
private[core] def receiveAsk(envelope: Envelope[?]): Unit

/** receive reply message by this method, the method will be call when this actor instance receive reply message
*
* @param reply
* @param envelope
* reply message receive by this actor instance
*/
private[core] def receiveReply(envelope: Envelope[?]): Unit

/** receive exception reply message by this method, the method will be call when this actor instance receive reply
* message
* @param exceptionMessage
* @param envelope
* exception reply message receive by this actor instance
*/
private[core] def receiveExceptionReply(envelope: Envelope[?]): Unit
Expand Down
2 changes: 1 addition & 1 deletion core/src/cc/otavia/core/actor/SocketChannelsActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import java.net.{InetAddress, InetSocketAddress, SocketAddress}
abstract class SocketChannelsActor[M <: Call] extends ChannelsActor[M] {

/** Request to connect to the given [[SocketAddress]]. This method return a channel which is not connected to the
* remote address, it only register this channel to [[Reactor]], when register operation completes, this actor will
* remote address, it only registers this channel to [[Reactor]], when register operation completes, this actor will
* receive a [[ReactorEvent.RegisterReply]] event, then this actor will call [[afterChannelRegisterReplyEvent]] to
* handle register result and connect to remote address.
*
Expand Down
7 changes: 3 additions & 4 deletions core/src/cc/otavia/core/stack/Stack.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@

package cc.otavia.core.stack

import cc.otavia.core.actor.{AbstractActor, Actor}
import cc.otavia.core.actor.AbstractActor
import cc.otavia.core.cache.Poolable
import cc.otavia.core.message.Call
import cc.otavia.core.util.Nextable

import scala.language.unsafeNulls

abstract class Stack extends Poolable {

private var stackState: StackState = StackState.start
private var nextState: StackState = _
private var nextState: StackState = _

// completed promise
private var completedHead: AbstractPromise[?] = _
Expand Down Expand Up @@ -63,7 +62,7 @@ abstract class Stack extends Poolable {
StackYield.SUSPEND
}

private[core] def getNextState(): StackState = {
private[core] def getNextState: StackState = {
val state = nextState
nextState = null
state
Expand Down
2 changes: 0 additions & 2 deletions core/src/cc/otavia/core/stack/StackState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package cc.otavia.core.stack

import cc.otavia.core.cache.Poolable
import cc.otavia.core.message.Reply
import cc.otavia.core.stack.helper.StartState

trait StackState {
Expand Down
4 changes: 2 additions & 2 deletions core/src/cc/otavia/core/stack/StackYield.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ sealed trait StackYield {
object StackYield {

private[core] val SUSPEND = new StackYield {
override def completed: Boolean = false
final override def completed: Boolean = false
}

private[core] val RETURN = new StackYield {
override def completed: Boolean = true
final override def completed: Boolean = true
}

}
2 changes: 1 addition & 1 deletion core/src/cc/otavia/core/stack/helper/StartState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ package cc.otavia.core.stack.helper
import cc.otavia.core.stack.{Stack, StackState}

/** The first [[StackState]] of a [[Stack]], it only have one constant value [[StackState.start]]. */
class StartState private[core] () extends StackState {
class StartState private[core] extends StackState {
final override def resumable(): Boolean = true
}
7 changes: 3 additions & 4 deletions core/src/cc/otavia/core/system/ActorSystem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,17 @@ object ActorSystem {
// buffer setting
private val DEFAULT_PAGE_SIZE: Int = 4
private val ENABLE_PAGE_SIZES: Array[Int] = Array(1, 2, 4, 8, 16)
private val K: Int = 1024

val PAGE_SIZE: Int = {
val size = SystemPropertyUtil.getInt("cc.otavia.buffer.page.size", DEFAULT_PAGE_SIZE)
if (ENABLE_PAGE_SIZES.contains(size)) size * K
if (ENABLE_PAGE_SIZES.contains(size)) size * 1024
else {
Report.report(
s"cc.otavia.buffer.page.size is set to $size, but only support ${ENABLE_PAGE_SIZES
.mkString("[", ", ", "]")}, set to default ${DEFAULT_PAGE_SIZE * K} ",
.mkString("[", ", ", "]")}, set to default ${DEFAULT_PAGE_SIZE * 1024} ",
"Buffer"
)
DEFAULT_PAGE_SIZE * K
DEFAULT_PAGE_SIZE * 1024
}
}

Expand Down
18 changes: 0 additions & 18 deletions serde-json/src/cc/otavia/json/JsonConstants.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package cc.otavia.json

import cc.otavia.util.ASCII

import java.nio.charset.StandardCharsets
import scala.language.unsafeNulls

Expand All @@ -30,22 +28,6 @@ object JsonConstants {

val TOKEN_NULL: Array[Byte] = "null".getBytes(StandardCharsets.US_ASCII)

val TOKEN_COMMA: Byte = ASCII.COMMA
val TOKEN_COLON: Byte = ASCII.COLON
val TOKEN_DOUBLE_QUOTE: Byte = ASCII.DOUBLE_QUOTE

val TOKEN_OBJECT_START: Byte = ASCII.BRACE_LEFT
val TOKEN_OBJECT_END: Byte = ASCII.BRACE_RIGHT

val TOKEN_ARRAY_START: Byte = ASCII.BRACKET_LEFT
val TOKEN_ARRAY_END: Byte = ASCII.BRACKET_RIGHT

val TOKEN_POINT: Byte = ASCII.DOT
val TOKEN_PLUS: Byte = ASCII.PLUS
val TOKEN_MINUS: Byte = ASCII.MINUS_SIGN
val TOKEN_ZERO: Byte = ASCII.ZERO
val TOKEN_NINE: Byte = ASCII.NINE

val TOKEN_NUMBERS: Array[Byte] = "1234567890".getBytes(StandardCharsets.US_ASCII)
val TOKEN_FLOATS: Array[Byte] = "1234567890.".getBytes(StandardCharsets.US_ASCII)

Expand Down
53 changes: 24 additions & 29 deletions serde-json/src/cc/otavia/json/JsonHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@
package cc.otavia.json

import cc.otavia.buffer.{Buffer, BufferUtils}
import cc.otavia.datatype.Money
import cc.otavia.json.types.*
import cc.otavia.serde.{Serde, SerdeOps}
import cc.otavia.util.ASCII

import java.math.{BigInteger, BigDecimal as JBigDecimal}
import java.nio.charset.{Charset, StandardCharsets}
import java.math.BigInteger
import java.nio.charset.StandardCharsets
import java.time.{Duration as JDuration, *}
import java.util.{Currency, Locale, UUID}
import scala.collection.mutable
import scala.compiletime.*
import java.util.UUID
import scala.concurrent.duration.Duration
import scala.deriving.Mirror
import scala.language.unsafeNulls

private[json] object JsonHelper {
Expand All @@ -45,47 +40,47 @@ private[json] object JsonHelper {
in.skipIfNextAre(token)
}

final def serializeObjectStart(out: Buffer): Unit = out.writeByte(JsonConstants.TOKEN_OBJECT_START)
final def serializeObjectStart(out: Buffer): Unit = out.writeByte(ASCII.BRACE_LEFT)

// TODO: replace to exceptXXX, if false throws error
final def skipObjectStart(in: Buffer): Boolean = {
skipBlanks(in)
in.skipIfNextIs(JsonConstants.TOKEN_OBJECT_START)
in.skipIfNextIs(ASCII.BRACE_LEFT)
}

final def serializeArrayStart(out: Buffer): Unit = out.writeByte(JsonConstants.TOKEN_ARRAY_START)
final def serializeArrayStart(out: Buffer): Unit = out.writeByte(ASCII.BRACKET_LEFT)

final def skipArrayStart(in: Buffer): Boolean = {
skipBlanks(in)
in.skipIfNextIs(JsonConstants.TOKEN_ARRAY_START)
in.skipIfNextIs(ASCII.BRACKET_LEFT)
}

final def serializeObjectEnd(out: Buffer): Unit = out.writeByte(JsonConstants.TOKEN_OBJECT_END)
final def serializeObjectEnd(out: Buffer): Unit = out.writeByte(ASCII.BRACE_RIGHT)

final def skipObjectEnd(in: Buffer): Boolean = {
skipBlanks(in)
in.skipIfNextIs(JsonConstants.TOKEN_OBJECT_END)
in.skipIfNextIs(ASCII.BRACE_RIGHT)
}

final def serializeArrayEnd(out: Buffer): Unit = out.writeByte(JsonConstants.TOKEN_ARRAY_END)
final def serializeArrayEnd(out: Buffer): Unit = out.writeByte(ASCII.BRACKET_RIGHT)

final def skipArrayEnd(in: Buffer): Boolean = {
skipBlanks(in)
in.skipIfNextIs(JsonConstants.TOKEN_ARRAY_END)
in.skipIfNextIs(ASCII.BRACKET_RIGHT)
}

final def serializeKey(key: String, out: Buffer): Unit = {
out.writeByte(JsonConstants.TOKEN_DOUBLE_QUOTE)
out.writeByte('\"')
BufferUtils.writeEscapedString(out, key)
out.writeByte(JsonConstants.TOKEN_DOUBLE_QUOTE)
out.writeByte(JsonConstants.TOKEN_COLON)
out.writeByte('\"')
out.writeByte(ASCII.COLON)
}

final def serializeKey(key: Array[Byte], out: Buffer): Unit = {
out.writeByte(JsonConstants.TOKEN_DOUBLE_QUOTE)
out.writeByte('\"')
out.writeBytes(key)
out.writeByte(JsonConstants.TOKEN_DOUBLE_QUOTE)
out.writeByte(JsonConstants.TOKEN_COLON)
out.writeByte('\"')
out.writeByte(ASCII.COLON)
}

final def serializeNull(out: Buffer): Unit = out.writeBytes(JsonConstants.TOKEN_NULL)
Expand All @@ -95,9 +90,9 @@ private[json] object JsonHelper {
final def serializeBoolean(boolean: Boolean, out: Buffer): Unit = BufferUtils.writeBooleanAsString(out, boolean)

final def serializeChar(char: Char, out: Buffer): Unit = {
out.writeByte(JsonConstants.TOKEN_DOUBLE_QUOTE)
out.writeByte('\"')
BufferUtils.writeEscapedChar(out, char)
out.writeByte(JsonConstants.TOKEN_DOUBLE_QUOTE)
out.writeByte('\"')
}

final def serializeShort(short: Short, out: Buffer): Unit = BufferUtils.writeShortAsString(out, short)
Expand Down Expand Up @@ -128,9 +123,9 @@ private[json] object JsonHelper {

final def deserializeChar(in: Buffer): Char = {
skipBlanks(in)
assert(in.skipIfNextIs(JsonConstants.TOKEN_DOUBLE_QUOTE), s"except \" but get ${in.readByte}")
assert(in.skipIfNextIs('\"'), s"except \" but get ${in.readByte}")
val b = BufferUtils.readEscapedChar(in)
assert(in.skipIfNextIs(JsonConstants.TOKEN_DOUBLE_QUOTE), s"except \" but get ${in.readByte}")
assert(in.skipIfNextIs('\"'), s"except \" but get ${in.readByte}")
b
}

Expand All @@ -155,8 +150,8 @@ private[json] object JsonHelper {

final def deserializeString(in: Buffer): String = {
skipBlanks(in)
assert(in.skipIfNextIs(JsonConstants.TOKEN_DOUBLE_QUOTE), s"except \" but get ${in.readByte}")
val len = in.bytesBefore(JsonConstants.TOKEN_DOUBLE_QUOTE) // TODO: escape
assert(in.skipIfNextIs('\"'), s"except \" but get ${in.readByte}")
val len = in.bytesBefore('\"'.toByte) // TODO: escape
val str = in.readCharSequence(len, StandardCharsets.UTF_8).toString
in.readByte
str
Expand Down
Loading

0 comments on commit 8d504a2

Please sign in to comment.