Skip to content

Commit

Permalink
implement file db (#76)
Browse files Browse the repository at this point in the history
* implement file and db for prebid-server

* move new functions to a new file
  • Loading branch information
HaoyangYuan2000 authored Feb 24, 2025
1 parent 1dc1cdb commit e1cf4ff
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
14 changes: 14 additions & 0 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,20 @@ func getJsonSyntaxError(testJSON []byte) (bool, string) {
}

func (deps *endpointDeps) getStoredRequests(ctx context.Context, requestJson []byte, impInfo []ImpExtPrebidData) (string, bool, map[string]json.RawMessage, map[string]json.RawMessage, []error) {
if isDbFetched(requestJson) {
dbReqMap, dbImpMap, parseErrs := parseDbStoredMaps(requestJson)
if len(parseErrs) > 0 {
return "", false, nil, nil, parseErrs
}

// same parameters as normal
storedBidRequestId, hasStoredBidRequest, err := getStoredRequestId(requestJson)
if err != nil {
return "", false, nil, nil, []error{err}
}

return storedBidRequestId, hasStoredBidRequest, dbReqMap, dbImpMap, nil
}
// Parse the Stored Request IDs from the BidRequest and Imps.
storedBidRequestId, hasStoredBidRequest, err := getStoredRequestId(requestJson)
if err != nil {
Expand Down
41 changes: 41 additions & 0 deletions endpoints/openrtb2/db_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package openrtb2

import (
"encoding/json"
"fmt"

"github.com/buger/jsonparser"
)

func isDbFetched(requestJson []byte) bool {
val, dataType, _, err := jsonparser.Get(requestJson, "ext", "db_fetched")
if err != nil {
return false
}
if dataType == jsonparser.Boolean && string(val) == "true" {
return true
}
return false
}

func parseDbStoredMaps(requestJson []byte) (map[string]json.RawMessage, map[string]json.RawMessage, []error) {
var errs []error
storedReqMap := make(map[string]json.RawMessage)
storedImpMap := make(map[string]json.RawMessage)

rawReqs, dt, _, err := jsonparser.Get(requestJson, "ext", "db_storedrequests")
if err == nil && dt == jsonparser.Object {
if e := json.Unmarshal(rawReqs, &storedReqMap); e != nil {
errs = append(errs, fmt.Errorf("failed to unmarshal db_storedrequests: %v", e))
}
}

rawImps, dt2, _, err2 := jsonparser.Get(requestJson, "ext", "db_storedimps")
if err2 == nil && dt2 == jsonparser.Object {
if e := json.Unmarshal(rawImps, &storedImpMap); e != nil {
errs = append(errs, fmt.Errorf("failed to unmarshal db_storedimps: %v", e))
}
}

return storedReqMap, storedImpMap, errs
}

0 comments on commit e1cf4ff

Please sign in to comment.