File size: 2,666 Bytes
8d55ddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
'''
Created on 01/10/2012
@author: Amr Hassan
'''
import psycopg2
from psycopg2 import extras
import math
import string
import sys
import DBConnection
import logging
from io import BytesIO
import numpy
import struct

class DBInterface(object):
    '''
    This class will handle the interface with the DB
    '''
    FormatMapping={'int':'INT',
                   'float':'FLOAT',
                   'long long':'BIGINT'                   
                   }
    
    
    
    
   
    def __init__(self,CurrentSAGEStruct,Options,CommRank):
        '''
        Constructor
        '''
        self.Options=Options       
        self.CurrentSAGEStruct=CurrentSAGEStruct        
        self.DBConnection=DBConnection.DBConnection(Options)       
    
             
    
        
    def CloseConnections(self):        
        self.DBConnection.CloseConnections()
        
                
           
            
    
  
    def GetListofUnProcessedTrees(self,CommSize,CommRank):
        return self.DBConnection.ExecuteQuerySQLStatment("SELECT * FROM TreeProcessingSummary where Processed=FALSE and mod(LoadingTreeID,"+str(CommSize)+")="+str(CommRank)+" order by LoadingTreeID;")        
    
    def SetTreeAsProcessed(self,TreeID):
        self.DBConnection.ExecuteNoQuerySQLStatment("UPDATE TreeProcessingSummary set Processed=TRUE where LoadingTreeID= "+str(TreeID))
    
        
    
        
        
    
    def CreateNewTree(self,TableID,TreeData):
        # TreeData Must be a numpy fields array with the correct format for postgresql
        
        try:
            cpyData = BytesIO()            
            TablePrefix=self.Options['PGDB:TreeTablePrefix']
            NewTableName=TablePrefix+str(TableID)            
            HostIndex=self.DBConnection.MapTableIDToServerIndex(TableID)          
            
            
            cpyData.write(struct.pack('!11sii', b'PGCOPY\n\377\r\n\0', 0, 0))
            cpyData.write(TreeData.tostring())
            cpyData.write(struct.pack('!h', -1))  # file trailer    
            logging.info("Start Copying Data....")
            cpyData.seek(0)
            self.DBConnection.ActiveCursors[HostIndex].copy_expert('COPY '+NewTableName+' FROM STDIN WITH BINARY', cpyData)       
            logging.info("Adding "+str(len(TreeData))+" Galaxies to Table"+str(TableID))
        except Exception as Exp:
            logging.info(">>>>>Error While Processing Tree")
            logging.info(type(Exp))
            logging.info(Exp.args)
            logging.info(Exp)            
            raw_input("PLease press enter to continue.....")