| // Copyright 2015 The Vanadium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| // Implements PersistentDataKey for each K<T> type, as described in types.vdl. |
| |
| package sbmodel |
| |
| import ( |
| wire "v.io/v23/services/syncbase" |
| "v.io/x/sensorlog/internal/config" |
| "v.io/x/sensorlog/internal/sbmodel/keyutil" |
| ) |
| |
| // PersistentDataKey is a type encapsulating data from the row key of a |
| // top-level value type persisted to Syncbase. |
| type PersistentDataKey interface { |
| // Collection returns the name of the Syncbase collection for the data type. |
| Collection() string |
| // Key returns the row key for the value. |
| Key() string |
| // Parse parses the row key for the value into self. Returns an error if key |
| // is malformed. |
| Parse(key string) error |
| } |
| |
| // devicecfg : <DevId> |
| func (_ KDeviceCfg) Collection() string { return "devicecfg" } |
| func (k *KDeviceCfg) Key() string { return keyutil.Join(k.DevId) } |
| func (k *KDeviceCfg) Parse(key string) error { |
| parts, err := keyutil.Split(key, 1) |
| if err != nil { |
| return err |
| } |
| k.DevId = parts[0] |
| return nil |
| } |
| |
| // streamdef : <DevId>/<StreamId> |
| func (_ KStreamDef) Collection() string { return "streamdef" } |
| func (k *KStreamDef) Key() string { return keyutil.Join(k.DevId, k.StreamId) } |
| func (k *KStreamDef) Parse(key string) error { |
| parts, err := keyutil.Split(key, 2) |
| if err != nil { |
| return err |
| } |
| k.DevId, k.StreamId = parts[0], parts[1] |
| return nil |
| } |
| |
| // sdata : <DevId>/<StreamId>/<Timestamp> |
| func (k KDataPoint) Collection() string { |
| return "sdata" |
| } |
| func (k *KDataPoint) Key() string { |
| return keyutil.Join(k.DevId, k.StreamId, keyutil.StringifyTime(k.Timestamp)) |
| } |
| func (k *KDataPoint) Parse(key string) error { |
| parts, err := keyutil.Split(key, 3) |
| if err != nil { |
| return err |
| } |
| timestamp, err := keyutil.ParseTime(parts[2]) |
| if err != nil { |
| return err |
| } |
| k.DevId, k.StreamId, k.Timestamp = parts[0], parts[1], timestamp |
| return nil |
| } |
| |
| // CollectionSpec defines a Syncbase collection, encapsulating a key prototype and |
| // permissions. |
| type CollectionSpec struct { |
| Prototype PersistentDataKey |
| ReadOnly bool |
| } |
| |
| // All top-level types persisted to master device Syncbase. |
| var MasterCollections = []CollectionSpec{ |
| {Prototype: &KDeviceCfg{}}, |
| {Prototype: &KStreamDef{}}, |
| {Prototype: &KDataPoint{}}, |
| } |
| |
| // All top-level types persisted to measured Syncbase. |
| var MeasuredCollections = []CollectionSpec{ |
| {Prototype: &KStreamDef{}, ReadOnly: true}, |
| {Prototype: &KDataPoint{}}, |
| } |
| |
| // CollectionId returns the collection id for the data type. |
| // TODO(ivanpi): Pass in user instead of using default. |
| func CollectionId(prototype PersistentDataKey) wire.Id { |
| return wire.Id{Blessing: config.User, Name: prototype.Collection()} |
| } |