2013-08-06 03:51:26 +00:00
|
|
|
package dbutil
|
2013-02-09 04:43:39 +00:00
|
|
|
|
|
|
|
import (
|
2013-05-09 13:41:06 +00:00
|
|
|
"github.com/vbatts/imgsrv/hash"
|
2013-05-09 16:13:46 +00:00
|
|
|
"github.com/vbatts/imgsrv/types"
|
2013-08-06 00:29:16 +00:00
|
|
|
"labix.org/v2/mgo"
|
2013-03-27 20:30:24 +00:00
|
|
|
"labix.org/v2/mgo/bson"
|
2013-02-09 04:43:39 +00:00
|
|
|
)
|
|
|
|
|
2013-08-06 03:51:26 +00:00
|
|
|
type Util struct {
|
|
|
|
Gfs *mgo.GridFS
|
|
|
|
}
|
2013-02-11 16:00:57 +00:00
|
|
|
|
2013-08-06 03:51:26 +00:00
|
|
|
func (u Util) GetFileByFilename(filename string) (this_file types.File, err error) {
|
|
|
|
err = u.Gfs.Find(bson.M{"filename": filename}).One(&this_file)
|
2013-03-27 20:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return this_file, err
|
|
|
|
}
|
|
|
|
return this_file, nil
|
2013-02-11 16:00:57 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 03:51:26 +00:00
|
|
|
func (u Util) GetFileRandom() (this_file types.File, err error) {
|
2013-05-09 13:41:06 +00:00
|
|
|
r := hash.Rand64()
|
2013-08-06 03:51:26 +00:00
|
|
|
err = u.Gfs.Find(bson.M{"random": bson.M{"$gt": r}}).One(&this_file)
|
2013-03-27 20:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return this_file, err
|
|
|
|
}
|
|
|
|
if len(this_file.Md5) == 0 {
|
2013-08-06 03:51:26 +00:00
|
|
|
err = u.Gfs.Find(bson.M{"random": bson.M{"$lt": r}}).One(&this_file)
|
2013-03-27 20:30:24 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return this_file, err
|
|
|
|
}
|
|
|
|
return this_file, nil
|
2013-02-11 16:00:57 +00:00
|
|
|
}
|
|
|
|
|
2013-05-09 16:13:46 +00:00
|
|
|
/* Check whether this types.File filename is on Mongo */
|
2013-08-06 03:51:26 +00:00
|
|
|
func (u Util) HasFileByFilename(filename string) (exists bool, err error) {
|
|
|
|
c, err := u.Gfs.Find(bson.M{"filename": filename}).Count()
|
2013-03-27 20:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
exists = (c > 0)
|
|
|
|
return exists, nil
|
2013-02-09 04:43:39 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 03:51:26 +00:00
|
|
|
func (u Util) HasFileByMd5(md5 string) (exists bool, err error) {
|
|
|
|
c, err := u.Gfs.Find(bson.M{"md5": md5}).Count()
|
2013-03-27 20:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
exists = (c > 0)
|
|
|
|
return exists, nil
|
2013-02-09 04:43:39 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 03:51:26 +00:00
|
|
|
func (u Util) HasFileByKeyword(keyword string) (exists bool, err error) {
|
|
|
|
c, err := u.Gfs.Find(bson.M{"metadata": bson.M{"keywords": keyword}}).Count()
|
2013-03-27 20:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
exists = (c > 0)
|
|
|
|
return exists, nil
|
2013-02-09 04:43:39 +00:00
|
|
|
}
|
2013-08-06 00:29:16 +00:00
|
|
|
|
2013-08-06 15:00:57 +00:00
|
|
|
/*
|
|
|
|
get a list of file extensions and their frequency count
|
|
|
|
*/
|
|
|
|
func (u Util) GetExtensions() (kp []types.IdCount, err error) {
|
|
|
|
job := &mgo.MapReduce{
|
|
|
|
Map: `
|
|
|
|
function() {
|
|
|
|
if (!this.filename) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = this.filename.split(".")
|
|
|
|
ext = s[s.length - 1] // get the last segment of the split
|
|
|
|
emit(ext,1);
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
Reduce: `
|
|
|
|
function(previous, current) {
|
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
for (index in current) {
|
|
|
|
count += current[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
if _, err := u.Gfs.Find(nil).MapReduce(job, &kp); err != nil {
|
|
|
|
return kp, err
|
|
|
|
}
|
|
|
|
return kp, nil
|
|
|
|
}
|
2013-08-06 03:51:26 +00:00
|
|
|
/*
|
|
|
|
get a list of keywords and their frequency count
|
|
|
|
*/
|
2013-08-06 14:37:58 +00:00
|
|
|
func (u Util) GetKeywords() (kp []types.IdCount, err error) {
|
2013-08-06 00:29:16 +00:00
|
|
|
job := &mgo.MapReduce{
|
|
|
|
Map: `
|
|
|
|
function() {
|
|
|
|
if (!this.metadata.keywords) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (index in this.metadata.keywords) {
|
|
|
|
emit(this.metadata.keywords[index], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
Reduce: `
|
|
|
|
function(previous, current) {
|
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
for (index in current) {
|
|
|
|
count += current[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
}
|
2013-08-06 03:51:26 +00:00
|
|
|
if _, err := u.Gfs.Find(nil).MapReduce(job, &kp); err != nil {
|
2013-08-06 00:29:16 +00:00
|
|
|
return kp, err
|
|
|
|
}
|
|
|
|
return kp, nil
|
|
|
|
}
|