-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoConsole.groovy
80 lines (75 loc) · 2.53 KB
/
MongoConsole.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@Grab('com.gmongo:gmongo:0.9.5')
@GrabConfig(systemClassLoader=true)
import com.gmongo.GMongo
import com.mongodb.*
import javax.swing.UIManager
import groovy.ui.Console
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.customizers.ImportCustomizer
import groovy.transform.*
def templates = [
'group': '''\
|def (dbName, colName) = ['test', 'test']
|def db = mongo.getDB(dbName)
|def key = [:]
|def cond = [:]
|def initial = [:]
|def reduce = \'''
|function(obj, prev) {
|
|}\'''
|
|db[colName].group(key, cond, initial, reduce)'''.stripMargin(),
'mapReduce': '''\
|import static com.mongodb.MapReduceCommand.OutputType.*
|
|def (dbName, colName) = ['test', 'test']
|def db = mongo.getDB(dbName)
|def map = \'''
|function() {
| emit(this._id, 1)
|}\'''
|def reduce = \'''
|function(key, vals) {
| print('key: ' + key + ', vals: ' + vals)
| var sum = 0
| vals.forEach(function() {
| sum += val
| })
| return sum
|}\'''
|def outputTarget = 'my_output'
|def outputType = REPLACE
|def query = [:]
|
|db[colName].mapReduce(map, reduce, outputTarget, outputType, query)
|db[outputTarget].find()'''.stripMargin()
]
def browse = java.awt.Desktop.desktop.&browse
def mongoMenu = {
menu('Mongo') {
menu('Templates') {
templates.each { name, template ->
menuItem name, actionPerformed: { inputEditor.textEditor.text = template }
}
def templateDir = new File(System.getProperty('user.home')+'/.groovy-consoles/mongo')
if (templateDir.exists()) {
separator()
templateDir.eachFileMatch(groovy.io.FileType.FILES, ~/.*\.groovy/) { file ->
menuItem file.name - ~/\.groovy$/, actionPerformed: { inputEditor.textEditor.text = file.text }
}
}
}
menu('Documents') {
menuItem 'MongoDB Manual', actionPerformed: { browse 'http://www.mongodb.org/display/DOCS/Manual'.toURI() }
menuItem 'Java Driver API', actionPerformed: { browse 'http://api.mongodb.org/java/current/'.toURI() }
}
}
}
UIManager.lookAndFeel = UIManager.systemLookAndFeelClassName
new Console(Console.class.classLoader.getRootLoader(), new Binding(mongo:new GMongo())).run(
Console.frameConsoleDelegates << [menuBarDelegate: {arg->
current.JMenuBar = build(arg)
current.JMenuBar.add(build(mongoMenu))
}]
)