[refactoring] cache refactoring (highlight)

Also, tests fixed a bit (at least they work).
parent 68d3856d
......@@ -63,7 +63,8 @@
"react-tooltip": "^4.2.8",
"secp256k1": "^4.0.2",
"sigma": "^2.4.0",
"twgl.js": "^5.0.4"
"twgl.js": "^5.0.4",
"uuid": "8.3.2"
},
"devDependencies": {
"@babel/core": "^7.15.0",
......
......@@ -100,11 +100,16 @@ let
#spago build
#build-purs
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 test --src-path output --test-path output
#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
pkgs.mkShell {
buildInputs = [
......@@ -126,6 +131,7 @@ pkgs.mkShell {
pkgs.nodejs
#pkgs.nodePackages.purescript-language-server
#pkgs.python # needed for msgpack etc
#nodejs_20_3_1
repl
serve
pkgs.pulp
......
......@@ -74,7 +74,6 @@ to generate this file without the comments in this block.
, "sequences"
, "simple-json"
, "simple-json-generics"
, "simplecrypto"
, "string-search"
, "strings"
, "strings-extra"
......
......@@ -148,7 +148,7 @@ compile ::
-> NgramsTable
-> Maybe String
-> Array HighlightElement
compile cache ngrams = maybe [] (highlightNgrams CTabTerms ngrams cache)
compile cache ngrams = maybe [] (highlightNgrams cache CTabTerms ngrams)
-- Runs
......
......@@ -12,7 +12,7 @@ import Data.List as List
import Data.Maybe (Maybe(..), fromMaybe)
import Data.Show.Generic (genericShow)
import Effect (Effect)
import Effect.Aff (throwError)
import Effect.Aff (Aff, throwError)
import Effect.Exception (error)
import Gargantext.Components.Bootstrap as B
import Gargantext.Components.Bootstrap.Types (ComponentStatus(..), Variant(..))
......@@ -27,7 +27,7 @@ import Gargantext.Routes (SessionRoute(Children, NodeAPI))
import Gargantext.Sessions (Session, get, put)
import Gargantext.Types (AffETableResult, NodeType(..), ID)
import Gargantext.Utils ((?))
import Gargantext.Utils.Crypto as Crypto
-- import Gargantext.Utils.Crypto as Crypto
import Gargantext.Utils.Reactix as R2
import Gargantext.Utils.Toestand as T2
import Reactix as R
......@@ -112,8 +112,8 @@ fieldsCodeEditorCpt = here.component "fieldsCodeEditorCpt" cpt
recomputeIndices :: FTFieldsWithIndex -> FTFieldsWithIndex
recomputeIndices (FTFieldsWithIndex lst) = FTFieldsWithIndex $ mapWithIndex (\idx -> \{ ftField } -> { idx, ftField }) lst
hash :: FTFieldWithIndex -> Hash
hash { idx, ftField } = Crypto.hash $ "--idx--" <> (show idx) <> "--field--" <> (show ftField)
-- hash :: FTFieldWithIndex -> Aff Hash
-- hash { idx, ftField } = Crypto.hash $ "--idx--" <> (show idx) <> "--field--" <> (show ftField)
type FieldCodeEditorProps =
( idx :: Int
......
......@@ -293,7 +293,7 @@ layoutWithContextNgramsCpt = here.component "layoutWithContextNgrams" cpt where
{ className: "document-layout__source__content w-100" <> collapsibleClasses
, id: getIdName "sources" }
source
,
,
btnSeeMore "sources"
]
,
......
......@@ -165,8 +165,8 @@ computeCache ngrams contextNgrams = { contextNgrams, pm, pats }
-- TODO: while this function works well with word boundaries,
-- it inserts too many spaces.
highlightNgrams :: CTabNgramType -> NgramsTable -> Record Cache -> String -> Array HighlightElement
highlightNgrams ntype table@(NgramsTable {ngrams_repo_elements: elts}) cache@{ pm, pats } input0 =
highlightNgrams :: Record Cache -> CTabNgramType -> NgramsTable -> String -> Array HighlightElement
highlightNgrams cache@{ pm, pats } ntype table@(NgramsTable {ngrams_repo_elements: elts}) input0 =
-- trace {pats, input0, input, ixs} \_ ->
A.fromFoldable ((\(s /\ ls)-> undb s /\ ls) <$> unsafePartial (foldl goFold ((input /\ Nil) : Nil) ixs))
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
import Crypto.Simple as Crypto
import Control.Promise (Promise, toAffE)
import Data.Set (Set)
import Data.Set as Set
import Data.Array as Array
import Effect (Effect)
import Effect.Aff (Aff)
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
-- Set String needs Set.map hash
-- Set Hash does not need Set.map hash (just concat)
type Hash = String
hash' :: forall a. Crypto.Hashable a => a -> String
hash' = Crypto.toString <<< Crypto.hash Crypto.SHA256
hash' :: String -> Aff String
hash' = getSHA256Hash
--hash' :: forall a. Crypto.Hashable a => a -> String
--hash' = Crypto.toString <<< Crypto.hash Crypto.SHA256
class IsHashable a where
hash :: a -> Hash
hash :: a -> Aff Hash
instance IsHashable String
where
hash = hash'
instance (Crypto.Hashable a, IsHashable a) => IsHashable (Array a)
where
hash = hash <<< Set.fromFoldable <<< map hash
--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
concat :: Array Hash -> String
concat = Array.foldl (<>) ""
instance IsHashable (Set String) where
hash = hash <<< concat <<< toArray
hash = hash <<< toArray
where
toArray :: forall a. Set a -> Array a
toArray = Set.toUnfoldable
concat :: Array Hash -> String
concat = Array.foldl (<>) ""
......@@ -119,19 +119,21 @@ spec =
let text = "To hash with backend"
let hashed = "8a69a94d164279af2b7d1443ce08da6184b3d7e815406076e148159c284b53c3"
-- ^ 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
let list = ["a","b"]
let hashed = "ab19ec537f09499b26f0f62eed7aefad46ab9f498e06a7328ce8e8ef90da6d86"
-- ^ hash from backend with text above
Crypto.hash list `shouldEqual` hashed
h <- Crypto.hash list
h`shouldEqual` hashed
------------------------------------------------------------------------
-- | TODO property based tests
it "Hash works with any order of list" do
let hash1 = Crypto.hash ["a","b"]
let hash2 = Crypto.hash ["b","a"]
hash1 <- Crypto.hash ["a","b"]
hash2 <- Crypto.hash ["b","a"]
hash1 `shouldEqual` hash2
------------------------------------------------------------------------
......
......@@ -3,11 +3,15 @@ module Test.Main where
import Prelude
import Effect (Effect)
import Effect.Aff (launchAff_)
-- import Effect.Uncurried (EffectFn1, runEffectFn1)
import Test.Spec.Discovery (discover)
import Test.Spec.Reporter.Console (consoleReporter)
import Test.Spec.Runner (runSpec)
-- foreign import _cryptoFix :: forall e. EffectFn1 e Unit
main :: Effect Unit
main = launchAff_ do
specs <- discover "Gargantext\\..*Spec"
runSpec [consoleReporter] specs
main = do
launchAff_ do
specs <- discover "Test\\.Gargantext\\..*Spec"
runSpec [consoleReporter] specs
......@@ -8778,6 +8778,11 @@ utility-types@^3.10.0:
resolved "https://registry.yarnpkg.com/utility-types/-/utility-types-3.10.0.tgz#ea4148f9a741015f05ed74fd615e1d20e6bed82b"
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:
version "3.4.0"
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