move FindAssetsFromFilter into worker

pull/541/head
sidhujag 2020-09-10 11:48:33 -07:00
parent f359a6f44c
commit 3e43744435
2 changed files with 55 additions and 42 deletions

View File

@ -1194,6 +1194,48 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option Acco
return r, nil
}
// find assets from cache that contain filter
func (w *Worker) FindAssetsFromFilter(filter string) []*AssetsSpecific {
start := time.Now()
if w.db.GetSetupAssetCacheFirstTime() == true {
if err := w.db.SetupAssetCache(); err != nil {
glog.Error("FindAssetsFromFilter SetupAssetCache ", err)
return nil
}
w.db.SetSetupAssetCacheFirstTime(false);
}
assetDetails := make([]*AssetsSpecific, 0)
filterLower := strings.ToLower(filter)
filterLower = strings.Replace(filterLower, "0x", "", -1)
for guid, assetCached := range w.db.GetAssetCache() {
foundAsset := false
symbolLower := strings.ToLower(assetCached.AssetObj.Symbol)
if strings.Contains(symbolLower, filterLower) {
foundAsset = true
} else if len(assetCached.AssetObj.Contract) > 0 && len(filterLower) > 5 {
contractStr := hex.EncodeToString(assetCached.AssetObj.Contract)
contractLower := strings.ToLower(contractStr)
if strings.Contains(contractLower, filterLower) {
foundAsset = true
}
}
if foundAsset == true {
assetSpecific := AssetsSpecific{
AssetGuid: guid,
Symbol: assetCached.AssetObj.Symbol,
Contract: "0x" + hex.EncodeToString(assetCached.AssetObj.Contract),
TotalSupply: (*bchain.Amount)(big.NewInt(assetCached.AssetObj.TotalSupply)),
Decimals: int(assetCached.AssetObj.Precision),
Txs: int(assetCached.Transactions),
}
json.Unmarshal(assetCached.AssetObj.PubData, &assetSpecific.PubData)
assetDetails = append(assetDetails, &assetSpecific)
}
}
glog.Info("FindAssetsFromFilter finished in ", time.Since(start))
return assetDetails
}
func (w *Worker) FindAssets(filter string, page int, txsOnPage int) *Assets {
page--
if page < 0 {
@ -1201,7 +1243,7 @@ func (w *Worker) FindAssets(filter string, page int, txsOnPage int) *Assets {
}
start := time.Now()
assetsFiltered := w.db.FindAssetsFromFilter(filter)
assetsFiltered := w.FindAssetsFromFilter(filter)
var from, to int
var pg Paging
pg, from, to, page = computePaging(len(assetsFiltered), page, txsOnPage)

View File

@ -335,47 +335,6 @@ func (d *RocksDB) SetupAssetCache() error {
return nil
}
// find assets from cache that contain filter
func (d *RocksDB) FindAssetsFromFilter(filter string) []*api.AssetsSpecific {
start := time.Now()
if SetupAssetCacheFirstTime == true {
if err := d.SetupAssetCache(); err != nil {
glog.Error("FindAssetsFromFilter SetupAssetCache ", err)
return nil
}
SetupAssetCacheFirstTime = false;
}
assetDetails := make([]*api.AssetsSpecific, 0)
filterLower := strings.ToLower(filter)
filterLower = strings.Replace(filterLower, "0x", "", -1)
for guid, assetCached := range AssetCache {
foundAsset := false
symbolLower := strings.ToLower(assetCached.AssetObj.Symbol)
if strings.Contains(symbolLower, filterLower) {
foundAsset = true
} else if len(assetCached.AssetObj.Contract) > 0 && len(filterLower) > 5 {
contractStr := hex.EncodeToString(assetCached.AssetObj.Contract)
contractLower := strings.ToLower(contractStr)
if strings.Contains(contractLower, filterLower) {
foundAsset = true
}
}
if foundAsset == true {
assetSpecific := api.AssetsSpecific{
AssetGuid: guid,
Symbol: assetCached.AssetObj.Symbol,
Contract: "0x" + hex.EncodeToString(assetCached.AssetObj.Contract),
TotalSupply: (*bchain.Amount)(big.NewInt(assetCached.AssetObj.TotalSupply)),
Decimals: int(assetCached.AssetObj.Precision),
Txs: int(assetCached.Transactions),
}
json.Unmarshal(assetCached.AssetObj.PubData, &assetSpecific.PubData)
assetDetails = append(assetDetails, &assetSpecific)
}
}
glog.Info("FindAssetsFromFilter finished in ", time.Since(start))
return assetDetails
}
func (d *RocksDB) storeAssets(wb *gorocksdb.WriteBatch, assets map[uint32]*bchain.Asset) error {
if assets == nil {
@ -402,6 +361,18 @@ func (d *RocksDB) storeAssets(wb *gorocksdb.WriteBatch, assets map[uint32]*bchai
return nil
}
func (d *RocksDB) GetAssetCache() *map[uint32]*bchain.Asset {
return &AssetCache
}
func (d *RocksDB) GetSetupAssetCacheFirstTime() bool {
return SetupAssetCacheFirstTime
}
func (d *RocksDB) SetSetupAssetCacheFirstTime(cacheVal bool) bool {
SetupAssetCacheFirstTime = cacheVal
}
func (d *RocksDB) GetAsset(guid uint32, assets map[uint32]*bchain.Asset) (*bchain.Asset, error) {
var assetDb *bchain.Asset
var assetL1 *bchain.Asset