https://github.com/tidwall/pair
create low memory key/value objects in Go
https://github.com/tidwall/pair
Last synced: 7 months ago
JSON representation
create low memory key/value objects in Go
- Host: GitHub
- URL: https://github.com/tidwall/pair
- Owner: tidwall
- License: mit
- Created: 2017-03-18T01:43:25.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-04-18T22:14:35.000Z (over 8 years ago)
- Last Synced: 2025-04-05T03:12:04.956Z (9 months ago)
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 21
- Watchers: 2
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Pair is a Go package that provides a low memory key/value object that takes up one allocation. It's useful for in-memory key/value stores and data structures where memory space is a concern.
Data structure
--------------
The allocation is a single packed block of bytes with the following format:
| ValueSize uint32 | KeySize uint32 | Value []byte | Key []byte |
|------------------|----------------|--------------|------------|
Using
-----
To start using Pair, install Go and run `go get`:
```
$ go get -u github.com/tidwall/pair
```
Create a new Pair:
```go
item := pair.New([]byte("user:2054:name"), []byte("Alice Tripplehorn"))
```
Access the Pair data:
```go
item.Key() []byte // returns the key portion of the pair.
item.Value() []byte // returns the value portion of the pair.
item.Size() int // returns the exact in-memory size of the item.
item.Zero() bool // returns true if the pair is unallocated.
```
Unsafe pointer access:
```go
item.Pointer() unsafe.Pointer // returns the base pointer
pair.FromPointer(ptr unsafe.Pointer) Pair // returns a Pair with provided base pointer
```
Contact
-------
Josh Baker [@tidwall](http://twitter.com/tidwall)
License
-------
Pair source code is available under the MIT [License](/LICENSE).