|
| 1 | +/* |
| 2 | +Copyright 2012 Twitter, Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package com.twitter.chill.algebird |
| 17 | + |
| 18 | +import com.esotericsoftware.kryo.Kryo |
| 19 | +import com.esotericsoftware.kryo.{ Serializer => KSerializer } |
| 20 | +import com.esotericsoftware.kryo.io.{ Input, Output } |
| 21 | + |
| 22 | +import com.twitter.algebird.{ |
| 23 | + AveragedValue, |
| 24 | + DecayedValue, |
| 25 | + HLL, |
| 26 | + HyperLogLog, |
| 27 | + HyperLogLogMonoid, |
| 28 | + Moments, |
| 29 | + SpaceSaver, |
| 30 | + SSOne, |
| 31 | + SSMany |
| 32 | +} |
| 33 | + |
| 34 | +import scala.collection.mutable.{ Map => MMap } |
| 35 | +import scala.collection.immutable.SortedMap |
| 36 | + |
| 37 | +class AveragedValueSerializer extends KSerializer[AveragedValue] { |
| 38 | + setImmutable(true) |
| 39 | + def write(kser: Kryo, out: Output, s: AveragedValue) { |
| 40 | + out.writeLong(s.count, true) |
| 41 | + out.writeDouble(s.value) |
| 42 | + } |
| 43 | + def read(kser: Kryo, in: Input, cls: Class[AveragedValue]): AveragedValue = |
| 44 | + AveragedValue(in.readLong(true), in.readDouble) |
| 45 | +} |
| 46 | + |
| 47 | +class MomentsSerializer extends KSerializer[Moments] { |
| 48 | + setImmutable(true) |
| 49 | + def write(kser: Kryo, out: Output, s: Moments) { |
| 50 | + out.writeLong(s.m0, true) |
| 51 | + out.writeDouble(s.m1) |
| 52 | + out.writeDouble(s.m2) |
| 53 | + out.writeDouble(s.m3) |
| 54 | + out.writeDouble(s.m4) |
| 55 | + } |
| 56 | + def read(kser: Kryo, in: Input, cls: Class[Moments]): Moments = { |
| 57 | + Moments(in.readLong(true), |
| 58 | + in.readDouble, |
| 59 | + in.readDouble, |
| 60 | + in.readDouble, |
| 61 | + in.readDouble) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class DecayedValueSerializer extends KSerializer[DecayedValue] { |
| 66 | + setImmutable(true) |
| 67 | + def write(kser: Kryo, out: Output, s: DecayedValue) { |
| 68 | + out.writeDouble(s.value) |
| 69 | + out.writeDouble(s.scaledTime) |
| 70 | + } |
| 71 | + def read(kser: Kryo, in: Input, cls: Class[DecayedValue]): DecayedValue = |
| 72 | + DecayedValue(in.readDouble, in.readDouble) |
| 73 | +} |
| 74 | + |
| 75 | +class HLLSerializer extends KSerializer[HLL] { |
| 76 | + setImmutable(true) |
| 77 | + def write(kser: Kryo, out: Output, s: HLL) { |
| 78 | + val bytes = HyperLogLog.toBytes(s) |
| 79 | + out.writeInt(bytes.size, true) |
| 80 | + out.writeBytes(bytes) |
| 81 | + } |
| 82 | + def read(kser: Kryo, in: Input, cls: Class[HLL]): HLL = { |
| 83 | + HyperLogLog.fromBytes(in.readBytes(in.readInt(true))) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +class HLLMonoidSerializer extends KSerializer[HyperLogLogMonoid] { |
| 88 | + setImmutable(true) |
| 89 | + val hllMonoids = MMap[Int, HyperLogLogMonoid]() |
| 90 | + def write(kser: Kryo, out: Output, mon: HyperLogLogMonoid) { |
| 91 | + out.writeInt(mon.bits, true) |
| 92 | + } |
| 93 | + def read(kser: Kryo, in: Input, cls: Class[HyperLogLogMonoid]): HyperLogLogMonoid = { |
| 94 | + val bits = in.readInt(true) |
| 95 | + hllMonoids.getOrElseUpdate(bits, new HyperLogLogMonoid(bits)) |
| 96 | + } |
| 97 | +} |
0 commit comments