Skip to content

Commit

Permalink
use converter to switch value between int and long
Browse files Browse the repository at this point in the history
  • Loading branch information
enicloom committed Sep 16, 2022
1 parent 6c65d87 commit c79bf43
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
*
* Copyright 2020 Netflix, Inc.
*
* Licensed 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
*
* http://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 com.netflix.genie.web.data.services.impl.jpa.converters;

import javax.annotation.Nullable;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

/**
* An {@link AttributeConverter} to convert {@link Integer} objects into {@link Long} for storage and vice versa.
*
* @author ltian
* @since 4.3.0
*/
@Converter
public class IntegerToLongConverter implements AttributeConverter<Long, Integer> {

/**
* {@inheritDoc}
*/
@Override
@Nullable
public Integer convertToDatabaseColumn(@Nullable final Long attribute) {
if (attribute == null) {
return null;
}

return attribute.intValue();
}

/**
* {@inheritDoc}
*/
@Override
@Nullable
public Long convertToEntityAttribute(@Nullable final Integer dbData) {
if (dbData == null) {
return null;
}

return Long.valueOf(dbData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.netflix.genie.web.data.services.impl.jpa.entities;

import com.fasterxml.jackson.databind.JsonNode;
import com.netflix.genie.web.data.services.impl.jpa.converters.IntegerToLongConverter;
import com.netflix.genie.web.data.services.impl.jpa.converters.JsonAttributeConverter;
import com.netflix.genie.web.exceptions.checked.PreconditionFailedException;
import lombok.Getter;
Expand Down Expand Up @@ -204,8 +205,8 @@ public class CommandEntity extends BaseEntity {
@Min(1)
private Integer gpu;

@Basic
@Column(name = "memory")
@Convert(converter = IntegerToLongConverter.class)
@Min(1)
private Long memory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.netflix.genie.web.data.services.impl.jpa.entities;

import com.fasterxml.jackson.databind.JsonNode;
import com.netflix.genie.web.data.services.impl.jpa.converters.IntegerToLongConverter;
import com.netflix.genie.web.data.services.impl.jpa.converters.JsonAttributeConverter;
import com.netflix.genie.web.data.services.impl.jpa.listeners.JobEntityListener;
import com.netflix.genie.web.data.services.impl.jpa.queries.predicates.PredicateUtils;
Expand Down Expand Up @@ -363,13 +364,13 @@ public class JobEntity extends BaseEntity implements
@Min(value = 0, message = "Can't have less than 0 GPU")
private Integer gpuUsed;

@Basic
@Column(name = "requested_memory", updatable = false)
@Convert(converter = IntegerToLongConverter.class)
@Min(value = 1, message = "Can't have less than 1 MB of memory allocated")
private Long requestedMemory;

@Basic
@Column(name = "memory_used")
@Convert(converter = IntegerToLongConverter.class)
@Min(value = 1, message = "Can't have less than 1 MB of memory allocated")
private Long memoryUsed;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ ALTER TABLE `jobs`
DROP COLUMN `check_delay`;
ALTER TABLE `jobs`
DROP COLUMN `v4`;
ALTER TABLE `jobs`
ALTER COLUMN `requested_memory` BIGINT(20) DEFAULT NULL;
ALTER TABLE `jobs`
ALTER COLUMN `memory_used` BIGINT(20) DEFAULT NULL;

ALTER TABLE `commands`
DROP COLUMN `check_delay`;
Expand All @@ -59,5 +55,3 @@ ALTER TABLE `commands`
ADD COLUMN `network_mbps` BIGINT(20) DEFAULT NULL;
ALTER TABLE `commands`
ADD COLUMN `images` TEXT DEFAULT NULL;
ALTER TABLE `commands`
ALTER COLUMN `memory` BIGINT(20) DEFAULT NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ ALTER TABLE `jobs`
ADD COLUMN `network_mbps_used` BIGINT(20) DEFAULT NULL,
DROP COLUMN `check_delay`,
DROP COLUMN `v4`,
MODIFY COLUMN `requested_memory` BIGINT(20) DEFAULT NULL,
MODIFY COLUMN `memory_used` BIGINT(20) DEFAULT NULL;
ALGORITHM=INPLACE, LOCK=NONE;

ALTER TABLE `commands`
DROP COLUMN `check_delay`,
Expand All @@ -38,4 +37,4 @@ ALTER TABLE `commands`
ADD COLUMN `disk_mb` BIGINT(20) DEFAULT NULL,
ADD COLUMN `network_mbps` BIGINT(20) DEFAULT NULL,
ADD COLUMN `images` TEXT DEFAULT NULL,
MODIFY COLUMN `memory` BIGINT(20) DEFAULT NULL;
ALGORITHM=INPLACE, LOCK=NONE;
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ ALTER TABLE jobs
ADD COLUMN requested_network_mbps BIGINT DEFAULT NULL,
ADD COLUMN network_mbps_used BIGINT DEFAULT NULL,
DROP COLUMN check_delay,
DROP COLUMN v4,
ALTER COLUMN requested_memory TYPE BIGINT,
ALTER COLUMN memory_used TYPE BIGINT;
DROP COLUMN v4;

ALTER TABLE commands
DROP COLUMN check_delay,
ADD COLUMN cpu INT DEFAULT NULL,
ADD COLUMN gpu INT DEFAULT NULL,
ADD COLUMN disk_mb BIGINT DEFAULT NULL,
ADD COLUMN network_mbps BIGINT DEFAULT NULL,
ADD COLUMN images TEXT DEFAULT NULL,
ALTER COLUMN memory TYPE BIGINT;
ADD COLUMN images TEXT DEFAULT NULL;

0 comments on commit c79bf43

Please sign in to comment.