2011-12-25 20:30:56 +00:00
|
|
|
/*
|
2012-07-01 19:06:34 +00:00
|
|
|
Copyright 2011 The Camlistore Authors.
|
2011-12-25 20:30:56 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2012-02-26 12:49:03 +00:00
|
|
|
package mongo
|
2011-12-25 20:30:56 +00:00
|
|
|
|
|
|
|
import (
|
Update from r60 to [almost] Go 1.
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
2012-02-19 05:53:06 +00:00
|
|
|
"errors"
|
2011-12-25 20:30:56 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2012-02-20 12:32:46 +00:00
|
|
|
"time"
|
2011-12-25 20:30:56 +00:00
|
|
|
|
Update from r60 to [almost] Go 1.
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
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/blobserver"
|
2012-02-26 12:49:03 +00:00
|
|
|
"camlistore.org/pkg/index"
|
Update from r60 to [almost] Go 1.
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
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/jsonconfig"
|
2011-12-25 20:30:56 +00:00
|
|
|
|
2012-07-01 19:06:34 +00:00
|
|
|
"camlistore.org/third_party/labix.org/v2/mgo"
|
|
|
|
"camlistore.org/third_party/labix.org/v2/mgo/bson"
|
2011-12-25 20:30:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// We explicitely separate the key and the value in a document,
|
|
|
|
// instead of simply storing as key:value, to avoid problems
|
|
|
|
// such as "." being an illegal char in a key name. Also because
|
|
|
|
// there is no way to do partial matching for key names (one can
|
|
|
|
// only check for their existence with bson.M{$exists: true}).
|
|
|
|
const (
|
|
|
|
collectionName = "keys"
|
2012-07-01 19:06:34 +00:00
|
|
|
mgoKey = "k"
|
|
|
|
mgoValue = "v"
|
2011-12-25 20:30:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MongoWrapper struct {
|
|
|
|
Servers string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Database string
|
|
|
|
Collection string
|
|
|
|
}
|
|
|
|
|
2012-10-23 19:45:46 +00:00
|
|
|
func (mgw *MongoWrapper) url() string {
|
|
|
|
if mgw.User == "" || mgw.Password == "" {
|
|
|
|
return mgw.Servers
|
|
|
|
}
|
|
|
|
return mgw.User + ":" + mgw.Password + "@" + mgw.Servers + "/" + mgw.Database
|
|
|
|
}
|
|
|
|
|
2012-02-20 12:32:46 +00:00
|
|
|
// Note that Ping won't work with old (1.2) mongo servers.
|
2012-02-21 14:25:29 +00:00
|
|
|
func (mgw *MongoWrapper) TestConnection(timeout time.Duration) bool {
|
2012-10-23 19:45:46 +00:00
|
|
|
session, err := mgo.DialWithTimeout(mgw.url(), timeout)
|
2011-12-25 20:30:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer session.Close()
|
2012-02-21 14:25:29 +00:00
|
|
|
session.SetSyncTimeout(timeout)
|
2011-12-25 20:30:56 +00:00
|
|
|
if err = session.Ping(); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
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
2012-02-19 05:53:06 +00:00
|
|
|
func (mgw *MongoWrapper) getConnection() (*mgo.Session, error) {
|
2011-12-25 20:30:56 +00:00
|
|
|
// TODO(mpl): do some "client caching" as in mysql, to avoid systematically dialing?
|
2012-10-23 19:45:46 +00:00
|
|
|
session, err := mgo.Dial(mgw.url())
|
2011-12-25 20:30:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
session.SetMode(mgo.Monotonic, true)
|
|
|
|
session.SetSafe(&mgo.Safe{})
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
|
2012-02-26 12:49:03 +00:00
|
|
|
// TODO(mpl): I'm only calling getCollection at the beginning, and
|
2011-12-25 20:30:56 +00:00
|
|
|
// keeping the collection around and reusing it everywhere, instead
|
|
|
|
// of calling getCollection everytime, because that's the easiest.
|
2012-02-26 12:49:03 +00:00
|
|
|
// But I can easily change that. Gustavo says it does not make
|
2011-12-25 20:30:56 +00:00
|
|
|
// much difference either way.
|
|
|
|
// Brad, what do you think?
|
Update from r60 to [almost] Go 1.
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
2012-02-19 05:53:06 +00:00
|
|
|
func (mgw *MongoWrapper) getCollection() (*mgo.Collection, error) {
|
2011-12-25 20:30:56 +00:00
|
|
|
session, err := mgw.getConnection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
session.SetSafe(&mgo.Safe{})
|
|
|
|
session.SetMode(mgo.Strong, true)
|
|
|
|
c := session.DB(mgw.Database).C(mgw.Collection)
|
2012-02-20 12:32:46 +00:00
|
|
|
return c, nil
|
2011-12-25 20:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
blobserver.RegisterStorageConstructor("mongodbindexer",
|
|
|
|
blobserver.StorageConstructor(newMongoIndexFromConfig))
|
|
|
|
}
|
|
|
|
|
2012-02-26 12:49:03 +00:00
|
|
|
func newMongoIndex(mgw *MongoWrapper) (*index.Index, error) {
|
2011-12-25 20:30:56 +00:00
|
|
|
db, err := mgw.getCollection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mongoStorage := &mongoKeys{db: db}
|
2012-02-26 12:49:03 +00:00
|
|
|
return index.New(mongoStorage), nil
|
2011-12-25 20:30:56 +00:00
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
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
2012-02-19 05:53:06 +00:00
|
|
|
func newMongoIndexFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) {
|
2011-12-25 20:30:56 +00:00
|
|
|
blobPrefix := config.RequiredString("blobSource")
|
|
|
|
mgw := &MongoWrapper{
|
2012-10-23 19:45:46 +00:00
|
|
|
Servers: config.OptionalString("host", "localhost"),
|
2011-12-25 20:30:56 +00:00
|
|
|
Database: config.RequiredString("database"),
|
2012-11-03 13:54:45 +00:00
|
|
|
User: config.OptionalString("user", ""),
|
|
|
|
Password: config.OptionalString("password", ""),
|
2011-12-25 20:30:56 +00:00
|
|
|
Collection: collectionName,
|
|
|
|
}
|
|
|
|
if err := config.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sto, err := ld.GetStorage(blobPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ix, err := newMongoIndex(mgw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ix.BlobSource = sto
|
|
|
|
|
|
|
|
// Good enough, for now:
|
|
|
|
ix.KeyFetcher = ix.BlobSource
|
|
|
|
|
|
|
|
if wipe := os.Getenv("CAMLI_MONGO_WIPE"); wipe != "" {
|
Update from r60 to [almost] Go 1.
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
2012-02-19 05:53:06 +00:00
|
|
|
dowipe, err := strconv.ParseBool(wipe)
|
2011-12-25 20:30:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if dowipe {
|
2012-02-26 12:49:03 +00:00
|
|
|
err = ix.Storage().Delete("")
|
2011-12-25 20:30:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ix, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation of index Iterator
|
|
|
|
type mongoStrIterator struct {
|
|
|
|
res bson.M
|
|
|
|
*mgo.Iter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s mongoStrIterator) Next() bool {
|
|
|
|
return s.Iter.Next(&s.res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s mongoStrIterator) Key() (key string) {
|
|
|
|
key, ok := (s.res[mgoKey]).(string)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s mongoStrIterator) Value() (value string) {
|
|
|
|
value, ok := (s.res[mgoValue]).(string)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
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
2012-02-19 05:53:06 +00:00
|
|
|
func (s mongoStrIterator) Close() error {
|
2011-12-25 20:30:56 +00:00
|
|
|
// TODO(mpl): think about anything more to be done here.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-14 04:19:36 +00:00
|
|
|
// Implementation of index.Storage
|
2011-12-25 20:30:56 +00:00
|
|
|
type mongoKeys struct {
|
|
|
|
mu sync.Mutex // guards db
|
|
|
|
db *mgo.Collection
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
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
2012-02-19 05:53:06 +00:00
|
|
|
func (mk *mongoKeys) Get(key string) (string, error) {
|
2011-12-25 20:30:56 +00:00
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
|
|
|
res := bson.M{}
|
|
|
|
q := mk.db.Find(&bson.M{mgoKey: key})
|
|
|
|
err := q.One(&res)
|
|
|
|
if err != nil {
|
2012-07-01 19:06:34 +00:00
|
|
|
if err == mgo.ErrNotFound {
|
2012-02-26 12:49:03 +00:00
|
|
|
return "", index.ErrNotFound
|
2011-12-25 20:30:56 +00:00
|
|
|
} else {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res[mgoValue].(string), err
|
|
|
|
}
|
|
|
|
|
2012-02-26 12:49:03 +00:00
|
|
|
func (mk *mongoKeys) Find(key string) index.Iterator {
|
2011-12-25 20:30:56 +00:00
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
|
|
|
// TODO(mpl): escape other special chars, or maybe replace $regex with something
|
|
|
|
// more suited if possible.
|
|
|
|
cleanedKey := strings.Replace(key, "|", `\|`, -1)
|
2012-07-01 19:06:34 +00:00
|
|
|
iter := mk.db.Find(&bson.M{mgoKey: &bson.M{"$regex": "^" + cleanedKey}}).Sort(mgoKey).Iter()
|
2011-12-25 20:30:56 +00:00
|
|
|
return mongoStrIterator{res: bson.M{}, Iter: iter}
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
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
2012-02-19 05:53:06 +00:00
|
|
|
func (mk *mongoKeys) Set(key, value string) error {
|
2011-12-25 20:30:56 +00:00
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
|
|
|
_, err := mk.db.Upsert(&bson.M{mgoKey: key}, &bson.M{mgoKey: key, mgoValue: value})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes the document with the matching key.
|
|
|
|
// If key is "", it removes all documents.
|
Update from r60 to [almost] Go 1.
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
2012-02-19 05:53:06 +00:00
|
|
|
func (mk *mongoKeys) Delete(key string) error {
|
2011-12-25 20:30:56 +00:00
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
|
|
|
if key == "" {
|
2012-07-01 19:06:34 +00:00
|
|
|
_, err := mk.db.RemoveAll(nil)
|
|
|
|
return err
|
2011-12-25 20:30:56 +00:00
|
|
|
}
|
|
|
|
return mk.db.Remove(&bson.M{mgoKey: key})
|
|
|
|
}
|
|
|
|
|
2012-02-26 12:49:03 +00:00
|
|
|
func (mk *mongoKeys) BeginBatch() index.BatchMutation {
|
|
|
|
return index.NewBatchMutation()
|
2011-12-25 20:30:56 +00:00
|
|
|
}
|
|
|
|
|
2012-02-26 12:49:03 +00:00
|
|
|
type batch interface {
|
|
|
|
Mutations() []index.Mutation
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mk *mongoKeys) CommitBatch(bm index.BatchMutation) error {
|
|
|
|
b, ok := bm.(batch)
|
2011-12-25 20:30:56 +00:00
|
|
|
if !ok {
|
2012-02-26 12:49:03 +00:00
|
|
|
return errors.New("invalid batch type")
|
2011-12-25 20:30:56 +00:00
|
|
|
}
|
2012-02-26 12:49:03 +00:00
|
|
|
|
2011-12-25 20:30:56 +00:00
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
2012-02-26 12:49:03 +00:00
|
|
|
for _, m := range b.Mutations() {
|
|
|
|
if m.IsDelete() {
|
|
|
|
if err := mk.db.Remove(bson.M{mgoKey: m.Key()}); err != nil {
|
2011-12-25 20:30:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2012-02-26 12:49:03 +00:00
|
|
|
if _, err := mk.db.Upsert(&bson.M{mgoKey: m.Key()}, &bson.M{mgoKey: m.Key(), mgoValue: m.Value()}); err != nil {
|
2011-12-25 20:30:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|