keith-turner commented on code in PR #4049: URL: https://github.com/apache/accumulo/pull/4049#discussion_r1428150829
########## core/src/main/java/org/apache/accumulo/core/fate/accumulo/AccumuloStore.java: ########## @@ -0,0 +1,288 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.accumulo.core.fate.accumulo; + +import static java.util.Collections.reverseOrder; +import static org.apache.accumulo.core.util.LazySingletons.RANDOM; + +import java.io.Serializable; +import java.util.Comparator; +import java.util.List; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import org.apache.accumulo.core.client.Scanner; +import org.apache.accumulo.core.client.TableNotFoundException; +import org.apache.accumulo.core.clientImpl.ClientContext; +import org.apache.accumulo.core.data.Key; +import org.apache.accumulo.core.data.Range; +import org.apache.accumulo.core.data.Value; +import org.apache.accumulo.core.fate.AbstractFateStore; +import org.apache.accumulo.core.fate.Fate.TxInfo; +import org.apache.accumulo.core.fate.ReadOnlyRepo; +import org.apache.accumulo.core.fate.Repo; +import org.apache.accumulo.core.fate.StackOverflowException; +import org.apache.accumulo.core.fate.accumulo.schema.FateSchema.RepoColumnFamily; +import org.apache.accumulo.core.fate.accumulo.schema.FateSchema.TxColumnFamily; +import org.apache.accumulo.core.fate.accumulo.schema.FateSchema.TxInfoColumnFamily; +import org.apache.accumulo.core.security.Authorizations; +import org.apache.accumulo.core.util.ColumnFQ; +import org.apache.accumulo.core.util.FastFormat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AccumuloStore<T> extends AbstractFateStore<T> { + + private static Logger log = LoggerFactory.getLogger(AccumuloStore.class); + + private static final Comparator<Entry<Key,Value>> repoComparator = + Comparator.comparing(o -> o.getKey().getColumnQualifier(), reverseOrder()); + + private final ClientContext context; + private final String tableName; + + public AccumuloStore(ClientContext context, String tableName) { + this.context = Objects.requireNonNull(context); + this.tableName = Objects.requireNonNull(tableName); + } + + @Override + public long create() { + long tid = RANDOM.get().nextLong() & 0x7fffffffffffffffL; + + // TODO = conditional mutation and retry if exists? + newMutator(tid).putStatus(TStatus.NEW).putCreateTime(System.currentTimeMillis()).mutate(); + + return tid; + } + + @Override + protected List<String> getTransactions() { + return scanTx(scanner -> { + scanner.setRange(new Range()); + scanner.fetchColumn(TxColumnFamily.STATUS_COLUMN.getColumnFamily(), + TxColumnFamily.STATUS_COLUMN.getColumnQualifier()); + return StreamSupport.stream(scanner.spliterator(), false) + .map(e -> e.getKey().getRow().toString()).collect(Collectors.toList()); + }); + } + + @Override + protected TStatus _getStatus(long tid) { + return scanTx(scanner -> { + scanner.setRange(getRow(tid)); + scanner.fetchColumn(TxColumnFamily.STATUS_COLUMN.getColumnFamily(), + TxColumnFamily.STATUS_COLUMN.getColumnQualifier()); + return StreamSupport.stream(scanner.spliterator(), false) + .map(e -> TStatus.valueOf(e.getValue().toString())).findFirst().orElse(TStatus.UNKNOWN); + }); + } + + @Override + protected FateTxStore<T> newFateTxStore(long tid, boolean isReserved) { + return new FateTxStoreImpl(tid, isReserved); + } + + static Range getRow(long tid) { + return new Range("tx_" + FastFormat.toHexString(tid)); + } + + private <T> FateMutatorImpl<T> newMutator(long tid) { + return new FateMutatorImpl<>(context, tableName, tid); + } + + private <R> R scanTx(Function<Scanner,R> func) { + try (Scanner scanner = context.createScanner(tableName, Authorizations.EMPTY)) { + return func.apply(scanner); + } catch (TableNotFoundException e) { + throw new IllegalStateException(tableName + " not found!", e); + } + } + + private class FateTxStoreImpl extends AbstractFateTxStoreImpl<T> { + + private FateTxStoreImpl(long tid, boolean isReserved) { + super(tid, isReserved); + } + + @Override + public Repo<T> top() { + verifyReserved(false); + + return scanTx(scanner -> { + scanner.setRange(getRow(tid)); + scanner.fetchColumnFamily(RepoColumnFamily.NAME); + return StreamSupport.stream(scanner.spliterator(), false).sorted(repoComparator).map(e -> { Review Comment: > But the only thing I found when searching in the code was was ReverseLexicoder but wasn't sure that made sense to use (or if i even would work here, I haven't looked close enough). Using `Lexicoder<Integer> lc = new ReverseLexicoder(new IntegerLexicoder())` would work nicely. It would not be human readable though. For this case since it has a small number of expected integers, making it human readable and counting down from 99 seems ok. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
