1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.aconfig.storage; 18 19 public class TableUtils { 20 21 private static final int[] HASH_PRIMES = 22 new int[] { 23 7, 24 17, 25 29, 26 53, 27 97, 28 193, 29 389, 30 769, 31 1543, 32 3079, 33 6151, 34 12289, 35 24593, 36 49157, 37 98317, 38 196613, 39 393241, 40 786433, 41 1572869, 42 3145739, 43 6291469, 44 12582917, 45 25165843, 46 50331653, 47 100663319, 48 201326611, 49 402653189, 50 805306457, 51 1610612741 52 }; 53 getTableSize(int numEntries)54 public static int getTableSize(int numEntries) { 55 for (int i : HASH_PRIMES) { 56 if (i < 2 * numEntries) continue; 57 return i; 58 } 59 throw new AconfigStorageException("Number of items in a hash table exceeds limit"); 60 } 61 getBucketIndex(byte[] val, int numBuckets)62 public static int getBucketIndex(byte[] val, int numBuckets) { 63 long hashVal = SipHasher13.hash(val); 64 return (int) Long.remainderUnsigned(hashVal, numBuckets); 65 } 66 67 public static class StorageFilesBundle { 68 public final PackageTable packageTable; 69 public final FlagTable flagTable; 70 public final FlagValueList flagValueList; 71 StorageFilesBundle(PackageTable pTable, FlagTable fTable, FlagValueList fValueList)72 public StorageFilesBundle (PackageTable pTable, FlagTable fTable, FlagValueList fValueList) { 73 this.packageTable = pTable; 74 this.flagTable = fTable; 75 this.flagValueList = fValueList; 76 } 77 } 78 } 79