Skip to main content

Extremely fast full-text-search algorithm and caching system

Project description

Hermes Stars Watchers

banner

Go Install

go get github.com/realTristan/Hermes

Python Install

pip install hermescloud

What is Hermes?

Hermes is an extremely fast full-text search algorithm and caching system written in Go. It's designed for API implementations which can be used by wrappers in other languages. Hermes has two notable algorithms. The first being the with-cache algorithm. When using the with-cache algorithm, you can set, get, store, etc. keys and values into the cache. The second being a no-cache algorithm that reads data from a map, or json file, and uses and array to store the data. Both of these algorithms provide full-text search query times from 10µs to 300µs.

Example of NoCache Full-Text-Search

If you want to use only the full-text-search features, then just import hermes and load it using a .json file. (as shown in /example). Note: For small to medium-sized datasets (like the ones I used in /data), Hermes works great. Although, as the words in the dataset increases, the full-text-search cache will take up significantly more memory.

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

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")
	
	// Create a schema. These are the fields that will be searched.
	var schema = map[string]bool{
		"name": true,
		"id": false,
	}

	// Search for a word in the cache
	// @params: query, limit, strict
	var res, _ = ft.Search("tristan", 100, false, schema)
	fmt.Println(res)
}

data.json

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

Example of Cache Full-Text-Search

The full-text-search from /cache is significantly slower than the nocache FTS. Why? Because the FTS in /cache requires more memory, keys, and utilizes a map instead of a slice to store data. If you want to use a cache along with the full text-search algorithm, import the files from /cache. To setup a cache, check out /cache/example or /cache/testing.

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

Code

package main

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

func main() {
	// Initialize the cache
	var cache *Hermes.Cache = Hermes.InitCache()
	
	// Initialize the full-text cache
	// MaxLength: 10, MaxBytes: -1 (no limit)
	cache.FTInit(10, -1)
	
	// Set the value in the cache
	cache.Set("user_id", map[string]interface{}{
		"name":       Hermes.WithFT("tristan"),
		"age":        17,
		"expiration": time.Now(),
	})
	
	// The keys you want to search through in the full-text search
	var schema map[string]bool = map[string]bool{
		"name":       true,
		"age":        false,
		"expiration": false,
	}

	// Search for a word in the cache and print the result
	var result, _ = cache.Search("tristan", 100, false, schema)
	fmt.Println(result)
}

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/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

cache.set

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
}

cache.delete

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
}

cache.get

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
}

cache.keys

About

Get all of the keys in the cache.

Example Request

{
  "function": "cache.keys"
}

Response

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

cache.values

About

Get all of the values in the cache.

Example Request

{
  "function": "cache.values"
}

Response

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

cache.length

About

Get the amount of keys stored in the cache.

Example Request

{
  "function": "cache.length"
}

Response

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

cache.clean

About

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

Example Request

{
  "function": "cache.clean"
}

Response

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

cache.info

About

Get the cache and full-text storage statistics.

Example Request

{
  "function": "cache.info"
}

Response

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

cache.exists

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

ft.init

About

Intialize the full text cache.

Example Request

{
  "function": "ft.init",
  "maxbytes": -1,
  "maxlength": -1
}

Response

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

ft.clean

About

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

Example Request

{
  "function": "ft.clean"
}

Response

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

ft.search

About

Search for a query in the full-text storage.

Example Request

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

Response

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

ft.search.oneword

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
}

ft.search.values

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
}

ft.search.withkey

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
}

ft.maxbytes.set

About

Set the maximum full-text storage size in bytes.

Example Request

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

Response

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

ft.maxlength.set

About

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

Example Request

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

Response

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

ft.storage

About

Get the current full-text storage.

Example Request

{
  "function": "ft.storage"
}

Response

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

ft.storage.size

About

Get the current full-text storage size in bytes.

Example Request

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

Response

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

ft.storage.length

About

Get the current full-text storage length.

Example Request

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

Response

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

ft.isinitialized

About

Get whether the full-text storage has been initialized.

Example Request

{
  "function": "ft.isinitialized"
}

Response

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

ft.indices.sequence

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hermescloud-0.0.4.tar.gz (9.0 kB view hashes)

Uploaded Source

Built Distribution

hermescloud-0.0.4-py3-none-any.whl (6.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page