improve naming a bit (#23)

This commit is contained in:
Adam Babik 2019-04-12 01:15:19 +02:00 committed by GitHub
parent d3c1dc5d30
commit 2afc19f21a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 12 deletions

View File

@ -12,7 +12,7 @@ run: build
.PHONY: run .PHONY: run
test: test:
go test ./... go test -a ./...
.PHONY: test .PHONY: test
test-race: test-race:

View File

@ -22,7 +22,7 @@ import (
type whisperServiceKeysManager struct { type whisperServiceKeysManager struct {
shh *whisper.Whisper shh *whisper.Whisper
passToSymMutex sync.RWMutex passToSymKeyMutex sync.RWMutex
passToSymKeyCache map[string]string passToSymKeyCache map[string]string
} }
@ -32,8 +32,8 @@ func (m *whisperServiceKeysManager) AddOrGetKeyPair(priv *ecdsa.PrivateKey) (str
} }
func (m *whisperServiceKeysManager) AddOrGetSymKeyFromPassword(password string) (string, error) { func (m *whisperServiceKeysManager) AddOrGetSymKeyFromPassword(password string) (string, error) {
m.passToSymMutex.Lock() m.passToSymKeyMutex.Lock()
defer m.passToSymMutex.Unlock() defer m.passToSymKeyMutex.Unlock()
if val, ok := m.passToSymKeyCache[password]; ok { if val, ok := m.passToSymKeyCache[password]; ok {
return val, nil return val, nil

View File

@ -35,8 +35,7 @@ type Chat struct {
lastClock int64 lastClock int64
ownMessages chan *protocol.Message // my private messages channel ownMessages chan *protocol.Message // my private messages channel
// TODO: make it a ring buffer. It will require loading newer messages // TODO: make it a ring buffer. It will require loading messages from the database.
// from the cache as well.
messages []*protocol.Message // all messages ordered by Clock messages []*protocol.Message // all messages ordered by Clock
messagesByHash map[string]*protocol.Message // quick access to messages by hash messagesByHash map[string]*protocol.Message // quick access to messages by hash
} }

View File

@ -62,6 +62,8 @@ func (d *Database) Messages(c Contact, from, to int64) (result []*protocol.Messa
limit := d.keyFromContact(c, to+1, nil) // because iter is right-exclusive limit := d.keyFromContact(c, to+1, nil) // because iter is right-exclusive
iter := d.db.NewIterator(&util.Range{Start: start, Limit: limit}, nil) iter := d.db.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
defer iter.Release()
for iter.Next() { for iter.Next() {
value := iter.Value() value := iter.Value()
buf := bytes.NewBuffer(value) buf := bytes.NewBuffer(value)
@ -77,7 +79,6 @@ func (d *Database) Messages(c Contact, from, to int64) (result []*protocol.Messa
result = append(result, &m) result = append(result, &m)
} }
iter.Release()
err = iter.Error() err = iter.Error()
return return
@ -98,14 +99,16 @@ func (d *Database) SaveMessages(c Contact, messages []*protocol.Message) error {
} }
data := buf.Bytes() data := buf.Bytes()
// Data from the buffer needs to be copied to another slice
// because a slice returned from Buffer.Bytes() is valid
// only until another write.
// As we batch writes and wait untill the loop is finished,
// slices with encoded messages must be available later.
value := make([]byte, len(data)) value := make([]byte, len(data))
copy(value, data) copy(value, data)
// The read value needs to be copied to another slice
// because a slice returned by Bytes() is valid only until
// another write.
// As we batch writes and wait untill the loop is finished,
// slices must be available later.
batch.Put(key, value) batch.Put(key, value)
buf.Reset() buf.Reset()
} }