private def buildItemIndex()

in atlas-core/src/main/scala/com/netflix/atlas/core/index/RoaringTagIndex.scala [122:180]


  private def buildItemIndex()
    : (Array[ItemId], RoaringKeyMap, RoaringValueMap, Array[Long], Array[Array[Int]]) = {

    // Sort items array based on the id, allows for efficient paging of requests using the id
    // as the offset
    logger.debug(s"building index with ${items.length} items, starting sort")
    val itemIds = new Array[ItemId](items.length)

    // Build the main index
    logger.debug(s"building index with ${items.length} items, create main key map")
    val kidx = new RoaringValueMap(-1)
    val idx = new RoaringKeyMap(-1)
    val itemTags = new Array[Array[Int]](items.length)
    val tagsSet = new LongHashSet(-1L, items.length)
    var pos = 0
    while (pos < items.length) {
      itemIds(pos) = items(pos).id
      itemTags(pos) = new Array[Int](2 * items(pos).tags.size)
      var itemTagsPos = 0
      items(pos).foreach { (k, v) =>
        val kp = keyMap.get(k, -1)
        var vidx = idx.get(kp)
        if (vidx == null) {
          vidx = new RoaringValueMap(-1)
          idx.put(kp, vidx)
        }

        // Add to value index
        val vp = valueMap.get(v, -1)
        var matchSet = vidx.get(vp)
        if (matchSet == null) {
          matchSet = new RoaringBitmap()
          vidx.put(vp, matchSet)
        }
        matchSet.add(pos)

        // Add to key index
        matchSet = kidx.get(kp)
        if (matchSet == null) {
          matchSet = new RoaringBitmap()
          kidx.put(kp, matchSet)
        }
        matchSet.add(pos)

        itemTags(pos)(itemTagsPos) = kp
        itemTags(pos)(itemTagsPos + 1) = vp
        itemTagsPos += 2

        val t = (kp.toLong << 32) | vp.toLong
        tagsSet.add(t)
      }
      pos += 1
    }

    val tagsArray = tagsSet.toArray
    util.Arrays.sort(tagsArray)

    (itemIds, idx, kidx, tagsArray, itemTags)
  }