[refactoring] cache refactoring (highlight)

Also, tests fixed a bit (at least they work).
parent 68d3856d
Pipeline #4269 canceled with stage
...@@ -63,7 +63,8 @@ ...@@ -63,7 +63,8 @@
"react-tooltip": "^4.2.8", "react-tooltip": "^4.2.8",
"secp256k1": "^4.0.2", "secp256k1": "^4.0.2",
"sigma": "^2.4.0", "sigma": "^2.4.0",
"twgl.js": "^5.0.4" "twgl.js": "^5.0.4",
"uuid": "8.3.2"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.15.0", "@babel/core": "^7.15.0",
......
...@@ -100,11 +100,16 @@ let ...@@ -100,11 +100,16 @@ let
#spago build #spago build
#build-purs #build-purs
echo "Testing" echo "Testing"
spago -x test.dhall test --main Test.Main spago -v -x test.dhall test --main Test.Main
# spago -x test.dhall bundle-app --main Test.Main --to dist/test-bundle.js --platform node
# pulp browserify --skip-compile -t dist/bundle.js --src-path output # pulp browserify --skip-compile -t dist/bundle.js --src-path output
# pulp test --src-path output --test-path output # pulp test --src-path output --test-path output
#NODE_PATH=output node -e "require('Test.Main').main();" #NODE_PATH=output node -e "require('Test.Main').main();"
''; '';
# TODO: Remove this when nixpkgs is updated to newer version (23.05
# has nodejs 20.2.1 and we need >= 20.3.1 for crypto to work)
#nodejs_20_3_1 = pkgs.callPackage ./nix/nodejs-v20.nix {};
in in
pkgs.mkShell { pkgs.mkShell {
buildInputs = [ buildInputs = [
...@@ -126,6 +131,7 @@ pkgs.mkShell { ...@@ -126,6 +131,7 @@ pkgs.mkShell {
pkgs.nodejs pkgs.nodejs
#pkgs.nodePackages.purescript-language-server #pkgs.nodePackages.purescript-language-server
#pkgs.python # needed for msgpack etc #pkgs.python # needed for msgpack etc
#nodejs_20_3_1
repl repl
serve serve
pkgs.pulp pkgs.pulp
......
...@@ -74,7 +74,6 @@ to generate this file without the comments in this block. ...@@ -74,7 +74,6 @@ to generate this file without the comments in this block.
, "sequences" , "sequences"
, "simple-json" , "simple-json"
, "simple-json-generics" , "simple-json-generics"
, "simplecrypto"
, "string-search" , "string-search"
, "strings" , "strings"
, "strings-extra" , "strings-extra"
......
...@@ -148,7 +148,7 @@ compile :: ...@@ -148,7 +148,7 @@ compile ::
-> NgramsTable -> NgramsTable
-> Maybe String -> Maybe String
-> Array HighlightElement -> Array HighlightElement
compile cache ngrams = maybe [] (highlightNgrams CTabTerms ngrams cache) compile cache ngrams = maybe [] (highlightNgrams cache CTabTerms ngrams)
-- Runs -- Runs
......
...@@ -12,7 +12,7 @@ import Data.List as List ...@@ -12,7 +12,7 @@ import Data.List as List
import Data.Maybe (Maybe(..), fromMaybe) import Data.Maybe (Maybe(..), fromMaybe)
import Data.Show.Generic (genericShow) import Data.Show.Generic (genericShow)
import Effect (Effect) import Effect (Effect)
import Effect.Aff (throwError) import Effect.Aff (Aff, throwError)
import Effect.Exception (error) import Effect.Exception (error)
import Gargantext.Components.Bootstrap as B import Gargantext.Components.Bootstrap as B
import Gargantext.Components.Bootstrap.Types (ComponentStatus(..), Variant(..)) import Gargantext.Components.Bootstrap.Types (ComponentStatus(..), Variant(..))
...@@ -27,7 +27,7 @@ import Gargantext.Routes (SessionRoute(Children, NodeAPI)) ...@@ -27,7 +27,7 @@ import Gargantext.Routes (SessionRoute(Children, NodeAPI))
import Gargantext.Sessions (Session, get, put) import Gargantext.Sessions (Session, get, put)
import Gargantext.Types (AffETableResult, NodeType(..), ID) import Gargantext.Types (AffETableResult, NodeType(..), ID)
import Gargantext.Utils ((?)) import Gargantext.Utils ((?))
import Gargantext.Utils.Crypto as Crypto -- import Gargantext.Utils.Crypto as Crypto
import Gargantext.Utils.Reactix as R2 import Gargantext.Utils.Reactix as R2
import Gargantext.Utils.Toestand as T2 import Gargantext.Utils.Toestand as T2
import Reactix as R import Reactix as R
...@@ -112,8 +112,8 @@ fieldsCodeEditorCpt = here.component "fieldsCodeEditorCpt" cpt ...@@ -112,8 +112,8 @@ fieldsCodeEditorCpt = here.component "fieldsCodeEditorCpt" cpt
recomputeIndices :: FTFieldsWithIndex -> FTFieldsWithIndex recomputeIndices :: FTFieldsWithIndex -> FTFieldsWithIndex
recomputeIndices (FTFieldsWithIndex lst) = FTFieldsWithIndex $ mapWithIndex (\idx -> \{ ftField } -> { idx, ftField }) lst recomputeIndices (FTFieldsWithIndex lst) = FTFieldsWithIndex $ mapWithIndex (\idx -> \{ ftField } -> { idx, ftField }) lst
hash :: FTFieldWithIndex -> Hash -- hash :: FTFieldWithIndex -> Aff Hash
hash { idx, ftField } = Crypto.hash $ "--idx--" <> (show idx) <> "--field--" <> (show ftField) -- hash { idx, ftField } = Crypto.hash $ "--idx--" <> (show idx) <> "--field--" <> (show ftField)
type FieldCodeEditorProps = type FieldCodeEditorProps =
( idx :: Int ( idx :: Int
......
...@@ -165,8 +165,8 @@ computeCache ngrams contextNgrams = { contextNgrams, pm, pats } ...@@ -165,8 +165,8 @@ computeCache ngrams contextNgrams = { contextNgrams, pm, pats }
-- TODO: while this function works well with word boundaries, -- TODO: while this function works well with word boundaries,
-- it inserts too many spaces. -- it inserts too many spaces.
highlightNgrams :: CTabNgramType -> NgramsTable -> Record Cache -> String -> Array HighlightElement highlightNgrams :: Record Cache -> CTabNgramType -> NgramsTable -> String -> Array HighlightElement
highlightNgrams ntype table@(NgramsTable {ngrams_repo_elements: elts}) cache@{ pm, pats } input0 = highlightNgrams cache@{ pm, pats } ntype table@(NgramsTable {ngrams_repo_elements: elts}) input0 =
-- trace {pats, input0, input, ixs} \_ -> -- trace {pats, input0, input, ixs} \_ ->
A.fromFoldable ((\(s /\ ls)-> undb s /\ ls) <$> unsafePartial (foldl goFold ((input /\ Nil) : Nil) ixs)) A.fromFoldable ((\(s /\ ls)-> undb s /\ ls) <$> unsafePartial (foldl goFold ((input /\ Nil) : Nil) ixs))
where where
......
//import crypto from 'crypto';
async function getSHA256Hash(input) {
const textAsBuffer = new TextEncoder().encode(input);
const hashBuffer = await globalThis.crypto.subtle.digest("SHA-256", textAsBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hash = hashArray
.map((item) => item.toString(16).padStart(2, "0"))
.join("");
return hash;
};
export const getSHA256HashImpl = (s) => () =>
getSHA256Hash(s);
module Gargantext.Utils.Crypto where module Gargantext.Utils.Crypto where
import Crypto.Simple as Crypto import Control.Promise (Promise, toAffE)
import Data.Set (Set) import Data.Set (Set)
import Data.Set as Set import Data.Set as Set
import Data.Array as Array import Data.Array as Array
import Effect (Effect)
import Effect.Aff (Aff)
import Gargantext.Prelude import Gargantext.Prelude
foreign import getSHA256HashImpl :: String -> Effect (Promise String)
getSHA256Hash :: String -> Aff String
getSHA256Hash = getSHA256HashImpl >>> toAffE
-- | TODO use newtype to disambiguate Set String and Set Hash -- | TODO use newtype to disambiguate Set String and Set Hash
-- Set String needs Set.map hash -- Set String needs Set.map hash
-- Set Hash does not need Set.map hash (just concat) -- Set Hash does not need Set.map hash (just concat)
type Hash = String type Hash = String
hash' :: forall a. Crypto.Hashable a => a -> String hash' :: String -> Aff String
hash' = Crypto.toString <<< Crypto.hash Crypto.SHA256 hash' = getSHA256Hash
--hash' :: forall a. Crypto.Hashable a => a -> String
--hash' = Crypto.toString <<< Crypto.hash Crypto.SHA256
class IsHashable a where class IsHashable a where
hash :: a -> Hash hash :: a -> Aff Hash
instance IsHashable String instance IsHashable String
where where
hash = hash' hash = hash'
instance (Crypto.Hashable a, IsHashable a) => IsHashable (Array a) --instance (Crypto.Hashable a, IsHashable a) => IsHashable (Array a)
-- where
-- hash = hash <<< Set.fromFoldable <<< map hash
instance IsHashable (Array String) where
hash = hash <<< concat
where where
hash = hash <<< Set.fromFoldable <<< map hash concat :: Array Hash -> String
concat = Array.foldl (<>) ""
instance IsHashable (Set String) where instance IsHashable (Set String) where
hash = hash <<< concat <<< toArray hash = hash <<< toArray
where where
toArray :: forall a. Set a -> Array a toArray :: forall a. Set a -> Array a
toArray = Set.toUnfoldable toArray = Set.toUnfoldable
concat :: Array Hash -> String
concat = Array.foldl (<>) ""
...@@ -119,19 +119,21 @@ spec = ...@@ -119,19 +119,21 @@ spec =
let text = "To hash with backend" let text = "To hash with backend"
let hashed = "8a69a94d164279af2b7d1443ce08da6184b3d7e815406076e148159c284b53c3" let hashed = "8a69a94d164279af2b7d1443ce08da6184b3d7e815406076e148159c284b53c3"
-- ^ hash from backend with text above -- ^ hash from backend with text above
Crypto.hash text `shouldEqual` hashed h <- Crypto.hash text
h `shouldEqual` hashed
it "Hash List with backend works" do it "Hash List with backend works" do
let list = ["a","b"] let list = ["a","b"]
let hashed = "ab19ec537f09499b26f0f62eed7aefad46ab9f498e06a7328ce8e8ef90da6d86" let hashed = "ab19ec537f09499b26f0f62eed7aefad46ab9f498e06a7328ce8e8ef90da6d86"
-- ^ hash from backend with text above -- ^ hash from backend with text above
Crypto.hash list `shouldEqual` hashed h <- Crypto.hash list
h`shouldEqual` hashed
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | TODO property based tests -- | TODO property based tests
it "Hash works with any order of list" do it "Hash works with any order of list" do
let hash1 = Crypto.hash ["a","b"] hash1 <- Crypto.hash ["a","b"]
let hash2 = Crypto.hash ["b","a"] hash2 <- Crypto.hash ["b","a"]
hash1 `shouldEqual` hash2 hash1 `shouldEqual` hash2
------------------------------------------------------------------------ ------------------------------------------------------------------------
......
...@@ -3,11 +3,15 @@ module Test.Main where ...@@ -3,11 +3,15 @@ module Test.Main where
import Prelude import Prelude
import Effect (Effect) import Effect (Effect)
import Effect.Aff (launchAff_) import Effect.Aff (launchAff_)
-- import Effect.Uncurried (EffectFn1, runEffectFn1)
import Test.Spec.Discovery (discover) import Test.Spec.Discovery (discover)
import Test.Spec.Reporter.Console (consoleReporter) import Test.Spec.Reporter.Console (consoleReporter)
import Test.Spec.Runner (runSpec) import Test.Spec.Runner (runSpec)
-- foreign import _cryptoFix :: forall e. EffectFn1 e Unit
main :: Effect Unit main :: Effect Unit
main = launchAff_ do main = do
specs <- discover "Gargantext\\..*Spec" launchAff_ do
specs <- discover "Test\\.Gargantext\\..*Spec"
runSpec [consoleReporter] specs runSpec [consoleReporter] specs
...@@ -8778,6 +8778,11 @@ utility-types@^3.10.0: ...@@ -8778,6 +8778,11 @@ utility-types@^3.10.0:
resolved "https://registry.yarnpkg.com/utility-types/-/utility-types-3.10.0.tgz#ea4148f9a741015f05ed74fd615e1d20e6bed82b" resolved "https://registry.yarnpkg.com/utility-types/-/utility-types-3.10.0.tgz#ea4148f9a741015f05ed74fd615e1d20e6bed82b"
integrity sha512-O11mqxmi7wMKCo6HKFt5AhO4BwY3VV68YU07tgxfz8zJTIxr4BpsezN49Ffwy9j3ZpwwJp4fkRwjRzq3uWE6Rg== integrity sha512-O11mqxmi7wMKCo6HKFt5AhO4BwY3VV68YU07tgxfz8zJTIxr4BpsezN49Ffwy9j3ZpwwJp4fkRwjRzq3uWE6Rg==
uuid@8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
uuid@^3.3.2: uuid@^3.3.2:
version "3.4.0" version "3.4.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee" resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment