mirror of
https://github.com/perkeep/perkeep.git
synced 2025-02-26 20:25:07 +00:00

A lot is still broken, but most stuff at least compiles now. The directory tree has been rearranged now too. Go libraries are now under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign". The go tool cannot yet fetch from arbitrary domains, but discussion is happening now on which mechanism to use to allow that. For now, put the camlistore root under $GOPATH/src. Typically $GOPATH is $HOME, so Camlistore should be at $HOME/src/camlistore.org. Then you can: $ go build ./server/camlistored ... etc The build.pl script is currently disabled. It'll be resurrected at some point, but with a very different role (helping create a fake GOPATH and running the go build command, if things are installed at the wrong place, and/or running fileembed generators). Many things are certainly broken. Many things are disabled. (MySQL, all indexing, etc). Many things need to be moved into camlistore.org/third_party/{code.google.com,github.com} and updated from their r60 to Go 1 versions, where applicable. The GoMySQL stuff should be updated to use database/sql and the ziutek library implementing database/sql/driver. Help wanted. Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
139 lines
3.4 KiB
Go
139 lines
3.4 KiB
Go
/*
|
|
Copyright 2011 Google Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package blobserver
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"camlistore.org/pkg/blobref"
|
|
)
|
|
|
|
type BlobHub interface {
|
|
// For new blobs to notify
|
|
NotifyBlobReceived(blob *blobref.BlobRef)
|
|
|
|
RegisterListener(ch chan *blobref.BlobRef)
|
|
UnregisterListener(ch chan *blobref.BlobRef)
|
|
|
|
RegisterBlobListener(blob *blobref.BlobRef, ch chan *blobref.BlobRef)
|
|
UnregisterBlobListener(blob *blobref.BlobRef, ch chan *blobref.BlobRef)
|
|
}
|
|
|
|
type SimpleBlobHub struct {
|
|
l sync.Mutex
|
|
|
|
listeners map[chan *blobref.BlobRef]bool
|
|
blobListeners map[string]map[chan *blobref.BlobRef]bool
|
|
}
|
|
|
|
func (h *SimpleBlobHub) NotifyBlobReceived(blob *blobref.BlobRef) {
|
|
h.l.Lock()
|
|
defer h.l.Unlock()
|
|
|
|
// Callback channels to notify, nil until non-empty
|
|
var notify []chan *blobref.BlobRef
|
|
|
|
// Append global listeners
|
|
for ch, _ := range h.listeners {
|
|
notify = append(notify, ch)
|
|
}
|
|
|
|
// Append blob-specific listeners
|
|
if h.blobListeners != nil {
|
|
blobstr := blob.String()
|
|
if set, ok := h.blobListeners[blobstr]; ok {
|
|
for ch, _ := range set {
|
|
notify = append(notify, ch)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Run in a separate Goroutine so NotifyBlobReceived doesn't block
|
|
// callers if callbacks are slow.
|
|
go func() {
|
|
for _, ch := range notify {
|
|
ch <- blob
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (h *SimpleBlobHub) RegisterListener(ch chan *blobref.BlobRef) {
|
|
h.l.Lock()
|
|
defer h.l.Unlock()
|
|
if h.listeners == nil {
|
|
h.listeners = make(map[chan *blobref.BlobRef]bool)
|
|
}
|
|
h.listeners[ch] = true
|
|
}
|
|
|
|
func (h *SimpleBlobHub) UnregisterListener(ch chan *blobref.BlobRef) {
|
|
h.l.Lock()
|
|
defer h.l.Unlock()
|
|
if h.listeners == nil {
|
|
panic("blobhub: UnregisterListener called without RegisterListener")
|
|
}
|
|
delete(h.listeners, ch)
|
|
}
|
|
|
|
func (h *SimpleBlobHub) RegisterBlobListener(blob *blobref.BlobRef, ch chan *blobref.BlobRef) {
|
|
h.l.Lock()
|
|
defer h.l.Unlock()
|
|
if h.blobListeners == nil {
|
|
h.blobListeners = make(map[string]map[chan *blobref.BlobRef]bool)
|
|
}
|
|
bstr := blob.String()
|
|
_, ok := h.blobListeners[bstr]
|
|
if !ok {
|
|
h.blobListeners[bstr] = make(map[chan *blobref.BlobRef]bool)
|
|
}
|
|
h.blobListeners[bstr][ch] = true
|
|
}
|
|
|
|
func (h *SimpleBlobHub) UnregisterBlobListener(blob *blobref.BlobRef, ch chan *blobref.BlobRef) {
|
|
h.l.Lock()
|
|
defer h.l.Unlock()
|
|
if h.blobListeners == nil {
|
|
panic("blobhub: UnregisterBlobListener called without RegisterBlobListener")
|
|
}
|
|
bstr := blob.String()
|
|
set, ok := h.blobListeners[bstr]
|
|
if !ok {
|
|
panic("blobhub: UnregisterBlobListener called without RegisterBlobListener for " + bstr)
|
|
}
|
|
delete(set, ch)
|
|
if len(set) == 0 {
|
|
delete(h.blobListeners, bstr)
|
|
}
|
|
}
|
|
|
|
type SimpleBlobHubPartitionMap struct {
|
|
hubLock sync.Mutex
|
|
hub BlobHub
|
|
}
|
|
|
|
func (spm *SimpleBlobHubPartitionMap) GetBlobHub() BlobHub {
|
|
spm.hubLock.Lock()
|
|
defer spm.hubLock.Unlock()
|
|
if spm.hub == nil {
|
|
// TODO: in the future, allow for different blob hub
|
|
// implementations rather than the
|
|
// everything-in-memory-on-a-single-machine SimpleBlobHub.
|
|
spm.hub = new(SimpleBlobHub)
|
|
}
|
|
return spm.hub
|
|
}
|