Skip to content

Commit 1e57cf0

Browse files
committed
Added method to improve the preallocate operation
1 parent bada63e commit 1e57cf0

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

CurrentDesignToOneDocumentDesign/src/cl/alma/onedocument/MongoManager.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class MongoManager implements Runnable {
4040
public static final String NOT_ASSIGNED = "na";
4141
public static final String DEFAULT_CHARACTER = "a";
4242
public static final int DEFAULT_PREALLOCATE_TIME = 1;
43+
public static final int MAX_VALUE_SIZE = 7;
4344

4445
private static final Logger log = Logger.getLogger(MongoManager.class);
4546
private static final Logger infoLog = Logger.getLogger("info_log");
@@ -57,6 +58,8 @@ public class MongoManager implements Runnable {
5758
private String threadName;
5859
private LinkedBlockingQueue<DBObject> queue;
5960

61+
private static BasicDBObject[] preallocatedDocuments;
62+
6063
static {
6164
numThreads = 0;
6265
preallocate_cont = new AtomicInteger();
@@ -66,6 +69,12 @@ public class MongoManager implements Runnable {
6669
new HashMap<String, DBCollection>(3)
6770
);
6871
documentBuffer = new DocumentBuffer<String>(N_MONITOR_POINTS);
72+
73+
// Creates several documents with different size of the value
74+
preallocatedDocuments = new BasicDBObject[MAX_VALUE_SIZE];
75+
for (int i=0; i<MAX_VALUE_SIZE; i++) {
76+
preallocatedDocuments[i] = preAllocate(i+1);
77+
}
6978
}
7079

7180
/**
@@ -79,7 +88,42 @@ public static MongoManager mongoManagerFactory(LinkedBlockingQueue<DBObject> que
7988

8089
return new MongoManager("MongoManager_"+numThreads, queue);
8190
}
91+
92+
/**
93+
* Returns the preallocate document with predefine value size.
94+
* @param valueSize Value size of the fields
95+
* @return
96+
*/
97+
public static BasicDBObject getPreallocatedDocument(int valueSize) {
98+
if (valueSize<0 || valueSize>=MAX_VALUE_SIZE)
99+
throw new IllegalArgumentException("Value size out of range.");
100+
101+
return preallocatedDocuments[valueSize];
102+
}
103+
104+
public static BasicDBObject getPreallocatedDocument(Metadata metadata, int valueSize) {
105+
if (valueSize<0 || valueSize>=MAX_VALUE_SIZE)
106+
throw new IllegalArgumentException("Value size out of range.");
107+
108+
BasicDBObject newDocument = (BasicDBObject) preallocatedDocuments[valueSize].clone();
109+
110+
newDocument.append("_id",metadata.getDocumentID().toString());
82111

112+
newDocument.append("metadata", new BasicDBObject().append(
113+
"date", metadata.getDocumentID().getStringDate()).append(
114+
"antenna", metadata.getDocumentID().getAntenna()).append(
115+
"component", metadata.getDocumentID().getComponent()).append(
116+
"property", metadata.getProperty()).append(
117+
"monitorPoint", metadata.getDocumentID().getMonitorPoint()).append(
118+
"location", metadata.getLocation()).append(
119+
"serialNumber", metadata.getSerialNumber()).append(
120+
"index", metadata.getIndex()).append(
121+
"sampleTime", metadata.getSampleTime())
122+
);
123+
124+
return newDocument;
125+
}
126+
83127
/**
84128
*
85129
* @param _mongo
@@ -508,6 +552,58 @@ public DBObject preAllocate(Metadata metadata, int valueSize) {
508552

509553
return preAllocatedDocument;
510554
}
555+
556+
/**
557+
* Creates a document with the necessary structure for a post-update of
558+
* its attributes. This method creates a document with all seconds, minutes
559+
* and hours of a day. Use it before upsert a document. This method does not
560+
* register the preallocated method in the internal buffer. There are two
561+
* ways to do that:
562+
* <br/>
563+
* 1) The automatic way: Set to <i>true</i> the preallocate argument of
564+
* the method upsert and let to the system the responsibility to manage it.
565+
* <br/>
566+
* 2) The manual way: If you want to manage the preallocate operation you
567+
* need to use these methods: isDocumentCreated(...), registerPreallocation(...)
568+
* and preAllocate(...)
569+
*
570+
* @param metadata Document metadatas
571+
* @param valueSize Size of the value that will be post-update
572+
* @return The preallocated document
573+
*/
574+
private static BasicDBObject preAllocate(int valueSize) {
575+
576+
// If the value size is equal or less than two, it use the
577+
// NOT_ASSIGNED string to represent a not assigned value and
578+
// for the values size greater or equal than three use the NOT_ASSIGNED
579+
// constant plus DEFAULT_CHARACTER for each character greater or
580+
// equal than three. In the second case, the size of the
581+
// value preallocated is equal to the size that will be assigned into
582+
// the upsert method
583+
String valueToPreallocate = NOT_ASSIGNED;
584+
for (int i=3; i<=valueSize; i++) {
585+
valueToPreallocate += DEFAULT_CHARACTER;
586+
}
587+
588+
BasicDBObject preAllocatedDocument = new BasicDBObject();
589+
BasicDBObjectBuilder hours = new BasicDBObjectBuilder();
590+
BasicDBObjectBuilder minutes = new BasicDBObjectBuilder();
591+
BasicDBObjectBuilder seconds = new BasicDBObjectBuilder();
592+
593+
for (int hour=0; hour<24; hour++) {
594+
for (int minute=0; minute<60; minute++) {
595+
for (int second=0; second<60; second++) {
596+
seconds.add(Integer.toString(second), valueToPreallocate);
597+
}
598+
minutes.add(Integer.toString(minute), seconds.get());
599+
}
600+
hours.add(Integer.toString(hour), minutes.get());
601+
}
602+
603+
preAllocatedDocument.put("hourly", hours.get());
604+
605+
return preAllocatedDocument;
606+
}
511607

512608
/**
513609
* Verifies if a document exist in the collection of the database.

CurrentDesignToOneDocumentDesign/test/cl/alma/onedocumenttest/MongoManagerTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,27 @@ public void testUpsertList() {
200200

201201
mongo.upsert(list, true);
202202
}
203+
204+
@Test
205+
public void testGetPreallocateDocument() {
206+
DBCollection coll = _database.getCollection("preallocateDocuments");
207+
208+
for (int i=0; i<MongoManager.MAX_VALUE_SIZE; i++) {
209+
coll.insert(MongoManager.getPreallocatedDocument(i));
210+
}
211+
}
212+
213+
@Test
214+
public void testGetPreallocateDocument2() {
215+
DBCollection coll = _database.getCollection("preallocateDocuments2");
216+
217+
DocumentID documentID = new DocumentID(2012, 2, 28, "DA41", "LLC",
218+
"POL_MON1");
219+
Metadata metadata = new Metadata(documentID, "ASDF Property", "TFING",
220+
"as76d6fh", 2, MongoManager.DEFAULT_PREALLOCATE_TIME);
221+
222+
for (int i=0; i<MongoManager.MAX_VALUE_SIZE; i++) {
223+
coll.insert(MongoManager.getPreallocatedDocument(metadata,i));
224+
}
225+
}
203226
}

0 commit comments

Comments
 (0)