forked from tadgh/ArgoRevisit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlogMongoQueries.py
80 lines (61 loc) · 2.25 KB
/
BlogMongoQueries.py
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
import logging
import math
import pickle
import random
import subprocess
from bson import Code
from pymongo import IndexModel, ASCENDING
from bench_utils import get_random_data_slice
from Query import Query
from BlogGlobal import mongo_data, mongo_db
from BlogSettings import (
DATA_SIZE,
FILES_DIR,
MONGO_FILENAME,
)
__author__ = 'Ahmed'
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
class Query1Mongo(Query):
def __init__(self):
super(Query1Mongo, self).__init__("Selection Query 1")
def prepare(self):
# getting 10 percent of data
self.arguments = get_random_data_slice(DATA_SIZE, 0.1)
def db_command(self):
return mongo_data.find(
{"$and": [
{"user_id": {"$gte": self.arguments[0]}},
{"user_id": {"$lt": self.arguments[1]}}
]}
)
class Query2Mongo(Query):
def __init__(self):
super(Query2Mongo, self).__init__("Selection Query 2")
def prepare(self):
# getting 10 percent of data
self.arguments = get_random_data_slice(DATA_SIZE, 0.1)
def db_command(self):
return mongo_data.find(
{"authored.comments": {
"$elemMatch": {"user_id": {"$gte": self.arguments[0], "$lt": self.arguments[1]}}}
},
{"authored.comments": 1},
)
class DropCollectionMongo(Query):
def __init__(self):
super(DropCollectionMongo, self).__init__("Dropping Data from Mongo")
def db_command(self):
mongo_db.drop_collection('blogdata')
class InitialLoadMongo(Query):
def __init__(self):
super(InitialLoadMongo, self).__init__("Initial Data Load")
def db_command(self):
#make a subprocess call to mongoimport
file_name = FILES_DIR + MONGO_FILENAME
load_data = subprocess.Popen(["mongoimport", "--db", "blogcompdb", "--collection", "blogdata", "--file", file_name], stdout=subprocess.PIPE)
load_data.communicate()
# create indexes on user_id and comments user_id
index1 = IndexModel([('user_id', ASCENDING)], name='user_id_index')
index2 = IndexModel([('authored.comments.user_id', ASCENDING)], name='comment_index')
mongo_data.create_indexes([index1, index2])