Code Monkey home page Code Monkey logo

cqlpb's Introduction

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:

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:

myMsg := &MyMessage{MyStringVal: "test"}
row, err := marshal.Marshal(myMsg, template)

You can then deserialize that row like so:

myMsg := &MyMessage{}
err := marshal.Unmarshal(myMsg, row)
myMsg.MyStringVal == "test" // true

cqlpb's People

Contributors

paralin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

westfly

cqlpb's Issues

Sub-field serialization

Changes:

  • [] Ability to serialize sub-fields in this priority:
    • Protobuf if possible + template field is a bytes field
    • JSON if template field is a string field

Will support user defined types down the road, but this is out of scope for this issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.