Use index singleton and relocate encoder package.

This commit is contained in:
Matt T. Proud 2012-11-25 16:07:05 +01:00
parent b9928266b5
commit 720442d3d3
8 changed files with 51 additions and 62 deletions

22
.gitignore vendored
View file

@ -1,22 +0,0 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe

View file

@ -1,4 +1,4 @@
package main package coding
type Encoder interface { type Encoder interface {
Encode() ([]byte, error) Encode() ([]byte, error)

View file

@ -1,4 +1,4 @@
package main package coding
import ( import (
"code.google.com/p/goprotobuf/proto" "code.google.com/p/goprotobuf/proto"

View file

@ -1,8 +1,12 @@
package main package main
import (
"github.com/matttproud/prometheus/coding"
)
type MembershipIndex interface { type MembershipIndex interface {
Has(key Encoder) (bool, error) Has(key coding.Encoder) (bool, error)
Put(key Encoder) error Put(key coding.Encoder) error
Drop(key Encoder) error Drop(key coding.Encoder) error
Close() error Close() error
} }

View file

@ -1,40 +1,41 @@
package main package main
import ( import (
"github.com/matttproud/prometheus/coding"
data "github.com/matttproud/prometheus/model/generated" data "github.com/matttproud/prometheus/model/generated"
) )
var (
existenceValue = coding.NewProtocolBufferEncoder(&data.MembershipIndexValueDDO{})
)
type LevigoMembershipIndex struct { type LevigoMembershipIndex struct {
persistence *LevigoPersistence persistence *LevigoPersistence
existenceValue Encoder
} }
func (l *LevigoMembershipIndex) Close() error { func (l *LevigoMembershipIndex) Close() error {
return l.persistence.Close() return l.persistence.Close()
} }
func (l *LevigoMembershipIndex) Has(key Encoder) (bool, error) { func (l *LevigoMembershipIndex) Has(key coding.Encoder) (bool, error) {
return l.persistence.Has(key) return l.persistence.Has(key)
} }
func (l *LevigoMembershipIndex) Drop(key Encoder) error { func (l *LevigoMembershipIndex) Drop(key coding.Encoder) error {
return l.persistence.Drop(key) return l.persistence.Drop(key)
} }
func (l *LevigoMembershipIndex) Put(key Encoder) error { func (l *LevigoMembershipIndex) Put(key coding.Encoder) error {
return l.persistence.Put(key, l.existenceValue) return l.persistence.Put(key, existenceValue)
} }
func NewLevigoMembershipIndex(storageRoot string, cacheCapacity, bitsPerBloomFilterEncoded int) (*LevigoMembershipIndex, error) { func NewLevigoMembershipIndex(storageRoot string, cacheCapacity, bitsPerBloomFilterEncoded int) (*LevigoMembershipIndex, error) {
var levigoPersistence *LevigoPersistence var levigoPersistence *LevigoPersistence
var levigoPersistenceError error var levigoPersistenceError error
existenceValue := NewProtocolBufferEncoder(&data.MembershipIndexValueDDO{})
if levigoPersistence, levigoPersistenceError = NewLevigoPersistence(storageRoot, cacheCapacity, bitsPerBloomFilterEncoded); levigoPersistenceError == nil { if levigoPersistence, levigoPersistenceError = NewLevigoPersistence(storageRoot, cacheCapacity, bitsPerBloomFilterEncoded); levigoPersistenceError == nil {
levigoMembershipIndex := &LevigoMembershipIndex{ levigoMembershipIndex := &LevigoMembershipIndex{
persistence: levigoPersistence, persistence: levigoPersistence,
existenceValue: existenceValue,
} }
return levigoMembershipIndex, nil return levigoMembershipIndex, nil
} }

View file

@ -4,6 +4,7 @@ import (
"code.google.com/p/goprotobuf/proto" "code.google.com/p/goprotobuf/proto"
"errors" "errors"
"fmt" "fmt"
"github.com/matttproud/prometheus/coding"
"github.com/matttproud/prometheus/coding/indexable" "github.com/matttproud/prometheus/coding/indexable"
data "github.com/matttproud/prometheus/model/generated" data "github.com/matttproud/prometheus/model/generated"
"github.com/matttproud/prometheus/utility" "github.com/matttproud/prometheus/utility"
@ -254,12 +255,12 @@ func fingerprintDDOFromByteArray(fingerprint []byte) *data.FingerprintDDO {
} }
func (l *LevigoMetricPersistence) hasIndexMetric(ddo *data.MetricDDO) (bool, error) { func (l *LevigoMetricPersistence) hasIndexMetric(ddo *data.MetricDDO) (bool, error) {
ddoKey := NewProtocolBufferEncoder(ddo) ddoKey := coding.NewProtocolBufferEncoder(ddo)
return l.metricMembershipIndex.Has(ddoKey) return l.metricMembershipIndex.Has(ddoKey)
} }
func (l *LevigoMetricPersistence) indexMetric(ddo *data.MetricDDO) error { func (l *LevigoMetricPersistence) indexMetric(ddo *data.MetricDDO) error {
ddoKey := NewProtocolBufferEncoder(ddo) ddoKey := coding.NewProtocolBufferEncoder(ddo)
return l.metricMembershipIndex.Put(ddoKey) return l.metricMembershipIndex.Put(ddoKey)
} }
@ -277,17 +278,17 @@ func fingerprintDDOForMessage(message proto.Message) (*data.FingerprintDDO, erro
} }
func (l *LevigoMetricPersistence) HasLabelPair(ddo *data.LabelPairDDO) (bool, error) { func (l *LevigoMetricPersistence) HasLabelPair(ddo *data.LabelPairDDO) (bool, error) {
ddoKey := NewProtocolBufferEncoder(ddo) ddoKey := coding.NewProtocolBufferEncoder(ddo)
return l.labelPairFingerprints.Has(ddoKey) return l.labelPairFingerprints.Has(ddoKey)
} }
func (l *LevigoMetricPersistence) HasLabelName(ddo *data.LabelNameDDO) (bool, error) { func (l *LevigoMetricPersistence) HasLabelName(ddo *data.LabelNameDDO) (bool, error) {
ddoKey := NewProtocolBufferEncoder(ddo) ddoKey := coding.NewProtocolBufferEncoder(ddo)
return l.labelNameFingerprints.Has(ddoKey) return l.labelNameFingerprints.Has(ddoKey)
} }
func (l *LevigoMetricPersistence) GetLabelPairFingerprints(ddo *data.LabelPairDDO) (*data.FingerprintCollectionDDO, error) { func (l *LevigoMetricPersistence) GetLabelPairFingerprints(ddo *data.LabelPairDDO) (*data.FingerprintCollectionDDO, error) {
ddoKey := NewProtocolBufferEncoder(ddo) ddoKey := coding.NewProtocolBufferEncoder(ddo)
if get, getError := l.labelPairFingerprints.Get(ddoKey); getError == nil { if get, getError := l.labelPairFingerprints.Get(ddoKey); getError == nil {
value := &data.FingerprintCollectionDDO{} value := &data.FingerprintCollectionDDO{}
if unmarshalError := proto.Unmarshal(get, value); unmarshalError == nil { if unmarshalError := proto.Unmarshal(get, value); unmarshalError == nil {
@ -302,7 +303,7 @@ func (l *LevigoMetricPersistence) GetLabelPairFingerprints(ddo *data.LabelPairDD
} }
func (l *LevigoMetricPersistence) GetLabelNameFingerprints(ddo *data.LabelNameDDO) (*data.FingerprintCollectionDDO, error) { func (l *LevigoMetricPersistence) GetLabelNameFingerprints(ddo *data.LabelNameDDO) (*data.FingerprintCollectionDDO, error) {
ddoKey := NewProtocolBufferEncoder(ddo) ddoKey := coding.NewProtocolBufferEncoder(ddo)
if get, getError := l.labelNameFingerprints.Get(ddoKey); getError == nil { if get, getError := l.labelNameFingerprints.Get(ddoKey); getError == nil {
value := &data.FingerprintCollectionDDO{} value := &data.FingerprintCollectionDDO{}
if unmarshalError := proto.Unmarshal(get, value); unmarshalError == nil { if unmarshalError := proto.Unmarshal(get, value); unmarshalError == nil {
@ -318,14 +319,14 @@ func (l *LevigoMetricPersistence) GetLabelNameFingerprints(ddo *data.LabelNameDD
} }
func (l *LevigoMetricPersistence) setLabelPairFingerprints(labelPair *data.LabelPairDDO, fingerprints *data.FingerprintCollectionDDO) error { func (l *LevigoMetricPersistence) setLabelPairFingerprints(labelPair *data.LabelPairDDO, fingerprints *data.FingerprintCollectionDDO) error {
labelPairEncoded := NewProtocolBufferEncoder(labelPair) labelPairEncoded := coding.NewProtocolBufferEncoder(labelPair)
fingerprintsEncoded := NewProtocolBufferEncoder(fingerprints) fingerprintsEncoded := coding.NewProtocolBufferEncoder(fingerprints)
return l.labelPairFingerprints.Put(labelPairEncoded, fingerprintsEncoded) return l.labelPairFingerprints.Put(labelPairEncoded, fingerprintsEncoded)
} }
func (l *LevigoMetricPersistence) setLabelNameFingerprints(labelName *data.LabelNameDDO, fingerprints *data.FingerprintCollectionDDO) error { func (l *LevigoMetricPersistence) setLabelNameFingerprints(labelName *data.LabelNameDDO, fingerprints *data.FingerprintCollectionDDO) error {
labelNameEncoded := NewProtocolBufferEncoder(labelName) labelNameEncoded := coding.NewProtocolBufferEncoder(labelName)
fingerprintsEncoded := NewProtocolBufferEncoder(fingerprints) fingerprintsEncoded := coding.NewProtocolBufferEncoder(fingerprints)
return l.labelNameFingerprints.Put(labelNameEncoded, fingerprintsEncoded) return l.labelNameFingerprints.Put(labelNameEncoded, fingerprintsEncoded)
} }
@ -384,8 +385,8 @@ func (l *LevigoMetricPersistence) appendFingerprints(ddo *data.MetricDDO) error
labelPairCollectionDDO := &data.LabelPairCollectionDDO{ labelPairCollectionDDO := &data.LabelPairCollectionDDO{
Member: ddo.LabelPair, Member: ddo.LabelPair,
} }
fingerprintKey := NewProtocolBufferEncoder(fingerprintDDO) fingerprintKey := coding.NewProtocolBufferEncoder(fingerprintDDO)
labelPairCollectionDDOEncoder := NewProtocolBufferEncoder(labelPairCollectionDDO) labelPairCollectionDDOEncoder := coding.NewProtocolBufferEncoder(labelPairCollectionDDO)
if putError := l.fingerprintLabelPairs.Put(fingerprintKey, labelPairCollectionDDOEncoder); putError == nil { if putError := l.fingerprintLabelPairs.Put(fingerprintKey, labelPairCollectionDDOEncoder); putError == nil {
labelCount := len(ddo.LabelPair) labelCount := len(ddo.LabelPair)
@ -463,8 +464,8 @@ func (l *LevigoMetricPersistence) AppendSample(sample *Sample) error {
Value: proto.Float32(float32(sample.Value)), Value: proto.Float32(float32(sample.Value)),
} }
sampleKeyEncoded := NewProtocolBufferEncoder(sampleKeyDDO) sampleKeyEncoded := coding.NewProtocolBufferEncoder(sampleKeyDDO)
sampleValueEncoded := NewProtocolBufferEncoder(sampleValueDDO) sampleValueEncoded := coding.NewProtocolBufferEncoder(sampleValueDDO)
if putError := l.fingerprintSamples.Put(sampleKeyEncoded, sampleValueEncoded); putError != nil { if putError := l.fingerprintSamples.Put(sampleKeyEncoded, sampleValueEncoded); putError != nil {
log.Printf("Could not append metric sample: %q\n", putError) log.Printf("Could not append metric sample: %q\n", putError)
@ -544,7 +545,7 @@ func (l *LevigoMetricPersistence) GetMetrics() ([]LabelPairs, error) {
log.Printf("!Has: %q\n", member.Signature) log.Printf("!Has: %q\n", member.Signature)
fingerprints.Add(*member.Signature) fingerprints.Add(*member.Signature)
log.Printf("fingerprints %q\n", fingerprints) log.Printf("fingerprints %q\n", fingerprints)
fingerprintEncoded := NewProtocolBufferEncoder(member) fingerprintEncoded := coding.NewProtocolBufferEncoder(member)
if labelPairCollectionRaw, labelPairCollectionRawError := l.fingerprintLabelPairs.Get(fingerprintEncoded); labelPairCollectionRawError == nil { if labelPairCollectionRaw, labelPairCollectionRawError := l.fingerprintLabelPairs.Get(fingerprintEncoded); labelPairCollectionRawError == nil {
log.Printf("labelPairCollectionRaw: %q\n", labelPairCollectionRaw) log.Printf("labelPairCollectionRaw: %q\n", labelPairCollectionRaw)
@ -592,7 +593,7 @@ func (l *LevigoMetricPersistence) GetWatermarksForMetric(metric Metric) (*Interv
Timestamp: indexable.EarliestTime, Timestamp: indexable.EarliestTime,
} }
if encode, encodeErr := NewProtocolBufferEncoder(start).Encode(); encodeErr == nil { if encode, encodeErr := coding.NewProtocolBufferEncoder(start).Encode(); encodeErr == nil {
iterator.Seek(encode) iterator.Seek(encode)
if iterator.Valid() { if iterator.Valid() {
@ -668,7 +669,7 @@ func (l *LevigoMetricPersistence) GetSamplesForMetric(metric Metric, interval In
emission := make([]Samples, 0) emission := make([]Samples, 0)
if encode, encodeErr := NewProtocolBufferEncoder(start).Encode(); encodeErr == nil { if encode, encodeErr := coding.NewProtocolBufferEncoder(start).Encode(); encodeErr == nil {
iterator.Seek(encode) iterator.Seek(encode)
for iterator = iterator; iterator.Valid(); iterator.Next() { for iterator = iterator; iterator.Valid(); iterator.Next() {

View file

@ -2,6 +2,7 @@ package main
import ( import (
"github.com/jmhodges/levigo" "github.com/jmhodges/levigo"
"github.com/matttproud/prometheus/coding"
"io" "io"
) )
@ -85,7 +86,7 @@ func (l *LevigoPersistence) Close() error {
return nil return nil
} }
func (l *LevigoPersistence) Get(value Encoder) ([]byte, error) { func (l *LevigoPersistence) Get(value coding.Encoder) ([]byte, error) {
var key []byte var key []byte
var keyError error var keyError error
@ -96,7 +97,7 @@ func (l *LevigoPersistence) Get(value Encoder) ([]byte, error) {
return nil, keyError return nil, keyError
} }
func (l *LevigoPersistence) Has(value Encoder) (bool, error) { func (l *LevigoPersistence) Has(value coding.Encoder) (bool, error) {
if value, getError := l.Get(value); getError != nil { if value, getError := l.Get(value); getError != nil {
return false, getError return false, getError
} else if value == nil { } else if value == nil {
@ -106,7 +107,7 @@ func (l *LevigoPersistence) Has(value Encoder) (bool, error) {
return true, nil return true, nil
} }
func (l *LevigoPersistence) Drop(value Encoder) error { func (l *LevigoPersistence) Drop(value coding.Encoder) error {
var key []byte var key []byte
var keyError error var keyError error
@ -122,7 +123,7 @@ func (l *LevigoPersistence) Drop(value Encoder) error {
return keyError return keyError
} }
func (l *LevigoPersistence) Put(key Encoder, value Encoder) error { func (l *LevigoPersistence) Put(key, value coding.Encoder) error {
var keyEncoded []byte var keyEncoded []byte
var keyError error var keyError error

View file

@ -1,15 +1,19 @@
package main package main
import (
"github.com/matttproud/prometheus/coding"
)
type Pair struct { type Pair struct {
Left []byte Left []byte
Right []byte Right []byte
} }
type Persistence interface { type Persistence interface {
Has(key Encoder) (bool, error) Has(key coding.Encoder) (bool, error)
Get(key Encoder) ([]byte, error) Get(key coding.Encoder) ([]byte, error)
GetAll() ([]Pair, error) GetAll() ([]Pair, error)
Drop(key Encoder) error Drop(key coding.Encoder) error
Put(key Encoder, value Encoder) error Put(key, value coding.Encoder) error
Close() error Close() error
} }