@Override public PColumn getPKColumn(String name) throws ColumnNotFoundException { List<PColumn> columns = columnsByName.get(name); int size = columns.size(); if (size == 0) { throw new ColumnNotFoundException(name); } if (size > 1) { do { PColumn column = columns.get(--size); if (column.getFamilyName() == null) { return column; } } while (size > 0); throw new ColumnNotFoundException(name); } return columns.get(0); }
@Override public void write(DataOutput output) throws IOException { Bytes.writeByteArray(output, name.getBytes()); WritableUtils.writeVInt(output, type.ordinal()); WritableUtils.writeVLong(output, sequenceNumber); output.writeLong(timeStamp); Bytes.writeByteArray( output, pkName == null ? ByteUtil.EMPTY_BYTE_ARRAY : Bytes.toBytes(pkName)); WritableUtils.writeVInt(output, allColumns.size()); for (int i = 0; i < allColumns.size(); i++) { PColumn column = allColumns.get(i); column.write(output); } stats.write(output); }
@Override public PColumn getColumn(String name) throws ColumnNotFoundException, AmbiguousColumnException { List<PColumn> columns = columnsByName.get(name); int size = columns.size(); if (size == 0) { throw new ColumnNotFoundException(name); } if (size > 1) { for (PColumn column : columns) { if (QueryConstants.DEFAULT_COLUMN_FAMILY.equals(column.getFamilyName().getString())) { // Allow ambiguity with default column, since a user would not know how to prefix it. return column; } } throw new AmbiguousColumnException(name); } return columns.get(0); }
private void init( PName name, PTableType type, long timeStamp, long sequenceNumber, String pkName, List<PColumn> columns, PTableStats stats) { this.name = name; this.type = type; this.timeStamp = timeStamp; this.sequenceNumber = sequenceNumber; this.columnsByName = ArrayListMultimap.create(columns.size(), 1); this.pkName = pkName; List<PColumn> pkColumns = Lists.newArrayListWithExpectedSize(columns.size() - 1); PColumn[] allColumns = new PColumn[columns.size()]; RowKeySchemaBuilder builder = new RowKeySchemaBuilder(); for (int i = 0; i < allColumns.length; i++) { PColumn column = columns.get(i); allColumns[column.getPosition()] = column; PName familyName = column.getFamilyName(); if (familyName == null) { pkColumns.add(column); builder.addField(column); } columnsByName.put(column.getName().getString(), column); } this.pkColumns = ImmutableList.copyOf(pkColumns); this.rowKeySchema = builder.setMinNullable(pkColumns.size()).build(); this.allColumns = ImmutableList.copyOf(allColumns); // Two pass so that column order in column families matches overall column order // and to ensure that column family order is constant int maxExpectedSize = allColumns.length - pkColumns.size(); // Maintain iteration order so that column families are ordered as they are listed Map<PName, List<PColumn>> familyMap = Maps.newLinkedHashMap(); for (PColumn column : allColumns) { PName familyName = column.getFamilyName(); if (familyName != null) { List<PColumn> columnsInFamily = familyMap.get(familyName); if (columnsInFamily == null) { columnsInFamily = Lists.newArrayListWithExpectedSize(maxExpectedSize); familyMap.put(familyName, columnsInFamily); } columnsInFamily.add(column); } } Iterator<Map.Entry<PName, List<PColumn>>> iterator = familyMap.entrySet().iterator(); PColumnFamily[] families = new PColumnFamily[familyMap.size()]; ImmutableMap.Builder<String, PColumnFamily> familyByString = ImmutableMap.builder(); ImmutableSortedMap.Builder<byte[], PColumnFamily> familyByBytes = ImmutableSortedMap.orderedBy(Bytes.BYTES_COMPARATOR); for (int i = 0; i < families.length; i++) { Map.Entry<PName, List<PColumn>> entry = iterator.next(); PColumnFamily family = new PColumnFamilyImpl(entry.getKey(), entry.getValue()); families[i] = family; familyByString.put(family.getName().getString(), family); familyByBytes.put(family.getName().getBytes(), family); } this.families = ImmutableList.copyOf(families); this.familyByBytes = familyByBytes.build(); this.familyByString = familyByString.build(); this.stats = stats; }
@Override public int newKey(ImmutableBytesWritable key, byte[][] values) { int i = 0; TrustedByteArrayOutputStream os = new TrustedByteArrayOutputStream(SchemaUtil.estimateKeyLength(this)); try { List<PColumn> columns = getPKColumns(); int nColumns = columns.size(); PColumn lastPKColumn = columns.get(nColumns - 1); while (i < values.length && i < nColumns) { PColumn column = columns.get(i); PDataType type = column.getDataType(); // This will throw if the value is null and the type doesn't allow null byte[] byteValue = values[i++]; if (byteValue == null) { byteValue = ByteUtil.EMPTY_BYTE_ARRAY; } // An empty byte array return value means null. Do this, // since a type may have muliple representations of null. // For example, VARCHAR treats both null and an empty string // as null. This way we don't need to leak that part of the // implementation outside of PDataType by checking the value // here. if (byteValue.length == 0 && !column.isNullable()) { throw new ConstraintViolationException( name.getString() + "." + column.getName().getString() + " may not be null"); } Integer byteSize = column.getByteSize(); if (type.isFixedWidth()) { // TODO: handle multi-byte characters if (byteValue.length != byteSize) { throw new ConstraintViolationException( name.getString() + "." + column.getName().getString() + " must be " + byteSize + " bytes (" + SchemaUtil.toString(type, byteValue) + ")"); } } else if (byteSize != null && byteValue.length > byteSize) { throw new ConstraintViolationException( name.getString() + "." + column.getName().getString() + " may not exceed " + byteSize + " bytes (" + SchemaUtil.toString(type, byteValue) + ")"); } os.write(byteValue, 0, byteValue.length); // Separate variable length column values in key with zero byte if (!type.isFixedWidth() && column != lastPKColumn) { os.write(SEPARATOR_BYTE); } } // If some non null pk values aren't set, then throw if (i < nColumns) { PColumn column = columns.get(i); PDataType type = column.getDataType(); if (type.isFixedWidth() || !column.isNullable()) { throw new ConstraintViolationException( name.getString() + "." + column.getName().getString() + " may not be null"); } // Separate variable length column values in key with zero byte if (column != lastPKColumn) { os.write(SEPARATOR_BYTE); } } key.set(os.getBuffer(), 0, os.size()); return i; } finally { try { os.close(); } catch (IOException e) { throw new RuntimeException(e); // Impossible } } }
/** * init station code map * * @param file station code file path */ private static void initStationCode(String file) { List<String> lstStation = new Vector<>(); strConfig = file + ".process"; File fsP = new File(strConfig); if (!fsP.exists()) { try { fsP.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } else { BufferedReader readp = null; try { String line = null; readp = new BufferedReader(new FileReader(fsP)); while ((line = readp.readLine()) != null) { processed.add(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (readp != null) { try { readp.close(); } catch (IOException e) { e.printStackTrace(); } } } } File fs = new File(file); BufferedReader reader = null; try { String stationName = null; String stationCode = null; String line = null; String[] splitLine = null; reader = new BufferedReader(new FileReader(fs)); while ((line = reader.readLine()) != null) { splitLine = line.split("\t"); if (splitLine.length != 2) { continue; } stationName = splitLine[0]; stationCode = splitLine[1]; mapStationCode.put(stationName, stationCode); lstStation.add(stationName); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader == null) { try { reader.close(); } catch (IOException e1) { e1.printStackTrace(); } } } int len = lstStation.size(); String start = null; for (int i = 0; i < len; i++) { start = lstStation.get(i); for (String end : lstStation) { if (start.equals(end) || processed.contains(start + ":" + end)) { continue; } lstAllProcessStation.add(new DefaultKeyValue(start, end)); } } }