https://github.com/paralin/cqlpb
Cassandra utilities and serialization in golang.
https://github.com/paralin/cqlpb
Last synced: 9 months ago
JSON representation
Cassandra utilities and serialization in golang.
- Host: GitHub
- URL: https://github.com/paralin/cqlpb
- Owner: paralin
- Created: 2016-06-13T00:38:56.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-07-22T20:22:58.000Z (over 9 years ago)
- Last Synced: 2025-06-10T04:50:27.160Z (9 months ago)
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Cassandra Protobuf
==================
Cassandra protobuf storage implementation in Go.
Methodology
===========
This code maps protobuf types to Cassandra schemas in the following migration-tolerant and intelligent way:
- Every row has a field "proto" with a binary blob.
- Row columns are automatically loaded into the proto after deserialization.
- This allows some fields to be put into the schema, and others to remain binary only.
Design
======
The design of this library is as follows:
- Accept a protobuf message and a map (string->interface).
- Fill the protobuf message with the "proto" field if it exists.
- Fill the remaining fields with the cassandra columns.
The same goes the opposite direction for serialization.
- Returns a map (string->interface)
- Accept a protobuf message and a map (string->interface with zero-value)
- Map serves as a template for a cql row
- Map MUST contain "proto" field of type "bytes"
- Map MUST contain at least one other field, presumably used as ID
- For each field in the map, check for a corresponding proto field
- If the names match but types do not, REFUSE to serialize (exit with error)
- Serialize any corresponding field to cassandra field, and zero it in the protobuf
- proto3 does not store zeroed fields, this will save space
- Serialize the protobuf to the proto field.
Usage
=====
First, according to your schema, build a template map, for example:
```go
template := make(map[string]interface{})
template["myStringVal"] = ""
template["myIntVal"] = int32(0)
template["myBoolVal"] = false
template["proto"] = make(0, []byte)
```
The column names should match the names of the fields in your proto file.
Any fields in your proto not specified in your template will be serialized in the "proto" field as a binary blob.
You can serialize a protobuf message to a cassandra map for insertion like so:
```go
myMsg := &MyMessage{MyStringVal: "test"}
row, err := marshal.Marshal(myMsg, template)
```
You can then deserialize that row like so:
```go
myMsg := &MyMessage{}
err := marshal.Unmarshal(myMsg, row)
myMsg.MyStringVal == "test" // true
```