Skip to content

Extremely Fast Full-Text-Search Algorithm and Caching System

License

Notifications You must be signed in to change notification settings

realTristan/hermes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hermes Stars Watchers

Screenshot 2023-06-03 at 1 55 31 PM

How to contribute

Hermes is still in a developmental phase. Helping with test cases, benchmarks, speed, and memory usage are a great way to contribute to the project.

Hermes Go

Direct access to the hermes cache/nocache functions.

go get github.com/realTristan/hermes

Hermes Cloud

Access the hermes cache functions via websocket.

Python

pip install hermescloud

What is Hermes?

Hermes is an extremely fast full-text search and caching framework written in Go. Hermes has a cache algorithm where you can set, get, store, etc. keys and values into the cache, and a no-cache algorithm that reads data from an array or json file and uses an array to store data. Both of these algorithms provide full-text search query speeds from 10µs to 300µs.

Example of NoCache

If you want to use only the full-text-search features, then import hermes/nocache. Load the data using a .json file or array of maps. No Cache is much faster than the With-Cache algorithm, but you can't update the data.

Code

package main

import (
  "fmt"
  "time"

  hermes "github.com/realTristan/hermes/nocache"
)

func main() {
  // Initialize the full-text cache
  var ft, _ = hermes.InitWithJson("data.json")

  // Search for a word in the cache
  var res, _ = ft.Search(hermes.SearchParams{
    Query:  "tristan",
    Limit:  100,
    Strict: false,
  })

  fmt.Println(res)
}

Benchmarks

Dataset Map Entries: 4,115
Dataset Total Words: 208,092
Dataset Map Size: ≈ 2.3MB

Query: Computer, Limit: 100, Strict: False => 36.5µs
Query: Computer, Limit: 100, Strict: True => 12.102µs

data.json

Used with hermes.InitWithJson()

[
  {
    "id": 1,
    "name": {
      "$hermes.full_text": true,
      "$hermes.value": "Tristan Simpson"
    },
  },
]

With-Cache Example

The with-cache algorithm is notably slower than the no-cache algorithm. Why? Because the with-cache algorithm uses maps to store data and not an array. Import hermes directly to use the with-cache features.

Code

package main

import (
  "fmt"
  "time"
  hermes "github.com/realTristan/hermes"
)

func main() {
  // Initialize the cache
  cache := hermes.InitCache()

  // MaxSize: 10, MaxBytes: -1 (no limit), MinWordLength: 3
  cache.FTInit(10, -1, 3)

  // Set the value in the cache
  cache.Set("user_id", map[string]any{
    "name":       cache.WithFT("tristan"),
    "age":        17,
  })

  // Search for a word in the cache and print the result
  result, err := cache.Search(hermes.SearchParams{
    Query:  "tristan",
    Limit:  100,
    Strict: false,
  })

  fmt.Println(result)
}

Benchmarks

Dataset Map Entries: 4,115
Dataset Total Words: 208,092
Dataset Map Size: ≈ 2.3MB

Query: Computer, Limit: 100, Strict: False => 263.7µs
Query: Computer, Limit: 100, Strict: True => 40.84µs

data.json

Used with cache.FTInitWithJson()

{
  "user_id": {
    "age": 17,
    "name": {
      "$hermes.full_text": true,
      "$hermes.value": "Tristan Simpson"
    },
  },
}

Hermes Cloud App

Install

Coming Soon

Custom Implementation

import (
  "github.com/gofiber/fiber/v2"
  hermes "github.com/realTristan/hermes"
  socket "github.com/realTristan/hermes/cloud/socket"
)

func main() {
  // Cache and fiber app
  cache := hermes.InitCache()
  app := fiber.New()

  // Set the router
  socket.SetRouter(app, cache)

  // Listen on port 3000
  app.Listen(":3000")
}

Websocket API

Cache

About

Set a value in the cache with the corresponding key.

Example Request

{
  "function": "cache.set",
  "key": "user_id",
  "value": base64{
    "name": "tristan"
  }
}

Response

{
  "success": true/false, 
  "data": nil
}

About

Delete the provided key, and the data correlated to it from the cache.

Example Request

{
  "function": "cache.delete",
  "key": "user_id"
}

Response

{
  "success": true/false, 
  "data": nil
}

About

Get data from the cache using a key.

Example Request

{
  "function": "cache.get",
  "key": "user_id"
}

Response

{
  "success": true/false, 
  "data": map[string]any
}

About

Get all of the keys in the cache.

Example Request

{
  "function": "cache.keys"
}

Response

{
  "success": true/false, 
  "data": []string
}

About

Get all of the values in the cache.

Example Request

{
  "function": "cache.values"
}

Response

{
  "success": true/false, 
  "data": []map[string]any
}

About

Get the amount of keys stored in the cache.

Example Request

{
  "function": "cache.length"
}

Response

{
  "success": true/false, 
  "data": int
}

About

Clean all the data in the cache, and full-text storage.

Example Request

{
  "function": "cache.clean"
}

Response

{
  "success": true/false, 
  "data": nil
}

About

Get the cache and full-text storage statistics.

Example Request

{
  "function": "cache.info"
}

Response

{
  "success": true/false, 
  "data": string
}

About

Get whether a key exists in the cache.

Example Request

{
  "function": "cache.exists",
  "key": "user_id"
}

Response

{
  "success": true/false, 
  "data": bool
}

Full-Text

About

Intialize the full text cache.

Example Request

{
  "function": "ft.init",
  "maxbytes": -1,
  "maxsize": -1,
  "minwordlength": 3
}

Response

{
  "success": true/false, 
  "data": nil
}

About

Clean all of the data in the full-text storage.

Example Request

{
  "function": "ft.clean"
}

Response

{
  "success": true/false, 
  "data": nil
}

About

Search for a query in the full-text storage.

Example Request

{
  "function": "ft.search",
  "query": "tristan",
  "strict": false,
  "limit": 10
}

Response

{
  "success": true/false, 
  "data": []map[string]any
}

About

Search for a single word in the full-text storage.

Example Request

{
  "function": "ft.search.oneword",
  "query": "tristan",
  "strict": false,
  "limit": 10
}

Response

{
  "success": true/false, 
  "data": []map[string]any
}

About

Search in the cache data values. (Slower)

Example Request

{
  "function": "ft.search.values",
  "query": "tristan",
  "limit": 10,
  "schema": {
    "key": true
  }
}

Response

{
  "success": true/false, 
  "data": []map[string]any
}

About

Search in the cache data values for a specific key.

Example Request

{
  "function": "ft.search.withkey",
  "query": "tristan",
  "key": "user_id",
  "limit": 10
}

Response

{
  "success": true/false, 
  "data": []map[string]any
}

About

Set the maximum full-text storage size in bytes.

Example Request

{
  "function": "ft.maxbytes.set",
  "maxbytes": -1
}

Response

{
  "success": true/false, 
  "data": nil
}

About

Set the maximum full-text storage words allowed to be stored.

Example Request

{
  "function": "ft.maxsize.set",
  "maxsize": -1
}

Response

{
  "success": true/false, 
  "data": nil
}

About

Get the current full-text storage.

Example Request

{
  "function": "ft.storage"
}

Response

{
  "success": true/false, 
  "data": map[string][]int
}

About

Get the current full-text storage size in bytes.

Example Request

{
  "function": "ft.storage.size"
}

Response

{
  "success": true/false, 
  "data": int
}

About

Get the current full-text storage length.

Example Request

{
  "function": "ft.storage.length"
}

Response

{
  "success": true/false, 
  "data": int
}

About

Get whether the full-text storage has been initialized.

Example Request

{
  "function": "ft.isinitialized"
}

Response

{
  "success": true/false, 
  "data": bool
}

About

Sequence the full-text storage indices.

Example Request

{
  "function": "ft.indices.sequence"
}

Response

{
  "success": true/false, 
  "data": nil
}

To-do

  • Testing Files
  • More Documentation
  • More Examples
  • Wrappers for other languages

License

MIT License

Copyright (c) 2023 Tristan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.