(params database.Params)
| 46 | } |
| 47 | |
| 48 | func (driver *Sharded) Query(params database.Params) (*database.Analytics, error) { |
| 49 | // Construct DBPath |
| 50 | dbPath := manager.DBPath{ |
| 51 | Name: params.DBName, |
| 52 | Directory: driver.directory, |
| 53 | } |
| 54 | |
| 55 | // Check if DB file exists |
| 56 | dbExists, err := driver.DBManager.DBExists(dbPath) |
| 57 | if err != nil { |
| 58 | driver.DBManager.Logger.Error("Error executing Query/DBExists on DB %s: %v\n", dbPath, err) |
| 59 | return nil, &errors.InternalError |
| 60 | } |
| 61 | |
| 62 | // DB doesn't exist |
| 63 | if !dbExists { |
| 64 | return nil, &errors.InvalidDatabaseName |
| 65 | } |
| 66 | |
| 67 | // At this point, there should be shards to query |
| 68 | // Get list of shards by reading directory |
| 69 | shards := listShards(dbPath) |
| 70 | analytics := database.Analytics{} |
| 71 | cachedRequest := cachedRequest(params.URL) |
| 72 | |
| 73 | // Read from each shard |
| 74 | for _, shardName := range shards { |
| 75 | |
| 76 | // Don't include shard if not in timerange |
| 77 | shardInt, err := shardNameToInt(shardName) |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | |
| 82 | startInt, endInt := timeRangeToInt(params.TimeRange) |
| 83 | if shardInt < startInt || shardInt > endInt { |
| 84 | continue |
| 85 | } |
| 86 | |
| 87 | // Get result if is cached |
| 88 | var shardAnalytics *database.Analytics |
| 89 | |
| 90 | cacheURL, err := formatURLForCache(params.URL, shardInt, startInt, endInt, params.TimeRange) |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | |
| 95 | cached, inCache := driver.cache.Get(cacheURL) |
| 96 | if inCache { |
| 97 | err = json.Unmarshal(cached, &shardAnalytics) |
| 98 | if err != nil { |
| 99 | driver.DBManager.Logger.Error("Error unmarshaling from cache: %v\n", err) |
| 100 | return nil, err |
| 101 | } |
| 102 | } else { |
| 103 | // Else query shard |
| 104 | // Construct each shard DBPath |
| 105 | shardPath := manager.DBPath{ |
nothing calls this directly
no test coverage detected