Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Latest commit

 

History

History
57 lines (47 loc) · 1.05 KB

README.md

File metadata and controls

57 lines (47 loc) · 1.05 KB

KeyMapStorage

Algorithm that performs a quick search through data stored in RAM based on primary keys

Example

Initialization

db := &Storage{}
db.Init()

Single write

type Person struct {
	FirstName string
	LastName string
	Age int
	City string
}
...
x := Person{"Daniil", "Furmanov", 10, "Moscow"}
// write with primary keys "FirstName" and "LastName"
db.Write(GetIdentifier(x, []string{"FirstName", "LastName"}), x)

Search

searchPattern := map[string]interface{} {
		"FirstName":  "Daniil",
		"LastName": "Furmanov",
}

segment := GetIdentifierFromMap(searchPattern, []string{"FirstName", "LastName"})

seg, err := db.FindSegment(segment)
if err != nil {
	fmt.Println("Segment not found")
	return
}

records := seg.FindRecords(searchPattern)

if len(records) > 0 {
	fmt.Println(records[0].Data)
} else {
	fmt.Println("Zero results found")
}

Benchmarks

Tested on MacBook Pro 15` (2014). Intel Core i7

Single write: 14.449µs
Write 1,000,000 records: 2.122833s
Search through 1,000,000 records by two parameters: 378.061µs