Search in sources :

Example 6 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class FileSystemKeySet method remove.

public void remove(Bytes name) {
    KeyDataMutator<Bytes> mutator = store.mutateData();
    List<NameKey> keys = keyUtil.toNameKeys(name);
    for (int i = keys.size() - 1; 0 <= i; i--) {
        NameKey key = keys.get(i);
        if (key.size == 0) {
            Bytes hash = keyUtil.hash(keyUtil.toName(keys.subList(0, i + 1)));
            NameKey minKey = boundingKey(hash, 0);
            NameKey maxKey = boundingKey(hash, 1);
            if (mutator.keys(keyUtil.toBytes(minKey), keyUtil.toBytes(maxKey)) == null)
                mutator.removeTerminal(keyUtil.toBytes(key));
        } else
            mutator.removeTerminal(keyUtil.toBytes(key));
    }
}
Also used : Bytes(suite.primitive.Bytes) NameKey(suite.fs.impl.FileSystemKeyUtil.NameKey)

Example 7 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class FileSystemMutatorImpl method read.

public Bytes read(Bytes name) {
    KeyDataStore<Bytes> store = mutate.source();
    KeyValueMutator<Bytes, Integer> kvm = store.mutate();
    KeyDataMutator<Bytes> kdm = store.mutateData();
    Bytes hash = keyUtil.hash(name);
    Integer size = kvm.get(key(hash, SIZEID, 0));
    if (size != null) {
        int seq = 0;
        BytesBuilder bb = new BytesBuilder();
        for (int s = 0; s < size; s += pageSize) bb.append(kdm.getPayload(key(hash, DATAID, seq++)));
        return bb.toBytes().range(0, size);
    } else
        return null;
}
Also used : Bytes(suite.primitive.Bytes) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 8 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class FileSystemMutatorImpl method replace.

public void replace(Bytes name, Bytes bytes) {
    KeyDataStore<Bytes> store = mutate.source();
    KeyValueMutator<Bytes, Integer> kvm = store.mutate();
    KeyDataMutator<Bytes> kdm = store.mutateData();
    FileSystemKeySet fsNameKeySet = new FileSystemKeySet(keyUtil, store);
    Bytes hash = keyUtil.hash(name);
    Bytes sizeKey = key(hash, SIZEID, 0);
    Bytes nameBytes0 = fsNameKeySet.list(name, null).first();
    boolean isRemove = Objects.equals(nameBytes0, name);
    boolean isCreate = bytes != null;
    if (isRemove) {
        int seq = 0, size = kvm.get(sizeKey);
        if (!isCreate)
            fsNameKeySet.remove(name);
        kvm.remove(sizeKey);
        for (int s = 0; s < size; s += pageSize) kdm.removePayload(key(hash, DATAID, seq++));
    }
    if (isCreate) {
        int pos = 0, seq = 0, size = bytes.size();
        while (pos < size) {
            int pos1 = min(pos + pageSize, size);
            kdm.putPayload(key(hash, DATAID, seq++), bytes.range(pos, pos1));
            pos = pos1;
        }
        kvm.put(sizeKey, size);
        if (!isRemove)
            fsNameKeySet.add(name);
    }
    store.end(true);
}
Also used : Bytes(suite.primitive.Bytes)

Example 9 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class FileSystemMutatorImpl method resize.

public void resize(Bytes name, int size1) {
    KeyDataStore<Bytes> store = mutate.source();
    KeyValueMutator<Bytes, Integer> kvm = store.mutate();
    KeyDataMutator<Bytes> kdm = store.mutateData();
    Bytes hash = keyUtil.hash(name);
    Bytes sizeKey = key(hash, SIZEID, 0);
    int size0 = kvm.get(sizeKey);
    int nPages0 = (size0 + pageSize - 1) / pageSize;
    int nPages1 = (size1 + pageSize - 1) / pageSize;
    for (int page = nPages1; page < nPages0; page++) kdm.removePayload(key(hash, DATAID, page));
    for (int page = nPages0; page < nPages1; page++) kdm.putPayload(key(hash, DATAID, page), Bytes.empty);
    kvm.put(sizeKey, size1);
    store.end(true);
}
Also used : Bytes(suite.primitive.Bytes)

Example 10 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class HttpUtil method httpJre.

@SuppressWarnings("unused")
private static HttpResult httpJre(String method, URL url, Outlet<Bytes> in, Map<String, String> headers) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod(method);
    headers.entrySet().forEach(e -> conn.setRequestProperty(e.getKey(), e.getValue()));
    try (OutputStream os = conn.getOutputStream()) {
        Bytes_.copy(in, os::write);
    }
    int responseCode = conn.getResponseCode();
    Outlet<Bytes> out = To.outlet(conn.getInputStream());
    if (// 
    responseCode == HttpURLConnection.HTTP_MOVED_PERM || // 
    responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
        String cookies1 = conn.getHeaderField("Set-Cookie");
        URL url1 = To.url(conn.getHeaderField("Location"));
        Map<String, String> headers1 = new HashMap<>(headers);
        if (cookies1 != null)
            headers1.put("Cookie", cookies1);
        return http(method, url1, in, headers1);
    } else if (responseCode == HttpURLConnection.HTTP_OK)
        return new HttpResult(responseCode, out);
    else
        throw new IOException(// 
        "HTTP returned " + responseCode + ": " + // 
        url + ": " + // 
        conn.getResponseMessage() + ": " + out.collect(As::string));
}
Also used : HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URL(java.net.URL) Bytes(suite.primitive.Bytes) As(suite.streamlet.As) HttpURLConnection(java.net.HttpURLConnection)

Aggregations

Bytes (suite.primitive.Bytes)56 Test (org.junit.Test)18 BytesBuilder (suite.primitive.Bytes.BytesBuilder)8 IOException (java.io.IOException)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Pair (suite.adt.pair.Pair)4 OutputStream (java.io.OutputStream)3 Path (java.nio.file.Path)3 To (suite.util.To)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Constants (suite.Constants)2 Amd64Interpret (suite.assembler.Amd64Interpret)2 Condition (suite.concurrent.Condition)2 Extent (suite.file.ExtentAllocator.Extent)2 JournalledPageFile (suite.file.JournalledPageFile)2 ImperativeCompiler (suite.ip.ImperativeCompiler)2 DataInput_ (suite.util.DataInput_)2