|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/src-d/code-annotation/server/dbutil" |
| 8 | +) |
| 9 | + |
| 10 | +type uastImportCmd struct { |
| 11 | + commandDesc |
| 12 | + Args struct { |
| 13 | + InternalDBPath string `description:"filepath to the internal SQLite database"` |
| 14 | + SourceDBPath string `description:"filepath to the SQLite database containing the UAST to import"` |
| 15 | + } `positional-args:"yes" required:"yes"` |
| 16 | +} |
| 17 | + |
| 18 | +var uastImportOpts = uastImportCmd{ |
| 19 | + commandDesc: commandDesc{ |
| 20 | + name: "uast-import", |
| 21 | + shortDesc: "Import UASTs", |
| 22 | + longDesc: "Adds UASTs to the sqlite://InternalDBPath database reading from sqlite://SourceDBPath database", |
| 23 | + }, |
| 24 | +} |
| 25 | + |
| 26 | +const ( |
| 27 | + filePairsWithoutUastQuery = `SELECT |
| 28 | + blob_id_a, uast_a IS NOT NULL as hasUastA, blob_id_b, uast_b IS NOT NULL as hasUastB |
| 29 | + FROM file_pairs |
| 30 | + WHERE hasUastA = 0 or hasUastB = 0` |
| 31 | + uastByBlobIDQuery = `SELECT uast_%s |
| 32 | + FROM files |
| 33 | + WHERE blob_id_%s = CAST($1 AS BLOB) |
| 34 | + LIMIT 1` |
| 35 | + updateByBlobIDQuery = `UPDATE file_pairs |
| 36 | + SET uast_%s = $1 |
| 37 | + WHERE blob_id_%s = $2 and uast_%s IS NULL` |
| 38 | + indexAddBlobID = `CREATE INDEX blob_id_%s ON file_pairs (blob_id_%s);` |
| 39 | +) |
| 40 | + |
| 41 | +type file struct { |
| 42 | + side string |
| 43 | + blobID string |
| 44 | +} |
| 45 | + |
| 46 | +func (c *uastImportCmd) Execute(args []string) error { |
| 47 | + internalDb, err := dbutil.Open(fmt.Sprintf(sqliteDSN, c.Args.InternalDBPath), false) |
| 48 | + if err != nil { |
| 49 | + log.Fatal(err) |
| 50 | + } |
| 51 | + |
| 52 | + defer internalDb.Close() |
| 53 | + |
| 54 | + sourceDb, err := dbutil.Open(fmt.Sprintf(sqliteDSN, c.Args.SourceDBPath), false) |
| 55 | + if err != nil { |
| 56 | + log.Fatal(err) |
| 57 | + } |
| 58 | + |
| 59 | + defer sourceDb.Close() |
| 60 | + |
| 61 | + fixSourceDb(sourceDb) |
| 62 | + |
| 63 | + files, fileReadfailures := getFilesToUpdate(internalDb) |
| 64 | + |
| 65 | + var rowsEdited, uastFails, uastsImported int64 |
| 66 | + for _, file := range files { |
| 67 | + affectedRows, err := importUastForBlobID(internalDb, sourceDb, file.side, file.blobID) |
| 68 | + if err != nil { |
| 69 | + fmt.Println(err) |
| 70 | + uastFails++ |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + rowsEdited += affectedRows |
| 75 | + uastsImported++ |
| 76 | + } |
| 77 | + |
| 78 | + fmt.Printf(` |
| 79 | +UASTs added into the internal DB: |
| 80 | +- processed files: %d |
| 81 | + - succeeded: |
| 82 | + - imported UASTs: %d |
| 83 | + - edited rows: %d |
| 84 | + - failed: %d |
| 85 | +- FilePair read errors: %d |
| 86 | +`, len(files), uastsImported, rowsEdited, uastFails, fileReadfailures) |
| 87 | + |
| 88 | + if fileReadfailures+uastFails > 0 { |
| 89 | + log.Fatal("Some rows could not be properly updated") |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +type files map[string]file |
| 96 | + |
| 97 | +func (f *files) add(blobID string, side string, ignore bool) { |
| 98 | + if ignore { |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + if _, ok := (*f)[blobID+"_"+side]; !ok { |
| 103 | + (*f)[blobID+"_"+side] = file{side: side, blobID: blobID} |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func getFilesToUpdate(internalDb dbutil.DB) (map[string]file, int64) { |
| 108 | + rows, err := internalDb.Query(filePairsWithoutUastQuery) |
| 109 | + if err != nil { |
| 110 | + log.Fatal(err) |
| 111 | + } |
| 112 | + |
| 113 | + defer rows.Close() |
| 114 | + |
| 115 | + filesToImport := files{} |
| 116 | + var failures int64 |
| 117 | + for rows.Next() { |
| 118 | + var blobIDA, blobIDB string |
| 119 | + var hasUastA, hasUastB int |
| 120 | + err := rows.Scan(&blobIDA, &hasUastA, &blobIDB, &hasUastB) |
| 121 | + if err != nil { |
| 122 | + fmt.Printf("Failed to read row from internal DB\nerror: %v\n", err) |
| 123 | + failures++ |
| 124 | + continue |
| 125 | + } |
| 126 | + |
| 127 | + filesToImport.add(blobIDA, leftFile, hasUastA == 1) |
| 128 | + filesToImport.add(blobIDB, rightFile, hasUastB == 1) |
| 129 | + } |
| 130 | + |
| 131 | + return filesToImport, failures |
| 132 | +} |
| 133 | + |
| 134 | +func importUastForBlobID(internalDb dbutil.DB, sourceDb dbutil.DB, side string, blobID string) (int64, error) { |
| 135 | + uast, err := getUastByBlobID(sourceDb, side, blobID) |
| 136 | + if err != nil { |
| 137 | + return 0, fmt.Errorf("uast_%s could not be retrieved for blobID#%s; %s", side, blobID, err) |
| 138 | + } |
| 139 | + |
| 140 | + return setUastToBlobID(internalDb, side, blobID, uast) |
| 141 | +} |
| 142 | + |
| 143 | +func getUastByBlobID(sourceDb dbutil.DB, side string, blobID string) ([]byte, error) { |
| 144 | + var uast []byte |
| 145 | + if err := sourceDb.QueryRow(fmt.Sprintf(uastByBlobIDQuery, side, side), blobID).Scan(&uast); err != nil { |
| 146 | + return nil, err |
| 147 | + } |
| 148 | + |
| 149 | + return uast, nil |
| 150 | +} |
| 151 | + |
| 152 | +func setUastToBlobID(internalDb dbutil.DB, side string, blobID string, uast []byte) (int64, error) { |
| 153 | + res, err := internalDb.Exec(fmt.Sprintf(updateByBlobIDQuery, side, side, side), uast, blobID) |
| 154 | + if err != nil { |
| 155 | + return 0, fmt.Errorf("uast_%s could not be saved for blobID#%s; %s", side, blobID, err) |
| 156 | + } |
| 157 | + |
| 158 | + rows, _ := res.RowsAffected() |
| 159 | + if rows == 0 { |
| 160 | + return 0, fmt.Errorf("no uast_%s to be imported for blobID#%s", side, blobID) |
| 161 | + } |
| 162 | + |
| 163 | + return rows, nil |
| 164 | +} |
| 165 | + |
| 166 | +func fixSourceDb(sourceDb dbutil.DB) error { |
| 167 | + if _, err := sourceDb.Exec(fmt.Sprintf(indexAddBlobID, leftFile, leftFile)); err != nil { |
| 168 | + return fmt.Errorf("can not create index over blob_id_%s; %s", leftFile, err) |
| 169 | + } |
| 170 | + |
| 171 | + if _, err := sourceDb.Exec(fmt.Sprintf(indexAddBlobID, rightFile, rightFile)); err != nil { |
| 172 | + return fmt.Errorf("can not create index over blob_id_%s; %s", rightFile, err) |
| 173 | + } |
| 174 | + |
| 175 | + return nil |
| 176 | +} |
0 commit comments