snowflake-core (empty) → 0.1.0.0
raw patch · 5 files changed
+199/−0 lines, 5 filesdep +QuickCheckdep +basedep +snowflake-coresetup-changed
Dependencies added: QuickCheck, base, snowflake-core, time-exts
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- snowflake-core.cabal +35/−0
- src/Snowflake.hs +61/−0
- test/Test.hs +71/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright KaiJia (c) 2016++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of KaiJia nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ snowflake-core.cabal view
@@ -0,0 +1,35 @@+name: snowflake-core+version: 0.1.0.0+synopsis: Initial project template from stack+description: Please see README.md+homepage: https://github.com/jiakai0419/snowflake#readme+license: BSD3+license-file: LICENSE+author: KaiJia+maintainer: jiakai0419@gmail.com+copyright: Copyright: (c) 2016 KaiJia+category: Demo+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: Snowflake+ build-depends: base >= 4.7 && < 5+ , time-exts+ default-language: Haskell2010++test-suite snowflake-test+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Test.hs+ build-depends: base+ , snowflake-core+ , QuickCheck+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/jiakai0419/snowflake
+ src/Snowflake.hs view
@@ -0,0 +1,61 @@+{-# LANGUAGE TemplateHaskell #-}+module Snowflake+ ( Timestamp+ , Conf(..)+ , IdWorker(..)+ , defaultConf+ , next+ , nexts+ ) where+++import Prelude hiding (sequence)+import Data.Int (Int64)+import Data.Maybe (Maybe, isNothing, fromJust)+import Data.Bits ( (.|.)+ , shiftL+ )+import Data.Time.Exts.Unix ( getCurrentUnixDateTimeMillis+ , UnixDateTimeMillis(..)+ )++type Timestamp = Int64++data Conf = Conf { _sequenceBits :: Int+ , _workerIdBits :: Int+ , _datacenterIdBits :: Int+ , _twepoch :: Timestamp -- timestamp base-line+ } deriving (Show)++data IdWorker = IdWorker { _sequence :: Int64+ , _workerId :: Int64+ , _datacenterId :: Int64+ , _timestamp :: Timestamp+ , _conf :: Conf+ } deriving (Show)++defaultConf = Conf { _sequenceBits = 12+ , _workerIdBits = 5+ , _datacenterIdBits = 5+ , _twepoch = 1472733628921+ }++next :: IdWorker -> Timestamp -> (Maybe Int64, IdWorker)+next worker@(IdWorker lastSq wid did lastTs (Conf sBits wBits dBits twepoch)) ts+ | lastTs == ts && lastSq == 2^sBits - 1 = (Nothing, worker)+ | otherwise = (Just newId, worker {_sequence = newSq, _timestamp = ts})+ where+ newSq = if lastTs == ts then lastSq + 1 else 0+ newId = tBit .|. dBit .|. wBit .|. newSq+ wBit = wid `shiftL` sBits+ dBit = did `shiftL` (sBits + wBits)+ tBit = (ts - twepoch) `shiftL` (sBits + wBits + dBits)++nexts :: IdWorker -> Int -> IO ([Int64], IdWorker)+nexts worker 0 = return ([], worker)+nexts worker n = do+ timestamp <- _udt_mil_base <$> getCurrentUnixDateTimeMillis+ let (maybeNewId, newWorker) = next worker timestamp in+ if isNothing maybeNewId+ then nexts worker n+ else (\(ids, wk) -> (fromJust maybeNewId : ids, wk)) <$> nexts newWorker (n-1)
+ test/Test.hs view
@@ -0,0 +1,71 @@+{-# LANGUAGE ScopedTypeVariables, TemplateHaskell #-}+module Main where++import Test.QuickCheck+import Test.QuickCheck.All+import Test.QuickCheck.Monadic+import Snowflake+import Data.Int (Int64)+import Data.List (sort)++--------------------------------------------------------------------------+-- constant++maxScale = 10^6++--------------------------------------------------------------------------+-- arbitrary instance++instance Arbitrary IdWorker where+ arbitrary = do+ sequence <- choose (0, 2^(_sequenceBits defaultConf) - 1) :: Gen Int64+ workerId <- choose (0, 2^(_workerIdBits defaultConf) - 1) :: Gen Int64+ datacenterId <- choose (0, 2^(_datacenterIdBits defaultConf) - 1) :: Gen Int64+ return IdWorker { _sequence = sequence+ , _workerId = workerId+ , _datacenterId = datacenterId+ , _timestamp = 0+ , _conf = defaultConf+ }++--------------------------------------------------------------------------+-- size consist with n++prop_sc worker n = 0 <= n && n <= maxScale ==> monadicIO $ do+ (ids, _) <- run $ nexts worker n+ assert $ length ids == n++--------------------------------------------------------------------------+-- strict ascending order++isStrictAsc :: Ord a => [a] -> Bool+isStrictAsc (x:y:xs)+ | x < y = isStrictAsc (y:xs)+ | otherwise = False+isStrictAsc _ = True++prop_asc worker n = 0 <= n && n <= maxScale ==> monadicIO $ do+ (ids, _) <- run $ nexts worker n+ assert $ isStrictAsc ids++--------------------------------------------------------------------------+-- no-repeat++isNoRepeat :: Ord a => [a] -> Bool+isNoRepeat = isNoRepeat' . sort++isNoRepeat' :: Eq a => [a] -> Bool+isNoRepeat' (x:y:xs)+ | x /= y = isNoRepeat' (y:xs)+ | otherwise = False+isNoRepeat' _ = True++prop_np worker n = 0 <= n && n <= maxScale ==> monadicIO $ do+ (ids, _) <- run $ nexts worker n+ assert $ isNoRepeat ids++--------------------------------------------------------------------------+-- main++return []+main = $quickCheckAll